Add the possibility for users to delete their account

This commit is contained in:
Rodolphe Breard 2018-11-08 15:29:59 +01:00
parent a0e80623cf
commit e1719f4773
10 changed files with 109 additions and 12 deletions

View file

@ -35,3 +35,18 @@ class ChangePasswordForm(forms.Form):
raise forms.ValidationError(error) raise forms.ValidationError(error)
if not user.check_password(old_pass): if not user.check_password(old_pass):
raise forms.ValidationError(_('The current password is incorrect.')) 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.'))

View file

@ -2,7 +2,7 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: 1.0\n" "Project-Id-Version: 1.0\n"
"Report-Msgid-Bugs-To: \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" "PO-Revision-Date: 2018-02-04 01:03+0100\n"
"Last-Translator: Khaganat <assoc@khaganat.net>\n" "Last-Translator: Khaganat <assoc@khaganat.net>\n"
"Language-Team: Khaganat <assoc@khaganat.net>\n" "Language-Team: Khaganat <assoc@khaganat.net>\n"
@ -12,7 +12,7 @@ msgstr ""
"Content-Transfer-Encoding: 8bit\n" "Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n" "Plural-Forms: nplurals=2; plural=(n != 1);\n"
#: forms.py:16 #: forms.py:16 forms.py:41
msgid "current_password" msgid "current_password"
msgstr "Please provide your 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." msgid "The new password does not match its confirmation."
msgstr "" msgstr ""
#: forms.py:37 #: forms.py:37 forms.py:52
msgid "The current password is incorrect." msgid "The current password is incorrect."
msgstr "" msgstr ""
@ -195,9 +195,22 @@ msgid "take_me_home"
msgstr "Take me home" msgstr "Take me home"
#: templates/neluser/settings/base.html:8 #: templates/neluser/settings/base.html:8
#: templates/neluser/settings/security/base.html:8
msgid "security" msgid "security"
msgstr "" 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 #: templates/neluser/settings/security/password.html:5
msgid "Password" msgid "Password"
msgstr "" msgstr ""
#: views.py:70
msgid "Your account has been deleted."
msgstr ""

View file

@ -2,7 +2,7 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: 1.0\n" "Project-Id-Version: 1.0\n"
"Report-Msgid-Bugs-To: \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" "PO-Revision-Date: 2018-02-04 01:03+0100\n"
"Last-Translator: Khaganat <assoc@khaganat.net>\n" "Last-Translator: Khaganat <assoc@khaganat.net>\n"
"Language-Team: Khaganat <assoc@khaganat.net>\n" "Language-Team: Khaganat <assoc@khaganat.net>\n"
@ -12,7 +12,7 @@ msgstr ""
"Content-Transfer-Encoding: 8bit\n" "Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\n" "Plural-Forms: nplurals=2; plural=(n > 1);\n"
#: forms.py:16 #: forms.py:16 forms.py:41
msgid "current_password" msgid "current_password"
msgstr "Veuillez indiquer votre mot de passe actuel" 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." msgid "The new password does not match its confirmation."
msgstr "Le nouveau mot de passe ne correspond pas avec sa 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." msgid "The current password is incorrect."
msgstr "Le mot de passe actuel est incorrect." msgstr "Le mot de passe actuel est incorrect."
@ -200,13 +200,26 @@ msgid "take_me_home"
msgstr "Retour à l'accueil" msgstr "Retour à l'accueil"
#: templates/neluser/settings/base.html:8 #: templates/neluser/settings/base.html:8
#: templates/neluser/settings/security/base.html:8
msgid "security" msgid "security"
msgstr "sécurité" 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 #: templates/neluser/settings/security/password.html:5
msgid "Password" msgid "Password"
msgstr "Mot de passe" msgstr "Mot de passe"
#: views.py:70
msgid "Your account has been deleted."
msgstr "Votre compte a été supprimé."
#~ msgid "NSFW content" #~ msgid "NSFW content"
#~ msgstr "Contenu sensible" #~ msgstr "Contenu sensible"

View file

@ -9,5 +9,5 @@
{% include "neluser/settings/tab.html" with tabname="security" lnk="password_change" %} {% include "neluser/settings/tab.html" with tabname="security" lnk="password_change" %}
</ul> </ul>
</div> </div>
{% block pannel %}{% endblock %} {% block panel %}{% endblock %}
{% endblock %} {% endblock %}

View file

@ -0,0 +1,4 @@
{% load i18n %}
<a class="panel-block{% if active_block == blockname %} is-active{% endif %}" href="{% url lnk %}">
{% trans blockname|capfirst %}
</a>

View file

@ -1,5 +1,17 @@
{% extends "neluser/settings/base.html" %} {% extends "neluser/settings/base.html" %}
{% load i18n %}
{% block pannel %} {% block panel %}
{% block inner_pannel %}{% endblock %} <div class="columns">
<div class="column is-one-quarter">
<nav class="panel">
<p class="panel-heading">{% trans "security"|capfirst %}</p>
{% include "neluser/settings/block.html" with blockname="password" lnk="password_change" %}
{% include "neluser/settings/block.html" with blockname="delete_account" lnk="delete_account" %}
</nav>
</div>
<div class="column">
{% block inner_panel %}{% endblock %}
</div>
</div>
{% endblock %} {% endblock %}

View file

@ -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 %}
<form action="" method="post">
{% csrf_token %}
{{ form|bulma }}
<button type="submit" class="button is-danger">{% trans "Permanently delete my account"|capfirst %}</button>
</form>
{% endblock %}

View file

@ -4,7 +4,7 @@
{% block title %}{% trans "Password"|capfirst %}{% endblock %} {% block title %}{% trans "Password"|capfirst %}{% endblock %}
{% block inner_pannel %} {% block inner_panel %}
<form action="" method="post"> <form action="" method="post">
{% csrf_token %} {% csrf_token %}
{{ form|bulma }} {{ form|bulma }}

View file

@ -60,4 +60,5 @@ urlpatterns = [
# Security # Security
path('settings/security/password/', views.ChangePasswordView.as_view(), name='password_change'), path('settings/security/password/', views.ChangePasswordView.as_view(), name='password_change'),
path('settings/security/delete_account/', views.DeleteAccountView.as_view(), name='delete_account'),
] ]

View file

@ -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.sites.shortcuts import get_current_site
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import LoginRequiredMixin 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.template.loader import render_to_string
from django.utils.translation import gettext_lazy as _
from django.core.mail import EmailMultiAlternatives from django.core.mail import EmailMultiAlternatives
from django.views.generic.edit import FormView from django.views.generic.edit import FormView
from django.utils.encoding import force_bytes from django.utils.encoding import force_bytes
from django.http import HttpResponse, Http404 from django.http import HttpResponse, Http404
from django.conf import settings from django.conf import settings
from django.urls import reverse_lazy from django.urls import reverse_lazy
from .forms import RegistrationForm, ChangePasswordForm from .forms import RegistrationForm, ChangePasswordForm, DeleteAccountForm
from .models import NelUser from .models import NelUser
@ -29,7 +32,7 @@ class ChangePasswordView(LoginRequiredMixin, FormView):
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs) context = super().get_context_data(**kwargs)
context['tab'] = 'security' context['tab'] = 'security'
context['block'] = 'password' context['active_block'] = 'password'
return context return context
def get_form_kwargs(self): def get_form_kwargs(self):
@ -45,6 +48,29 @@ class ChangePasswordView(LoginRequiredMixin, FormView):
return super().form_valid(form) 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): def send_activation_email(request, user):
current_site = get_current_site(request) current_site = get_current_site(request)
context = { context = {