Coverage for apis_core/apis_relations/utils.py: 26%

35 statements  

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

1import itertools 

2 

3from django.contrib.contenttypes.models import ContentType 

4from django.db.models import Q 

5from django_tables2 import RequestConfig 

6 

7from apis_core.apis_relations.models import Property, TempTriple 

8from apis_core.apis_relations.tables import get_generic_triple_table 

9from apis_core.utils.settings import get_entity_settings_by_modelname 

10 

11 

12def get_content_types_with_allowed_relation_from( 

13 content_type: ContentType, 

14) -> list[ContentType]: 

15 """Returns a list of ContentTypes to which the given ContentTypes may be related by a Property""" 

16 

17 # Find all the properties where the entity is either subject or object 

18 properties_with_entity_as_subject = Property.objects.filter( 

19 subj_class=content_type 

20 ).prefetch_related("obj_class") 

21 properties_with_entity_as_object = Property.objects.filter( 

22 obj_class=content_type 

23 ).prefetch_related("subj_class") 

24 

25 content_type_querysets = [] 

26 

27 # Where entity is subject, get all the object content_types 

28 for p in properties_with_entity_as_subject: 

29 objs = p.obj_class.all() 

30 content_type_querysets.append(objs) 

31 # Where entity is object, get all the subject content_types 

32 for p in properties_with_entity_as_object: 

33 subjs = p.subj_class.all() 

34 content_type_querysets.append(subjs) 

35 

36 # Join querysets with itertools.chain, call set to make unique, and extract the model class 

37 return set(itertools.chain(*content_type_querysets)) 

38 

39 

40def triple_sidebar(obj: object, request, detail=True): 

41 content_type = ContentType.objects.get_for_model(obj) 

42 side_bar = [] 

43 

44 triples_related_all = ( 

45 TempTriple.objects_inheritance.filter(Q(subj__pk=obj.pk) | Q(obj__pk=obj.pk)) 

46 .all() 

47 .select_subclasses() 

48 ) 

49 

50 for other_content_type in get_content_types_with_allowed_relation_from( 

51 content_type 

52 ): 

53 triples_related_by_entity = triples_related_all.filter( 

54 (Q(subj__self_contenttype=other_content_type) & Q(obj__pk=obj.pk)) 

55 | (Q(obj__self_contenttype=other_content_type) & Q(subj__pk=obj.pk)) 

56 ) 

57 

58 table_class = get_generic_triple_table( 

59 other_entity_class_name=other_content_type.model, 

60 entity_pk_self=obj.pk, 

61 detail=detail, 

62 ) 

63 

64 prefix = f"{other_content_type.model}" 

65 title_card = other_content_type.name 

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

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

68 entity_settings = get_entity_settings_by_modelname(content_type.model) 

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

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

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

72 side_bar.append( 

73 ( 

74 title_card, 

75 tb_object, 

76 tab_id, 

77 tb_object_open, 

78 ) 

79 ) 

80 return side_bar