Commit f88db7da53c71516561bcc5053b9479aa81e50aa

Authored by fbormann
1 parent ade5a5e2

removed links and queries from api app and moved everything to reports app

api/templates/api/report.html
... ... @@ -1,31 +0,0 @@
1   -{% extends 'base.html' %}
2   -
3   -{% load static i18n pagination %}
4   -{% load django_bootstrap_breadcrumbs %}
5   -
6   -{% block breadcrumbs %}
7   - {{ block.super }}
8   -
9   - {% trans 'Mural: General' as general %}
10   -
11   - {% breadcrumb general 'mural:manage_general' %}
12   -{% endblock %}
13   -
14   -{% block content %}
15   -
16   -
17   - {% for user, datum in data.items %}
18   -
19   -
20   - <p>
21   - <ul>
22   -
23   - {{user}} :
24   - {% for key, value in datum.items %}
25   - <li>{{key}}: {{value}}</li>
26   - {% endfor %}
27   - </ul>
28   -
29   - </p>
30   - {% endfor %}
31   -{% endblock content %}
32 0 \ No newline at end of file
api/urls.py
... ... @@ -14,7 +14,6 @@ router.register(r&#39;usersapi&#39;, UserViewSet)
14 14  
15 15 urlpatterns = [
16 16 #API REST
17   - url(r'^report/$', views.ReportView.as_view(), name='report'),
18 17  
19 18 url(r'^', include(router.urls)),
20 19 ]
21 20 \ No newline at end of file
... ...
api/views.py
1 1 from django.shortcuts import render
2   -from django.http import HttpResponse, JsonResponse
3   -
4   -
5   -import django.views.generic as generic
6   -from mural.models import SubjectPost, Comment, MuralVisualizations
7   -from django.db.models import Q
8   -from django.contrib.auth.mixins import LoginRequiredMixin
9   -from datetime import datetime, date
10   -from subjects.models import Subject
11   -
12   -from log.models import Log
13   -
14   -class ReportView(LoginRequiredMixin, generic.TemplateView):
15   - template_name = "api/report.html"
16   -
17   - def get_context_data(self, **kwargs):
18   - context = {}
19   - params = self.request.GET
20   -
21   - if params['subject_id'] and params['init_date'] and params['end_date']:
22   - subject_id = params['subject_id']
23   - subject = Subject.objects.get(id=subject_id)
24   - data = {}
25   - students = subject.students.all()
26   - formats = ["%d/%m/%Y", "%m/%d/%Y"] #so it accepts english and portuguese date formats
27   - for fmt in formats:
28   - try:
29   - init_date = datetime.strptime(params['init_date'], fmt)
30   - end_date = datetime.strptime(params['end_date'], fmt)
31   - except ValueError:
32   - pass
33   -
34   - for student in students:
35   - interactions = {}
36   - #first columns
37   - interactions['subject_name'] = subject.name
38   - interactions['username'] = student.social_name
39   - interactions['init_date'] = init_date
40   - interactions['end_date'] = end_date
41   -
42   - help_posts_made_by_user = SubjectPost.objects.filter(action="help",space__id=subject.id, user=student,
43   - create_date__range=(init_date, end_date))
44   -
45   - #number of help posts created by the student
46   - interactions['doubts_count'] = help_posts_made_by_user.count()
47   -
48   - help_posts = SubjectPost.objects.filter(action="help", create_date__range=(init_date, end_date),
49   - space__id=subject_id)
50   -
51   - #comments count on help posts created by the student
52   - interactions['comments_count'] = Comment.objects.filter(post__in = help_posts.filter(user=student),
53   - create_date__range=(init_date, end_date)).count()
54   -
55   -
56   - #count the amount of comments made by the student on posts made by one of the professors
57   - interactions['comments_professor_count'] = Comment.objects.filter(post__in = help_posts.filter(user__in= subject.professor.all()), create_date__range=(init_date, end_date),
58   - user=student).count()
59   -
60   - #comments made by the user on other users posts
61   - interactions['comments_on_others_count'] = Comment.objects.filter(post__in = help_posts.exclude(user=student),
62   - create_date__range=(init_date, end_date),
63   - user= student).count()
64   -
65   -
66   -
67   - comments_by_teacher = Comment.objects.filter(user__in=subject.professor.all())
68   - help_posts_ids = []
69   - for comment in comments_by_teacher:
70   - help_posts_ids.append(comment.post.id)
71   - #number of help posts created by the user that the teacher commented on
72   - interactions['help_posts_commented_by_teacher'] = help_posts.filter(user=student, id__in = help_posts_ids).count()
73   -
74   -
75   - comments_by_others = Comment.objects.filter(user__in=subject.students.exclude(id = student.id))
76   - help_posts_ids = []
77   - for comment in comments_by_teacher:
78   - help_posts_ids.append(comment.post.id)
79   - #number of help posts created by the user others students commented on
80   - interactions['help_posts_commented_by_others'] = help_posts.filter(user=student, id__in = help_posts_ids).count()
81   -
82   - #Number of student visualizations on the mural of the subject
83   - interactions['mural_visualizations_count'] = MuralVisualizations.objects.filter(post__in = SubjectPost.objects.filter(space__id=subject.id),
84   - user = student).count()
85   -
86   -
87   - #VAR20 - number of access to mural between 6 a.m to 12a.m.
88   - interactions['access_subject_between_6_to_12_am'] = Log.objects.filter(action="access", resource="subject",
89   - user_id= student.id, context__contains = {'subject_id' : subject.id}, datetime__hour__range = (5, 11)).count()
90   -
91   - #VAR21 - number of access to mural between 6 a.m to 12a.m.
92   - interactions['access_subject_between_0_to_6_pm'] = Log.objects.filter(action="access", resource="subject",
93   - user_id= student.id, context__contains = {'subject_id' : subject.id}, datetime__hour__range = (11, 17)).count()
94   - #VAR22
95   - interactions['access_subject_between_6_to_12_pm'] = Log.objects.filter(action="access", resource="subject",
96   - user_id= student.id, context__contains = {'subject_id' : subject.id}, datetime__hour__range = (17, 23)).count()
97   -
98   - #VAR23
99   - interactions['access_subject_between_0_to_6_am'] = Log.objects.filter(action="access", resource="subject",
100   - user_id= student.id, context__contains = {'subject_id' : subject.id}, datetime__hour__range = (23, 5)).count()
101   -
102   - #VAR24 through 30
103   - day_numbers = [0, 1, 2, 3, 4, 5, 6]
104   - day_names = ["sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"]
105   - for day_num in day_numbers:
106   - interactions['access_subject_'+day_names[day_num]] = Log.objects.filter(action="access", resource="subject",
107   - user_id= student.id, context__contains = {'subject_id' : subject.id}, datetime__week_day = day_num).count()
108   -
109   - data[student] = interactions
110   -
111   - context["data"] = data
112   -
113   -
114   - return context
115   -
116 2  
... ...
reports/__init__.py 0 → 100644
reports/admin.py 0 → 100644
... ... @@ -0,0 +1,3 @@
  1 +from django.contrib import admin
  2 +
  3 +# Register your models here.
... ...
reports/apps.py 0 → 100644
... ... @@ -0,0 +1,5 @@
  1 +from django.apps import AppConfig
  2 +
  3 +
  4 +class ReportsConfig(AppConfig):
  5 + name = 'reports'
... ...
reports/migrations/__init__.py 0 → 100644
reports/models.py 0 → 100644
... ... @@ -0,0 +1,3 @@
  1 +from django.db import models
  2 +
  3 +# Create your models here.
... ...
reports/templates/reports/report.html 0 → 100644
... ... @@ -0,0 +1,31 @@
  1 +{% extends 'base.html' %}
  2 +
  3 +{% load static i18n pagination %}
  4 +{% load django_bootstrap_breadcrumbs %}
  5 +
  6 +{% block breadcrumbs %}
  7 + {{ block.super }}
  8 +
  9 + {% trans 'Mural: General' as general %}
  10 +
  11 + {% breadcrumb general 'mural:manage_general' %}
  12 +{% endblock %}
  13 +
  14 +{% block content %}
  15 +
  16 +
  17 + {% for user, datum in data.items %}
  18 +
  19 +
  20 + <p>
  21 + <ul>
  22 +
  23 + {{user}} :
  24 + {% for key, value in datum.items %}
  25 + <li>{{key}}: {{value}}</li>
  26 + {% endfor %}
  27 + </ul>
  28 +
  29 + </p>
  30 + {% endfor %}
  31 +{% endblock content %}
0 32 \ No newline at end of file
... ...
reports/tests.py 0 → 100644
... ... @@ -0,0 +1,3 @@
  1 +from django.test import TestCase
  2 +
  3 +# Create your tests here.
... ...
reports/urls.py 0 → 100644
... ... @@ -0,0 +1,7 @@
  1 +from django.conf.urls import url, include
  2 +from . import views
  3 +
  4 +
  5 +urlpatterns = [
  6 + url(r'^report/$', views.ReportView.as_view(), name='report'),
  7 +]
0 8 \ No newline at end of file
... ...
reports/views.py 0 → 100644
... ... @@ -0,0 +1,116 @@
  1 +from django.shortcuts import render
  2 +from django.http import HttpResponse, JsonResponse
  3 +
  4 +
  5 +import django.views.generic as generic
  6 +from mural.models import SubjectPost, Comment, MuralVisualizations
  7 +from django.db.models import Q
  8 +from django.contrib.auth.mixins import LoginRequiredMixin
  9 +from datetime import datetime, date
  10 +from subjects.models import Subject
  11 +
  12 +from log.models import Log
  13 +
  14 +class ReportView(LoginRequiredMixin, generic.TemplateView):
  15 + template_name = "api/report.html"
  16 +
  17 + def get_context_data(self, **kwargs):
  18 + context = {}
  19 + params = self.request.GET
  20 +
  21 + if params['subject_id'] and params['init_date'] and params['end_date']:
  22 + subject_id = params['subject_id']
  23 + subject = Subject.objects.get(id=subject_id)
  24 + data = {}
  25 + students = subject.students.all()
  26 + formats = ["%d/%m/%Y", "%m/%d/%Y"] #so it accepts english and portuguese date formats
  27 + for fmt in formats:
  28 + try:
  29 + init_date = datetime.strptime(params['init_date'], fmt)
  30 + end_date = datetime.strptime(params['end_date'], fmt)
  31 + except ValueError:
  32 + pass
  33 +
  34 + for student in students:
  35 + interactions = {}
  36 + #first columns
  37 + interactions['subject_name'] = subject.name
  38 + interactions['username'] = student.social_name
  39 + interactions['init_date'] = init_date
  40 + interactions['end_date'] = end_date
  41 +
  42 + help_posts_made_by_user = SubjectPost.objects.filter(action="help",space__id=subject.id, user=student,
  43 + create_date__range=(init_date, end_date))
  44 +
  45 + #number of help posts created by the student
  46 + interactions['doubts_count'] = help_posts_made_by_user.count()
  47 +
  48 + help_posts = SubjectPost.objects.filter(action="help", create_date__range=(init_date, end_date),
  49 + space__id=subject_id)
  50 +
  51 + #comments count on help posts created by the student
  52 + interactions['comments_count'] = Comment.objects.filter(post__in = help_posts.filter(user=student),
  53 + create_date__range=(init_date, end_date)).count()
  54 +
  55 +
  56 + #count the amount of comments made by the student on posts made by one of the professors
  57 + interactions['comments_professor_count'] = Comment.objects.filter(post__in = help_posts.filter(user__in= subject.professor.all()), create_date__range=(init_date, end_date),
  58 + user=student).count()
  59 +
  60 + #comments made by the user on other users posts
  61 + interactions['comments_on_others_count'] = Comment.objects.filter(post__in = help_posts.exclude(user=student),
  62 + create_date__range=(init_date, end_date),
  63 + user= student).count()
  64 +
  65 +
  66 +
  67 + comments_by_teacher = Comment.objects.filter(user__in=subject.professor.all())
  68 + help_posts_ids = []
  69 + for comment in comments_by_teacher:
  70 + help_posts_ids.append(comment.post.id)
  71 + #number of help posts created by the user that the teacher commented on
  72 + interactions['help_posts_commented_by_teacher'] = help_posts.filter(user=student, id__in = help_posts_ids).count()
  73 +
  74 +
  75 + comments_by_others = Comment.objects.filter(user__in=subject.students.exclude(id = student.id))
  76 + help_posts_ids = []
  77 + for comment in comments_by_teacher:
  78 + help_posts_ids.append(comment.post.id)
  79 + #number of help posts created by the user others students commented on
  80 + interactions['help_posts_commented_by_others'] = help_posts.filter(user=student, id__in = help_posts_ids).count()
  81 +
  82 + #Number of student visualizations on the mural of the subject
  83 + interactions['mural_visualizations_count'] = MuralVisualizations.objects.filter(post__in = SubjectPost.objects.filter(space__id=subject.id),
  84 + user = student).count()
  85 +
  86 +
  87 + #VAR20 - number of access to mural between 6 a.m to 12a.m.
  88 + interactions['access_subject_between_6_to_12_am'] = Log.objects.filter(action="access", resource="subject",
  89 + user_id= student.id, context__contains = {'subject_id' : subject.id}, datetime__hour__range = (5, 11)).count()
  90 +
  91 + #VAR21 - number of access to mural between 6 a.m to 12a.m.
  92 + interactions['access_subject_between_0_to_6_pm'] = Log.objects.filter(action="access", resource="subject",
  93 + user_id= student.id, context__contains = {'subject_id' : subject.id}, datetime__hour__range = (11, 17)).count()
  94 + #VAR22
  95 + interactions['access_subject_between_6_to_12_pm'] = Log.objects.filter(action="access", resource="subject",
  96 + user_id= student.id, context__contains = {'subject_id' : subject.id}, datetime__hour__range = (17, 23)).count()
  97 +
  98 + #VAR23
  99 + interactions['access_subject_between_0_to_6_am'] = Log.objects.filter(action="access", resource="subject",
  100 + user_id= student.id, context__contains = {'subject_id' : subject.id}, datetime__hour__range = (23, 5)).count()
  101 +
  102 + #VAR24 through 30
  103 + day_numbers = [0, 1, 2, 3, 4, 5, 6]
  104 + day_names = ["sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"]
  105 + for day_num in day_numbers:
  106 + interactions['access_subject_'+day_names[day_num]] = Log.objects.filter(action="access", resource="subject",
  107 + user_id= student.id, context__contains = {'subject_id' : subject.id}, datetime__week_day = day_num).count()
  108 +
  109 + data[student] = interactions
  110 +
  111 + context["data"] = data
  112 +
  113 +
  114 + return context
  115 +
  116 +
... ...