def test_make_email_primary(db): signal_called = False def _signal_fn(sender, old, new): nonlocal signal_called signal_called = True assert sender is user assert old == '*****@*****.**' assert new == '*****@*****.**' with signals.users.primary_email_changed.connected_to(_signal_fn): user = User(first_name='Guinea', last_name='Pig', email='*****@*****.**') db.session.add(user) db.session.flush() with pytest.raises(ValueError): user.make_email_primary('*****@*****.**') user.secondary_emails = {'*****@*****.**', '*****@*****.**'} db.session.flush() assert not signal_called user.make_email_primary('*****@*****.**') assert signal_called db.session.expire(user) assert user.email == '*****@*****.**' assert user.secondary_emails == {'*****@*****.**', '*****@*****.**'}
def create_user(email, data, identity=None, settings=None, other_emails=None, from_moderation=True): """Create a new user. This may also convert a pending user to a proper user in case the email address matches such a user. :param email: The primary email address of the user. :param data: The data used to populate the user. :param identity: An `Identity` to associate with the user. :param settings: A dict containing user settings. :param other_emails: A set of email addresses that are also used to check for a pending user. They will also be added as secondary emails to the user. :param from_moderation: Whether the user was created through the moderation process or manually by an admin. """ if other_emails is None: other_emails = set() if settings is None: settings = {} settings.setdefault('timezone', config.DEFAULT_TIMEZONE) settings.setdefault('lang', config.DEFAULT_LOCALE) settings.setdefault('suggest_categories', False) # Get a pending user if there is one user = User.query.filter(~User.is_deleted, User.is_pending, User.all_emails.in_({email} | set(other_emails))).first() if not user: user = User() if email in user.secondary_emails: # This can happen if there's a pending user who has a secondary email # for some weird reason which should now become the primary email... user.make_email_primary(email) else: user.email = email user.populate_from_dict(data, skip={'synced_fields'}) user.is_pending = False user.secondary_emails |= other_emails user.favorite_users.add(user) if identity is not None: user.identities.add(identity) db.session.add(user) db.session.flush() user.populate_from_dict(data, keys={ 'synced_fields' }) # this is a setting, so the user must have an ID user.settings.set_multi(settings) db.session.flush() signals.users.registered.send(user, from_moderation=from_moderation, identity=identity) db.session.flush() return user
def test_make_email_primary(db): user = User(first_name='Guinea', last_name='Pig', email='*****@*****.**') db.session.add(user) db.session.flush() with pytest.raises(ValueError): user.make_email_primary('*****@*****.**') user.secondary_emails = {'*****@*****.**', '*****@*****.**'} db.session.flush() user.make_email_primary('*****@*****.**') db.session.expire(user) assert user.email == '*****@*****.**' assert user.secondary_emails == {'*****@*****.**', '*****@*****.**'}
def test_make_email_primary(db): user = User(first_name='Guinea', last_name='Pig', email='*****@*****.**') db.session.add(user) db.session.flush() with pytest.raises(ValueError): user.make_email_primary('*****@*****.**') user.secondary_emails = {'*****@*****.**', '*****@*****.**'} db.session.flush() user.make_email_primary('*****@*****.**') db.session.expire(user) assert user.email == '*****@*****.**' assert user.secondary_emails == {'*****@*****.**', '*****@*****.**'}
def create_user(email, data, identity=None, settings=None, other_emails=None, from_moderation=True): """Create a new user. This may also convert a pending user to a proper user in case the email address matches such a user. :param email: The primary email address of the user. :param data: The data used to populate the user. :param identity: An `Identity` to associate with the user. :param settings: A dict containing user settings. :param other_emails: A set of email addresses that are also used to check for a pending user. They will also be added as secondary emails to the user. :param from_moderation: Whether the user was created through the moderation process or manually by an admin. """ if other_emails is None: other_emails = set() # Get a pending user if there is one user = User.find_first( ~User.is_deleted, User.is_pending, User.all_emails.contains(db.func.any(list({email} | set(other_emails))))) if not user: user = User() if email in user.secondary_emails: # This can happen if there's a pending user who has a secondary email # for some weird reason which should now become the primary email... user.make_email_primary(email) else: user.email = email user.populate_from_dict(data) user.is_pending = False user.secondary_emails |= other_emails user.favorite_users.add(user) if identity is not None: user.identities.add(identity) db.session.add(user) if settings: user.settings.set_multi(settings) db.session.flush() signals.users.registered.send(user, from_moderation=from_moderation, identity=identity) db.session.flush() return user
def create_user(email, data, identity=None, settings=None, other_emails=None, from_moderation=True): """Create a new user. This may also convert a pending user to a proper user in case the email address matches such a user. :param email: The primary email address of the user. :param data: The data used to populate the user. :param identity: An `Identity` to associate with the user. :param settings: A dict containing user settings. :param other_emails: A set of email addresses that are also used to check for a pending user. They will also be added as secondary emails to the user. :param from_moderation: Whether the user was created through the moderation process or manually by an admin. """ if other_emails is None: other_emails = set() if settings is None: settings = {} settings.setdefault('timezone', config.DEFAULT_TIMEZONE) settings.setdefault('lang', config.DEFAULT_LOCALE) settings.setdefault('suggest_categories', False) # Get a pending user if there is one user = User.query.filter(~User.is_deleted, User.is_pending, User.all_emails.in_({email} | set(other_emails))).first() if not user: user = User() if email in user.secondary_emails: # This can happen if there's a pending user who has a secondary email # for some weird reason which should now become the primary email... user.make_email_primary(email) else: user.email = email user.populate_from_dict(data, skip={'synced_fields'}) user.is_pending = False user.secondary_emails |= other_emails user.favorite_users.add(user) if identity is not None: user.identities.add(identity) db.session.add(user) db.session.flush() user.populate_from_dict(data, keys={'synced_fields'}) # this is a setting, so the user must have an ID user.settings.set_multi(settings) db.session.flush() signals.users.registered.send(user, from_moderation=from_moderation, identity=identity) db.session.flush() return user
def create_user(email, data, identity=None, settings=None, other_emails=None, from_moderation=True): """Create a new user. This may also convert a pending user to a proper user in case the email address matches such a user. :param email: The primary email address of the user. :param data: The data used to populate the user. :param identity: An `Identity` to associate with the user. :param settings: A dict containing user settings. :param other_emails: A set of email addresses that are also used to check for a pending user. They will also be added as secondary emails to the user. :param from_moderation: Whether the user was created through the moderation process or manually by an admin. """ if other_emails is None: other_emails = set() # Get a pending user if there is one user = User.find_first(~User.is_deleted, User.is_pending, User.all_emails.contains(db.func.any(list({email} | set(other_emails))))) if not user: user = User() if email in user.secondary_emails: # This can happen if there's a pending user who has a secondary email # for some weird reason which should now become the primary email... user.make_email_primary(email) else: user.email = email user.populate_from_dict(data) user.is_pending = False user.secondary_emails |= other_emails user.favorite_users.add(user) if identity is not None: user.identities.add(identity) db.session.add(user) if settings: user.settings.set_multi(settings) db.session.flush() signals.users.registered.send(user, from_moderation=from_moderation, identity=identity) db.session.flush() return user
def _create_user(self, form, handler, pending_user): data = form.data if pending_user: user = pending_user user.is_pending = False else: user = User() form.populate_obj(user, skip={'email'}) if form.email.data in user.secondary_emails: # This can happen if there's a pending user who has a secondary email # for some weird reason which should now become the primary email... user.make_email_primary(form.email.data) else: user.email = form.email.data identity = handler.create_identity(data) user.identities.add(identity) user.secondary_emails |= handler.get_all_emails(form) - {user.email} user.favorite_users.add(user) db.session.add(user) minfo = HelperMaKaCInfo.getMaKaCInfoInstance() timezone = session.timezone if timezone == 'LOCAL': timezone = Config.getInstance().getDefaultTimezone() user.settings.set('timezone', timezone) user.settings.set('lang', session.lang or minfo.getLang()) handler.update_user(user, form) db.session.flush() # notify everyone of user creation signals.users.registered.send(user) login_user(user, identity) msg = _('You have sucessfully registered your Indico profile. ' 'Check <a href="{url}">your profile</a> for further details and settings.') flash(Markup(msg).format(url=url_for('users.user_profile')), 'success') db.session.flush() return handler.redirect_success()
def _create_user(self, form, handler, pending_user): data = form.data if pending_user: user = pending_user user.is_pending = False else: user = User() form.populate_obj(user, skip={'email'}) if form.email.data in user.secondary_emails: # This can happen if there's a pending user who has a secondary email # for some weird reason which should now become the primary email... user.make_email_primary(form.email.data) else: user.email = form.email.data identity = handler.create_identity(data) user.identities.add(identity) user.secondary_emails |= handler.get_all_emails(form) - {user.email} user.favorite_users.add(user) db.session.add(user) minfo = HelperMaKaCInfo.getMaKaCInfoInstance() timezone = session.timezone if timezone == 'LOCAL': timezone = Config.getInstance().getDefaultTimezone() user.settings.set('timezone', timezone) user.settings.set('lang', session.lang or minfo.getLang()) handler.update_user(user, form) db.session.flush() # notify everyone of user creation signals.users.registered.send(user) login_user(user, identity) msg = _('You have sucessfully registered your Indico profile. ' 'Check <a href="{url}">your profile</a> for further details and settings.') flash(Markup(msg).format(url=url_for('users.user_profile')), 'success') db.session.flush() return handler.redirect_success()