Commit c08c401471d4e0ca8906f1c937e8645bceb16dae

Authored by fbormann
1 parent af21a506

now report exports xls files properly, still has to see if my concurrent solution works

reports/migrations/0003_auto_20170323_1517.py 0 → 100644
... ... @@ -0,0 +1,20 @@
  1 +# -*- coding: utf-8 -*-
  2 +# Generated by Django 1.10.4 on 2017-03-23 18:17
  3 +from __future__ import unicode_literals
  4 +
  5 +from django.db import migrations, models
  6 +
  7 +
  8 +class Migration(migrations.Migration):
  9 +
  10 + dependencies = [
  11 + ('reports', '0002_reportxls'),
  12 + ]
  13 +
  14 + operations = [
  15 + migrations.AlterField(
  16 + model_name='reportxls',
  17 + name='xls_data',
  18 + field=models.TextField(null=True),
  19 + ),
  20 + ]
... ...
reports/migrations/0004_auto_20170323_1520.py 0 → 100644
... ... @@ -0,0 +1,20 @@
  1 +# -*- coding: utf-8 -*-
  2 +# Generated by Django 1.10.4 on 2017-03-23 18:20
  3 +from __future__ import unicode_literals
  4 +
  5 +from django.db import migrations, models
  6 +
  7 +
  8 +class Migration(migrations.Migration):
  9 +
  10 + dependencies = [
  11 + ('reports', '0003_auto_20170323_1517'),
  12 + ]
  13 +
  14 + operations = [
  15 + migrations.AlterField(
  16 + model_name='reportxls',
  17 + name='xls_data',
  18 + field=models.TextField(),
  19 + ),
  20 + ]
... ...
reports/migrations/0005_auto_20170323_1603.py 0 → 100644
... ... @@ -0,0 +1,20 @@
  1 +# -*- coding: utf-8 -*-
  2 +# Generated by Django 1.10.4 on 2017-03-23 19:03
  3 +from __future__ import unicode_literals
  4 +
  5 +from django.db import migrations, models
  6 +
  7 +
  8 +class Migration(migrations.Migration):
  9 +
  10 + dependencies = [
  11 + ('reports', '0004_auto_20170323_1520'),
  12 + ]
  13 +
  14 + operations = [
  15 + migrations.AlterField(
  16 + model_name='reportxls',
  17 + name='xls_data',
  18 + field=models.FileField(upload_to='excel/'),
  19 + ),
  20 + ]
... ...
reports/migrations/0006_auto_20170323_1629.py 0 → 100644
... ... @@ -0,0 +1,20 @@
  1 +# -*- coding: utf-8 -*-
  2 +# Generated by Django 1.10.4 on 2017-03-23 19:29
  3 +from __future__ import unicode_literals
  4 +
  5 +from django.db import migrations, models
  6 +
  7 +
  8 +class Migration(migrations.Migration):
  9 +
  10 + dependencies = [
  11 + ('reports', '0005_auto_20170323_1603'),
  12 + ]
  13 +
  14 + operations = [
  15 + migrations.AlterField(
  16 + model_name='reportxls',
  17 + name='xls_data',
  18 + field=models.FileField(upload_to='files/'),
  19 + ),
  20 + ]
... ...
reports/models.py
... ... @@ -17,7 +17,7 @@ class ReportCSV(models.Model):
17 17 class ReportXLS(models.Model):
18 18  
19 19 user = models.ForeignKey(User)
20   - xls_data = models.TextField()
  20 + xls_data = models.FileField(upload_to = 'files/')
21 21  
22 22 class Meta:
23 23 verbose_name = "ReportCSV"
... ...
reports/templates/reports/view.html
... ... @@ -65,10 +65,11 @@
65 65 <ul id="report-info">
66 66 <li> {{data.values|length}} {% trans "register(s)" %} </li>
67 67 <li>
68   - <a href="{% url 'subjects:reports:download_report_csv' %}"><i class="fa fa-download" aria-hidden="true"></i> {% trans "Interactions Data" %}</a>
  68 + <a href="{% url 'subjects:reports:download_report_csv' %}"><i class="fa fa-download" aria-hidden="true"></i> {% trans "Interactions Data (.csv)" %}</a>
69 69  
70 70  
71 71 </li>
  72 + <li><a href="{% url 'subjects:reports:download_report_xls' %}"><i class="fa fa-download" aria-hidden="true"></i> {% trans "Interactions Data (.xls)" %}</a></li>
72 73 </ul>
73 74  
74 75  
... ...
reports/urls.py
... ... @@ -8,4 +8,5 @@ urlpatterns = [
8 8 url(r'^get/resources/$', views.get_resources, name='get_resource_and_tags'),
9 9 url(r'^get/tags/$', views.get_tags, name='get_tags'),
10 10 url(r'^post/download_report/$', views.download_report_csv, name="download_report_csv"),
  11 + url(r'^post/download_report/excel$', views.download_report_xls, name="download_report_xls"),
11 12 ]
12 13 \ No newline at end of file
... ...
reports/views.py
... ... @@ -4,9 +4,9 @@ from django.utils.translation import ugettext_lazy as _
4 4  
5 5 from django import forms
6 6 from django.core.urlresolvers import reverse_lazy
7   -
  7 +from amadeus import settings
8 8 from django.contrib import messages
9   -
  9 +from os.path import join
10 10 import django.views.generic as generic
11 11 from mural.models import SubjectPost, Comment, MuralVisualizations
12 12 from django.db.models import Q
... ... @@ -20,6 +20,7 @@ from collections import OrderedDict
20 20 from django.forms import formset_factory
21 21 from .models import ReportCSV, ReportXLS
22 22 import pandas as pd
  23 +from io import BytesIO
23 24  
24 25 class ReportView(LoginRequiredMixin, generic.FormView):
25 26 template_name = "reports/create.html"
... ... @@ -156,27 +157,32 @@ class ViewReportView(LoginRequiredMixin, generic.TemplateView):
156 157 report.save()
157 158  
158 159 #for excel files
159   - """ if ReportXLS.objects.filter(user= self.request.user).count() > 0:
  160 + if ReportXLS.objects.filter(user= self.request.user).count() > 0:
160 161 report = ReportXLS.objects.get(user=self.request.user)
161 162 report.delete()
162 163  
163   - df.drop(df.columns[[0]], axis=1, inplace=True)
164   - writer = pd.ExcelWriter('pandas_simple.xlsx')
165   - report = ReportXLS(user= self.request.user, xls_data = df.to_excel(writer))
166   - report.save()"""
167   -
  164 + path = join(settings.MEDIA_ROOT, 'files' , 'report'+str(self.request.user.id)+'.xls')
  165 + writer = pd.ExcelWriter(path)
  166 + df.to_excel(writer, sheet_name='first_sheet')
  167 + writer.save()
  168 + report = ReportXLS(user= self.request.user )
  169 + report.xls_data.name = path
  170 + report.save()
168 171  
169 172 return context
170 173  
171   - """
172   - Subject: subject where the report is being created
173   - topics_query: it's either one of the topics or all of them
174   - init_date: When the reports filter of dates stars
175   - end_date: When the reports filter of dates end
176   - resources_type_names: resources subclasses name that were selected
177   - tags_id = ID of tag objects that were selected
178   - """
  174 +
179 175 def get_mural_data(self, subject, topics_query, init_date, end_date, resources_type_names, tags_id):
  176 + """
  177 +
  178 + Process all the data to be brough by the report
  179 + Subject: subject where the report is being created
  180 + topics_query: it's either one of the topics or all of them
  181 + init_date: When the reports filter of dates stars
  182 + end_date: When the reports filter of dates end
  183 + resources_type_names: resources subclasses name that were selected
  184 + tags_id = ID of tag objects that were selected
  185 + """
180 186 data = {}
181 187 students = subject.students.all()
182 188 formats = ["%d/%m/%Y", "%m/%d/%Y", "%Y-%m-%d"] #so it accepts english and portuguese date formats
... ... @@ -199,9 +205,9 @@ class ViewReportView(LoginRequiredMixin, generic.TemplateView):
199 205  
200 206 #For each student in the subject
201 207 for student in students:
202   - data[student] = []
  208 + data[student.id] = []
203 209  
204   - data[student].append(student.social_name)
  210 + data[student.id].append(student.social_name)
205 211  
206 212 interactions = OrderedDict()
207 213  
... ... @@ -289,7 +295,7 @@ class ViewReportView(LoginRequiredMixin, generic.TemplateView):
289 295 interactions[_("Class")] = ""
290 296 interactions[_("Performance")] = ""
291 297 for value in interactions.values():
292   - data[student].append(value)
  298 + data[student.id].append(value)
293 299  
294 300  
295 301 for key in interactions.keys():
... ... @@ -404,4 +410,12 @@ def download_report_csv(request):
404 410 response = HttpResponse(report.csv_data,content_type='text/csv')
405 411 response['Content-Disposition'] = 'attachment; filename="report.csv"'
406 412  
  413 + return response
  414 +
  415 +def download_report_xls(request):
  416 + report = ReportXLS.objects.get(user= request.user)
  417 +
  418 + response = HttpResponse(report.xls_data,content_type='application/ms-excel')
  419 + response['Content-Disposition'] = 'attachment; filename="report.xls"'
  420 +
407 421 return response
408 422 \ No newline at end of file
... ...