Skip to content

Commit

Permalink
Create the ability to send different kinds of emails to Users who are…
Browse files Browse the repository at this point in the history
… either is_active or not, also allow for all combinations of settings.REREGISTRATION_SHOW_RESPONSE and settings.SEND_REREGISTRATION_EMAIL
  • Loading branch information
oliver-zhou committed Jul 12, 2017
1 parent 75f9e00 commit b3c694c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 11 deletions.
14 changes: 14 additions & 0 deletions djoser/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,20 @@ def get_context(self):
return context


class UserReregistrationInactiveEmailFactory(UserEmailFactoryBase):
subject_template_name = 'reregistration_email_subject.txt'
plain_body_template_name = 'reregistration_inactive_email_body.txt'
if settings.USE_HTML_EMAIL_TEMPLATES:
html_body_template_name = 'reregistration_inactive_email_body.html'

def get_context(self):
context = super(UserReregistrationInactiveEmailFactory, self).get_context()
context['url'] = settings.PASSWORD_RESET_CONFIRM_URL.format(
**context
)
return context


class UserConfirmationEmailFactory(UserEmailFactoryBase):
subject_template_name = 'confirmation_email_subject.txt'
plain_body_template_name = 'confirmation_email_body.txt'
Expand Down
38 changes: 27 additions & 11 deletions djoser/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,20 @@ class RegistrationView(generics.CreateAPIView):
permission_classes = (
permissions.AllowAny,
)
_users = None

def create(self, request, *args, **kwargs):
if not settings.REREGISTRATION_SHOW_RESPONSE:
try:
username_input = {User.USERNAME_FIELD: request.POST.get(User.USERNAME_FIELD)}
existing_user = User.objects.get(**username_input)
serializer = self.get_serializer(instance=existing_user)
try:
email_users = self.get_email_users(request.POST.get('email'))
for user in email_users:
serializer = self.get_serializer(instance=user)
if settings.SEND_REREGISTRATION_EMAIL:
self.send_reregistration_email(existing_user)
self.send_reregistration_email(user)
headers = self.get_success_headers(serializer.data)
if not settings.REREGISTRATION_SHOW_RESPONSE and email_users:
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
except User.DoesNotExist:
pass
except User.DoesNotExist:
pass
response = super(RegistrationView, self).create(request, *args, **kwargs)
return response

Expand Down Expand Up @@ -95,12 +96,27 @@ def send_confirmation_email(self, user):
email.send()

def send_reregistration_email(self, user):
email_factory = utils.UserReregistrationEmailFactory.from_request(
self.request, user=user
)
if user.is_active:
email_factory = utils.UserReregistrationEmailFactory.from_request(
self.request, user=user
)
else:
email_factory = utils.UserReregistrationInactiveEmailFactory.from_request(
self.request, user=user
)
email = email_factory.create()
email.send()

def get_email_users(self, email):
if self._users is None:
email_field_name = get_user_email_field_name(User)
email_users_kwargs = {
email_field_name + '__iexact': email,
}
email_users = User._default_manager.filter(**email_users_kwargs)
self._users = [u for u in email_users if u.has_usable_password()]
return self._users


class LoginView(utils.ActionViewMixin, generics.GenericAPIView):
"""
Expand Down

0 comments on commit b3c694c

Please sign in to comment.