mirror of
https://gitlab.com/allianceauth/allianceauth.git
synced 2025-07-09 12:30:15 +02:00
100 lines
3.5 KiB
Python
100 lines
3.5 KiB
Python
from __future__ import unicode_literals
|
|
|
|
from django.contrib import admin
|
|
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
|
|
from django.contrib.auth.models import User
|
|
from django.utils.text import slugify
|
|
from django import forms
|
|
from authentication.models import State, get_guest_state
|
|
from alliance_auth.hooks import get_hooks
|
|
from services.hooks import ServicesHook
|
|
|
|
|
|
def make_service_hooks_update_groups_action(service):
|
|
"""
|
|
Make a admin action for the given service
|
|
:param service: services.hooks.ServicesHook
|
|
:return: fn to update services groups for the selected users
|
|
"""
|
|
def update_service_groups(modeladmin, request, queryset):
|
|
for user in queryset: # queryset filtering doesn't work here?
|
|
service.update_groups(user)
|
|
|
|
update_service_groups.__name__ = str('update_{}_groups'.format(slugify(service.name)))
|
|
update_service_groups.short_description = "Sync groups for selected {} accounts".format(service.title)
|
|
return update_service_groups
|
|
|
|
|
|
def make_service_hooks_sync_nickname_action(service):
|
|
"""
|
|
Make a sync_nickname admin action for the given service
|
|
:param service: services.hooks.ServicesHook
|
|
:return: fn to sync nickname for the selected users
|
|
"""
|
|
def sync_nickname(modeladmin, request, queryset):
|
|
for user in queryset: # queryset filtering doesn't work here?
|
|
service.sync_nickname(user)
|
|
|
|
sync_nickname.__name__ = str('sync_{}_nickname'.format(slugify(service.name)))
|
|
sync_nickname.short_description = "Sync nicknames for selected {} accounts".format(service.title)
|
|
return sync_nickname
|
|
|
|
|
|
class UserAdmin(BaseUserAdmin):
|
|
"""
|
|
Extending Django's UserAdmin model
|
|
"""
|
|
def get_actions(self, request):
|
|
actions = super(BaseUserAdmin, self).get_actions(request)
|
|
|
|
for hook in get_hooks('services_hook'):
|
|
svc = hook()
|
|
# Check update_groups is redefined/overloaded
|
|
if svc.update_groups.__module__ != ServicesHook.update_groups.__module__:
|
|
action = make_service_hooks_update_groups_action(svc)
|
|
actions[action.__name__] = (action,
|
|
action.__name__,
|
|
action.short_description)
|
|
# Create sync nickname action if service implements it
|
|
if svc.sync_nickname.__module__ != ServicesHook.sync_nickname.__module__:
|
|
action = make_service_hooks_sync_nickname_action(svc)
|
|
actions[action.__name__] = (action,
|
|
action.__name__,
|
|
action.short_description)
|
|
|
|
return actions
|
|
|
|
# Re-register UserAdmin
|
|
try:
|
|
admin.site.unregister(User)
|
|
finally:
|
|
admin.site.register(User, UserAdmin)
|
|
|
|
|
|
class StateForm(forms.ModelForm):
|
|
def _is_none_state(self):
|
|
instance = getattr(self, 'instance', None)
|
|
if instance and instance.pk:
|
|
return instance == get_guest_state()
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
super(StateForm, self).__init__(*args, **kwargs)
|
|
if self._is_none_state():
|
|
self.fields['name'].widget.attrs['readonly'] = True
|
|
|
|
def clean_name(self):
|
|
if self._is_none_state():
|
|
return self.instance.name
|
|
return self.cleaned_data['name']
|
|
|
|
|
|
@admin.register(State)
|
|
class StateAdmin(admin.ModelAdmin):
|
|
form = StateForm
|
|
|
|
@staticmethod
|
|
def has_delete_permission(request, obj=None):
|
|
if obj == get_guest_state():
|
|
return False
|
|
|