Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[bug 698092] Prevent deleting users.

* Monkeypatch away the ability to delete users from the admin.
* Add activate/deactive bulk actions.
  • Loading branch information...
commit a6351bb1ca0c378e940c58b9f588e369ea4dea46 1 parent e3234d0
James Socol authored
Showing with 26 additions and 0 deletions.
  1. +2 −0  apps/users/admin.py
  2. +24 −0 apps/users/monkeypatch.py
View
2  apps/users/admin.py
@@ -1,6 +1,7 @@
from django import forms
from django.contrib import admin
+from users import monkeypatch
from users.models import Profile
@@ -51,3 +52,4 @@ def save_model(self, request, obj, form, change):
obj.save()
admin.site.register(Profile, ProfileAdmin)
+monkeypatch.patch_user_admin()
View
24 apps/users/monkeypatch.py
@@ -0,0 +1,24 @@
+from django.contrib.auth.admin import UserAdmin
+
+
+def _activate_users(admin, request, qs):
+ num = qs.update(is_active=True)
+ msg = '%s users activated.' % num if num != 1 else 'One user activated.'
+ admin.message_user(request, msg)
+_activate_users.short_description = u'Activate selected users'
+
+
+def _deactivate_users(admin, request, qs):
+ num = qs.update(is_active=False)
+ msg = ('%s users deactivated.' % num if num != 1 else
+ 'One user deactivated.')
+ admin.message_user(request, msg)
+_deactivate_users.short_description = u'Deactivate selected users'
+
+
+def patch_user_admin():
+ """Prevent User objects from being deleted, even by superusers."""
+ if not getattr(UserAdmin, '_monkeyed', False):
+ UserAdmin._monkeyed = True
+ UserAdmin.has_delete_permission = lambda *a, **kw: False
+ UserAdmin.actions = [_activate_users, _deactivate_users]
Please sign in to comment.
Something went wrong with that request. Please try again.