Coverage for apis_core/history/utils.py: 22%

27 statements  

« prev     ^ index     » next       coverage.py v7.6.4, created at 2024-11-22 07:51 +0000

1from django.conf import settings 

2from django.contrib.contenttypes.models import ContentType 

3from django.db.models import Q 

4from django_tables2 import RequestConfig 

5 

6from apis_core.utils.settings import get_entity_settings_by_modelname 

7 

8 

9def triple_sidebar_history(obj: object, request, detail=True): 

10 side_bar = [] 

11 

12 triples_related_all = obj.get_triples_for_version() 

13 if obj.version_tag is not None: 

14 triples_related_all = triples_related_all.filter( 

15 Q(version_tag=obj.version_tag) 

16 | Q(version_tag__contains=f"{obj.version_tag},") 

17 ) 

18 content_type = ContentType.objects.get_for_model(obj.instance_type) 

19 if "apis_core.apis_relations" in settings.INSTALLED_APPS: 

20 from apis_core.apis_relations.tables import get_generic_triple_table 

21 from apis_core.apis_relations.utils import ( 

22 get_content_types_with_allowed_relation_from, 

23 ) 

24 

25 for other_content_type in get_content_types_with_allowed_relation_from( 

26 content_type 

27 ): 

28 triples_related_by_entity = triples_related_all.filter( 

29 ( 

30 Q(subj__self_contenttype=other_content_type) 

31 & Q(obj__pk=obj.instance.pk) 

32 ) 

33 | ( 

34 Q(obj__self_contenttype=other_content_type) 

35 & Q(subj__pk=obj.instance.pk) 

36 ) 

37 ) 

38 

39 table_class = get_generic_triple_table( 

40 other_entity_class_name=other_content_type.model, 

41 entity_pk_self=obj.instance.pk, 

42 detail=detail, 

43 ) 

44 

45 prefix = f"{other_content_type.model}" 

46 title_card = other_content_type.name 

47 tb_object = table_class(data=triples_related_by_entity, prefix=prefix) 

48 tb_object_open = request.GET.get(prefix + "page", None) 

49 entity_settings = get_entity_settings_by_modelname(content_type.model) 

50 per_page = entity_settings.get("relations_per_page", 10) 

51 RequestConfig(request, paginate={"per_page": per_page}).configure(tb_object) 

52 tab_id = f"triple_form_{content_type.model}_to_{other_content_type.model}" 

53 side_bar.append( 

54 ( 

55 title_card, 

56 tb_object, 

57 tab_id, 

58 tb_object_open, 

59 ) 

60 ) 

61 return side_bar