Coverage for apis_core/generic/templatetags/generic.py: 36%
72 statements
« prev ^ index » next coverage.py v7.6.10, created at 2025-02-19 16:54 +0000
« prev ^ index » next coverage.py v7.6.10, created at 2025-02-19 16:54 +0000
1from django import template
2from django.apps import apps
3from django.contrib.contenttypes.models import ContentType
5from apis_core.core.templatetags.core import get_model_fields
6from apis_core.generic.abc import GenericModel
7from apis_core.generic.helpers import template_names_via_mro
9register = template.Library()
12@register.filter
13def contenttype(model):
14 return ContentType.objects.get_for_model(model)
17@register.simple_tag
18def modeldict(instance, fields=None, exclude=None):
19 data = {}
20 for f in get_model_fields(instance):
21 if not getattr(f, "editable", False):
22 continue
23 if fields is not None and f.name not in fields:
24 continue
25 if exclude and f.name in exclude:
26 continue
27 field = instance._meta.get_field(f.name)
28 data[field] = instance._get_FIELD_display(field)
29 if getattr(field, "remote_field", False):
30 data[field] = getattr(instance, field.name)
31 if getattr(field, "m2m_field_name", False):
32 values = getattr(instance, field.name).all()
33 data[field] = ", ".join([str(value) for value in values])
34 return data
37@register.simple_tag
38def contenttypes(app_labels=None):
39 if app_labels:
40 app_labels = app_labels.split(",")
41 return ContentType.objects.filter(app_label__in=app_labels)
42 return ContentType.objects.all()
45def is_genericmodel(content_type: ContentType):
46 model_class = content_type.model_class()
47 return model_class is not None and issubclass(model_class, GenericModel)
50@register.simple_tag
51def genericmodel_content_types():
52 """
53 Retrieve all models which inherit from GenericModel class
54 and return their ContentType.
55 """
56 genericmodels = list(
57 filter(
58 lambda content_type: is_genericmodel(content_type),
59 ContentType.objects.all(),
60 )
61 )
62 return genericmodels
65@register.simple_tag
66def pure_genericmodel_content_types():
67 """
68 Retrieve all models which inherit from GenericModel class
69 but are not Collections, Entities, Relations or History models
70 """
71 parents = []
72 if apps.is_installed("apis_core.collections"):
73 collections = apps.get_app_config("collections")
74 parents.append(collections.models_module.SkosCollection)
75 parents.append(collections.models_module.SkosCollectionContentObject)
76 if apps.is_installed("apis_core.relations"):
77 relations = apps.get_app_config("relations")
78 parents.append(relations.models_module.Relation)
79 if apps.is_installed("apis_core.history"):
80 history = apps.get_app_config("history")
81 parents.append(history.models_module.APISHistoryTableBase)
82 if apps.is_installed("apis_core.apis_entities"):
83 entities = apps.get_app_config("apis_entities")
84 parents.append(entities.models_module.AbstractEntity)
85 genericmodels = [
86 ct
87 for ct in set(genericmodel_content_types())
88 if not issubclass(ct.model_class(), tuple(parents))
89 ]
90 return genericmodels
93@register.filter
94def get_attribute(obj, attribute):
95 return getattr(obj, attribute, None)
98@register.filter
99def content_type_count(content_type):
100 """
101 Return the number of objects having a specific content type
102 """
103 return content_type.model_class().objects.count()
106@register.simple_tag
107def template_list(obj, suffix):
108 return template_names_via_mro(type(obj), suffix)
111@register.simple_tag(takes_context=True)
112def any_view_permission(context, content_types):
113 user = context.request.user
114 return any(
115 [user.has_perm(ct.model_class().get_view_permission()) for ct in content_types]
116 )