def get_default_queryset(self): user = self.get_user() serialized_emails = [] for email in user.emails.all(): primary = email.address == user.username hashed_id = hashids.encode(email.id) serialized_email = UserEmail(email_id=hashed_id, address=email.address, confirmed=True, verified=True, primary=primary) serialized_emails.append(serialized_email) email_verifications = user.email_verifications or {} for token, detail in email_verifications.iteritems(): is_merge = Email.objects.filter(address=detail['email']).exists() serialized_unconfirmed_email = UserEmail( email_id=token, address=detail['email'], confirmed=detail['confirmed'], verified=False, primary=False, is_merge=is_merge, ) serialized_emails.append(serialized_unconfirmed_email) return serialized_emails
def get_object(self): email_id = self.kwargs['email_id'] user = self.get_user() # check to see if it's a confirmed email with hashed id decoded_id = hashids.decode(email_id) if decoded_id: try: email = user.emails.get(id=decoded_id[0]) except Email.DoesNotExist: email = None else: primary = email.address == user.username address = email.address confirmed = True # check to see if it's an unconfirmed email with a token elif user.unconfirmed_emails: try: email = user.email_verifications[email_id] address = email['email'] confirmed = email['confirmed'] primary = False except KeyError: email = None if not email: raise NotFound return UserEmail(email_id=email_id, address=address, confirmed=confirmed, primary=primary)
def get_queryset(self): user = self.get_user() serialized_emails = [] for email in user.emails.all(): primary = email.address == user.username hashed_id = hashids.encode(email.id) serialized_email = UserEmail(email_id=hashed_id, address=email.address, confirmed=True, primary=primary) serialized_emails.append(serialized_email) email_verifications = user.email_verifications or [] for token in email_verifications: detail = user.email_verifications[token] serialized_unconfirmed_email = UserEmail( email_id=token, address=detail['email'], confirmed=detail['confirmed'], primary=False) serialized_emails.append(serialized_unconfirmed_email) return serialized_emails
def get_object(self): email_id = self.kwargs['email_id'] user = self.get_user() email = None # check to see if it's a confirmed email with hashed id decoded_id = hashids.decode(email_id) if decoded_id: try: email = user.emails.get(id=decoded_id[0]) except Email.DoesNotExist: email = None else: primary = email.address == user.username address = email.address confirmed = True verified = True is_merge = False # check to see if it's an unconfirmed email with a token elif user.unconfirmed_emails: try: email = user.email_verifications[email_id] address = email['email'] confirmed = email['confirmed'] verified = False primary = False is_merge = Email.objects.filter(address=address).exists() except KeyError: email = None if not email: raise NotFound # check for resend confirmation email query parameter in a GET request if self.request.method == 'GET' and is_truthy( self.request.query_params.get('resend_confirmation')): if not confirmed and settings.CONFIRM_REGISTRATIONS_BY_EMAIL: if throttle_period_expired(user.email_last_sent, settings.SEND_EMAIL_THROTTLE): send_confirm_email(user, email=address, renew=True) user.email_last_sent = timezone.now() user.save() return UserEmail(email_id=email_id, address=address, confirmed=confirmed, verified=verified, primary=primary, is_merge=is_merge)