Commit 482e0322 authored by Bryan  BRANCOTTE's avatar Bryan BRANCOTTE

When deleting an account, list all objects that prevent user from deleting its account

parent 832f6850
Pipeline #32786 passed with stage
in 56 seconds
{% extends 'basetheme_bootstrap/small_form_host.html' %}
{% load basetheme_bootstrap %}
{% load i18n %}
{% block content %}
{% if protected|length == 0 %}
<div class="col-12 mb-2">
{%trans "You are about to delete your account, all data linked to your account will be removed." %}
</div>
{% else %}
<div class="col-12 mb-2">
{%blocktrans%}You want to delete your account, however not all data linked to your account can be removed.<br/>Hereafter
are listed the objects preventing you account to be deleted. Please either manually delete them, or edit them so they
do not remain linked to your account.{%endblocktrans%}
</div>
<div class="col-12">
<ul>
{% for p in protected %}
<li>
{%if p.get_absolute_url %}
<a href="{{p.get_absolute_url}}" target="_blank">{{p}}</a>
{%else%}
<b>{{p}}</b><br/>
Class name:{{p|get_class}}<br/>
Primary key:{{p.pk}}
{%endif%}
</li>
{% endfor %}
</ul>
</div>
{% endif%}
{{block.super}}
{% endblock content%}
{% block form %}{% if protected|length == 0 %}{{block.super}}{%endif%}{% endblock form %}
......@@ -112,3 +112,8 @@ def basetheme_bootstrap_template_if_not_redefined(template_name):
return localized_template_name
except template.TemplateDoesNotExist:
return "basetheme_bootstrap/" + template_name
@register.filter(name='get_class')
def get_class(value):
return value.__class__.__name__
\ No newline at end of file
import logging
from django.db import router
from django import forms
from django.conf import settings
from django.contrib import messages
from django.contrib.admin.utils import NestedObjects
from django.contrib.auth import update_session_auth_hash, authenticate, login, get_user_model
from django.contrib.auth.decorators import login_required
from django.contrib.auth.forms import PasswordChangeForm
......@@ -187,13 +189,18 @@ def user_delete(request):
messages.error(request, e)
else:
form = UserDeleteForm(instance=request.user)
return render(request, 'registration/small_form_host.html', {
# Inspired from auth/admin.py#get_deleted_objects
collector = NestedObjects(using=router.db_for_write(get_user_model()))
collector.collect([request.user,])
return render(request, 'registration/account_deletion.html', {
'title': ugettext('Account deletion'),
'submit_text': ugettext('Delete account and all related data'),
'form': form,
'protected': collector.protected,
'medium_width': True,
'btn_classes': 'btn-lg btn-danger text-center',
'btn_container_classes': 'text-center',
'custom_css_width':' ',
})
......
......@@ -7,7 +7,7 @@ readme = open('README.rst').read()
setup(
name='django-basetheme-bootstrap',
version='0.2.37',
version='0.2.38',
description='Django Basetheme Bootstrap',
long_description=readme,
author='Bryan Brancotte',
......
# Generated by Django 3.0.4 on 2020-06-23 11:58
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('test_app_1', '0002_auto_20191015_1154'),
]
operations = [
migrations.CreateModel(
name='ProtectedModel',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('owner', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to=settings.AUTH_USER_MODEL)),
],
),
]
from django.conf import settings
from django.contrib.auth import get_user_model
from django.db import models
from django.utils.translation import gettext_lazy as _
from basetheme_bootstrap import user_preferences
class ProtectedModel(models.Model):
owner = models.ForeignKey(
get_user_model(),
on_delete=models.PROTECT,
)
# def get_absolute_url(self):
# return "rr"
class MyUserPreferences(user_preferences.UserPreferencesAbstractModel, models.Model):
preferences_groups = {
"First group": {"hide_help", "hide_help_3", "hide_help_4", "a_time_field"},
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment