2021-12-22 18:03:21 +00:00
|
|
|
"""Special helpers for generic collections."""
|
2024-04-15 17:43:22 +00:00
|
|
|
|
2021-12-05 14:34:46 +00:00
|
|
|
import json
|
|
|
|
|
|
2010-04-23 15:37:18 +00:00
|
|
|
from django.contrib import admin
|
|
|
|
|
from django.contrib.contenttypes.models import ContentType
|
2021-12-05 14:34:46 +00:00
|
|
|
from django.urls import NoReverseMatch, reverse
|
2010-04-23 15:37:18 +00:00
|
|
|
|
2016-02-15 21:47:21 +00:00
|
|
|
|
2010-04-23 15:37:18 +00:00
|
|
|
class GenericCollectionInlineModelAdmin(admin.options.InlineModelAdmin):
|
2021-12-22 18:03:21 +00:00
|
|
|
"""Inline admin for generic model collections."""
|
|
|
|
|
|
2010-04-23 15:37:18 +00:00
|
|
|
ct_field = "content_type"
|
|
|
|
|
ct_fk_field = "object_id"
|
2015-04-23 15:33:52 +00:00
|
|
|
|
2017-10-13 03:47:58 +00:00
|
|
|
def get_content_types(self):
|
2021-12-22 18:03:21 +00:00
|
|
|
"""Get the content types supported by this collection."""
|
2021-12-05 14:34:46 +00:00
|
|
|
ctypes = ContentType.objects.all().order_by("id").values_list("id", "app_label", "model")
|
2017-10-13 03:47:58 +00:00
|
|
|
elements = {}
|
|
|
|
|
for x, y, z in ctypes:
|
|
|
|
|
try:
|
|
|
|
|
elements[x] = reverse("admin:%s_%s_changelist" % (y, z))
|
|
|
|
|
except NoReverseMatch:
|
|
|
|
|
continue
|
|
|
|
|
return json.dumps(elements)
|
2015-04-23 15:33:52 +00:00
|
|
|
|
|
|
|
|
def get_formset(self, request, obj=None, **kwargs):
|
2021-12-22 18:03:21 +00:00
|
|
|
"""Get the formset for the generic collection."""
|
2015-04-23 15:33:52 +00:00
|
|
|
result = super(GenericCollectionInlineModelAdmin, self).get_formset(request, obj, **kwargs)
|
2017-10-13 03:47:58 +00:00
|
|
|
result.content_types = self.get_content_types()
|
2010-04-23 15:37:18 +00:00
|
|
|
result.ct_fk_field = self.ct_fk_field
|
|
|
|
|
return result
|
|
|
|
|
|
2017-10-13 03:47:58 +00:00
|
|
|
class Media:
|
2021-12-05 14:34:46 +00:00
|
|
|
js = ("contentrelations/js/genericlookup.js",)
|
2017-10-13 03:47:58 +00:00
|
|
|
|
2016-02-15 21:47:21 +00:00
|
|
|
|
2010-04-23 15:37:18 +00:00
|
|
|
class GenericCollectionTabularInline(GenericCollectionInlineModelAdmin):
|
2021-12-22 18:03:21 +00:00
|
|
|
"""Tabular model admin for a generic collection."""
|
|
|
|
|
|
2021-12-05 14:34:46 +00:00
|
|
|
template = "admin/edit_inline/gen_coll_tabular.html"
|
2010-04-23 15:37:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class GenericCollectionStackedInline(GenericCollectionInlineModelAdmin):
|
2021-12-22 18:03:21 +00:00
|
|
|
"""Stacked model admin for a generic collection."""
|
|
|
|
|
|
2021-12-05 14:34:46 +00:00
|
|
|
template = "admin/edit_inline/gen_coll_stacked.html"
|