|
| 1 | +from django.contrib import admin, messages |
| 2 | +from django.core.cache import cache |
| 3 | +from django.urls import path |
| 4 | +from django.shortcuts import redirect |
| 5 | + |
| 6 | +from .service import KeycloakService |
| 7 | + |
| 8 | + |
| 9 | +class UserGroupAdmin(admin.ModelAdmin): |
| 10 | + list_display = ("path",) |
| 11 | + search_fields = ("id", "path") |
| 12 | + readonly_fields = ("id", "path") |
| 13 | + |
| 14 | + # To show ManyToMany fields with a horizontal filter widget |
| 15 | + # filter_horizontal = ("allowed_entities",) |
| 16 | + |
| 17 | + change_list_template = "user_groups_changelist.html" |
| 18 | + |
| 19 | + # To show fields in this order in the detail view |
| 20 | + # fieldsets = ( |
| 21 | + # ( |
| 22 | + # None, |
| 23 | + # { |
| 24 | + # "fields": ( |
| 25 | + # "id", |
| 26 | + # "path", |
| 27 | + # "allow_all_entities", |
| 28 | + # "allowed_entities", |
| 29 | + # ) |
| 30 | + # }, |
| 31 | + # ), |
| 32 | + # ) |
| 33 | + |
| 34 | + def has_add_permission(self, request): |
| 35 | + # Manual addition of user groups is not allowed |
| 36 | + return False |
| 37 | + |
| 38 | + def has_delete_permission(self, request, obj=None): |
| 39 | + # Manual deletion of user groups is not allowed |
| 40 | + return False |
| 41 | + |
| 42 | + # To show annotated fields in the list view |
| 43 | + # def get_queryset(self, request): |
| 44 | + # return ( |
| 45 | + # super() |
| 46 | + # .get_queryset(request) |
| 47 | + # .annotate(allowed_job_configs_count=Count("allowed_job_configs")) |
| 48 | + # ) |
| 49 | + |
| 50 | + def get_urls(self): |
| 51 | + return [ |
| 52 | + path("sync-with-keycloak/", self.sync_groups_with_keycloak), |
| 53 | + *super().get_urls(), |
| 54 | + ] |
| 55 | + |
| 56 | + @staticmethod |
| 57 | + def sync_groups_with_keycloak(request): # noqa |
| 58 | + """Syncs user groups with Keycloak""" |
| 59 | + |
| 60 | + try: |
| 61 | + KeycloakService().sync_user_groups(raise_exceptions=True) |
| 62 | + messages.success(request, "User groups synced successfully.") |
| 63 | + except Exception as e: |
| 64 | + messages.error(request, f"Error syncing user groups: {e}") |
| 65 | + |
| 66 | + return redirect("..") |
| 67 | + |
| 68 | + # To allow sorting by annotated fields |
| 69 | + # @admin.display( |
| 70 | + # description="Allowed Job Configs", ordering="allowed_job_configs_count" |
| 71 | + # ) |
| 72 | + # def allowed_job_configs_count(self, obj): |
| 73 | + # return obj.allowed_job_configs_count |
| 74 | + |
| 75 | + def changelist_view(self, request, extra_context=None): |
| 76 | + """ |
| 77 | + When the list view is accessed, sync the user groups from Keycloak. |
| 78 | + Cache the sync for 10 minutes to avoid excessive requests. |
| 79 | + """ |
| 80 | + cache_key = "keycloak_group_sync_lock" |
| 81 | + |
| 82 | + if cache.get(cache_key) is None: |
| 83 | + KeycloakService().sync_user_groups() |
| 84 | + cache.set(cache_key, "true", 60 * 10) |
| 85 | + |
| 86 | + return super().changelist_view(request, extra_context) |
0 commit comments