def _user_from_avatar(self, avatar, **kwargs): email = sanitize_email( convert_to_unicode(avatar.email).lower().strip()) secondary_emails = { sanitize_email(convert_to_unicode(x).lower().strip()) for x in avatar.secondaryEmails } secondary_emails = { x for x in secondary_emails if x and is_valid_mail(x, False) and x != email } # we handle deletion later. otherwise it might be set before secondary_emails which would # result in those emails not being marked as deleted is_deleted = kwargs.pop('is_deleted', False) user = User( id=int(avatar.id), email=email, first_name=convert_to_unicode(avatar.name).strip() or 'UNKNOWN', last_name=convert_to_unicode(avatar.surName).strip() or 'UNKNOWN', title=USER_TITLE_MAP.get(avatar.title, UserTitle.none), phone=convert_to_unicode(avatar.telephone[0]).strip(), affiliation=convert_to_unicode(avatar.organisation[0]).strip(), address=convert_to_unicode(avatar.address[0]).strip(), secondary_emails=secondary_emails, is_blocked=avatar.status == 'disabled', **kwargs) if is_deleted or not is_valid_mail(user.email): user.is_deleted = True return user
def testMutlipleInvalidEmails(self): emails = [ ' x$y@@; [email protected] ; [email protected]' ] for email in emails: print is_valid_mail(email) self.assertEqual(utils.validMail(email), is_valid_mail(email))
def validate_business(form, field): """Valiates a TouchNet business string. It can either be an email address or a touchnet business account ID. """ if not is_valid_mail(field.data, multi=False) and not re.match(r'^[a-zA-Z0-9]{13}$', field.data): raise ValidationError(_('Invalid email address / touchnet ID'))
def testMultipleValidEmails(self): emails = [ ' [email protected]; [email protected] , [email protected] ' ] for email in emails: self.assertEqual(utils.validMail(email), is_valid_mail(email))
def _migrate_category(self, old_cat, position): # unlimited visibility is 999 but we have a 994 for some reason.. since nobody # has 900 levels of nesting we can just go for that threshold instead visibility = None if old_cat._visibility > 900 else old_cat._visibility if visibility == 0: self.print_warning( "Raising visibility from 'invisible' to 'category-only'", event_id=old_cat.id) visibility = 1 emails = re.split( r'[\s;,]+', convert_to_unicode(getattr(old_cat, '_notifyCreationList', ''))) emails = {sanitize_email(email).lower() for email in emails} emails = sorted(email for email in emails if is_valid_mail(email, False)) default_themes = self._process_default_themes(old_cat) title = self._fix_title(convert_to_unicode(old_cat.name), old_cat.id) if is_legacy_id(old_cat.id): # if category has a legacy (non-numeric) ID, generate a new ID # and establish a mapping (for URL redirection) new_id = self.gen_categ_id() db.session.add( LegacyCategoryMapping(legacy_category_id=old_cat.id, category_id=new_id)) self.print_success('%[white!]{:6s}%[reset] -> %[cyan]{}'.format( old_cat.id, new_id)) else: new_id = int(old_cat.id) if hasattr(old_cat, '_timezone'): tz_name = old_cat._timezone else: tz_name = self.makac_info._timezone cat = Category(id=int(new_id), position=position, title=title, description=convert_to_unicode(old_cat.description), visibility=visibility, timezone=convert_to_unicode(tz_name), event_creation_notification_emails=emails, default_event_themes=default_themes, suggestions_disabled=getattr(old_cat, '_suggestions_disabled', False)) if not self.quiet: self.print_success(cat.title, event_id=cat.id) if old_cat._icon: self._process_icon(cat, old_cat._icon) self._process_protection(cat, old_cat) self.migrate_category_attachments(cat, old_cat) cat.children = [(self._migrate_category(old_subcat, i)) for i, old_subcat in enumerate( sorted(old_cat.subcategories.itervalues(), key=attrgetter('_order')), 1)] # add to user favorites for user in self.global_ns.user_favorite_categories[old_cat.id]: user.favorite_categories.add(cat) self.global_ns.legacy_category_ids[old_cat.id] = cat return cat
def _process_principal_emails(self, principal_cls, principals, emails, name, read_access=None, full_access=None, roles=None, allow_emails=True): emails = {sanitize_email(convert_to_unicode(email).lower()) for email in emails} emails = {email for email in emails if is_valid_mail(email, False)} for email in emails: self._process_principal(principal_cls, principals, email, name, read_access, full_access, roles, allow_emails=allow_emails)
def _migrate_category(self, old_cat, position): # unlimited visibility is 999 but we have a 994 for some reason.. since nobody # has 900 levels of nesting we can just go for that threshold instead visibility = None if old_cat._visibility > 900 else old_cat._visibility if visibility == 0: self.print_warning("Raising visibility from 'invisible' to 'category-only'", event_id=old_cat.id) visibility = 1 emails = re.split(r'[\s;,]+', convert_to_unicode(getattr(old_cat, '_notifyCreationList', ''))) emails = {sanitize_email(email).lower() for email in emails} emails = sorted(email for email in emails if is_valid_mail(email, False)) default_themes = self._process_default_themes(old_cat) title = self._fix_title(convert_to_unicode(old_cat.name), old_cat.id) cat = Category(id=int(old_cat.id), position=position, title=title, description=convert_to_unicode(old_cat.description), visibility=visibility, timezone=convert_to_unicode(old_cat._timezone), event_creation_notification_emails=emails, default_event_themes=default_themes, suggestions_disabled=getattr(old_cat, '_suggestions_disabled', False)) if not self.quiet: self.print_success(cat.title, event_id=cat.id) if old_cat._icon: self._process_icon(cat, old_cat._icon) self._process_protection(cat, old_cat) cat.children = [(self._migrate_category(old_subcat, i)) for i, old_subcat in enumerate(sorted(old_cat.subcategories.itervalues(), key=attrgetter('_order')), 1)] return cat
def migrate_settings(self): print cformat('%{white!}migrating settings') ChatPlugin.settings.delete_all() type_opts = self.zodb_root['plugins']['InstantMessaging']._PluginBase__options opts = self.zodb_root['plugins']['InstantMessaging']._PluginType__plugins['XMPP']._PluginBase__options host = convert_to_unicode(opts['chatServerHost']._PluginOption__value) admin_emails = [x.email for x in opts['admins']._PluginOption__value] ChatPlugin.settings.set('admins', convert_principal_list(opts['admins'])) ChatPlugin.settings.set('server', host) ChatPlugin.settings.set('muc_server', 'conference.{}'.format(host)) settings_map = { 'additionalEmails': 'notify_emails', 'indicoUsername': '******', 'indicoPassword': '******', 'ckEditor': 'how_to_connect' } for old, new in settings_map.iteritems(): value = opts[old]._PluginOption__value if isinstance(value, basestring): value = convert_to_unicode(value).strip() elif new == 'notify_emails': value = [email for email in set(value + admin_emails) if is_valid_mail(email, multi=False)] ChatPlugin.settings.set(new, value) if opts['activateLogs']._PluginOption__value: ChatPlugin.settings.set('log_url', 'https://{}/logs/'.format(host)) chat_links = [] for item in type_opts['customLinks']._PluginOption__value: link = item['structure'].replace('[chatroom]', '{room}').replace('[host]', '{server}') link = re.sub(r'(?<!conference\.)\{server}', host, link) link = link.replace('conference.{server}', '{server}') # {server} is now the MUC server chat_links.append({'title': item['name'], 'link': link}) ChatPlugin.settings.set('chat_links', chat_links) db.session.commit()
def validate_business(form, field): """Valiates a PayPal business string. It can either be an email address or a paypal business account ID. """ if not is_valid_mail(field.data, multi=False) and not re.match(r'^[a-zA-Z0-9]{13}$', field.data): raise ValidationError(_('Invalid email address / paypal ID'))
def testNotAllowedMultipleInValidEmails(self): emails = [ ' x$y@@; [email protected] ; [email protected]' ] for email in emails: self.assertEqual(utils.validMail(email, allowMultiple=False), is_valid_mail(email, multi=False))
def import_registrations_from_csv(regform, fileobj, skip_moderation=True, notify_users=False): """Import event registrants from a CSV file into a form.""" reader = csv.reader(fileobj.read().splitlines()) query = db.session.query(Registration.email).with_parent(regform).filter( Registration.is_active) registered_emails = {email for (email, ) in query} used_emails = set() todo = [] for row_num, row in enumerate(reader, 1): try: first_name, last_name, affiliation, position, phone, email = [ to_unicode(value).strip() for value in row ] email = email.lower() except ValueError: raise UserValueError( _('Row {}: malformed CSV data - please check that the number of columns is correct' ).format(row_num)) if not email: raise UserValueError( _('Row {}: missing e-mail address').format(row_num)) if not is_valid_mail(email, multi=False): raise UserValueError( _('Row {}: invalid e-mail address').format(row_num)) if not first_name or not last_name: raise UserValueError( _('Row {}: missing first or last name').format(row_num)) if email in registered_emails: raise UserValueError( _('Row {}: a registration with this email already exists'). format(row_num)) if email in used_emails: raise UserValueError( _('Row {}: email address is not unique').format(row_num)) used_emails.add(email) todo.append({ 'email': email, 'first_name': first_name.title(), 'last_name': last_name.title(), 'affiliation': affiliation, 'phone': phone, 'position': position }) return [ create_registration(regform, data, notify_user=notify_users, skip_moderation=skip_moderation) for data in todo ]
def prompt_email(prompt="Enter email: "): while True: try: email = unicode(raw_input(prompt.encode(sys.stderr.encoding)), sys.stdin.encoding).strip() except (EOFError, KeyboardInterrupt): # ^D or ^C print return None if is_valid_mail(email): return email else: warning(u"Email format is invalid")
def testOneInvalidEmail(self): emails = [ 'atom@cern', 'higgs#[email protected]', 'Abc.example.com', 'A@b@[email protected]', 'a"b(c)d,e:f;g<h>i[j\k][email protected]', 'just"not"*****@*****.**', 'this is"not\[email protected]', 'this\ still\"not\\[email protected]', '[email protected]' ] for email in emails: # self.assertFalse(is_valid_mail(email)) self.assertEqual(utils.validMail(email), is_valid_mail(email))
def _user_from_avatar(self, avatar, **kwargs): email = sanitize_email(convert_to_unicode(avatar.email).lower().strip()) secondary_emails = {sanitize_email(convert_to_unicode(x).lower().strip()) for x in avatar.secondaryEmails} secondary_emails = {x for x in secondary_emails if x and is_valid_mail(x, False) and x != email} # we handle deletion later. otherwise it might be set before secondary_emails which would # result in those emails not being marked as deleted is_deleted = kwargs.pop('is_deleted', False) user = User(id=int(avatar.id), email=email, first_name=convert_to_unicode(avatar.name).strip() or 'UNKNOWN', last_name=convert_to_unicode(avatar.surName).strip() or 'UNKNOWN', title=USER_TITLE_MAP.get(avatar.title, UserTitle.none), phone=convert_to_unicode(avatar.telephone[0]).strip(), affiliation=convert_to_unicode(avatar.organisation[0]).strip(), address=convert_to_unicode(avatar.address[0]).strip(), secondary_emails=secondary_emails, is_blocked=avatar.status == 'disabled', **kwargs) if is_deleted or not is_valid_mail(user.email): user.is_deleted = True return user
def process_emails(self, principals, emails, name, color, full_access=None, roles=None): emails = { sanitize_email(convert_to_unicode(email).lower()) for email in emails } emails = {email for email in emails if is_valid_mail(email, False)} for email in emails: self.process_principal(principals, email, name, color, full_access, roles)
def migrate_settings(self): print cformat('%{white!}migrating settings') rb_settings.delete_all() opts = self.zodb_root['plugins']['RoomBooking']._PluginBase__options # Admins & authorized users/groups rb_settings.set('authorized_principals', convert_principal_list(opts['AuthorisedUsersGroups'])) rb_settings.set('admin_principals', convert_principal_list(opts['Managers'])) # Assistance emails emails = [email for email in opts['assistanceNotificationEmails']._PluginOption__value if is_valid_mail(email, False)] rb_settings.set('assistance_emails', emails) # Simple settings rb_settings.set('notification_before_days', opts['notificationBefore']._PluginOption__value) db.session.commit()
def migrate_settings(self): print cformat('%{white!}migrating settings') rb_settings.delete_all() opts = self.zodb_root['plugins']['RoomBooking']._PluginBase__options # Admins & authorized users/groups rb_settings.set('authorized_principals', convert_principal_list(opts['AuthorisedUsersGroups'])) rb_settings.set('admin_principals', convert_principal_list(opts['Managers'])) # Assistance emails emails = [email for email in opts['assistanceNotificationEmails']._PluginOption__value if is_valid_mail(email, False)] rb_settings.set('assistance_emails', emails) # Simple settings rb_settings.set('notification_hour', opts['notificationHour']._PluginOption__value) rb_settings.set('notification_before_days', opts['notificationBefore']._PluginOption__value) db.session.commit()
def migrate_event_settings(self): print cformat('%{white!}migrating event settings') default_method_name = PaypalPaymentPlugin.settings.get('method_name') EventSetting.delete_all(PaypalPaymentPlugin.event_settings.module) account_id_re = re.compile(r'^[a-zA-Z0-9]{13}$') for event in committing_iterator(self._iter_events(), 25): pp = event._modPay.payMods['PayPal'] business = pp._business.strip() if not business or (not is_valid_mail(business, multi=False) and not account_id_re.match(business)): print cformat(' - %{yellow!}event {} skipped (business: {})').format(event.id, business or '(none)') continue PaypalPaymentPlugin.event_settings.set(event, 'enabled', True) method_name = convert_to_unicode(pp._title) if method_name.lower() == 'paypal': method_name = default_method_name PaypalPaymentPlugin.event_settings.set(event, 'method_name', method_name) PaypalPaymentPlugin.event_settings.set(event, 'business', pp._business) print cformat(' - %{cyan}event {} (business: {})').format(event.id, pp._business)
def testOneValidEmail(self): emails = [ '*****@*****.**', '*****@*****.**', '*****@*****.**', '*****@*****.**', '*****@*****.**', '*****@*****.**', '*****@*****.**', 'user@[IPv6:2001:db8:1ff::a0b:dbd0]', '"much.more unusual"@example.com', '"*****@*****.**"@example.com', '"very.(),:;<>[]\".VERY.\"very@\\ \"very\".unusual"@strange.example.com', 'postbox@com', 'admin@mailserver1', "!#$%&'*+-/=?^_`{}|[email protected]", """()<>[]:,;@\\\"!#$%&'*+-/=?^_`{}| ~.a"@example.org""", '" "@example.org', 'üñîçøðé@example.com', 'üñîçøðé@üñîçøðé.com' ] for email in emails: self.assertEqual(utils.validMail(email), is_valid_mail(email))
def migrate_settings(self): print cformat('%{white!}migrating settings') ChatPlugin.settings.delete_all() type_opts = self.zodb_root['plugins'][ 'InstantMessaging']._PluginBase__options opts = self.zodb_root['plugins'][ 'InstantMessaging']._PluginType__plugins[ 'XMPP']._PluginBase__options host = convert_to_unicode(opts['chatServerHost']._PluginOption__value) admin_emails = [x.email for x in opts['admins']._PluginOption__value] ChatPlugin.settings.set('admins', convert_principal_list(opts['admins'])) ChatPlugin.settings.set('server', host) ChatPlugin.settings.set('muc_server', 'conference.{}'.format(host)) settings_map = { 'additionalEmails': 'notify_emails', 'indicoUsername': '******', 'indicoPassword': '******', 'ckEditor': 'how_to_connect' } for old, new in settings_map.iteritems(): value = opts[old]._PluginOption__value if isinstance(value, basestring): value = convert_to_unicode(value).strip() elif new == 'notify_emails': value = [ email for email in set(value + admin_emails) if is_valid_mail(email, multi=False) ] ChatPlugin.settings.set(new, value) if opts['activateLogs']._PluginOption__value: ChatPlugin.settings.set('log_url', 'https://{}/logs/'.format(host)) chat_links = [] for item in type_opts['customLinks']._PluginOption__value: link = item['structure'].replace('[chatroom]', '{room}').replace( '[host]', '{server}') link = re.sub(r'(?<!conference\.)\{server}', host, link) link = link.replace('conference.{server}', '{server}') # {server} is now the MUC server chat_links.append({'title': item['name'], 'link': link}) ChatPlugin.settings.set('chat_links', chat_links) db.session.commit()
def migrate_settings(main_root): print cformat('%{white!}migrating settings') rb_settings.delete_all() opts = main_root['plugins']['RoomBooking']._PluginBase__options # Admins & authorized users/groups for old_key, new_key in (('AuthorisedUsersGroups', 'authorized_principals'), ('Managers', 'admin_principals')): principals = set() for principal in opts[old_key].getValue(): if principal.__class__.__name__ == 'Avatar': principals.add(('Avatar', principal.id)) else: principals.add(('Group', principal.id)) rb_settings.set(new_key, list(principals)) # Assistance emails emails = [email for email in opts['assistanceNotificationEmails'].getValue() if is_valid_mail(email, False)] rb_settings.set('assistance_emails', emails) # Simple settings rb_settings.set('notification_hour', opts['notificationHour'].getValue()) rb_settings.set('notification_before_days', opts['notificationBefore'].getValue()) db.session.commit()
def _migrate_category(self, old_cat, position): # unlimited visibility is 999 but we have a 994 for some reason.. since nobody # has 900 levels of nesting we can just go for that threshold instead visibility = None if old_cat._visibility > 900 else old_cat._visibility if visibility == 0: self.print_warning( "Raising visibility from 'invisible' to 'category-only'", event_id=old_cat.id) visibility = 1 emails = re.split( r'[\s;,]+', convert_to_unicode(getattr(old_cat, '_notifyCreationList', ''))) emails = {sanitize_email(email).lower() for email in emails} emails = sorted(email for email in emails if is_valid_mail(email, False)) default_themes = self._process_default_themes(old_cat) title = self._fix_title(convert_to_unicode(old_cat.name), old_cat.id) cat = Category(id=int(old_cat.id), position=position, title=title, description=convert_to_unicode(old_cat.description), visibility=visibility, timezone=convert_to_unicode(old_cat._timezone), event_creation_notification_emails=emails, default_event_themes=default_themes, suggestions_disabled=getattr(old_cat, '_suggestions_disabled', False)) if not self.quiet: self.print_success(cat.title, event_id=cat.id) if old_cat._icon: self._process_icon(cat, old_cat._icon) self._process_protection(cat, old_cat) cat.children = [(self._migrate_category(old_subcat, i)) for i, old_subcat in enumerate( sorted(old_cat.subcategories.itervalues(), key=attrgetter('_order')), 1)] return cat
def import_registrations_from_csv(regform, fileobj, skip_moderation=True, notify_users=False): """Import event registrants from a CSV file into a form.""" reader = csv.reader(fileobj.read().splitlines()) query = db.session.query(Registration.email).with_parent(regform).filter(Registration.is_active) registered_emails = {email for (email,) in query} used_emails = set() todo = [] for row_num, row in enumerate(reader, 1): try: first_name, last_name, affiliation, position, phone, email = [to_unicode(value).strip() for value in row] email = email.lower() except ValueError: raise UserValueError(_('Row {}: malformed CSV data - please check that the number of columns is correct') .format(row_num)) if not email: raise UserValueError(_('Row {}: missing e-mail address').format(row_num)) if not is_valid_mail(email, multi=False): raise UserValueError(_('Row {}: invalid e-mail address').format(row_num)) if not first_name or not last_name: raise UserValueError(_('Row {}: missing first or last name').format(row_num)) if email in registered_emails: raise UserValueError(_('Row {}: a registration with this email already exists').format(row_num)) if email in used_emails: raise UserValueError(_('Row {}: email address is not unique').format(row_num)) used_emails.add(email) todo.append({ 'email': email, 'first_name': first_name.title(), 'last_name': last_name.title(), 'affiliation': affiliation, 'phone': phone, 'position': position }) return [create_registration(regform, data, notify_user=notify_users, skip_moderation=skip_moderation) for data in todo]
def __call__(self, form, field): if field.data and not is_valid_mail(field.data, self.multi): msg = _(u'Invalid email address list') if self.multi else _( u'Invalid email address') raise ValidationError(msg)
def migrate_settings(self): print cformat("%{white!}migrating settings") rb_settings.delete_all() opts = self.zodb_root["plugins"]["RoomBooking"]._PluginBase__options # Admins & authorized users/groups rb_settings.set("authorized_principals", convert_principal_list(opts["AuthorisedUsersGroups"])) rb_settings.set("admin_principals", convert_principal_list(opts["Managers"])) # Assistance emails emails = [ email for email in opts["assistanceNotificationEmails"]._PluginOption__value if is_valid_mail(email, False) ] rb_settings.set("assistance_emails", emails) # Simple settings rb_settings.set("notification_hour", opts["notificationHour"]._PluginOption__value) rb_settings.set("notification_before_days", opts["notificationBefore"]._PluginOption__value) db.session.commit()
def _proc_email(val): val = conv(val).strip() if not is_valid_mail(val, multi=False): raise click.UsageError(u'invalid email') return val
def _check_email(email): if is_valid_mail(email, False): return True _warn('Invalid email address') return False
def _validate_item(self, line): if not is_valid_mail(line, False): raise ValueError(_(u'Invalid email address: {}').format(line))
def __call__(self, form, field): if field.data and not is_valid_mail(field.data, self.multi): msg = _(u'Invalid email address list') if self.multi else _(u'Invalid email address') raise ValidationError(msg)
def validate_contact_emails(self, field): for email in field.data: if not is_valid_mail(email, False): raise ValidationError( _('Invalid email address: {}').format(escape(email)))
def process_emails(self, event, principals, emails, name, color, full_access=None, roles=None): emails = {sanitize_email(convert_to_unicode(email).lower()) for email in emails} emails = {email for email in emails if is_valid_mail(email, False)} for email in emails: self.process_principal(event, principals, email, name, color, full_access, roles)
def validate_contact_emails(self, field): for email in field.data: if not is_valid_mail(email, False): raise ValidationError(_('Invalid email address: {}').format(escape(email)))
def _validate_item(self, line): if not is_valid_mail(line, False): raise ValueError( _(u'Invalid email address: {}').format(escape(line)))