Ticket #8159: 8159.diff

File 8159.diff, 3.2 KB (added by Graham King, 16 years ago)

Adds a warning when deleting yourself, and doesn't log the delete and set the message, so the foreignkey doesn't break

  • django/contrib/admin/templates/admin/auth/user/delete_confirmation.html

     
     1{% extends "admin/delete_confirmation.html" %}
     2{% load i18n %}
     3
     4{% block content %}
     5{% ifequal user object %}
     6<ul class="errorlist">
     7        <li>{% trans "You are attempting to delete yourself" %}</li>
     8</ul>
     9{% endifequal %}
     10{{block.super}}
     11{% endblock %}
  • django/contrib/auth/admin.py

     
    108108            'root_path': self.admin_site.root_path,
    109109        }, context_instance=RequestContext(request))
    110110
     111    def log_deletion(self, request, object, object_repr):
     112        'Do not record if the current user id is None, i.e. it has been deleted - ticket #8159'
     113       
     114        if request.user.id:
     115            super(UserAdmin, self).log_deletion(request, object, object_repr)
     116   
     117    def message_user(self, request, message):
     118        'Do not message if the current user id is None, i.e. it has been deleted - ticket #8159'
     119       
     120        if request.user.id:
     121            super(UserAdmin, self).message_user(request, message)
    111122
     123
    112124admin.site.register(Group, GroupAdmin)
    113125admin.site.register(User, UserAdmin)
    114126
  • django/contrib/auth/__init__.py

     
    11import datetime
    22from django.core.exceptions import ImproperlyConfigured
     3from django.db.models.signals import post_delete
    34
    45SESSION_KEY = '_auth_user_id'
    56BACKEND_SESSION_KEY = '_auth_user_backend'
     
    7879        request.user = AnonymousUser()
    7980
    8081def get_user(request):
    81     from django.contrib.auth.models import AnonymousUser
     82    from django.contrib.auth.models import User, AnonymousUser
    8283    try:
    8384        user_id = request.session[SESSION_KEY]
    8485        backend_path = request.session[BACKEND_SESSION_KEY]
     
    8687        user = backend.get_user(user_id) or AnonymousUser()
    8788    except KeyError:
    8889        user = AnonymousUser()
     90       
     91    if not user.is_anonymous():
     92        post_delete.connect(user.check_if_deleted, sender=User)
     93
    8994    return user
  • django/contrib/auth/models.py

     
    287287                raise SiteProfileNotAvailable
    288288        return self._profile_cache
    289289
     290    def check_if_deleted(self, sender, **kwargs):
     291        'Check if this User has just been deleted, and set our id to None if so'
     292        instance = kwargs['instance']
     293        id_name = User._meta.pk.attname
     294        if getattr(self, id_name) == getattr(instance, id_name):
     295            setattr(self, id_name, None)
     296
     297
    290298class Message(models.Model):
    291299    """
    292300    The message system is a lightweight way to queue messages for given
Back to Top