def test_create_inactive_user(self): user = create_inactive_user('*****@*****.**', 'first last') moderators = Group.objects.get(name='moderators') self.assertEqual(user.email, '*****@*****.**') self.assertEqual(user.full_name, 'first last') self.assertEqual(user.is_active, False) self.assertEqual(user.is_moderator, False) self.assertNotIn(moderators, user.groups.all())
def request_invitation(request): """ Allow a member of the public to request an account invitation. """ site = get_current_site(request) if request.method == 'POST': form = RequestInvitationForm(request.POST, request=request) if form.is_valid(): # Create inactive user first_name = form.cleaned_data['first_name'] last_name = form.cleaned_data['last_name'] email = form.cleaned_data['email'] new_user = create_inactive_user(email, first_name, last_name) # Add additional details comments = form.cleaned_data['comments'] new_user.registration_method = new_user.REQUESTED new_user.applied_datetime = now() new_user.application_comments = comments new_user.save() # Send email(s) to moderator(s) alerting them of new # account application moderators = User.objects.filter(is_moderator=True, is_active=True) url = request.build_absolute_uri(reverse( 'moderation:review-applications')) subject = _('New account request at {}'.format(site.name)) template = ( 'moderation/emails/notify_moderators_of_new_application.html' ) for moderator in moderators: send_connect_email(subject=subject, template=template, recipient=moderator, site=site, url=url) return redirect('accounts:request-invitation-done') else: form = RequestInvitationForm(request=request) context = { 'form': form, } return render(request, 'accounts/request_invitation.html', context)
def request_invitation(request): """ Allow a member of the public to request an account invitation. """ site = get_current_site(request) if request.method == 'POST': form = RequestInvitationForm(request.POST, request=request) if form.is_valid(): # Create inactive user full_name = form.cleaned_data['full_name'] email = form.cleaned_data['email'] new_user = create_inactive_user(email, full_name) # Add additional details comments = form.cleaned_data['comments'] new_user.registration_method = new_user.REQUESTED new_user.applied_datetime = now() new_user.application_comments = comments new_user.save() # Send email(s) to moderator(s) alerting them of new # account application moderators = User.objects.filter(is_moderator=True, is_active=True) url = request.build_absolute_uri( reverse('moderation:review-applications')) subject = _('New account request at {}'.format(site.name)) template = ( 'moderation/emails/notify_moderators_of_new_application.html') for moderator in moderators: send_connect_email(subject=subject, template=template, recipient=moderator, site=site, url=url) return redirect('accounts:request-invitation-done') else: form = RequestInvitationForm(request=request) context = { 'form': form, } return render(request, 'accounts/request_invitation.html', context)
def invite_new_user(self, email, full_name): """ Invite an inactive user (who needs to activate their account). Returns none if user already exists. """ User = get_user_model() if self.is_moderator and self.has_perm('accounts.invite_user'): try: User.objects.get(email=email) except User.DoesNotExist: new_user = create_inactive_user(email, full_name) new_user.registration_method = new_user.INVITED new_user.moderator = self new_user.moderator_decision = new_user.PRE_APPROVED new_user.decision_datetime = timezone.now() new_user.auth_token = generate_unique_id() new_user.save() return new_user else: return None else: raise PermissionDenied