def confirm_and_activate_account(self, email_template=None, email_subject=None, email_sender=None): """Confirms the email address, and activates the associated account if not already activated Side effect: Once an email address is confirmed by an account, no other accounts can have that email address in a pending (unconfirmed state) """ # TODO: what if an account was purposefully deactivated? we should not activate it. but how would we know the difference? if not self.is_confirmed: self.is_confirmed = True self.save() if self.replacing: from htk.apps.accounts.utils.notifiers import notify_user_email_update notify_user_email_update(self.user, self.replacing, self.email) self.delete_replaced_email() was_activated = self.user.profile.activate( email_template=email_template, email_subject=email_subject, email_sender=email_sender) # purge all other records with same email addresses that aren't confirmed # NOTE: Be careful to not delete the wrong ones! UserEmail.objects.filter(email=self.email, is_confirmed=False).delete() return was_activated
def set_primary_email(self, email): """Set the primary email address for `self.user` This is typically called from UserEmail.set_primary_email Assumes that there is no other auth.User with this email, so doesn't check """ user = self.user if self.has_email(email): old_email = user.email user.email = email user.save() from htk.apps.accounts.utils.notifiers import notify_user_email_update notify_user_email_update(user, old_email, email) else: pass return user
def set_primary_email(self, email): """Set the primary email address for `self.user` This is typically called from UserEmail.set_primary_email Assumes that there is no other auth.User with this email, so doesn't check """ user = self.user if self.has_email(email): old_email = user.email user.email = email user.save() from htk.apps.accounts.utils.notifiers import notify_user_email_update notify_user_email_update(user, old_email, email) else: pass return user
def confirm_and_activate_account(self, email_template=None, email_subject=None, email_sender=None): """Confirms the email address, and activates the associated account if not already activated Side effect: Once an email address is confirmed by an account, no other accounts can have that email address in a pending (unconfirmed state) """ # TODO: what if an account was purposefully deactivated? we should not activate it. but how would we know the difference? if not self.is_confirmed: self.is_confirmed = True self.save() if self.replacing: from htk.apps.accounts.utils.notifiers import notify_user_email_update notify_user_email_update(self.user, self.replacing, self.email) self.delete_replaced_email() was_activated = self.user.profile.activate(email_template=email_template, email_subject=email_subject, email_sender=email_sender) # purge all other records with same email addresses that aren't confirmed # NOTE: Be careful to not delete the wrong ones! UserEmail.objects.filter(email=self.email, is_confirmed=False).delete() return was_activated
def associate_user_email(user, email, replacing=None, domain=None, email_template=None, email_subject=None, email_sender=None, confirmed=False): """Associates `email` with `user` Resulting UserEmail.is_confirmed = `confirmed`, default False Side effect: sends an activation email if `confirmed` == False Requires: `user` and `email` to be valid `email` cannot be confirmed by any other user `email` cannot already be associated with THIS `user` If `replacing` is specified, it denotes that it is being replaced by `email` """ from htk.apps.accounts.models import UserEmail user_email = None if user and email: existing_user = get_user_by_email(email) should_associate = False if existing_user is None: # email address must not be associated to another account should_associate = True elif user == existing_user: if user.is_active: # an existing active account should_associate = True else: # a new registration should_associate = True else: # skip association # This email address is either: # a) already confirmed on another account # b) not already confirmed, and not a new registration should_associate = False if should_associate: user_email = get_user_email(user, email) if user_email is None: user_email = UserEmail.objects.create(user=user, email=email, is_confirmed=confirmed, replacing=replacing) if confirmed or user_email.is_confirmed: # don't need to send activation email for a pre-confirmed address # pre-confirmed email can come from a social auth provider user_email.confirm_and_activate_account() if replacing: from htk.apps.accounts.utils.notifiers import notify_user_email_update notify_user_email_update(user, replacing, email) elif not user_email.is_confirmed: domain = domain or htk_setting('HTK_DEFAULT_EMAIL_SENDING_DOMAIN') try: user_email.send_activation_email(domain, template=email_template, subject=email_subject, sender=email_sender) except: request = get_current_request() rollbar.report_exc_info() else: pass else: pass else: # invalid user or email pass return user_email
def associate_user_email(user, email, replacing=None, domain=None, email_template=None, email_subject=None, email_sender=None, confirmed=False): """Associates `email` with `user` Resulting UserEmail.is_confirmed = `confirmed`, default False Side effect: sends an activation email if `confirmed` == False Requires: `user` and `email` to be valid `email` cannot be confirmed by any other user `email` cannot already be associated with THIS `user` If `replacing` is specified, it denotes that it is being replaced by `email` """ from htk.apps.accounts.models import UserEmail user_email = None if user and email: existing_user = get_user_by_email(email) should_associate = False if existing_user is None: # email address must not be associated to another account should_associate = True elif user == existing_user: if user.is_active: # an existing active account should_associate = True else: # a new registration should_associate = True else: # skip association # This email address is either: # a) already confirmed on another account # b) not already confirmed, and not a new registration should_associate = False if should_associate: user_email = get_user_email(user, email) if user_email is None: user_email = UserEmail.objects.create(user=user, email=email, is_confirmed=confirmed, replacing=replacing) if confirmed or user_email.is_confirmed: # don't need to send activation email for a pre-confirmed address # pre-confirmed email can come from a social auth provider user_email.confirm_and_activate_account() if replacing: from htk.apps.accounts.utils.notifiers import notify_user_email_update notify_user_email_update(user, replacing, email) elif not user_email.is_confirmed: domain = domain or htk_setting('HTK_DEFAULT_EMAIL_SENDING_DOMAIN') try: user_email.send_activation_email(domain, template=email_template, subject=email_subject, sender=email_sender) except: request = get_current_request() rollbar.report_exc_info() else: pass else: pass else: # invalid user or email pass return user_email