Commit 78f2432575e2496e5ca3b046de4d1dca31452108

Authored by fbormann
1 parent 98e35be1

created model for retrieving simple csv report files and now it's possibl to download csv files

reports/locale/pt_BR/LC_MESSAGES/django.po
@@ -8,7 +8,7 @@ msgid "" @@ -8,7 +8,7 @@ msgid ""
8 msgstr "" 8 msgstr ""
9 "Project-Id-Version: PACKAGE VERSION\n" 9 "Project-Id-Version: PACKAGE VERSION\n"
10 "Report-Msgid-Bugs-To: \n" 10 "Report-Msgid-Bugs-To: \n"
11 -"POT-Creation-Date: 2017-03-17 00:26-0300\n" 11 +"POT-Creation-Date: 2017-03-20 18:40-0300\n"
12 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" 12 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
13 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" 13 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
14 "Language-Team: LANGUAGE <LL@li.org>\n" 14 "Language-Team: LANGUAGE <LL@li.org>\n"
@@ -66,20 +66,20 @@ msgstr &quot;Esta data deve ser igual ou após&quot; @@ -66,20 +66,20 @@ msgstr &quot;Esta data deve ser igual ou após&quot;
66 msgid "This date should be right or before " 66 msgid "This date should be right or before "
67 msgstr "Esta data deve ser igual ou anterior à" 67 msgstr "Esta data deve ser igual ou anterior à"
68 68
69 -#: templates/reports/_form.html:4 69 +#: templates/reports/_form.html:19
70 msgid "General Parameters" 70 msgid "General Parameters"
71 msgstr "Escolha o Tópico e o Período" 71 msgstr "Escolha o Tópico e o Período"
72 72
73 -#: templates/reports/_form.html:28 73 +#: templates/reports/_form.html:37
74 msgid "Choose the Data Source (is possible to pick more than one)" 74 msgid "Choose the Data Source (is possible to pick more than one)"
75 msgstr "Escolha a fonte de dados (é possível escolher mais de uma)" 75 msgstr "Escolha a fonte de dados (é possível escolher mais de uma)"
76 76
77 -#: templates/reports/_form.html:65 77 +#: templates/reports/_form.html:62
78 msgid "Interaction with resources" 78 msgid "Interaction with resources"
79 msgstr "Interação com recursos" 79 msgstr "Interação com recursos"
80 80
81 -#: templates/reports/_form.html:100  
82 -msgid "Search" 81 +#: templates/reports/_form.html:97
  82 +msgid "Search Report"
83 msgstr "Buscar" 83 msgstr "Buscar"
84 84
85 #: templates/reports/create.html:27 templates/reports/view.html:33 85 #: templates/reports/create.html:27 templates/reports/view.html:33
@@ -134,84 +134,83 @@ msgstr &quot;Dados de interação&quot; @@ -134,84 +134,83 @@ msgstr &quot;Dados de interação&quot;
134 msgid "Report created successfully" 134 msgid "Report created successfully"
135 msgstr "Relatório criado com sucesso!" 135 msgstr "Relatório criado com sucesso!"
136 136
137 -#: views.py:168 137 +#: views.py:169
138 msgid "Number of help posts created by the user." 138 msgid "Number of help posts created by the user."
139 msgstr "Número de postagens de dúvidas criadas no mural da disciplina." 139 msgstr "Número de postagens de dúvidas criadas no mural da disciplina."
140 140
141 -#: views.py:174 141 +#: views.py:175
142 msgid "Amount of comments on help posts created by the student." 142 msgid "Amount of comments on help posts created by the student."
143 msgstr "" 143 msgstr ""
144 "Número de comentários criados para as próprias postagens de dúvidas no mural " 144 "Número de comentários criados para as próprias postagens de dúvidas no mural "
145 "da disciplina." 145 "da disciplina."
146 146
147 -#: views.py:179 147 +#: views.py:180
148 msgid "Amount of comments made by the student on teachers help posts." 148 msgid "Amount of comments made by the student on teachers help posts."
149 msgstr "" 149 msgstr ""
150 "Número de comentários às postagens de dúvidas no mural da disciplina criadas " 150 "Número de comentários às postagens de dúvidas no mural da disciplina criadas "
151 "pelo professor." 151 "pelo professor."
152 152
153 -#: views.py:183 153 +#: views.py:184
154 msgid "Amount of comments made by the student on other students help posts." 154 msgid "Amount of comments made by the student on other students help posts."
155 msgstr "" 155 msgstr ""
156 "Número de comentários às postagens de dúvidas no mural da disciplina criadas " 156 "Número de comentários às postagens de dúvidas no mural da disciplina criadas "
157 "por outros estudantes." 157 "por outros estudantes."
158 158
159 -#: views.py:194 159 +#: views.py:195
160 msgid "Number of help posts created by the user that the teacher commented on." 160 msgid "Number of help posts created by the user that the teacher commented on."
161 msgstr "" 161 msgstr ""
162 "Número de comentários às postagens de dúvidas no mural da disciplina criadas " 162 "Número de comentários às postagens de dúvidas no mural da disciplina criadas "
163 "por outros estudantes" 163 "por outros estudantes"
164 164
165 -#: views.py:202 165 +#: views.py:203
166 msgid "Number of help posts created by the user others students commented on." 166 msgid "Number of help posts created by the user others students commented on."
167 msgstr "" 167 msgstr ""
168 " Número de postagens de dúvidas criadas no mural da disciplina que foram " 168 " Número de postagens de dúvidas criadas no mural da disciplina que foram "
169 "comentadas por outros estudantes." 169 "comentadas por outros estudantes."
170 170
171 -#: views.py:205 171 +#: views.py:206
172 msgid "Number of student visualizations on the mural of the subject." 172 msgid "Number of student visualizations on the mural of the subject."
173 msgstr "Número de visualizações do mural da disciplina." 173 msgstr "Número de visualizações do mural da disciplina."
174 174
175 -#: views.py:217 175 +#: views.py:218
176 msgid "Number of access to mural between 6 a.m to 12a.m. ." 176 msgid "Number of access to mural between 6 a.m to 12a.m. ."
177 msgstr "" 177 msgstr ""
178 "Número de acessos ao ambiente virtual da disciplina no horário de 06h às 12h." 178 "Número de acessos ao ambiente virtual da disciplina no horário de 06h às 12h."
179 179
180 -#: views.py:221 180 +#: views.py:222
181 msgid "Number of access to mural between 0 p.m to 6p.m. ." 181 msgid "Number of access to mural between 0 p.m to 6p.m. ."
182 msgstr "" 182 msgstr ""
183 "Número de acessos ao ambiente virtual da disciplina no horário de 12h às 18h." 183 "Número de acessos ao ambiente virtual da disciplina no horário de 12h às 18h."
184 184
185 -#: views.py:224 185 +#: views.py:225
186 msgid "Number of access to mural between 6 p.m to 12p.m. ." 186 msgid "Number of access to mural between 6 p.m to 12p.m. ."
187 msgstr "" 187 msgstr ""
188 "Número de acessos ao ambiente virtual da disciplina no horário de 18h às 24h." 188 "Número de acessos ao ambiente virtual da disciplina no horário de 18h às 24h."
189 189
190 -#: views.py:228 190 +#: views.py:229
191 msgid "Number of access to mural between 0 a.m to 6a.m. ." 191 msgid "Number of access to mural between 0 a.m to 6a.m. ."
192 msgstr "" 192 msgstr ""
193 "Número de acessos ao ambiente virtual da disciplina no horário de 24h às 06h." 193 "Número de acessos ao ambiente virtual da disciplina no horário de 24h às 06h."
194 194
195 -#: views.py:233  
196 -msgid "sunday"  
197 -msgstr "domingo"  
198 -  
199 -#: views.py:236 views.py:239 195 +#: views.py:237 views.py:240
200 msgid "Number of access to the subject on " 196 msgid "Number of access to the subject on "
201 msgstr "Número de acessos ao assunto na(o) " 197 msgstr "Número de acessos ao assunto na(o) "
202 198
203 -#: views.py:242 199 +#: views.py:243
204 msgid "Number of distinct days the user access the subject. " 200 msgid "Number of distinct days the user access the subject. "
205 msgstr "Número de dias distintos que acessou o ambiente virtual da disciplina." 201 msgstr "Número de dias distintos que acessou o ambiente virtual da disciplina."
206 202
207 -#: views.py:243 203 +#: views.py:244
208 msgid "Class" 204 msgid "Class"
209 msgstr "Classe" 205 msgstr "Classe"
210 206
211 -#: views.py:244 207 +#: views.py:245
212 msgid "Performance" 208 msgid "Performance"
213 msgstr "Desempenho" 209 msgstr "Desempenho"
214 210
  211 +#~ msgid "sunday"
  212 +#~ msgstr "domingo"
  213 +
215 #~ msgid "Topics to select data from" 214 #~ msgid "Topics to select data from"
216 #~ msgstr "Tópico do qual os dados serão retirados" 215 #~ msgstr "Tópico do qual os dados serão retirados"
217 216
reports/migrations/0001_initial.py 0 → 100644
@@ -0,0 +1,31 @@ @@ -0,0 +1,31 @@
  1 +# -*- coding: utf-8 -*-
  2 +# Generated by Django 1.10.4 on 2017-03-21 00:06
  3 +from __future__ import unicode_literals
  4 +
  5 +from django.conf import settings
  6 +from django.db import migrations, models
  7 +import django.db.models.deletion
  8 +
  9 +
  10 +class Migration(migrations.Migration):
  11 +
  12 + initial = True
  13 +
  14 + dependencies = [
  15 + migrations.swappable_dependency(settings.AUTH_USER_MODEL),
  16 + ]
  17 +
  18 + operations = [
  19 + migrations.CreateModel(
  20 + name='ReportCSV',
  21 + fields=[
  22 + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
  23 + ('csv_data', models.TextField()),
  24 + ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
  25 + ],
  26 + options={
  27 + 'verbose_name_plural': 'ReportCSVs',
  28 + 'verbose_name': 'ReportCSV',
  29 + },
  30 + ),
  31 + ]
reports/models.py
1 from django.db import models 1 from django.db import models
2 - 2 +from users.models import User
3 # Create your models here. 3 # Create your models here.
  4 +class ReportCSV(models.Model):
  5 +
  6 + user = models.ForeignKey(User)
  7 + csv_data = models.TextField()
  8 +
  9 + class Meta:
  10 + verbose_name = "ReportCSV"
  11 + verbose_name_plural = "ReportCSVs"
  12 +
  13 + def __str__(self):
  14 + pass
  15 +
4 \ No newline at end of file 16 \ No newline at end of file
reports/templates/reports/_form.html
@@ -94,7 +94,7 @@ @@ -94,7 +94,7 @@
94 </div> 94 </div>
95 95
96 <div class="row text-center"> 96 <div class="row text-center">
97 - <input type="submit" value="{% trans 'Search' %}" class="btn btn-success btn-raised" /> 97 + <input type="submit" value="{% trans 'Search Report' %}" class="btn btn-success btn-raised" />
98 </div> 98 </div>
99 </form> 99 </form>
100 100
reports/templates/reports/view.html
@@ -65,7 +65,9 @@ @@ -65,7 +65,9 @@
65 <ul id="report-info"> 65 <ul id="report-info">
66 <li> {{data.values|length}} {% trans "register(s)" %} </li> 66 <li> {{data.values|length}} {% trans "register(s)" %} </li>
67 <li> 67 <li>
68 - <i class="fa fa-download" aria-hidden="true"></i> {% trans "Interactions Data" %} 68 + <a href="{% url 'subjects:reports:download_report' %}?{{csv_data}}"><i class="fa fa-download" aria-hidden="true"></i> {% trans "Interactions Data" %}</a>
  69 +
  70 +
69 </li> 71 </li>
70 </ul> 72 </ul>
71 73
reports/urls.py
@@ -7,4 +7,5 @@ urlpatterns = [ @@ -7,4 +7,5 @@ urlpatterns = [
7 url(r'^view/interactions/$', views.ViewReportView.as_view(), name='view_report'), 7 url(r'^view/interactions/$', views.ViewReportView.as_view(), name='view_report'),
8 url(r'^get/resources/$', views.get_resources, name='get_resource_and_tags'), 8 url(r'^get/resources/$', views.get_resources, name='get_resource_and_tags'),
9 url(r'^get/tags/$', views.get_tags, name='get_tags'), 9 url(r'^get/tags/$', views.get_tags, name='get_tags'),
  10 + url(r'^post/download_report/$', views.download_report, name="download_report"),
10 ] 11 ]
11 \ No newline at end of file 12 \ No newline at end of file
reports/views.py
@@ -18,6 +18,8 @@ from log.models import Log @@ -18,6 +18,8 @@ from log.models import Log
18 from topics.models import Resource, Topic 18 from topics.models import Resource, Topic
19 from collections import OrderedDict 19 from collections import OrderedDict
20 from django.forms import formset_factory 20 from django.forms import formset_factory
  21 +from .models import ReportCSV
  22 +import pandas as pd
21 23
22 class ReportView(LoginRequiredMixin, generic.FormView): 24 class ReportView(LoginRequiredMixin, generic.FormView):
23 template_name = "reports/create.html" 25 template_name = "reports/create.html"
@@ -133,10 +135,25 @@ class ViewReportView(LoginRequiredMixin, generic.TemplateView): @@ -133,10 +135,25 @@ class ViewReportView(LoginRequiredMixin, generic.TemplateView):
133 135
134 resources = params_data.getlist('resource') 136 resources = params_data.getlist('resource')
135 tags = params_data.getlist('tag') 137 tags = params_data.getlist('tag')
136 - if params_data['from_mural']:  
137 - #I used getlist method so it can get more than one tag and one resource class_name  
138 - context['data'], context['header'] = self.get_mural_data(subject, params_data['init_date'], params_data['end_date'],  
139 - resources, tags ) 138 + self.from_mural = params_data['from_mural']
  139 + #I used getlist method so it can get more than one tag and one resource class_name
  140 + context['data'], context['header'] = self.get_mural_data(subject, params_data['init_date'], params_data['end_date'],
  141 + resources, tags )
  142 +
  143 +
  144 + #this is to save the csv for further download
  145 + df = pd.DataFrame.from_dict(context['data'], orient='index')
  146 +
  147 + df.columns = context['header']
  148 + #so it does not exist more than one report CSV available for that user to download
  149 + if ReportCSV.objects.filter(user= self.request.user).count() > 0:
  150 + report = ReportCSV.objects.get(user=self.request.user)
  151 + report.delete()
  152 +
  153 +
  154 + report = ReportCSV(user= self.request.user, csv_data = df.to_csv())
  155 + report.save()
  156 +
140 return context 157 return context
141 158
142 def get_mural_data(self, subject, init_date, end_date, resources_id, tags_id): 159 def get_mural_data(self, subject, init_date, end_date, resources_id, tags_id):
@@ -158,52 +175,53 @@ class ViewReportView(LoginRequiredMixin, generic.TemplateView): @@ -158,52 +175,53 @@ class ViewReportView(LoginRequiredMixin, generic.TemplateView):
158 175
159 data[student].append(student.social_name) 176 data[student].append(student.social_name)
160 177
161 - interactions = OrderedDict() 178 + interactions = OrderedDict()
  179 +
162 #interactions['username'] = student.social_name 180 #interactions['username'] = student.social_name
163 -  
164 - help_posts_made_by_user = SubjectPost.objects.filter(action="help",space__id=subject.id, user=student,  
165 - create_date__range=(init_date, end_date)) 181 + if self.from_mural == "True":
  182 + help_posts_made_by_user = SubjectPost.objects.filter(action="help",space__id=subject.id, user=student,
  183 + create_date__range=(init_date, end_date))
166 184
167 - #number of help posts created by the student  
168 - interactions[_('Number of help posts created by the user.')] = help_posts_made_by_user.count() 185 + #number of help posts created by the student
  186 + interactions[_('Number of help posts created by the user.')] = help_posts_made_by_user.count()
169 187
170 - help_posts = SubjectPost.objects.filter(action="help", create_date__range=(init_date, end_date),  
171 - space__id=subject.id) 188 + help_posts = SubjectPost.objects.filter(action="help", create_date__range=(init_date, end_date),
  189 + space__id=subject.id)
172 190
173 - #comments count on help posts created by the student  
174 - interactions[_('Amount of comments on help posts created by the student.')] = Comment.objects.filter(post__in = help_posts.filter(user=student),  
175 - create_date__range=(init_date, end_date)).count()  
176 - 191 + #comments count on help posts created by the student
  192 + interactions[_('Amount of comments on help posts created by the student.')] = Comment.objects.filter(post__in = help_posts.filter(user=student),
  193 + create_date__range=(init_date, end_date)).count()
  194 +
177 195
178 - #count the amount of comments made by the student on posts made by one of the professors  
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),  
180 - user=student).count() 196 + #count the amount of comments made by the student on posts made by one of the professors
  197 + 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),
  198 + user=student).count()
181 199
182 - #comments made by the user on other users posts  
183 - interactions[_('Amount of comments made by the student on other students help posts.')] = Comment.objects.filter(post__in = help_posts.exclude(user=student),  
184 - create_date__range=(init_date, end_date),  
185 - user= student).count()  
186 -  
187 -  
188 -  
189 - comments_by_teacher = Comment.objects.filter(user__in=subject.professor.all())  
190 - help_posts_ids = []  
191 - for comment in comments_by_teacher:  
192 - help_posts_ids.append(comment.post.id)  
193 - #number of help posts created by the user that the teacher commented on  
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()  
195 -  
196 -  
197 - comments_by_others = Comment.objects.filter(user__in=subject.students.exclude(id = student.id))  
198 - help_posts_ids = []  
199 - for comment in comments_by_teacher:  
200 - help_posts_ids.append(comment.post.id)  
201 - #number of help posts created by the user others students commented on  
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()  
203 -  
204 - #Number of student visualizations on the mural of the subject  
205 - interactions[_('Number of student visualizations on the mural of the subject.')] = MuralVisualizations.objects.filter(post__in = SubjectPost.objects.filter(space__id=subject.id),  
206 - user = student).count() 200 + #comments made by the user on other users posts
  201 + interactions[_('Amount of comments made by the student on other students help posts.')] = Comment.objects.filter(post__in = help_posts.exclude(user=student),
  202 + create_date__range=(init_date, end_date),
  203 + user= student).count()
  204 +
  205 +
  206 +
  207 + comments_by_teacher = Comment.objects.filter(user__in=subject.professor.all())
  208 + help_posts_ids = []
  209 + for comment in comments_by_teacher:
  210 + help_posts_ids.append(comment.post.id)
  211 + #number of help posts created by the user that the teacher commented on
  212 + 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()
  213 +
  214 +
  215 + comments_by_others = Comment.objects.filter(user__in=subject.students.exclude(id = student.id))
  216 + help_posts_ids = []
  217 + for comment in comments_by_teacher:
  218 + help_posts_ids.append(comment.post.id)
  219 + #number of help posts created by the user others students commented on
  220 + interactions[_('Number of help posts created by the user others students commented on.')] = help_posts.filter(user=student, id__in = help_posts_ids).count()
  221 +
  222 + #Number of student visualizations on the mural of the subject
  223 + interactions[_('Number of student visualizations on the mural of the subject.')] = MuralVisualizations.objects.filter(post__in = SubjectPost.objects.filter(space__id=subject.id),
  224 + user = student).count()
207 225
208 226
209 #VAR08 through VAR_019 of documenttation: 227 #VAR08 through VAR_019 of documenttation:
@@ -317,3 +335,12 @@ def get_tags(request): @@ -317,3 +335,12 @@ def get_tags(request):
317 335
318 data['tags'] = [ {'id':tag.id, 'name':tag.name} for tag in tags] 336 data['tags'] = [ {'id':tag.id, 'name':tag.name} for tag in tags]
319 return JsonResponse(data) 337 return JsonResponse(data)
  338 +
  339 +
  340 +def download_report(request):
  341 + report = ReportCSV.objects.get(user=request.user)
  342 +
  343 + response = HttpResponse(report.csv_data,content_type='text/csv')
  344 + response['Content-Disposition'] = 'attachment; filename="report.csv"'
  345 +
  346 + return response
320 \ No newline at end of file 347 \ No newline at end of file