Coverage for apis_core/apis_relations/views.py: 21%

78 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-09-16 07:42 +0000

1import json 

2 

3from django.contrib.auth.decorators import login_required 

4from django.contrib.contenttypes.models import ContentType 

5from django.http import HttpResponse 

6from django.template.loader import render_to_string 

7 

8from apis_core.apis_entities.autocomplete3 import PropertyAutocomplete 

9from apis_core.apis_entities.utils import get_entity_classes 

10from apis_core.apis_relations.forms import GenericTripleForm 

11from apis_core.apis_relations.models import Property, TempTriple, Triple 

12from apis_core.generic.views import List 

13 

14 

15class GenericRelationView(List): 

16 def setup(self, *args, **kwargs): 

17 super().setup(*args, **kwargs) 

18 if self.kwargs["entity"] == "property": 

19 self.model = Property 

20 else: 

21 self.model = Triple 

22 self.queryset = self.model.objects.all() 

23 

24 

25# TODO RDF: After full conversion to ne ajax logic, remove this function 

26@login_required 

27def get_form_ajax(request): 

28 """Returns forms rendered in html""" 

29 

30 form_name = request.POST.get("FormName") 

31 SiteID = int(request.POST.get("SiteID")) 

32 ButtonText = request.POST.get("ButtonText") 

33 ObjectID = request.POST.get("ObjectID") 

34 entity_type_self_str = request.POST.get("entity_type") 

35 

36 if ObjectID is None and form_name.startswith("triple_form_"): 

37 # If this is the case, then instantiate an empty form 

38 

39 entity_type_other_str = form_name.split("_to_")[1] 

40 

41 form = GenericTripleForm( 

42 entity_type_self_str=entity_type_self_str, 

43 entity_type_other_str=entity_type_other_str, 

44 ) 

45 

46 elif ObjectID is not None and SiteID is not None: 

47 # If this is the case, then instantiate a form and pre-load with existing data 

48 

49 triple = TempTriple.objects.get(pk=ObjectID) 

50 property_instance = triple.prop 

51 

52 if triple.subj.pk == SiteID: 

53 entity_instance_self = triple.subj 

54 entity_instance_other = triple.obj 

55 property_direction = PropertyAutocomplete.SELF_SUBJ_OTHER_OBJ_STR 

56 elif triple.obj.pk == SiteID: 

57 entity_instance_self = triple.obj 

58 entity_instance_other = triple.subj 

59 property_direction = PropertyAutocomplete.SELF_OBJ_OTHER_SUBJ_STR 

60 else: 

61 raise Exception("SiteID was not found in triple") 

62 

63 entity_type_other_str = entity_instance_other.__class__.__name__ 

64 entity_type_self_str = entity_instance_self.__class__.__name__ 

65 form_name = f"triple_form_{entity_type_self_str.lower()}_to_{entity_type_other_str.lower()}" 

66 

67 form = GenericTripleForm( 

68 entity_type_self_str=entity_type_self_str, 

69 entity_type_other_str=entity_type_other_str, 

70 ) 

71 form.load_subj_obj_prop( 

72 entity_instance_self, 

73 entity_instance_other, 

74 property_instance, 

75 property_direction, 

76 ) 

77 form.load_remaining_data_from_triple(triple) 

78 

79 else: 

80 raise Exception("Missing necessary form data") 

81 

82 param_dict = { 

83 "entity_type": entity_type_self_str, 

84 "form": form, 

85 "type1": form_name, 

86 "url2": "save_ajax_" + form_name, 

87 "button_text": ButtonText, 

88 "ObjectID": ObjectID, 

89 "SiteID": SiteID, 

90 } 

91 

92 rendered_form_str = render_to_string( 

93 "apis_relations/_ajax_form.html", # rel_form_logic_breadcrumb (for refinding the implicit connections) 

94 context=param_dict, 

95 ) 

96 

97 data = {"tab": form_name, "form": rendered_form_str} 

98 

99 return HttpResponse(json.dumps(data), content_type="application/json") 

100 

101 

102# TODO RDF: Re-implement highlighter and label form 

103@login_required 

104def save_ajax_form( 

105 request, entity_type, kind_form, SiteID, ObjectID=False 

106): # rel_form_logic_breadcrumb (for refinding the implicit connections) 

107 """Tests validity and saves AjaxForms, returns them when validity test fails""" 

108 

109 self_other = kind_form.split("triple_form_")[1].split("_to_") 

110 entity_type_self_str = self_other[0] 

111 entity_type_other_str = self_other[1] 

112 contenttypes = [ 

113 ContentType.objects.get_for_model(model) for model in get_entity_classes() 

114 ] 

115 entity_type_self_class = next( 

116 filter(lambda x: x.model == entity_type_self_str.lower(), contenttypes) 

117 ).model_class() 

118 entity_type_other_class = next( 

119 filter(lambda x: x.model == entity_type_other_str.lower(), contenttypes) 

120 ).model_class() 

121 entity_instance_self = entity_type_self_class.objects.get(pk=SiteID) 

122 entity_instance_other = entity_type_other_class.get_or_create_uri( 

123 uri=request.POST["other_entity"] 

124 ) 

125 start_date_written = request.POST["start_date_written"] 

126 end_date_written = request.POST["end_date_written"] 

127 property_param_dict = {} 

128 for param_pair in request.POST["property"].split("__"): 

129 param_pair_split = param_pair.split(":") 

130 property_param_dict[param_pair_split[0]] = param_pair_split[1] 

131 property_instance = Property.objects.get(pk=property_param_dict["id"]) 

132 property_direction = property_param_dict["direction"] 

133 

134 form = GenericTripleForm(entity_type_self_str, entity_type_other_str) 

135 form.load_subj_obj_prop( 

136 entity_instance_self, 

137 entity_instance_other, 

138 property_instance, 

139 property_direction, 

140 ) 

141 

142 if ObjectID is not False: 

143 triple = TempTriple.objects.get(pk=ObjectID) 

144 form.load_remaining_data_from_triple(triple) 

145 

146 form.load_remaining_data_from_input( 

147 start_date_written, 

148 end_date_written, 

149 ) 

150 form.fields["notes"].initial = request.POST["notes"] 

151 form.save() 

152 

153 data = { 

154 "test": True, 

155 "tab": kind_form, 

156 "call_function": "EntityRelationForm_response", 

157 "instance": form.instance.get_web_object(), 

158 "table_html": form.get_html_table( 

159 entity_instance_self, entity_instance_other, request 

160 ).as_html(request), 

161 "text": None, 

162 "right_card": True, 

163 } 

164 return HttpResponse(json.dumps(data), content_type="application/json")