diff --git a/readthedocs/core/forms.py b/readthedocs/core/forms.py index e0753a2ec..8d0017e18 100644 --- a/readthedocs/core/forms.py +++ b/readthedocs/core/forms.py @@ -4,6 +4,7 @@ from __future__ import absolute_import from builtins import object import logging +from django.contrib.auth.models import User from haystack.forms import SearchForm from haystack.query import SearchQuerySet from django import forms @@ -44,6 +45,22 @@ class UserProfileForm(forms.ModelForm): return profile +class UserDeleteForm(forms.ModelForm): + username = CharField(label=_('Username'), help_text=_('Please type your username to confirm.')) + + class Meta(object): + model = User + fields = ['username'] + + def clean_username(self): + data = self.cleaned_data['username'] + + if self.instance.username != data: + raise forms.ValidationError(_("Username does not match!")) + + return data + + class FacetField(forms.MultipleChoiceField): """ diff --git a/readthedocs/profiles/urls/private.py b/readthedocs/profiles/urls/private.py index dc2e11883..cdebde0a7 100644 --- a/readthedocs/profiles/urls/private.py +++ b/readthedocs/profiles/urls/private.py @@ -18,4 +18,5 @@ urlpatterns = [ 'template_name': 'profiles/private/edit_profile.html', }, name='profiles_profile_edit'), + url(r'^delete/', views.delete_account, name='delete_account') ] diff --git a/readthedocs/profiles/views.py b/readthedocs/profiles/views.py index 79f2b40a8..b8cecfa7b 100644 --- a/readthedocs/profiles/views.py +++ b/readthedocs/profiles/views.py @@ -1,16 +1,21 @@ """Views for creating, editing and viewing site-specific user profiles.""" from __future__ import absolute_import + +from django.contrib import messages +from django.contrib.auth import logout from django.contrib.auth.decorators import login_required from django.contrib.auth.models import User from django.core.exceptions import ObjectDoesNotExist from django.core.urlresolvers import reverse from django.http import Http404 from django.http import HttpResponseRedirect -from django.shortcuts import get_object_or_404 +from django.shortcuts import get_object_or_404, render, redirect from django.shortcuts import render_to_response from django.template import RequestContext +from readthedocs.core.forms import UserDeleteForm + def create_profile(request, form_class, success_url=None, template_name='profiles/private/create_profile.html', @@ -183,6 +188,27 @@ def edit_profile(request, form_class, success_url=None, edit_profile = login_required(edit_profile) +@login_required() +def delete_account(request): + form = UserDeleteForm() + template_name = 'profiles/private/delete_account.html' + + if request.method == 'POST': + form = UserDeleteForm(instance=request.user, data=request.POST) + if form.is_valid(): + + # Do not delete the account permanently because it may create disaster + # Inactive the user instead. + request.user.is_active = False + request.user.save() + logout(request) + messages.info(request, 'You have successfully deleted your account') + + return redirect('homepage') + + return render(request, template_name, {'form': form}) + + def profile_detail(request, username, public_profile_field=None, template_name='profiles/public/profile_detail.html', extra_context=None): diff --git a/readthedocs/templates/profiles/base_profile_edit.html b/readthedocs/templates/profiles/base_profile_edit.html index b33d0ab2f..ef9a916e8 100644 --- a/readthedocs/templates/profiles/base_profile_edit.html +++ b/readthedocs/templates/profiles/base_profile_edit.html @@ -49,6 +49,7 @@
  • {% trans "Connected Services" %}
  • {% trans "Change Password" %}
  • {% trans "Change Email" %}
  • +
  • {% trans "Delete Account" %}
  • {% trans "Gold" %}
  • {% endblock %} diff --git a/readthedocs/templates/profiles/private/delete_account.html b/readthedocs/templates/profiles/private/delete_account.html new file mode 100644 index 000000000..6a76f5c75 --- /dev/null +++ b/readthedocs/templates/profiles/private/delete_account.html @@ -0,0 +1,17 @@ +{% extends "profiles/base_profile_edit.html" %} + +{% load i18n %} + +{% block title %}{% trans "Delete Account" %}{% endblock %} + +{% block profile-admin-delete-account %}active{% endblock %} + +{% block edit_content_header %} {% trans "Delete Account" %} {% endblock %} + +{% block edit_content %} +
    + {% csrf_token %} + {{ form }} + +
    +{% endblock %}