From patchwork Wed Sep 1 16:57:49 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Finucane X-Patchwork-Id: 1523343 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; dkim=fail reason="key not found in DNS" header.d=that.guru header.i=@that.guru header.a=rsa-sha256 header.s=x header.b=BA/F6mS7; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.ozlabs.org (client-ip=112.213.38.117; helo=lists.ozlabs.org; envelope-from=patchwork-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org; receiver=) Received: from lists.ozlabs.org (lists.ozlabs.org [112.213.38.117]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4H09hg2vt7z9sW8 for ; Thu, 2 Sep 2021 03:16:51 +1000 (AEST) Received: from boromir.ozlabs.org (localhost [IPv6:::1]) by lists.ozlabs.org (Postfix) with ESMTP id 4H09hg1ns0z2yJT for ; Thu, 2 Sep 2021 03:16:51 +1000 (AEST) Authentication-Results: lists.ozlabs.org; dkim=fail reason="key not found in DNS" header.d=that.guru header.i=@that.guru header.a=rsa-sha256 header.s=x header.b=BA/F6mS7; dkim-atps=neutral X-Original-To: patchwork@lists.ozlabs.org Delivered-To: patchwork@lists.ozlabs.org Authentication-Results: lists.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=that.guru (client-ip=136.175.108.76; helo=mail-108-mta76.mxroute.com; envelope-from=stephen@that.guru; receiver=) Authentication-Results: lists.ozlabs.org; dkim=fail reason="key not found in DNS" header.d=that.guru header.i=@that.guru header.a=rsa-sha256 header.s=x header.b=BA/F6mS7; dkim-atps=neutral Received: from mail-108-mta76.mxroute.com (mail-108-mta76.mxroute.com [136.175.108.76]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 4H09hM5Rwvz2yR7 for ; Thu, 2 Sep 2021 03:16:35 +1000 (AEST) Received: from filter004.mxroute.com ([149.28.56.236] filter004.mxroute.com) (Authenticated sender: mN4UYu2MZsgR) by mail-108-mta76.mxroute.com (ZoneMTA) with ESMTPSA id 17ba25cc05200074ba.001 for (version=TLSv1/SSLv3 cipher=ECDHE-RSA-AES128-GCM-SHA256); Wed, 01 Sep 2021 17:16:32 +0000 X-Zone-Loop: f76c4c6955add1ddc975da23a566a87021a025667948 X-Originating-IP: [149.28.56.236] DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=that.guru; s=x; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=2ki0d7GcplR0dWcO9kWZbbcpQLlU0/0xXK9nfBfEJO4=; b=BA/F6mS7/dplfKhzx2r2qoxGC2 b/YJHgxCosYJtsa++GDJssUdEWd1PcXDfFCTV82sxmAyVLlU/loFg/NPCwV3FJEa6gXOVSMKrSceW 7p6vgrf8rzqHFTn35kaIJmfVKaYC6p60ia8QQMGw1qhXpTTXSy33tSNwRX2i+wS3DIxJtSDaDOf3m YA4kRZqMW1c3KPbDR95J2D/3GGmRwjVlfIxCizNR6ooanKwU+EaRZjhop+sexetv3YpPSypQ3jfDu NY923z73pDemhHhY0MnS9y74WY7nbXARGe5K4GJ9OruCV+JuqPaJocnZvOc27z6nEhnqPLQm7dCog BNCOJPDQ==; From: Stephen Finucane To: patchwork@lists.ozlabs.org Subject: [RFC PATCH v2 12/19] view: Simplify user opt-in, opt-out Date: Wed, 1 Sep 2021 17:57:49 +0100 Message-Id: <20210901165756.181192-13-stephen@that.guru> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210901165756.181192-1-stephen@that.guru> References: <20210901165756.181192-1-stephen@that.guru> MIME-Version: 1.0 X-AuthUser: stephen@that.guru X-BeenThere: patchwork@lists.ozlabs.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Patchwork development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: patchwork-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Patchwork" If we already have a user account associated with an email address, why make the user jump through hoops when opting in or out of emails. It's silly. Signed-off-by: Stephen Finucane --- patchwork/views/user.py | 78 +++++++++-------------------------------- 1 file changed, 16 insertions(+), 62 deletions(-) diff --git patchwork/views/user.py patchwork/views/user.py index d1a1180e..440aa38a 100644 --- patchwork/views/user.py +++ patchwork/views/user.py @@ -103,50 +103,6 @@ def register_confirm(request, conf): return render(request, 'patchwork/registration-confirm.html') -def _opt_in(request, email): - conf = EmailConfirmation(type='optin', email=email) - conf.save() - - context = {'confirmation': conf} - subject = render_to_string('patchwork/mails/optin-request-subject.txt') - message = render_to_string( - 'patchwork/mails/optin-request.txt', context, request=request) - - try: - send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [email]) - except smtplib.SMTPException: - messages.error( - request, - 'An error occurred while submitting this request. ' - 'Please contact an administrator.' - ) - return False - - return True - - -def _opt_out(request, email): - conf = EmailConfirmation(type='optout', email=email) - conf.save() - - context = {'confirmation': conf} - subject = render_to_string('patchwork/mails/optout-request-subject.txt') - message = render_to_string( - 'patchwork/mails/optout-request.txt', context, request=request) - - try: - send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [email]) - except smtplib.SMTPException: - messages.error( - request, - 'An error occurred while submitting this request. ' - 'Please contact an administrator.' - ) - return False - - return True - - def _send_confirmation_email(request, email): conf = EmailConfirmation(type='userperson', user=request.user, email=email) conf.save() @@ -228,30 +184,28 @@ def profile(request): user_email_optin_form = forms.UserEmailOptinForm( user=request.user, data=request.POST) if user_email_optin_form.is_valid(): - if _opt_in( - request, user_email_optin_form.cleaned_data['email'], - ): - messages.success( - request, - 'Requested opt-in to email from Patchwork. ' - 'Check your email for confirmation.', - ) - return HttpResponseRedirect(reverse('user-profile')) + EmailOptout.objects.filter( + email=user_email_optin_form.cleaned_data['email'], + ).delete() + messages.success( + request, + 'Opt-in into email from Patchwork.' + ) + return HttpResponseRedirect(reverse('user-profile')) messages.error(request, 'Error opting into email.') elif form_name == forms.UserEmailOptoutForm.name: user_email_optout_form = forms.UserEmailOptoutForm( user=request.user, data=request.POST) if user_email_optout_form.is_valid(): - if _opt_out( - request, user_email_optout_form.cleaned_data['email'], - ): - messages.success( - request, - 'Requested opt-out from email from Patchwork. ' - 'Check your email for confirmation.', - ) - return HttpResponseRedirect(reverse('user-profile')) + EmailOptout( + email=user_email_optout_form.cleaned_data['email'], + ).save() + messages.success( + request, + 'Opted-out from email from Patchwork.' + ) + return HttpResponseRedirect(reverse('user-profile')) messages.error(request, 'Error opting out of email.') elif form_name == UserForm.name: