def link(request): context = {} if request.method == 'POST': form = EmailForm(request.POST) if form.is_valid(): conf = EmailConfirmation(type='userperson', user=request.user, email=form.cleaned_data['email']) conf.save() context['confirmation'] = conf try: send_mail( 'Patchwork email address confirmation', render_to_string('patchwork/user-link.mail', context, request=request), settings.DEFAULT_FROM_EMAIL, [form.cleaned_data['email']]) except smtplib.SMTPException: context['confirmation'] = None context['error'] = ('An error occurred during confirmation. ' 'Please try again later') else: form = EmailForm() context['linkform'] = form return render(request, 'patchwork/user-link.html', context)
def link(request): context = {} if request.method == 'POST': form = EmailForm(request.POST) if form.is_valid(): conf = EmailConfirmation(type='userperson', user=request.user, email=form.cleaned_data['email']) conf.save() context['confirmation'] = conf try: send_mail('Patchwork email address confirmation', render_to_string('patchwork/user-link.mail', context, request=request), settings.DEFAULT_FROM_EMAIL, [form.cleaned_data['email']]) except smtplib.SMTPException: context['confirmation'] = None context['error'] = ('An error occurred during confirmation. ' 'Please try again later') else: form = EmailForm() context['linkform'] = form return render(request, 'patchwork/user-link.html', context)
def register(request): context = {} if request.method == 'POST': form = RegistrationForm(request.POST) if form.is_valid(): data = form.cleaned_data # create inactive user user = auth.models.User.objects.create_user( data['username'], data['email'], data['password']) user.is_active = False user.first_name = data.get('first_name', '') user.last_name = data.get('last_name', '') user.save() # create confirmation conf = EmailConfirmation(type='registration', user=user, email=user.email) conf.save() # send email subject = 'Patchwork account confirmation' message = render_to_string('patchwork/activation_email.txt', { 'site': Site.objects.get_current(), 'confirmation': conf }) send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [conf.email]) # setting 'confirmation' in the template indicates success context['confirmation'] = conf else: form = RegistrationForm() context['form'] = form return render(request, 'patchwork/registration_form.html', context)
def _optinout(request, action, description): context = {} mail_template = 'patchwork/%s-request.mail' % action html_template = 'patchwork/%s-request.html' % action if request.method != 'POST': return HttpResponseRedirect(reverse(settings)) form = EmailForm(data=request.POST) if not form.is_valid(): context['error'] = ('There was an error in the %s form. Please ' 'review the form and re-submit.' % description) context['form'] = form return render(request, html_template, context) email = form.cleaned_data['email'] if action == 'optin' and \ EmailOptout.objects.filter(email=email).count() == 0: context['error'] = ("The email address %s is not on the patchwork " "opt-out list, so you don't need to opt back in" % email) context['form'] = form return render(request, html_template, context) conf = EmailConfirmation(type=action, email=email) conf.save() context['confirmation'] = conf mail = render_to_string(mail_template, context, request=request) try: send_mail('Patchwork %s confirmation' % description, mail, conf_settings.DEFAULT_FROM_EMAIL, [email]) context['email_sent'] = True except smtplib.SMTPException: context['error'] = ('An error occurred during confirmation . ' 'Please try again later.') context['admins'] = conf_settings.ADMINS return render(request, html_template, context)
def _optinout(request, action, description): context = {} mail_template = 'patchwork/%s-request.mail' % action html_template = 'patchwork/%s-request.html' % action if request.method != 'POST': return HttpResponseRedirect(reverse(settings)) form = OptinoutRequestForm(data=request.POST) if not form.is_valid(): context['error'] = ('There was an error in the %s form. Please ' 'review the form and re-submit.' % description) context['form'] = form return render(request, html_template, context) email = form.cleaned_data['email'] if action == 'optin' and \ EmailOptout.objects.filter(email=email).count() == 0: context['error'] = ("The email address %s is not on the patchwork " "opt-out list, so you don't need to opt back in" % email) context['form'] = form return render(request, html_template, context) conf = EmailConfirmation(type=action, email=email) conf.save() context['confirmation'] = conf mail = render_to_string(mail_template, context, request=request) try: send_mail('Patchwork %s confirmation' % description, mail, conf_settings.DEFAULT_FROM_EMAIL, [email]) context['email_sent'] = True except Exception: context['error'] = ('An error occurred during confirmation . ' 'Please try again later.') context['admins'] = conf_settings.ADMINS return render(request, html_template, context)
def register(request): context = {} if request.method == 'POST': form = RegistrationForm(request.POST) if form.is_valid(): data = form.cleaned_data # create inactive user user = auth.models.User.objects.create_user(data['username'], data['email'], data['password']) user.is_active = False user.first_name = data.get('first_name', '') user.last_name = data.get('last_name', '') user.save() # create confirmation conf = EmailConfirmation(type='registration', user=user, email=user.email) conf.save() # send email subject = 'Patchwork account confirmation' message = render_to_string( 'patchwork/activation_email.txt', {'site': Site.objects.get_current(), 'confirmation': conf}) send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [conf.email]) # setting 'confirmation' in the template indicates success context['confirmation'] = conf else: form = RegistrationForm() context['form'] = form return render(request, 'patchwork/registration_form.html', context)
def send_notifications(): date_limit = datetime.datetime.now() - datetime.timedelta( minutes=settings.NOTIFICATION_DELAY_MINUTES) # We delay sending notifications to a user if they have other # notifications that are still in the "pending" state. To do this, # we compare the total number of patch change notifications queued # for each user against the number of "ready" notifications. qs = PatchChangeNotification.objects.all() qs2 = PatchChangeNotification.objects\ .filter(last_modified__lt=date_limit)\ .values('patch__submitter')\ .annotate(count=Count('patch__submitter')) qs2 = {elem['patch__submitter']: elem['count'] for elem in qs2} groups = itertools.groupby(qs.order_by('patch__submitter'), lambda n: n.patch.submitter) errors = [] for (recipient, notifications) in groups: notifications = list(notifications) if recipient.id not in qs2 or qs2[recipient.id] < len(notifications): continue projects = set([n.patch.project.linkname for n in notifications]) def delete_notifications(): pks = [n.pk for n in notifications] PatchChangeNotification.objects.filter(pk__in=pks).delete() if EmailOptout.is_optout(recipient.email): delete_notifications() continue context = { 'site': Site.objects.get_current(), 'notifications': notifications, 'projects': projects, } subject = render_to_string( 'patchwork/patch-change-notification-subject.text', context).strip() content = render_to_string('patchwork/patch-change-notification.mail', context) message = EmailMessage(subject=subject, body=content, from_email=settings.NOTIFICATION_FROM_EMAIL, to=[recipient.email], headers={'Precedence': 'bulk'}) try: message.send() except Exception as ex: errors.append((recipient, ex)) continue delete_notifications() return errors
def send_notifications(): date_limit = datetime.datetime.now() - datetime.timedelta( minutes=settings.NOTIFICATION_DELAY_MINUTES) # We delay sending notifications to a user if they have other # notifications that are still in the "pending" state. To do this, # we compare the total number of patch change notifications queued # for each user against the number of "ready" notifications. qs = PatchChangeNotification.objects.all() qs2 = PatchChangeNotification.objects\ .filter(last_modified__lt=date_limit)\ .values('patch__submitter')\ .annotate(count=Count('patch__submitter')) qs2 = {elem['patch__submitter']: elem['count'] for elem in qs2} groups = itertools.groupby(qs.order_by('patch__submitter'), lambda n: n.patch.submitter) errors = [] for (recipient, notifications) in groups: notifications = list(notifications) if recipient.id not in qs2 or qs2[recipient.id] < len(notifications): continue projects = set([n.patch.project.linkname for n in notifications]) def delete_notifications(): pks = [n.pk for n in notifications] PatchChangeNotification.objects.filter(pk__in=pks).delete() if EmailOptout.is_optout(recipient.email): delete_notifications() continue context = { 'site': Site.objects.get_current(), 'notifications': notifications, 'projects': projects, } subject = render_to_string( 'patchwork/patch-change-notification-subject.text', context).strip() content = render_to_string('patchwork/patch-change-notification.mail', context) message = EmailMessage(subject=subject, body=content, from_email=settings.NOTIFICATION_FROM_EMAIL, to=[recipient.email], headers={'Precedence': 'bulk'}) try: message.send() except smtplib.SMTPException as ex: errors.append((recipient, ex)) continue delete_notifications() return errors