Commit af21a506a2675c18239f431c9705893d2050cee5

Authored by fbormann
1 parent f852e9fc

added support for topic filtering on reports

Showing 1 changed file with 37 additions and 12 deletions   Show diff stats
reports/views.py
... ... @@ -139,7 +139,7 @@ class ViewReportView(LoginRequiredMixin, generic.TemplateView):
139 139  
140 140 self.from_mural = params_data['from_mural']
141 141  
142   - context['data'], context['header'] = self.get_mural_data(subject, params_data['init_date'], params_data['end_date'],
  142 + context['data'], context['header'] = self.get_mural_data(subject, context['topic_name'], params_data['init_date'], params_data['end_date'],
143 143 resources, tags )
144 144  
145 145  
... ... @@ -168,7 +168,15 @@ class ViewReportView(LoginRequiredMixin, generic.TemplateView):
168 168  
169 169 return context
170 170  
171   - def get_mural_data(self, subject, init_date, end_date, resources_id, tags_id):
  171 + """
  172 + Subject: subject where the report is being created
  173 + topics_query: it's either one of the topics or all of them
  174 + init_date: When the reports filter of dates stars
  175 + end_date: When the reports filter of dates end
  176 + resources_type_names: resources subclasses name that were selected
  177 + tags_id = ID of tag objects that were selected
  178 + """
  179 + def get_mural_data(self, subject, topics_query, init_date, end_date, resources_type_names, tags_id):
172 180 data = {}
173 181 students = subject.students.all()
174 182 formats = ["%d/%m/%Y", "%m/%d/%Y", "%Y-%m-%d"] #so it accepts english and portuguese date formats
... ... @@ -179,7 +187,10 @@ class ViewReportView(LoginRequiredMixin, generic.TemplateView):
179 187  
180 188 except ValueError:
181 189 pass
182   -
  190 + if topics_query == _("All"):
  191 + topics = subject.topic_subject.all()
  192 + else:
  193 + topics = Topic.objects.get(id=topics_query)
183 194 header = ['User']
184 195  
185 196 #I use this so the system can gather data up to end_date 11h59 p.m.
... ... @@ -242,8 +253,8 @@ class ViewReportView(LoginRequiredMixin, generic.TemplateView):
242 253  
243 254  
244 255 #VAR08 through VAR_019 of documenttation:
245   - if len(resources_id) > 0:
246   - resources_data = self.get_resources_and_tags_data(resources_id, tags_id, student, subject, init_date, end_date)
  256 + if len(resources_type_names) > 0:
  257 + resources_data = self.get_resources_and_tags_data(resources_type_names, tags_id, student, subject, topics, init_date, end_date)
247 258 for key, value in resources_data.items():
248 259 interactions[key] = value
249 260  
... ... @@ -285,18 +296,32 @@ class ViewReportView(LoginRequiredMixin, generic.TemplateView):
285 296 header.append(key)
286 297 return data, header
287 298  
288   - def get_resources_and_tags_data(self, resources_types, tags, student, subject, init_date, end_date):
  299 + def get_resources_and_tags_data(self, resources_types, tags, student, subject, topics, init_date, end_date):
289 300 data = OrderedDict()
290   - print(tags)
  301 +
291 302 for i in range(len(resources_types)):
292 303  
293   - resources = Resource.objects.select_related(resources_types[i].lower()).filter(tags__in = tags, topic__in=subject.topic_subject.all())
  304 + if isinstance(topics,Topic):
  305 + resources = Resource.objects.select_related(resources_types[i].lower()).filter(tags__in = tags, topic=topics)
  306 + else:
  307 + resources = Resource.objects.select_related(resources_types[i].lower()).filter(tags__in = tags, topic__in=topics)
294 308 distinct_resources = 0
295 309 total_count = 0
  310 +
296 311 for resource in resources:
297   - count = Log.objects.filter(action="view", resource=resources_types[i].lower(),
298   - user_id = student.id, context__contains = {'subject_id': subject.id,
299   - resources_types[i].lower()+'_id': resource.id}, datetime__range=(init_date, end_date)).count()
  312 + if isinstance(topics,Topic):
  313 + #or it selected only one topic to work with
  314 + count = Log.objects.filter(action="view", resource=resources_types[i].lower(),
  315 + user_id = student.id, context__contains = {'subject_id': subject.id,
  316 + resources_types[i].lower()+'_id': resource.id, 'topic_id': topics.id}, datetime__range=(init_date, end_date)).count()
  317 +
  318 + else:
  319 + #or the user selected all
  320 +
  321 + count = Log.objects.filter(action="view", resource=resources_types[i].lower(),
  322 + user_id = student.id, context__contains = {'subject_id': subject.id,
  323 + resources_types[i].lower()+'_id': resource.id}, datetime__range=(init_date, end_date)).count()
  324 +
300 325 if count > 0:
301 326 distinct_resources += 1
302 327 total_count += count
... ... @@ -368,7 +393,7 @@ def get_tags(request):
368 393  
369 394 #adding empty tag for the purpose of giving the user this option for adicional behavior
370 395 tags = list(tags)
371   - tags.append(Tag(name=""))
  396 + tags.append(Tag(name=" "))
372 397 data['tags'] = [ {'id':tag.id, 'name':tag.name} for tag in tags]
373 398 return JsonResponse(data)
374 399  
... ...