From patchwork Wed Aug 11 21:36:58 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Finucane X-Patchwork-Id: 1516052 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org 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=) 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=VKTg0d8O; dkim-atps=neutral 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 4GlPCd4NdMz9sSs for ; Thu, 12 Aug 2021 08:10:53 +1000 (AEST) Received: from boromir.ozlabs.org (localhost [IPv6:::1]) by lists.ozlabs.org (Postfix) with ESMTP id 4GlPCd2g8fz3cKv for ; Thu, 12 Aug 2021 08:10:53 +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=VKTg0d8O; 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.154; helo=mail-108-mta154.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=VKTg0d8O; dkim-atps=neutral Received: from mail-108-mta154.mxroute.com (mail-108-mta154.mxroute.com [136.175.108.154]) (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 4GlPBt5TTvz3bmn for ; Thu, 12 Aug 2021 08:10:14 +1000 (AEST) Received: from filter004.mxroute.com ([149.28.56.236] filter004.mxroute.com) (Authenticated sender: mN4UYu2MZsgR) by mail-108-mta154.mxroute.com (ZoneMTA) with ESMTPSA id 17b3743f77600074ba.002 for (version=TLSv1/SSLv3 cipher=ECDHE-RSA-AES128-GCM-SHA256); Wed, 11 Aug 2021 22:10:06 +0000 X-Zone-Loop: d137a9ac0415d90c5e864a370b950057f42d8bdaaf5b 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=VKTg0d8OFLRyAU7zhw82X1RfwB YeOfxnOBeSyI1zbgj88/Ifi9dAW44oJu09LKAsgBH0UGszw2krHs/P4u9KsxTKKjnJyS9TsRgBQ5W jGkTJsMR8xVPX4LpyR3NmHDUiu0w8AdlXB3h26u8eUWaZ4SCFmsGhYUJORYKOTy5ypYNBGsLuk/IL GlqH3ENFcRdNGxZ8OGlQdL/1LRBfyJmE9oyeamZruvtMAgPuDh8kngoqpHLV2sjgOXO9RJ9HGkvG5 k/Bpq2X6lctJKF1oDrBVpeNHNMkWVvFyu9q4u7COdL8nuJugtJSbHJp8HNxHuPOlQRHmXs5qaetsC BZ05TpTg==; From: Stephen Finucane To: patchwork@lists.ozlabs.org Subject: [RFC PATCH 12/19] view: Simplify user opt-in, opt-out Date: Wed, 11 Aug 2021 22:36:58 +0100 Message-Id: <20210811213705.36293-13-stephen@that.guru> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210811213705.36293-1-stephen@that.guru> References: <20210811213705.36293-1-stephen@that.guru> MIME-Version: 1.0 X-AuthUser: stephen@that.guru X-Zone-Spam-Resolution: no action X-Zone-Spam-Status: No, score=5, required=15, tests=[ARC_NA=0, MID_CONTAINS_FROM=1, FROM_HAS_DN=0, RCPT_COUNT_THREE=0, TO_DN_SOME=0, R_MISSING_CHARSET=2.5, RCVD_COUNT_ZERO=0, FROM_EQ_ENVFROM=0, MIME_TRACE=0, BROKEN_CONTENT_TYPE=1.5, NEURAL_SPAM=0] 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: