Repository: nextcloud/appstore

Issue:
Delete user account
A user should be able to delete his account by hitting and confirming it on the download page. The confirmation should not be able to trigger by accident, Github's delete repo ui is a good example.

Before deleting his account, a user will be warned that all his comments and apps will be deleted.



Discussion:

Resolving patch:
diff --git a/nextcloudappstore/core/user/forms.py b/nextcloudappstore/core/user/forms.py
--- a/nextcloudappstore/core/user/forms.py
+++ b/nextcloudappstore/core/user/forms.py
@@ -1,5 +1,7 @@
 from django import forms
 from captcha.fields import ReCaptchaField
+from django.forms import EmailField
+from django.utils.translation import ugettext_lazy as _
 
 
 class SignupFormRecaptcha(forms.Form):
@@ -8,3 +10,19 @@ class SignupFormRecaptcha(forms.Form):
 
     def signup(self, request, user):
         pass
+
+
+class DeleteAccountForm(forms.Form):
+    email = EmailField(required=True, label=_('Your e-mail address'))
+
+    def __init__(self, *args, **kwargs):
+        self.user = kwargs.pop('user', None)
+        super().__init__(*args, **kwargs)
+
+    def clean_email(self):
+        email = self.cleaned_data.get('email')
+        if self.user and self.user.email == email:
+            return email
+        else:
+            raise forms.ValidationError(_(
+                'The given e-mail address does not match your e-mail address'))
diff --git a/nextcloudappstore/core/user/views.py b/nextcloudappstore/core/user/views.py
--- a/nextcloudappstore/core/user/views.py
+++ b/nextcloudappstore/core/user/views.py
@@ -3,9 +3,30 @@
 from django.contrib.auth.mixins import LoginRequiredMixin
 from django.contrib.auth.models import User
 from django.core.urlresolvers import reverse_lazy
+from django.shortcuts import redirect, render
 from django.views.generic import TemplateView
 from django.views.generic import UpdateView
 
+from nextcloudappstore.core.user.forms import DeleteAccountForm
+
+
+class DeleteAccountView(LoginRequiredMixin, TemplateView):
+    template_name = 'user/delete-account.html'
+
+    def get_context_data(self, **kwargs):
+        context = super().get_context_data(**kwargs)
+        context['form'] = DeleteAccountForm()
+        context['acc_page'] = 'delete-account'
+        return context
+
+    def post(self, request, *args, **kwargs):
+        form = DeleteAccountForm(request.POST, user=request.user)
+        if form.is_valid():
+            request.user.delete()
+            return redirect(reverse_lazy('home'))
+        else:
+            return render(request, self.template_name, {'form': form})
+
 
 class AccountView(LoginRequiredMixin, UpdateView):
     """Display and allow changing of the user's name."""
diff --git a/nextcloudappstore/urls.py b/nextcloudappstore/urls.py
--- a/nextcloudappstore/urls.py
+++ b/nextcloudappstore/urls.py
@@ -4,7 +4,7 @@
 from django.conf.urls import url, include
 from django.contrib import admin
 from nextcloudappstore.core.user.views import PasswordView, AccountView, \
-    APITokenView
+    APITokenView, DeleteAccountView
 from nextcloudappstore.core.views import CategoryAppListView, AppDetailView, \
     app_description, AppReleasesView, AppUploadView, LegalNoticeView
 
@@ -19,6 +19,8 @@
         name='account-password'),
     url(r'^account/token/?$', APITokenView.as_view(),
         name='account-api-token'),
+    url(r'^account/delete/?$', DeleteAccountView.as_view(),
+        name='account-deletion'),
     url(r'^legal/?$', LegalNoticeView.as_view(), name='legal-notice'),
     url(r'^categories/(?P<id>[\w]*)/?$', CategoryAppListView.as_view(),
         name='category-app-list'),


Test patch: