From e1719f4773593b98000498c892bceaa690950d71 Mon Sep 17 00:00:00 2001 From: Rodolphe Breard Date: Thu, 8 Nov 2018 15:29:59 +0100 Subject: [PATCH] Add the possibility for users to delete their account --- neluser/forms.py | 15 ++++++++++ neluser/locale/en/LC_MESSAGES/django.po | 19 ++++++++++-- neluser/locale/fr/LC_MESSAGES/django.po | 19 ++++++++++-- neluser/templates/neluser/settings/base.html | 2 +- neluser/templates/neluser/settings/block.html | 4 +++ .../neluser/settings/security/base.html | 16 ++++++++-- .../settings/security/delete_account.html | 13 ++++++++ .../neluser/settings/security/password.html | 2 +- neluser/urls.py | 1 + neluser/views.py | 30 +++++++++++++++++-- 10 files changed, 109 insertions(+), 12 deletions(-) create mode 100644 neluser/templates/neluser/settings/block.html create mode 100644 neluser/templates/neluser/settings/security/delete_account.html diff --git a/neluser/forms.py b/neluser/forms.py index 088165a..28cf166 100644 --- a/neluser/forms.py +++ b/neluser/forms.py @@ -35,3 +35,18 @@ class ChangePasswordForm(forms.Form): raise forms.ValidationError(error) if not user.check_password(old_pass): raise forms.ValidationError(_('The current password is incorrect.')) + + +class DeleteAccountForm(forms.Form): + current_password = forms.CharField(label=_('current_password'), widget=forms.PasswordInput) + + def __init__(self, *args, **kwargs): + self.request = kwargs.pop('request') + return super().__init__(*args, **kwargs) + + def clean(self): + cleaned_data = super().clean() + password = cleaned_data.get('current_password') + user = self.request.user + if not user.check_password(password): + raise forms.ValidationError(_('The current password is incorrect.')) diff --git a/neluser/locale/en/LC_MESSAGES/django.po b/neluser/locale/en/LC_MESSAGES/django.po index 4e571ee..c578295 100644 --- a/neluser/locale/en/LC_MESSAGES/django.po +++ b/neluser/locale/en/LC_MESSAGES/django.po @@ -2,7 +2,7 @@ msgid "" msgstr "" "Project-Id-Version: 1.0\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2018-11-08 14:17+0100\n" +"POT-Creation-Date: 2018-11-08 15:03+0100\n" "PO-Revision-Date: 2018-02-04 01:03+0100\n" "Last-Translator: Khaganat \n" "Language-Team: Khaganat \n" @@ -12,7 +12,7 @@ msgstr "" "Content-Transfer-Encoding: 8bit\n" "Plural-Forms: nplurals=2; plural=(n != 1);\n" -#: forms.py:16 +#: forms.py:16 forms.py:41 msgid "current_password" msgstr "Please provide your current password" @@ -28,7 +28,7 @@ msgstr "Please confirm the new password" msgid "The new password does not match its confirmation." msgstr "" -#: forms.py:37 +#: forms.py:37 forms.py:52 msgid "The current password is incorrect." msgstr "" @@ -195,9 +195,22 @@ msgid "take_me_home" msgstr "Take me home" #: templates/neluser/settings/base.html:8 +#: templates/neluser/settings/security/base.html:8 msgid "security" msgstr "" +#: templates/neluser/settings/security/delete_account.html:5 +msgid "delete_account" +msgstr "Delete account" + +#: templates/neluser/settings/security/delete_account.html:11 +msgid "Permanently delete my account" +msgstr "" + #: templates/neluser/settings/security/password.html:5 msgid "Password" msgstr "" + +#: views.py:70 +msgid "Your account has been deleted." +msgstr "" diff --git a/neluser/locale/fr/LC_MESSAGES/django.po b/neluser/locale/fr/LC_MESSAGES/django.po index ca164d2..ba08856 100644 --- a/neluser/locale/fr/LC_MESSAGES/django.po +++ b/neluser/locale/fr/LC_MESSAGES/django.po @@ -2,7 +2,7 @@ msgid "" msgstr "" "Project-Id-Version: 1.0\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2018-11-08 14:17+0100\n" +"POT-Creation-Date: 2018-11-08 15:03+0100\n" "PO-Revision-Date: 2018-02-04 01:03+0100\n" "Last-Translator: Khaganat \n" "Language-Team: Khaganat \n" @@ -12,7 +12,7 @@ msgstr "" "Content-Transfer-Encoding: 8bit\n" "Plural-Forms: nplurals=2; plural=(n > 1);\n" -#: forms.py:16 +#: forms.py:16 forms.py:41 msgid "current_password" msgstr "Veuillez indiquer votre mot de passe actuel" @@ -28,7 +28,7 @@ msgstr "Veuillez confirmer le nouveau mot de passe" msgid "The new password does not match its confirmation." msgstr "Le nouveau mot de passe ne correspond pas avec sa confirmation." -#: forms.py:37 +#: forms.py:37 forms.py:52 msgid "The current password is incorrect." msgstr "Le mot de passe actuel est incorrect." @@ -200,13 +200,26 @@ msgid "take_me_home" msgstr "Retour à l'accueil" #: templates/neluser/settings/base.html:8 +#: templates/neluser/settings/security/base.html:8 msgid "security" msgstr "sécurité" +#: templates/neluser/settings/security/delete_account.html:5 +msgid "delete_account" +msgstr "Suppression du compte" + +#: templates/neluser/settings/security/delete_account.html:11 +msgid "Permanently delete my account" +msgstr "Définitivement supprimer mon compte" + #: templates/neluser/settings/security/password.html:5 msgid "Password" msgstr "Mot de passe" +#: views.py:70 +msgid "Your account has been deleted." +msgstr "Votre compte a été supprimé." + #~ msgid "NSFW content" #~ msgstr "Contenu sensible" diff --git a/neluser/templates/neluser/settings/base.html b/neluser/templates/neluser/settings/base.html index aecf5a1..3d6e241 100644 --- a/neluser/templates/neluser/settings/base.html +++ b/neluser/templates/neluser/settings/base.html @@ -9,5 +9,5 @@ {% include "neluser/settings/tab.html" with tabname="security" lnk="password_change" %} -{% block pannel %}{% endblock %} +{% block panel %}{% endblock %} {% endblock %} diff --git a/neluser/templates/neluser/settings/block.html b/neluser/templates/neluser/settings/block.html new file mode 100644 index 0000000..c895919 --- /dev/null +++ b/neluser/templates/neluser/settings/block.html @@ -0,0 +1,4 @@ +{% load i18n %} + + {% trans blockname|capfirst %} + diff --git a/neluser/templates/neluser/settings/security/base.html b/neluser/templates/neluser/settings/security/base.html index f1e1921..6bd98c2 100644 --- a/neluser/templates/neluser/settings/security/base.html +++ b/neluser/templates/neluser/settings/security/base.html @@ -1,5 +1,17 @@ {% extends "neluser/settings/base.html" %} +{% load i18n %} -{% block pannel %} -{% block inner_pannel %}{% endblock %} +{% block panel %} +
+
+ +
+
+ {% block inner_panel %}{% endblock %} +
+
{% endblock %} diff --git a/neluser/templates/neluser/settings/security/delete_account.html b/neluser/templates/neluser/settings/security/delete_account.html new file mode 100644 index 0000000..9fe0f15 --- /dev/null +++ b/neluser/templates/neluser/settings/security/delete_account.html @@ -0,0 +1,13 @@ +{% extends "neluser/settings/security/base.html" %} +{% load bulma_tags %} +{% load i18n %} + +{% block title %}{% trans "delete_account"|capfirst %}{% endblock %} + +{% block inner_panel %} +
+ {% csrf_token %} + {{ form|bulma }} + +
+{% endblock %} diff --git a/neluser/templates/neluser/settings/security/password.html b/neluser/templates/neluser/settings/security/password.html index 295956c..9d50e66 100644 --- a/neluser/templates/neluser/settings/security/password.html +++ b/neluser/templates/neluser/settings/security/password.html @@ -4,7 +4,7 @@ {% block title %}{% trans "Password"|capfirst %}{% endblock %} -{% block inner_pannel %} +{% block inner_panel %}
{% csrf_token %} {{ form|bulma }} diff --git a/neluser/urls.py b/neluser/urls.py index 0c66d64..c215c17 100644 --- a/neluser/urls.py +++ b/neluser/urls.py @@ -60,4 +60,5 @@ urlpatterns = [ # Security path('settings/security/password/', views.ChangePasswordView.as_view(), name='password_change'), + path('settings/security/delete_account/', views.DeleteAccountView.as_view(), name='delete_account'), ] diff --git a/neluser/views.py b/neluser/views.py index 47c3963..1215b02 100644 --- a/neluser/views.py +++ b/neluser/views.py @@ -4,14 +4,17 @@ from django.contrib.auth.tokens import default_token_generator from django.contrib.sites.shortcuts import get_current_site from django.contrib.auth.decorators import login_required from django.contrib.auth.mixins import LoginRequiredMixin +from django.contrib.auth import logout +from django.contrib import messages from django.template.loader import render_to_string +from django.utils.translation import gettext_lazy as _ from django.core.mail import EmailMultiAlternatives from django.views.generic.edit import FormView from django.utils.encoding import force_bytes from django.http import HttpResponse, Http404 from django.conf import settings from django.urls import reverse_lazy -from .forms import RegistrationForm, ChangePasswordForm +from .forms import RegistrationForm, ChangePasswordForm, DeleteAccountForm from .models import NelUser @@ -29,7 +32,7 @@ class ChangePasswordView(LoginRequiredMixin, FormView): def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['tab'] = 'security' - context['block'] = 'password' + context['active_block'] = 'password' return context def get_form_kwargs(self): @@ -45,6 +48,29 @@ class ChangePasswordView(LoginRequiredMixin, FormView): return super().form_valid(form) +class DeleteAccountView(LoginRequiredMixin, FormView): + template_name = 'neluser/settings/security/delete_account.html' + form_class = DeleteAccountForm + success_url = reverse_lazy('delete_account') + + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + context['tab'] = 'security' + context['active_block'] = 'delete_account' + return context + + def get_form_kwargs(self): + kw = super().get_form_kwargs() + kw['request'] = self.request + return kw + + def form_valid(self, form): + self.request.user.delete() + logout(self.request) + messages.info(self.request, _('Your account has been deleted.')) + return super().form_valid(form) + + def send_activation_email(request, user): current_site = get_current_site(request) context = {