def invite_user(request): """ Invite a new user """ moderator = request.user site = get_current_site(request) invitation_form = InviteMemberForm(request.POST) if invitation_form.is_valid(): # Invite user full_name = invitation_form.cleaned_data["full_name"] email = invitation_form.cleaned_data["email"] new_user = moderator.invite_new_user(email, full_name) # Log moderation event msg_type = ModerationLogMsg.INVITATION log_comment = _("{} invited {}".format(moderator.get_full_name(), new_user.get_full_name())) log_moderator_event(msg_type=msg_type, user=new_user, moderator=moderator, comment=log_comment) # Send email subject = _("Welcome to {}".format(site.name)) template = "moderation/emails/invite_new_user.html" token = new_user.auth_token url = request.build_absolute_uri(reverse("accounts:activate-account", args=[token])) send_connect_email(subject=subject, template=template, recipient=new_user, sender=moderator, site=site, url=url) messages.success(request, _("{} has been invited to {}.".format(new_user.get_full_name(), site.name))) return redirect("moderation:moderators") else: return moderation_home(request, invitation_form=invitation_form)
def invite_user_to_reactivate_account(user, request): """ Send an email to a user asking them if they'd like to reactivate their account. """ # Build and send a reactivation link for closed account user.auth_token = generate_unique_id() # Reset token user.auth_token_is_used = False user.save() site = get_current_site(request) url = request.build_absolute_uri( reverse('accounts:activate-account', args=[user.auth_token])) # Send email subject = _('Reactivate your {} account'.format(site.name)) template = 'accounts/emails/reactivate_account.html' send_connect_email(subject=subject, template=template, recipient=user, site=site, url=url) return user
def review_applications(request): """ Review all pending applications. """ moderator = request.user site = get_current_site(request) pending = User.objects.filter(registration_method="REQ", decision_datetime=None, is_active=False) form = ModerateApplicationForm() if request.method == "POST": form = ModerateApplicationForm(request.POST) user = get_object_or_404(User, id=request.POST["user_id"]) if form.is_valid(): decision = form.cleaned_data["decision"] comments = form.cleaned_data["comments"] if decision == "APP": confirmation_message = _( "{}'s account application " "has been approved.".format(user.get_full_name().title()) ) moderator.approve_user_application(user) # Set log and email settings msg_type = ModerationLogMsg.APPROVAL url = request.build_absolute_uri(reverse("accounts:activate-account", args=[user.auth_token])) subject = _("Welcome to {}".format(site.name)) template = "moderation/emails/approve_user.html" elif decision == "REJ": confirmation_message = _( "{}'s account application " "has been rejected.".format(user.get_full_name().title()) ) moderator.reject_user_application(user) # Set log and email settings msg_type = ModerationLogMsg.REJECTION url = "" subject = _(("Unfortunately, your application to {} " "was not successful").format(site.name)) template = "moderation/emails/reject_user.html" # Log moderation event log_comment = "{}".format(comments) log_moderator_event(msg_type=msg_type, user=user, moderator=moderator, comment=log_comment) # Send moderation email send_connect_email(subject=subject, template=template, recipient=user, sender=moderator, site=site, url=url) messages.success(request, confirmation_message) return redirect("moderation:review-applications") context = {"pending": pending, "form": form} return render(request, "moderation/review_applications.html", context)
def reinvite_user(request): """ Reinvite a user. """ moderator = request.user site = get_current_site(request) reinvitation_form = ReInviteMemberForm(request.POST, moderator=request.user) if reinvitation_form.is_valid(): user_id = reinvitation_form.cleaned_data['user_id'] user = User.objects.get(id=user_id) # We don't need to get_object_or_404 here as we've already checked # this in our form's clean method. if not user.auth_token_is_used: email = reinvitation_form.cleaned_data['email'] moderator.reinvite_user(user, email) # Log moderation event msg_type = ModerationLogMsg.REINVITATION log_comment = _('{} resent invitation to {}'.format( moderator.get_full_name(), user.get_full_name())) log_moderator_event(msg_type=msg_type, user=user, moderator=moderator, comment=log_comment) # Send email url = request.build_absolute_uri( reverse('accounts:activate-account', args=[user.auth_token])) subject = _('Activate your {} account'.format(site.name)) template = 'moderation/emails/reinvite_user.html' send_connect_email(subject=subject, template=template, recipient=user, sender=moderator, site=site, url=url) messages.success(request, _('{} has been reinvited to {}.'.format( user.get_full_name().title(), site.name))) return redirect('moderation:moderators') else: return moderation_home(request, reinvitation_form=reinvitation_form)
def send_email_to_offending_user(subject, template): """ Wrapper function for sending email to the user who the abuse report has been made against. """ send_connect_email(subject=subject, template=template, recipient=logged_against, logged_against=logged_against, site=site, comments=comments)
def report_abuse(request, user_id): """ Allow any user to report another user for abusive behaviour. """ logged_against = get_object_or_404(User, id=user_id) logged_by = request.user if request.POST: form = ReportAbuseForm(request.POST) if form.is_valid(): abuse_comment = form.cleaned_data['comments'] AbuseReport.objects.create( logged_by=logged_by, logged_against=logged_against, abuse_comment=abuse_comment, ) # Send email(s) to moderator(s) alerting them of new report. # Do not nofity moderators where the report is logged against them # or when they have made the complaint moderators = (User.objects.filter(is_moderator=True, is_active=True) .exclude(id=logged_against.id) .exclude(id=logged_by.id)) site = get_current_site(request) url = request.build_absolute_uri( reverse('moderation:review-abuse')) subject = _('New abuse report at {}'.format(site.name)) template = ( 'moderation/emails/notify_moderators_of_abuse_report.html' ) for moderator in moderators: send_connect_email(subject=subject, template=template, recipient=moderator, site=site, url=url) return redirect('moderation:abuse-report-logged') else: form = ReportAbuseForm() context = { 'form': form, 'logged_against': logged_against, 'logged_by': logged_by, } return render(request, 'moderation/report_abuse.html', context)
def report_abuse(request, user_id): """ Allow any user to report another user for abusive behaviour. """ logged_against = get_object_or_404(User, id=user_id) logged_by = request.user if request.POST: form = ReportAbuseForm(request.POST) if form.is_valid(): abuse_comment = form.cleaned_data['comments'] AbuseReport.objects.create( logged_by=logged_by, logged_against=logged_against, abuse_comment=abuse_comment, ) # Send email(s) to moderator(s) alerting them of new report. # Do not nofity moderators where the report is logged against them moderators = (User.objects.filter(is_moderator=True, is_active=True) .exclude(id=logged_against.id)) site = get_current_site(request) url = request.build_absolute_uri( reverse('moderation:review-abuse')) subject = _('New abuse report at {}'.format(site.name)) template = ( 'moderation/emails/notify_moderators_of_abuse_report.html' ) for moderator in moderators: send_connect_email(subject=subject, template=template, recipient=moderator, site=site, url=url) return redirect('moderation:abuse-report-logged') else: form = ReportAbuseForm() context = { 'form': form, 'logged_against': logged_against, 'logged_by': logged_by, } return render(request, 'moderation/report_abuse.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 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_user(request): """ Invite a new user """ moderator = request.user site = get_current_site(request) invitation_form = InviteMemberForm(request.POST) if invitation_form.is_valid(): # Invite user full_name = invitation_form.cleaned_data['full_name'] email = invitation_form.cleaned_data['email'] new_user = moderator.invite_new_user(email, full_name) # Log moderation event msg_type = ModerationLogMsg.INVITATION log_comment = _('{} invited {}'.format(moderator.get_full_name(), new_user.get_full_name())) log_moderator_event(msg_type=msg_type, user=new_user, moderator=moderator, comment=log_comment) # Send email subject = _('Welcome to {}'.format(site.name)) template = 'moderation/emails/invite_new_user.html' token = new_user.auth_token url = request.build_absolute_uri( reverse('accounts:activate-account', args=[token])) send_connect_email(subject=subject, template=template, recipient=new_user, sender=moderator, site=site, url=url) messages.success(request, _('{} has been invited to {}.'.format( new_user.get_full_name(), site.name))) return redirect('moderation:moderators') else: return moderation_home(request, invitation_form=invitation_form)
def test_can_send_connect_email(self): subject = 'Test email' template = 'emails/email_base.html' recipient = UserFactory(email='*****@*****.**') site = SiteFactory(domain='mydomain.com') site.config = SiteConfigFactory(site=site) # Sender != from email, but rather the user who has sent the message sender = UserFactory(email='*****@*****.**') url = 'http://testurl.com' comments = 'comment', logged_against = UserFactory(email='*****@*****.**') email = send_connect_email(subject, template, recipient, site, sender, url, comments, logged_against) self.assertEqual(email, 1) # send_email returns no. of emails sent
def review_applications(request): """ Review all pending applications. """ moderator = request.user site = get_current_site(request) pending = User.objects.filter(registration_method='REQ', decision_datetime=None, is_active=False) form = ModerateApplicationForm() if request.method == 'POST': form = ModerateApplicationForm(request.POST) user = get_object_or_404(User, id=request.POST['user_id']) if form.is_valid(): decision = form.cleaned_data['decision'] comments = form.cleaned_data['comments'] if decision == 'APP': confirmation_message = _("{}'s account application " "has been approved.".format( user.get_full_name().title())) moderator.approve_user_application(user) # Set log and email settings msg_type = ModerationLogMsg.APPROVAL url = request.build_absolute_uri( reverse('accounts:activate-account', args=[user.auth_token])) subject = _('Welcome to {}'.format(site.name)) template = 'moderation/emails/approve_user.html' elif decision == 'REJ': confirmation_message = _("{}'s account application " "has been rejected.".format( user.get_full_name().title())) moderator.reject_user_application(user) # Set log and email settings msg_type = ModerationLogMsg.REJECTION url = '' subject = _(('Unfortunately, your application to {} ' 'was not successful').format(site.name)) template = 'moderation/emails/reject_user.html' # Log moderation event log_comment = '{}'.format(comments) log_moderator_event(msg_type=msg_type, user=user, moderator=moderator, comment=log_comment) # Send moderation email send_connect_email(subject=subject, template=template, recipient=user, sender=moderator, site=site, url=url) messages.success(request, confirmation_message) return redirect('moderation:review-applications') context = { 'pending': pending, 'form': form, } return render(request, 'moderation/review_applications.html', context)