Coverage for apis_core/core/templatetags/core.py: 62%

29 statements  

« prev     ^ index     » next       coverage.py v7.6.8, created at 2024-12-20 09:24 +0000

1from django import template 

2from django.conf import settings 

3 

4from apis_core import __version__ 

5 

6register = template.Library() 

7 

8 

9@register.simple_tag 

10def shared_url(): 

11 return getattr(settings, "SHARED_URL", "/static/") 

12 

13 

14@register.simple_tag 

15def page_range(paginator, number): 

16 return paginator.get_elided_page_range(number=number) 

17 

18 

19@register.filter 

20def opts(obj): 

21 return obj._meta 

22 

23 

24@register.filter 

25def model_meta(content_type, field): 

26 return getattr(content_type.model_class()._meta, field) 

27 

28 

29@register.simple_tag 

30def apis_version(): 

31 return __version__ 

32 

33 

34@register.simple_tag 

35def git_repository_url(): 

36 return getattr( 

37 settings, "GIT_REPOSITORY_URL", "https://github.com/acdh-oeaw/apis-core-rdf/" 

38 ) 

39 

40 

41@register.simple_tag 

42def get_model_fields( 

43 model, include_parents=True, include_hidden=False, exclude_parent_links=True 

44): 

45 """ 

46 Return all fields from a model. 

47 This uses the Django built in `get_fields` method but also allows to exlude parent links. 

48 Parent links are the `_ptr` fields that point to a parent class. 

49 """ 

50 fields = model._meta.get_fields(include_parents, include_hidden) 

51 if exclude_parent_links: 

52 parent_links = model._meta.parents.values() 

53 fields = [field for field in fields if field not in parent_links] 

54 return fields