Skip to content

Commit

Permalink
[fix] Fixed possible admin unregistration failures
Browse files Browse the repository at this point in the history
Unregister model admins only if needed to avoid errors
when other modules import openwisp_users.admin.
  • Loading branch information
nemesifier committed Mar 18, 2023
1 parent 5502369 commit 452ad6e
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions openwisp_users/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from django.conf import settings
from django.contrib import admin, messages
from django.contrib.admin.actions import delete_selected
from django.contrib.admin.sites import NotRegistered
from django.contrib.admin.utils import model_ngettext
from django.contrib.auth import get_user_model
from django.contrib.auth.admin import GroupAdmin as BaseGroupAdmin
Expand Down Expand Up @@ -646,22 +645,25 @@ def get_user(self, obj):

# unregister some admin components to keep the admin interface simple
# we can re-enable these models later when they will be really needed
admin.site.unregister(apps.get_model('account', 'EmailAddress'))
EmailAddress = apps.get_model('account', 'EmailAddress')
if admin.site.is_registered(EmailAddress):
admin.site.unregister(EmailAddress)

if allauth_settings.SOCIALACCOUNT_ENABLED:
for model in [
('socialaccount', 'SocialApp'),
('socialaccount', 'SocialToken'),
('socialaccount', 'SocialAccount'),
]:
admin.site.unregister(apps.get_model(*model))
model_class = apps.get_model(*model)
if admin.site.is_registered(model_class):
admin.site.unregister(model_class)

if 'rest_framework.authtoken' in settings.INSTALLED_APPS: # pragma: no cover
TokenProxy = apps.get_model('authtoken', 'TokenProxy')

try:
if admin.site.is_registered(TokenProxy):
admin.site.unregister(TokenProxy)
except NotRegistered:
pass


def user_not_allowed_to_change_owner(user, obj):
Expand Down

0 comments on commit 452ad6e

Please sign in to comment.