Commit da7a4c8a20cd1356abbe8090fbc25c0900a2d3e9

Authored by fbormann
1 parent 232a28bc

now it gets resource type and all tags only related to that resource type filter…

…ed by topic selected on form field
reports/templates/reports/create.html
@@ -50,7 +50,7 @@ @@ -50,7 +50,7 @@
50 <script type="text/javascript" src="{% static "reports/js/report.js" %}"></script> 50 <script type="text/javascript" src="{% static "reports/js/report.js" %}"></script>
51 <script> 51 <script>
52 52
53 - var subject_id = "{{subject.id}}"; 53 +
54 $('.resource-tag-formset').formset({ 54 $('.resource-tag-formset').formset({
55 addText: 'add data source', 55 addText: 'add data source',
56 deleteText: 'remove data source', 56 deleteText: 'remove data source',
@@ -63,7 +63,8 @@ @@ -63,7 +63,8 @@
63 63
64 } 64 }
65 //Set initial tag options 65 //Set initial tag options
66 - $.get("{% url 'subjects:reports:get_tags' %}?resource_id="+fields[0].value, function(data){ 66 + var form_topic = $("select#id_topic :selected").filter(":selected").val(); //get user selected topic option
  67 + $.get("{% url 'subjects:reports:get_tags' %}?resource_class_name="+fields[0].value+"&subject_id={{subject.id}}"+"&topic_choice="+form_topic, function(data){
67 fields[1].options.length = 0; 68 fields[1].options.length = 0;
68 for(var j = 0; j < data.tags.length; j++){ 69 for(var j = 0; j < data.tags.length; j++){
69 fields[1].options[fields[1].options.length] = new Option(data.tags[j].name,data.tags[j].id); 70 fields[1].options[fields[1].options.length] = new Option(data.tags[j].name,data.tags[j].id);
@@ -72,8 +73,10 @@ @@ -72,8 +73,10 @@
72 73
73 //Modify tags fields - on resource type value modifies 74 //Modify tags fields - on resource type value modifies
74 fields[0].onchange = function(item){ 75 fields[0].onchange = function(item){
75 - //it request all tags associated with that resource  
76 - $.get("{% url 'subjects:reports:get_tags' %}?resource_id="+item.target.value, function(data){ 76 + //it request all tags associated with that resource
  77 + var form_topic = $("select#id_topic :selected").filter(":selected").val(); //get user selected topic option
  78 +
  79 + $.get("{% url 'subjects:reports:get_tags' %}?resource_class_name="+item.target.value+"&"+"subject_id={{subject.id}}"+"&topic_choice="+form_topic, function(data){
77 fields[1].options.length = 0; 80 fields[1].options.length = 0;
78 for(var j = 0; j < data.tags.length; j++){ 81 for(var j = 0; j < data.tags.length; j++){
79 fields[1].options[fields[1].options.length] = new Option(data.tags[j].name,data.tags[j].id); 82 fields[1].options[fields[1].options.length] = new Option(data.tags[j].name,data.tags[j].id);
reports/views.py
@@ -230,31 +230,36 @@ class ViewReportView(LoginRequiredMixin, generic.TemplateView): @@ -230,31 +230,36 @@ class ViewReportView(LoginRequiredMixin, generic.TemplateView):
230 230
231 231
232 def get_resources(request): 232 def get_resources(request):
233 - subject = Subject.objects.get(id=request.GET['subject_id'])  
234 233
235 - topics = subject.topic_subject.all()  
236 - #get all resources associated with topics  
237 - resources = []  
238 - tags = []  
239 - for topic in topics:  
240 - resources_set = topic.resource_topic.all()  
241 - for resource in resources_set:  
242 - for tag in resource.tags.all():  
243 - tags.append(tag)  
244 - resources.append(resource) 234 + #get all possible resources
  235 + classes = Resource.__subclasses__()
245 236
246 data = {} 237 data = {}
247 -  
248 - data['resources']= [ {'id':resource.id, 'name':resource.name} for resource in resources] 238 +
  239 +
  240 + data['resources']= [ {'id':class_name.__name__, 'name':class_name.__name__} for class_name in classes]
249 return JsonResponse(data) 241 return JsonResponse(data)
250 242
251 243
252 def get_tags(request): 244 def get_tags(request):
253 - resource = Resource.objects.get(id=request.GET['resource_id']) 245 + resource_type = request.GET['resource_class_name']
  246 + subject = Subject.objects.get(id=request.GET['subject_id'])
  247 + topic_choice = request.GET["topic_choice"]
  248 + if topic_choice.lower() == "all":
  249 + topics = subject.topic_subject.all()
  250 + else:
  251 + topics = [Topic.objects.get(id=int(topic_choice))]
254 data = {} 252 data = {}
255 tags = [] 253 tags = []
  254 + for topic in topics:
  255 + resource_set = Resource.objects.select_related(resource_type.lower()).filter(topic = topic)
  256 +
  257 + for resource in resource_set:
  258 + if resource._my_subclass == resource_type.lower():
  259 + for tag in resource.tags.all():
  260 + tags.append(tag)
  261 +
256 262
257 - for tag in resource.tags.all():  
258 - tags.append(tag) 263 +
259 data['tags'] = [ {'id':tag.id, 'name':tag.name} for tag in tags] 264 data['tags'] = [ {'id':tag.id, 'name':tag.name} for tag in tags]
260 return JsonResponse(data) 265 return JsonResponse(data)