Coverage for apis_core/apis_entities/templatetags/apis_entities.py: 57%

21 statements  

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

1from django import template 

2from django.contrib.contenttypes.models import ContentType 

3 

4from apis_core.apis_entities.models import AbstractEntity 

5from apis_core.apis_entities.utils import get_entity_classes 

6 

7register = template.Library() 

8 

9 

10@register.simple_tag 

11def url_replace(request, field, value): 

12 dict_ = request.GET.copy() 

13 dict_[field] = value 

14 

15 return dict_.urlencode() 

16 

17 

18def is_entity(content_type: ContentType): 

19 model_class = content_type.model_class() 

20 return model_class is not None and issubclass(model_class, AbstractEntity) 

21 

22 

23@register.simple_tag 

24def entities_content_types(): 

25 """ 

26 Retrieve all models which inherit from AbstractEntity class 

27 and return their ContentType. 

28 """ 

29 entities = list( 

30 filter( 

31 lambda content_type: is_entity(content_type), 

32 ContentType.objects.all(), 

33 ) 

34 ) 

35 return entities 

36 

37 

38@register.simple_tag 

39def entities_verbose_name_plural_listview_url(): 

40 """ 

41 Return all entities verbose names together with their list uri, sorted in alphabetical order 

42 USED BY: 

43 * `core/base.html` 

44 """ 

45 ret = { 

46 entity._meta.verbose_name_plural: entity.get_listview_url() 

47 for entity in get_entity_classes() 

48 } 

49 return sorted(ret.items())