Commit 502256a8ba0f0c9dfad3c233224f2b237f0f0d20

Authored by fbormann
1 parent 1cdb1240

added safety check for no errors in code and ordered data report

reports/forms.py
@@ -11,6 +11,7 @@ class BaseResourceAndTagFormset(BaseFormSet): @@ -11,6 +11,7 @@ class BaseResourceAndTagFormset(BaseFormSet):
11 Adds validation to check that no two links have the same anchor or URL 11 Adds validation to check that no two links have the same anchor or URL
12 and that all links have both an anchor and URL. 12 and that all links have both an anchor and URL.
13 """ 13 """
  14 + print(self.errors)
14 if any(self.errors): 15 if any(self.errors):
15 return 16 return
16 17
@@ -26,8 +27,9 @@ class ResourceAndTagForm(forms.Form): @@ -26,8 +27,9 @@ class ResourceAndTagForm(forms.Form):
26 super(ResourceAndTagForm, self).__init__(*args, **kwargs) 27 super(ResourceAndTagForm, self).__init__(*args, **kwargs)
27 if kwargs.get('initial'): 28 if kwargs.get('initial'):
28 initial = kwargs['initial'] 29 initial = kwargs['initial']
29 - self.fields['resource'].choices = [(classes.__name__, classes.__name__) for classes in initial['class_name']] 30 + self.fields['resource'].choices = [(classes.__name__.lower(), classes.__name__.lower()) for classes in initial['class_name']]
30 self.fields['tag'].choices = [(tag.id, tag.name) for tag in initial['tag']] 31 self.fields['tag'].choices = [(tag.id, tag.name) for tag in initial['tag']]
  32 +
31 33
32 34
33 class CreateInteractionReportForm(forms.Form): 35 class CreateInteractionReportForm(forms.Form):
reports/templates/reports/create.html
@@ -89,5 +89,7 @@ @@ -89,5 +89,7 @@
89 }); 89 });
90 90
91 $('.resource-tag-formset').remove(); //I remove the element so there is no empty form with no data, I can't override this behavior on the previous function call 91 $('.resource-tag-formset').remove(); //I remove the element so there is no empty form with no data, I can't override this behavior on the previous function call
  92 +
  93 + $('#id_form-TOTAL_FORMS')[0].value = 0;
92 </script> 94 </script>
93 {% endblock content %} 95 {% endblock content %}
94 \ No newline at end of file 96 \ No newline at end of file
reports/views.py
@@ -16,7 +16,7 @@ from subjects.models import Subject, Tag @@ -16,7 +16,7 @@ from subjects.models import Subject, Tag
16 from .forms import CreateInteractionReportForm, ResourceAndTagForm, BaseResourceAndTagFormset 16 from .forms import CreateInteractionReportForm, ResourceAndTagForm, BaseResourceAndTagFormset
17 from log.models import Log 17 from log.models import Log
18 from topics.models import Resource, Topic 18 from topics.models import Resource, Topic
19 - 19 +from collections import OrderedDict
20 from django.forms import formset_factory 20 from django.forms import formset_factory
21 21
22 class ReportView(LoginRequiredMixin, generic.FormView): 22 class ReportView(LoginRequiredMixin, generic.FormView):
@@ -130,10 +130,13 @@ class ViewReportView(LoginRequiredMixin, generic.TemplateView): @@ -130,10 +130,13 @@ class ViewReportView(LoginRequiredMixin, generic.TemplateView):
130 context['init_date'] = params_data['init_date'] 130 context['init_date'] = params_data['init_date']
131 context['end_date'] = params_data['end_date'] 131 context['end_date'] = params_data['end_date']
132 context['subject'] = subject 132 context['subject'] = subject
  133 +
  134 + resources = params_data.getlist('resource')
  135 + tags = params_data.getlist('tag')
133 if params_data['from_mural']: 136 if params_data['from_mural']:
134 #I used getlist method so it can get more than one tag and one resource class_name 137 #I used getlist method so it can get more than one tag and one resource class_name
135 context['data'], context['header'] = self.get_mural_data(subject, params_data['init_date'], params_data['end_date'], 138 context['data'], context['header'] = self.get_mural_data(subject, params_data['init_date'], params_data['end_date'],
136 - params_data.getlist('resource'), params_data.getlist('tag')) 139 + resources, tags )
137 return context 140 return context
138 141
139 def get_mural_data(self, subject, init_date, end_date, resources_id, tags_id): 142 def get_mural_data(self, subject, init_date, end_date, resources_id, tags_id):
@@ -155,40 +158,40 @@ class ViewReportView(LoginRequiredMixin, generic.TemplateView): @@ -155,40 +158,40 @@ class ViewReportView(LoginRequiredMixin, generic.TemplateView):
155 158
156 data[student].append(student.social_name) 159 data[student].append(student.social_name)
157 160
158 - interactions = {} 161 + interactions = OrderedDict()
159 #interactions['username'] = student.social_name 162 #interactions['username'] = student.social_name
160 163
161 help_posts_made_by_user = SubjectPost.objects.filter(action="help",space__id=subject.id, user=student, 164 help_posts_made_by_user = SubjectPost.objects.filter(action="help",space__id=subject.id, user=student,
162 create_date__range=(init_date, end_date)) 165 create_date__range=(init_date, end_date))
163 166
164 #number of help posts created by the student 167 #number of help posts created by the student
165 - interactions['number of help posts created by the user'] = help_posts_made_by_user.count() 168 + interactions[_('Number of help posts created by the user.')] = help_posts_made_by_user.count()
166 169
167 help_posts = SubjectPost.objects.filter(action="help", create_date__range=(init_date, end_date), 170 help_posts = SubjectPost.objects.filter(action="help", create_date__range=(init_date, end_date),
168 space__id=subject.id) 171 space__id=subject.id)
169 172
170 #comments count on help posts created by the student 173 #comments count on help posts created by the student
171 - interactions['amount of comments on help posts created by the student'] = Comment.objects.filter(post__in = help_posts.filter(user=student), 174 + interactions[_('Amount of comments on help posts created by the student.')] = Comment.objects.filter(post__in = help_posts.filter(user=student),
172 create_date__range=(init_date, end_date)).count() 175 create_date__range=(init_date, end_date)).count()
173 176
174 177
175 #count the amount of comments made by the student on posts made by one of the professors 178 #count the amount of comments made by the student on posts made by one of the professors
176 - 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), 179 + 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),
177 user=student).count() 180 user=student).count()
178 181
179 #comments made by the user on other users posts 182 #comments made by the user on other users posts
180 - interactions['amount of comments made by the student on other students help posts'] = Comment.objects.filter(post__in = help_posts.exclude(user=student), 183 + interactions[_('Amount of comments made by the student on other students help posts.')] = Comment.objects.filter(post__in = help_posts.exclude(user=student),
181 create_date__range=(init_date, end_date), 184 create_date__range=(init_date, end_date),
182 user= student).count() 185 user= student).count()
183 186
184 187
185 - 188 +
186 comments_by_teacher = Comment.objects.filter(user__in=subject.professor.all()) 189 comments_by_teacher = Comment.objects.filter(user__in=subject.professor.all())
187 help_posts_ids = [] 190 help_posts_ids = []
188 for comment in comments_by_teacher: 191 for comment in comments_by_teacher:
189 help_posts_ids.append(comment.post.id) 192 help_posts_ids.append(comment.post.id)
190 #number of help posts created by the user that the teacher commented on 193 #number of help posts created by the user that the teacher commented on
191 - 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() 194 + 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()
192 195
193 196
194 comments_by_others = Comment.objects.filter(user__in=subject.students.exclude(id = student.id)) 197 comments_by_others = Comment.objects.filter(user__in=subject.students.exclude(id = student.id))
@@ -196,30 +199,33 @@ class ViewReportView(LoginRequiredMixin, generic.TemplateView): @@ -196,30 +199,33 @@ class ViewReportView(LoginRequiredMixin, generic.TemplateView):
196 for comment in comments_by_teacher: 199 for comment in comments_by_teacher:
197 help_posts_ids.append(comment.post.id) 200 help_posts_ids.append(comment.post.id)
198 #number of help posts created by the user others students commented on 201 #number of help posts created by the user others students commented on
199 - interactions['number of help posts created by the user others students commented on'] = help_posts.filter(user=student, id__in = help_posts_ids).count() 202 + interactions[_('Number of help posts created by the user others students commented on.')] = help_posts.filter(user=student, id__in = help_posts_ids).count()
200 203
201 #Number of student visualizations on the mural of the subject 204 #Number of student visualizations on the mural of the subject
202 - interactions['Number of student visualizations on the mural of the subject'] = MuralVisualizations.objects.filter(post__in = SubjectPost.objects.filter(space__id=subject.id), 205 + interactions[_('Number of student visualizations on the mural of the subject.')] = MuralVisualizations.objects.filter(post__in = SubjectPost.objects.filter(space__id=subject.id),
203 user = student).count() 206 user = student).count()
204 207
205 208
206 #VAR08 through VAR_019 of documenttation: 209 #VAR08 through VAR_019 of documenttation:
207 - resources_data = self.get_resources_and_tags_data(resources_id, tags_id, student, subject) 210 + if len(resources_id) > 0:
  211 + resources_data = self.get_resources_and_tags_data(resources_id, tags_id, student, subject)
  212 + for key, value in resources_data.items():
  213 + interactions[key] = value
  214 +
208 215
209 - interactions = {**interactions, **resources_data}  
210 #VAR20 - number of access to mural between 6 a.m to 12a.m. 216 #VAR20 - number of access to mural between 6 a.m to 12a.m.
211 - interactions[' number of access to mural between 6 a.m to 12a.m.'] = Log.objects.filter(action="access", resource="subject", 217 + interactions[_('Number of access to mural between 6 a.m to 12a.m. .')] = Log.objects.filter(action="access", resource="subject",
212 user_id= student.id, context__contains = {'subject_id' : subject.id}, datetime__hour__range = (5, 11)).count() 218 user_id= student.id, context__contains = {'subject_id' : subject.id}, datetime__hour__range = (5, 11)).count()
213 219
214 #VAR21 - number of access to mural between 0 p.m to 6p.m. 220 #VAR21 - number of access to mural between 0 p.m to 6p.m.
215 - interactions['number of access to mural between 0 p.m to 6p.m.'] = Log.objects.filter(action="access", resource="subject", 221 + interactions[_('Number of access to mural between 0 p.m to 6p.m. .')] = Log.objects.filter(action="access", resource="subject",
216 user_id= student.id, context__contains = {'subject_id' : subject.id}, datetime__hour__range = (11, 17)).count() 222 user_id= student.id, context__contains = {'subject_id' : subject.id}, datetime__hour__range = (11, 17)).count()
217 #VAR22 223 #VAR22
218 - interactions['number of access to mural between 6 p.m to 12p.m.'] = Log.objects.filter(action="access", resource="subject", 224 + interactions[_('Number of access to mural between 6 p.m to 12p.m. .')] = Log.objects.filter(action="access", resource="subject",
219 user_id= student.id, context__contains = {'subject_id' : subject.id}, datetime__hour__range = (17, 23)).count() 225 user_id= student.id, context__contains = {'subject_id' : subject.id}, datetime__hour__range = (17, 23)).count()
220 226
221 #VAR23 227 #VAR23
222 - interactions['number of access to mural between 0 a.m to 6a.m.'] = Log.objects.filter(action="access", resource="subject", 228 + interactions[_('Number of access to mural between 0 a.m to 6a.m. .')] = Log.objects.filter(action="access", resource="subject",
223 user_id= student.id, context__contains = {'subject_id' : subject.id}, datetime__hour__range = (23, 5)).count() 229 user_id= student.id, context__contains = {'subject_id' : subject.id}, datetime__hour__range = (23, 5)).count()
224 230
225 #VAR24 through 30 231 #VAR24 through 30
@@ -227,13 +233,13 @@ class ViewReportView(LoginRequiredMixin, generic.TemplateView): @@ -227,13 +233,13 @@ class ViewReportView(LoginRequiredMixin, generic.TemplateView):
227 day_names = ["sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"] 233 day_names = ["sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"]
228 distinct_days = 0 234 distinct_days = 0
229 for day_num in day_numbers: 235 for day_num in day_numbers:
230 - interactions['number of access to the subject on '+ day_names[day_num]] = Log.objects.filter(action="access", resource="subject", 236 + interactions['Number of access to the subject on '+ day_names[day_num]] = Log.objects.filter(action="access", resource="subject",
231 user_id= student.id, context__contains = {'subject_id' : subject.id}, datetime__week_day = day_num).count() 237 user_id= student.id, context__contains = {'subject_id' : subject.id}, datetime__week_day = day_num).count()
232 #to save the distinct days the user has accessed 238 #to save the distinct days the user has accessed
233 - if interactions['number of access to the subject on '+ day_names[day_num]] > 0: 239 + if interactions['Number of access to the subject on '+ day_names[day_num]] > 0:
234 distinct_days += 1 240 distinct_days += 1
235 241
236 - interactions['number of distinct days the user access the subject'] = distinct_days 242 + interactions[_('Number of distinct days the user access the subject')] = distinct_days
237 243
238 for value in interactions.values(): 244 for value in interactions.values():
239 data[student].append(value) 245 data[student].append(value)
@@ -263,7 +269,7 @@ def get_resources(request): @@ -263,7 +269,7 @@ def get_resources(request):
263 269
264 data = {} 270 data = {}
265 subject = Subject.objects.get(id=request.GET['subject_id']) 271 subject = Subject.objects.get(id=request.GET['subject_id'])
266 - 272 +
267 topic_choice = request.GET["topic_choice"] 273 topic_choice = request.GET["topic_choice"]
268 if topic_choice.lower() == "all": 274 if topic_choice.lower() == "all":
269 topics = subject.topic_subject.all() 275 topics = subject.topic_subject.all()