Commit da7a4c8a20cd1356abbe8090fbc25c0900a2d3e9
1 parent
232a28bc
Exists in
master
and in
3 other branches
now it gets resource type and all tags only related to that resource type filter…
…ed by topic selected on form field
Showing
2 changed files
with
28 additions
and
20 deletions
Show diff stats
reports/templates/reports/create.html
| ... | ... | @@ -50,7 +50,7 @@ |
| 50 | 50 | <script type="text/javascript" src="{% static "reports/js/report.js" %}"></script> |
| 51 | 51 | <script> |
| 52 | 52 | |
| 53 | - var subject_id = "{{subject.id}}"; | |
| 53 | + | |
| 54 | 54 | $('.resource-tag-formset').formset({ |
| 55 | 55 | addText: 'add data source', |
| 56 | 56 | deleteText: 'remove data source', |
| ... | ... | @@ -63,7 +63,8 @@ |
| 63 | 63 | |
| 64 | 64 | } |
| 65 | 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 | 68 | fields[1].options.length = 0; |
| 68 | 69 | for(var j = 0; j < data.tags.length; j++){ |
| 69 | 70 | fields[1].options[fields[1].options.length] = new Option(data.tags[j].name,data.tags[j].id); |
| ... | ... | @@ -72,8 +73,10 @@ |
| 72 | 73 | |
| 73 | 74 | //Modify tags fields - on resource type value modifies |
| 74 | 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 | 80 | fields[1].options.length = 0; |
| 78 | 81 | for(var j = 0; j < data.tags.length; j++){ |
| 79 | 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 | 230 | |
| 231 | 231 | |
| 232 | 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 | 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 | 241 | return JsonResponse(data) |
| 250 | 242 | |
| 251 | 243 | |
| 252 | 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 | 252 | data = {} |
| 255 | 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 | 264 | data['tags'] = [ {'id':tag.id, 'name':tag.name} for tag in tags] |
| 260 | 265 | return JsonResponse(data) | ... | ... |