|
|
|
@ -8,8 +8,10 @@ from django.http import HttpResponse, HttpResponseRedirect
|
|
|
|
from django.urls import path, reverse
|
|
|
|
from django.urls import path, reverse
|
|
|
|
from django.db import models
|
|
|
|
from django.db import models
|
|
|
|
from django.contrib.admin import helpers, widgets
|
|
|
|
from django.contrib.admin import helpers, widgets
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
import rules.contrib.admin
|
|
|
|
import rules.contrib.admin
|
|
|
|
from rules.permissions import perm_exists
|
|
|
|
from rules.permissions import perm_exists
|
|
|
|
|
|
|
|
from utils import OrderedSet
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class FieldPermissionsAdminMixin:
|
|
|
|
class FieldPermissionsAdminMixin:
|
|
|
|
@ -174,6 +176,62 @@ class CommonAdminMixin(FieldPermissionsAdminMixin, ChangeViewAdminMixin, Filtere
|
|
|
|
|
|
|
|
|
|
|
|
# For any other type of field, just call its formfield() method.
|
|
|
|
# For any other type of field, just call its formfield() method.
|
|
|
|
return db_field.formfield(**kwargs)
|
|
|
|
return db_field.formfield(**kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
|
|
|
def field_key(self):
|
|
|
|
|
|
|
|
return f"{self.model._meta.app_label}_{self.model.__name__}".lower()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_excluded_fields(self):
|
|
|
|
|
|
|
|
return OrderedSet(settings.CUSTOM_MODEL_FIELDS.get(self.field_key, {}).get('exclude', []))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_included_fields(self):
|
|
|
|
|
|
|
|
return OrderedSet(settings.CUSTOM_MODEL_FIELDS.get(self.field_key, {}).get('fields', []))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_fieldsets(self, request, obj=None):
|
|
|
|
|
|
|
|
included = self.get_included_fields()
|
|
|
|
|
|
|
|
excluded = self.get_excluded_fields()
|
|
|
|
|
|
|
|
original_fieldsets = super().get_fieldsets(request, obj)
|
|
|
|
|
|
|
|
if original_fieldsets:
|
|
|
|
|
|
|
|
print(f"get_fieldsets called for {self.field_key}")
|
|
|
|
|
|
|
|
print(f"Original fieldsets: {original_fieldsets}")
|
|
|
|
|
|
|
|
new_fieldsets = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for title, attrs in original_fieldsets:
|
|
|
|
|
|
|
|
fields = attrs.get("fields", [])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Flatten groupings like tuples if needed
|
|
|
|
|
|
|
|
filtered_fields = [
|
|
|
|
|
|
|
|
f for f in fields
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
|
|
|
(not included or f in included)
|
|
|
|
|
|
|
|
and f not in excluded
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if filtered_fields:
|
|
|
|
|
|
|
|
new_fieldsets.append((title, dict(attrs, **{"fields": filtered_fields})))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if new_fieldsets:
|
|
|
|
|
|
|
|
print(f"Filtered fieldsets: {new_fieldsets}")
|
|
|
|
|
|
|
|
return new_fieldsets
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_fields(self, request, obj=None):
|
|
|
|
|
|
|
|
fields = OrderedSet(super().get_fields(request, obj) or [])
|
|
|
|
|
|
|
|
custom_fields = self.get_included_fields() - self.get_excluded_fields()
|
|
|
|
|
|
|
|
if custom_fields:
|
|
|
|
|
|
|
|
print(f"get_fields called for {self.field_key}, fields: {fields}, custom_fields: {custom_fields}")
|
|
|
|
|
|
|
|
return list(custom_fields)
|
|
|
|
|
|
|
|
return list(fields)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_exclude(self, request, obj=None):
|
|
|
|
|
|
|
|
excluded = OrderedSet(super().get_exclude(request, obj) or [])
|
|
|
|
|
|
|
|
custom_excluded = self.get_excluded_fields() - self.get_included_fields()
|
|
|
|
|
|
|
|
if custom_excluded:
|
|
|
|
|
|
|
|
print(f"get_exclude called for {self.field_key}, excluded: {excluded}, custom_excluded: {custom_excluded}")
|
|
|
|
|
|
|
|
return list(excluded | custom_excluded)
|
|
|
|
|
|
|
|
return list(excluded)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CommonAdminInlineMixin(CommonAdminMixin):
|
|
|
|
class CommonAdminInlineMixin(CommonAdminMixin):
|
|
|
|
|