diff --git a/api/templates/api/report.html b/api/templates/api/report.html deleted file mode 100644 index a712523..0000000 --- a/api/templates/api/report.html +++ /dev/null @@ -1,31 +0,0 @@ -{% extends 'base.html' %} - -{% load static i18n pagination %} -{% load django_bootstrap_breadcrumbs %} - -{% block breadcrumbs %} - {{ block.super }} - - {% trans 'Mural: General' as general %} - - {% breadcrumb general 'mural:manage_general' %} -{% endblock %} - -{% block content %} - - - {% for user, datum in data.items %} - - -

-

- -

- {% endfor %} -{% endblock content %} \ No newline at end of file diff --git a/api/urls.py b/api/urls.py index 030495a..2dcf277 100644 --- a/api/urls.py +++ b/api/urls.py @@ -14,7 +14,6 @@ router.register(r'usersapi', UserViewSet) urlpatterns = [ #API REST - url(r'^report/$', views.ReportView.as_view(), name='report'), url(r'^', include(router.urls)), ] \ No newline at end of file diff --git a/api/views.py b/api/views.py index 8b19d4b..2800278 100644 --- a/api/views.py +++ b/api/views.py @@ -1,116 +1,2 @@ from django.shortcuts import render -from django.http import HttpResponse, JsonResponse - - -import django.views.generic as generic -from mural.models import SubjectPost, Comment, MuralVisualizations -from django.db.models import Q -from django.contrib.auth.mixins import LoginRequiredMixin -from datetime import datetime, date -from subjects.models import Subject - -from log.models import Log - -class ReportView(LoginRequiredMixin, generic.TemplateView): - template_name = "api/report.html" - - def get_context_data(self, **kwargs): - context = {} - params = self.request.GET - - if params['subject_id'] and params['init_date'] and params['end_date']: - subject_id = params['subject_id'] - subject = Subject.objects.get(id=subject_id) - data = {} - students = subject.students.all() - formats = ["%d/%m/%Y", "%m/%d/%Y"] #so it accepts english and portuguese date formats - for fmt in formats: - try: - init_date = datetime.strptime(params['init_date'], fmt) - end_date = datetime.strptime(params['end_date'], fmt) - except ValueError: - pass - - for student in students: - interactions = {} - #first columns - interactions['subject_name'] = subject.name - interactions['username'] = student.social_name - interactions['init_date'] = init_date - interactions['end_date'] = end_date - - help_posts_made_by_user = SubjectPost.objects.filter(action="help",space__id=subject.id, user=student, - create_date__range=(init_date, end_date)) - - #number of help posts created by the student - interactions['doubts_count'] = help_posts_made_by_user.count() - - help_posts = SubjectPost.objects.filter(action="help", create_date__range=(init_date, end_date), - space__id=subject_id) - - #comments count on help posts created by the student - interactions['comments_count'] = Comment.objects.filter(post__in = help_posts.filter(user=student), - create_date__range=(init_date, end_date)).count() - - - #count the amount of comments made by the student on posts made by one of the professors - interactions['comments_professor_count'] = Comment.objects.filter(post__in = help_posts.filter(user__in= subject.professor.all()), create_date__range=(init_date, end_date), - user=student).count() - - #comments made by the user on other users posts - interactions['comments_on_others_count'] = Comment.objects.filter(post__in = help_posts.exclude(user=student), - create_date__range=(init_date, end_date), - user= student).count() - - - - comments_by_teacher = Comment.objects.filter(user__in=subject.professor.all()) - help_posts_ids = [] - for comment in comments_by_teacher: - help_posts_ids.append(comment.post.id) - #number of help posts created by the user that the teacher commented on - interactions['help_posts_commented_by_teacher'] = help_posts.filter(user=student, id__in = help_posts_ids).count() - - - comments_by_others = Comment.objects.filter(user__in=subject.students.exclude(id = student.id)) - help_posts_ids = [] - for comment in comments_by_teacher: - help_posts_ids.append(comment.post.id) - #number of help posts created by the user others students commented on - interactions['help_posts_commented_by_others'] = help_posts.filter(user=student, id__in = help_posts_ids).count() - - #Number of student visualizations on the mural of the subject - interactions['mural_visualizations_count'] = MuralVisualizations.objects.filter(post__in = SubjectPost.objects.filter(space__id=subject.id), - user = student).count() - - - #VAR20 - number of access to mural between 6 a.m to 12a.m. - interactions['access_subject_between_6_to_12_am'] = Log.objects.filter(action="access", resource="subject", - user_id= student.id, context__contains = {'subject_id' : subject.id}, datetime__hour__range = (5, 11)).count() - - #VAR21 - number of access to mural between 6 a.m to 12a.m. - interactions['access_subject_between_0_to_6_pm'] = Log.objects.filter(action="access", resource="subject", - user_id= student.id, context__contains = {'subject_id' : subject.id}, datetime__hour__range = (11, 17)).count() - #VAR22 - interactions['access_subject_between_6_to_12_pm'] = Log.objects.filter(action="access", resource="subject", - user_id= student.id, context__contains = {'subject_id' : subject.id}, datetime__hour__range = (17, 23)).count() - - #VAR23 - interactions['access_subject_between_0_to_6_am'] = Log.objects.filter(action="access", resource="subject", - user_id= student.id, context__contains = {'subject_id' : subject.id}, datetime__hour__range = (23, 5)).count() - - #VAR24 through 30 - day_numbers = [0, 1, 2, 3, 4, 5, 6] - day_names = ["sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"] - for day_num in day_numbers: - interactions['access_subject_'+day_names[day_num]] = Log.objects.filter(action="access", resource="subject", - user_id= student.id, context__contains = {'subject_id' : subject.id}, datetime__week_day = day_num).count() - - data[student] = interactions - - context["data"] = data - - - return context - diff --git a/reports/__init__.py b/reports/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/reports/__init__.py diff --git a/reports/admin.py b/reports/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/reports/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/reports/apps.py b/reports/apps.py new file mode 100644 index 0000000..62cb669 --- /dev/null +++ b/reports/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class ReportsConfig(AppConfig): + name = 'reports' diff --git a/reports/migrations/__init__.py b/reports/migrations/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/reports/migrations/__init__.py diff --git a/reports/models.py b/reports/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/reports/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/reports/templates/reports/report.html b/reports/templates/reports/report.html new file mode 100644 index 0000000..a712523 --- /dev/null +++ b/reports/templates/reports/report.html @@ -0,0 +1,31 @@ +{% extends 'base.html' %} + +{% load static i18n pagination %} +{% load django_bootstrap_breadcrumbs %} + +{% block breadcrumbs %} + {{ block.super }} + + {% trans 'Mural: General' as general %} + + {% breadcrumb general 'mural:manage_general' %} +{% endblock %} + +{% block content %} + + + {% for user, datum in data.items %} + + +

+

+ +

+ {% endfor %} +{% endblock content %} \ No newline at end of file diff --git a/reports/tests.py b/reports/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/reports/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/reports/urls.py b/reports/urls.py new file mode 100644 index 0000000..6ed3dc4 --- /dev/null +++ b/reports/urls.py @@ -0,0 +1,7 @@ +from django.conf.urls import url, include +from . import views + + +urlpatterns = [ + url(r'^report/$', views.ReportView.as_view(), name='report'), +] \ No newline at end of file diff --git a/reports/views.py b/reports/views.py new file mode 100644 index 0000000..8b19d4b --- /dev/null +++ b/reports/views.py @@ -0,0 +1,116 @@ +from django.shortcuts import render +from django.http import HttpResponse, JsonResponse + + +import django.views.generic as generic +from mural.models import SubjectPost, Comment, MuralVisualizations +from django.db.models import Q +from django.contrib.auth.mixins import LoginRequiredMixin +from datetime import datetime, date +from subjects.models import Subject + +from log.models import Log + +class ReportView(LoginRequiredMixin, generic.TemplateView): + template_name = "api/report.html" + + def get_context_data(self, **kwargs): + context = {} + params = self.request.GET + + if params['subject_id'] and params['init_date'] and params['end_date']: + subject_id = params['subject_id'] + subject = Subject.objects.get(id=subject_id) + data = {} + students = subject.students.all() + formats = ["%d/%m/%Y", "%m/%d/%Y"] #so it accepts english and portuguese date formats + for fmt in formats: + try: + init_date = datetime.strptime(params['init_date'], fmt) + end_date = datetime.strptime(params['end_date'], fmt) + except ValueError: + pass + + for student in students: + interactions = {} + #first columns + interactions['subject_name'] = subject.name + interactions['username'] = student.social_name + interactions['init_date'] = init_date + interactions['end_date'] = end_date + + help_posts_made_by_user = SubjectPost.objects.filter(action="help",space__id=subject.id, user=student, + create_date__range=(init_date, end_date)) + + #number of help posts created by the student + interactions['doubts_count'] = help_posts_made_by_user.count() + + help_posts = SubjectPost.objects.filter(action="help", create_date__range=(init_date, end_date), + space__id=subject_id) + + #comments count on help posts created by the student + interactions['comments_count'] = Comment.objects.filter(post__in = help_posts.filter(user=student), + create_date__range=(init_date, end_date)).count() + + + #count the amount of comments made by the student on posts made by one of the professors + interactions['comments_professor_count'] = Comment.objects.filter(post__in = help_posts.filter(user__in= subject.professor.all()), create_date__range=(init_date, end_date), + user=student).count() + + #comments made by the user on other users posts + interactions['comments_on_others_count'] = Comment.objects.filter(post__in = help_posts.exclude(user=student), + create_date__range=(init_date, end_date), + user= student).count() + + + + comments_by_teacher = Comment.objects.filter(user__in=subject.professor.all()) + help_posts_ids = [] + for comment in comments_by_teacher: + help_posts_ids.append(comment.post.id) + #number of help posts created by the user that the teacher commented on + interactions['help_posts_commented_by_teacher'] = help_posts.filter(user=student, id__in = help_posts_ids).count() + + + comments_by_others = Comment.objects.filter(user__in=subject.students.exclude(id = student.id)) + help_posts_ids = [] + for comment in comments_by_teacher: + help_posts_ids.append(comment.post.id) + #number of help posts created by the user others students commented on + interactions['help_posts_commented_by_others'] = help_posts.filter(user=student, id__in = help_posts_ids).count() + + #Number of student visualizations on the mural of the subject + interactions['mural_visualizations_count'] = MuralVisualizations.objects.filter(post__in = SubjectPost.objects.filter(space__id=subject.id), + user = student).count() + + + #VAR20 - number of access to mural between 6 a.m to 12a.m. + interactions['access_subject_between_6_to_12_am'] = Log.objects.filter(action="access", resource="subject", + user_id= student.id, context__contains = {'subject_id' : subject.id}, datetime__hour__range = (5, 11)).count() + + #VAR21 - number of access to mural between 6 a.m to 12a.m. + interactions['access_subject_between_0_to_6_pm'] = Log.objects.filter(action="access", resource="subject", + user_id= student.id, context__contains = {'subject_id' : subject.id}, datetime__hour__range = (11, 17)).count() + #VAR22 + interactions['access_subject_between_6_to_12_pm'] = Log.objects.filter(action="access", resource="subject", + user_id= student.id, context__contains = {'subject_id' : subject.id}, datetime__hour__range = (17, 23)).count() + + #VAR23 + interactions['access_subject_between_0_to_6_am'] = Log.objects.filter(action="access", resource="subject", + user_id= student.id, context__contains = {'subject_id' : subject.id}, datetime__hour__range = (23, 5)).count() + + #VAR24 through 30 + day_numbers = [0, 1, 2, 3, 4, 5, 6] + day_names = ["sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"] + for day_num in day_numbers: + interactions['access_subject_'+day_names[day_num]] = Log.objects.filter(action="access", resource="subject", + user_id= student.id, context__contains = {'subject_id' : subject.id}, datetime__week_day = day_num).count() + + data[student] = interactions + + context["data"] = data + + + return context + + -- libgit2 0.21.2