views.py
10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
from django.shortcuts import render
from django.http import HttpResponse, JsonResponse
from django.utils.translation import ugettext_lazy as _
from django import forms
from django.core.urlresolvers import reverse_lazy
from django.contrib import messages
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 .forms import CreateInteractionReportForm, ResourceAndTagForm
from log.models import Log
from topics.models import Resource, Topic
from django.forms import formset_factory
class ReportView(LoginRequiredMixin, generic.FormView):
template_name = "reports/create.html"
form_class = CreateInteractionReportForm
def get_initial(self):
"""
Returns the initial data to use for forms on this view.
"""
initial = {}
params = self.request.GET
subject = Subject.objects.get(id=params['subject_id'])
topics = subject.topic_subject.all()
initial['subject'] = subject
initial['topic'] = topics
initial['end_date'] = date.today()
return initial
def get_context_data(self, **kwargs):
context = super(ReportView, self).get_context_data(**kwargs)
subject = Subject.objects.get(id=self.request.GET['subject_id'])
context['subject'] = subject
topics = subject.topic_subject.all()
#get all resources associated with topics
resources = []
tags = []
for topic in topics:
resources_set = topic.resource_topic.all()
for resource in resources_set:
for tag in resource.tags.all():
tags.append(tag)
resources.append(resource)
context['resources'] = resources
context['tags'] = tags
#set formset
resourceTagFormSet = formset_factory(ResourceAndTagForm)
resourceTagFormSet = resourceTagFormSet()
context['resource_tag_formset'] = resourceTagFormSet
return context
def get_success_url(self):
messages.success(self.request, _("Report created successfully"))
get_params = "?"
#passing form data through GET
for key, value in self.form_data.items():
get_params += key + "=" + str(value) + "&"
for form_data in self.formset_data:
for key, value in form_data.items():
get_params += key + "=" + str(value) + "&"
#retrieving subject id for data purposes
for key, value in self.request.GET.items():
get_params += key + "=" + str(value)
return reverse_lazy('subjects:reports:view_report', kwargs={}) + get_params
def post(self, request, *args, **kwargs):
"""
Handles POST requests, instantiating a form instance with the passed
POST variables and then checked for validity.
"""
form = self.get_form()
subject = Subject.objects.get(id=self.request.GET['subject_id'])
topics = subject.topic_subject.all()
#get all resources associated with topics
resources = []
tags = []
for topic in topics:
resources_set = topic.resource_topic.all()
for resource in resources_set:
for tag in resource.tags.all():
tags.append(tag)
resources.append(resource)
resourceTagFormSet = formset_factory(ResourceAndTagForm)
resources_formset = resourceTagFormSet(self.request.POST, initial=[{'resource':resources, 'tag':tags}])
if form.is_valid() and resources_formset.is_valid():
self.form_data = form.cleaned_data
self.formset_data = resources_formset.cleaned_data
return self.form_valid(form)
else:
return self.form_invalid(form)
class ViewReportView(LoginRequiredMixin, generic.TemplateView):
template_name = "reports/view.html"
def get_context_data(self, **kwargs):
context = {}
params_data = self.request.GET
subject = Subject.objects.get(id=params_data['subject_id'])
context['subject_name'] = subject.name
context['topic_name'] = params_data['topic']
context['init_date'] = params_data['init_date']
context['end_date'] = params_data['end_date']
context['subject'] = subject
if params_data['from_mural']:
context['data'], context['header'] = self.get_mural_data(subject, params_data['init_date'], params_data['end_date'])
return context
def get_mural_data(self, subject, init_date, end_date):
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(init_date, fmt)
end_date = datetime.strptime(end_date, fmt)
except ValueError:
pass
header = ['User']
for student in students:
data[student] = []
data[student].append(student.social_name)
interactions = {}
#interactions['username'] = student.social_name
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['number of help posts created by the user'] = 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['amount of comments on help posts created by the student'] = 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['amount of comments made by the student on teachers help posts'] = 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['amount of comments made by the student on other students help posts'] = 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['Number of help posts created by the user that the teacher commented on'] = 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['number of help posts created by the user others students commented on'] = help_posts.filter(user=student, id__in = help_posts_ids).count()
#Number of student visualizations on the mural of the subject
interactions['Number of student visualizations on the mural of the subject'] = 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[' number of access to mural between 6 a.m to 12a.m.'] = 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 0 p.m to 6p.m.
interactions['number of access to mural between 0 p.m to 6p.m.'] = 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[' number of access to mural between 6 p.m to 12p.m.'] = 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[' number of access to mural between 0 a.m to 6a.m.'] = 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['number of access to the subject on '+ 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()
for value in interactions.values():
data[student].append(value)
for key in interactions.keys():
header.append(key)
return data, header
def get_resources(request):
subject = Subject.objects.get(id=request.GET['subject_id'])
topics = subject.topic_subject.all()
#get all resources associated with topics
resources = []
tags = []
for topic in topics:
resources_set = topic.resource_topic.all()
for resource in resources_set:
for tag in resource.tags.all():
tags.append(tag)
resources.append(resource)
data = {}
data['resources']= [ {'id':resource.id, 'name':resource.name} for resource in resources]
return JsonResponse(data)
def get_tags(request):
resource = Resource.objects.get(id=request.GET['resource_id'])
data = {}
tags = []
for tag in resource.tags.all():
tags.append(tag)
data['tags'] = [ {'id':tag.id, 'name':tag.name} for tag in tags]
return JsonResponse(data)