Commit b10e1ae1e94969e8999a5d29608e01d5fdca9a90

Authored by Zambom
1 parent e88be4ef

Changing the way that goals xls file are disponibilized for download in bulletin form

bulletin/sheets/xls/future.xls 0 → 100644
No preview for this file type
bulletin/templates/bulletin/_form.html
@@ -70,9 +70,7 @@ @@ -70,9 +70,7 @@
70 70
71 <div class="form-group{% if form.has_error %} has-error {% endif %} is-fileinput"> 71 <div class="form-group{% if form.has_error %} has-error {% endif %} is-fileinput">
72 <label for="{{ form.file_content.auto_id }}">{{ form.file_content.label }} <span>*</span></label> 72 <label for="{{ form.file_content.auto_id }}">{{ form.file_content.label }} <span>*</span></label>
73 - {% with 'xls/'|add:goal_file as file_static %}  
74 - <a href="{% static file_static %}"> {% trans "Click to download a xls file with the data of the goals" %}</a>  
75 - {% endwith %} 73 + <a href="{% url 'bulletin:download_file' file=goal_file %}"> {% trans "Click to download a xls file with the data of the goals" %}</a>
76 {% render_field form.file_content class='file-selector' %} 74 {% render_field form.file_content class='file-selector' %}
77 75
78 <div class="input-group common-file-input"> 76 <div class="input-group common-file-input">
bulletin/urls.py
@@ -11,4 +11,5 @@ urlpatterns = [ @@ -11,4 +11,5 @@ urlpatterns = [
11 url(r'^view/(?P<slug>[\w_-]+)/$', views.InsideView.as_view(), name = 'view'), 11 url(r'^view/(?P<slug>[\w_-]+)/$', views.InsideView.as_view(), name = 'view'),
12 url(r'^chart/(?P<slug>[\w_-]+)/$', views.StatisticsView.as_view(), name = 'get_chart'), 12 url(r'^chart/(?P<slug>[\w_-]+)/$', views.StatisticsView.as_view(), name = 'get_chart'),
13 url(r'^send-message/(?P<slug>[\w_-]+)/$', views.SendMessage.as_view(), name = 'send_message'), 13 url(r'^send-message/(?P<slug>[\w_-]+)/$', views.SendMessage.as_view(), name = 'send_message'),
  14 + url(r'^download_file/(?P<file>[\w_-]+)/$', views.download_excel, name = 'download_file'),
14 ] 15 ]
bulletin/views.py
@@ -6,6 +6,8 @@ from django.utils.translation import ugettext_lazy as _ @@ -6,6 +6,8 @@ from django.utils.translation import ugettext_lazy as _
6 from django.contrib.auth.mixins import LoginRequiredMixin 6 from django.contrib.auth.mixins import LoginRequiredMixin
7 from django.http import JsonResponse 7 from django.http import JsonResponse
8 8
  9 +from os import path
  10 +
9 from amadeus.permissions import has_subject_permissions, has_resource_permissions 11 from amadeus.permissions import has_subject_permissions, has_resource_permissions
10 from .utils import brodcast_dificulties 12 from .utils import brodcast_dificulties
11 from goals.models import Goals,GoalItem,MyGoals 13 from goals.models import Goals,GoalItem,MyGoals
@@ -386,7 +388,7 @@ class CreateView(LoginRequiredMixin, LogMixin, generic.edit.CreateView): @@ -386,7 +388,7 @@ class CreateView(LoginRequiredMixin, LogMixin, generic.edit.CreateView):
386 itens_da_meta = sorted(list(metas), key = lambda met: met.id) 388 itens_da_meta = sorted(list(metas), key = lambda met: met.id)
387 alunos = sorted(list(meta_geral.topic.subject.students.all()), key = lambda e: e.id) 389 alunos = sorted(list(meta_geral.topic.subject.students.all()), key = lambda e: e.id)
388 create_excel_file(alunos, itens_da_meta,meta_geral) 390 create_excel_file(alunos, itens_da_meta,meta_geral)
389 - context['goal_file'] = str(meta_geral.slug)+".xls" 391 + context['goal_file'] = str(meta_geral.slug)
390 392
391 393
392 return context 394 return context
@@ -405,6 +407,23 @@ class CreateView(LoginRequiredMixin, LogMixin, generic.edit.CreateView): @@ -405,6 +407,23 @@ class CreateView(LoginRequiredMixin, LogMixin, generic.edit.CreateView):
405 407
406 return success_url 408 return success_url
407 409
  410 +def download_excel(request, file):
  411 + filepath = 'bulletin/sheets/xls/' + file + '.xls'
  412 +
  413 + if not path.exists(filepath):
  414 + raise Http404()
  415 +
  416 + response = HttpResponse(open(filepath, 'rb').read())
  417 + response['Content-Type'] = 'application/force-download'
  418 + response['Pragma'] = 'public'
  419 + response['Expires'] = '0'
  420 + response['Cache-Control'] = 'must-revalidate, post-check=0, pre-check=0'
  421 + response['Content-Disposition'] = 'attachment; filename=%s' % (file + '.xls')
  422 + response['Content-Transfer-Encoding'] = 'binary'
  423 + response['Content-Length'] = str(path.getsize(filepath))
  424 +
  425 + return response
  426 +
408 def create_excel_file(estudantes,metas,meta): 427 def create_excel_file(estudantes,metas,meta):
409 workbook = xlwt.Workbook() 428 workbook = xlwt.Workbook()
410 worksheet = workbook.add_sheet(u'Bulletin') 429 worksheet = workbook.add_sheet(u'Bulletin')
@@ -426,7 +445,7 @@ def create_excel_file(estudantes,metas,meta): @@ -426,7 +445,7 @@ def create_excel_file(estudantes,metas,meta):
426 contador_estudante += 1 445 contador_estudante += 1
427 446
428 path1 = os.path.join(settings.BASE_DIR,'bulletin') 447 path1 = os.path.join(settings.BASE_DIR,'bulletin')
429 - path2 = os.path.join(path1,'static') 448 + path2 = os.path.join(path1,'sheets')
430 path3 = os.path.join(path2,'xls') 449 path3 = os.path.join(path2,'xls')
431 450
432 nome = str(meta.slug) + ".xls" 451 nome = str(meta.slug) + ".xls"
@@ -576,7 +595,7 @@ class UpdateView(LoginRequiredMixin, LogMixin, generic.UpdateView): @@ -576,7 +595,7 @@ class UpdateView(LoginRequiredMixin, LogMixin, generic.UpdateView):
576 itens_da_meta = sorted(list(metas), key = lambda met: met.id) 595 itens_da_meta = sorted(list(metas), key = lambda met: met.id)
577 alunos = sorted(list(meta_geral.topic.subject.students.all()), key = lambda e: e.id) 596 alunos = sorted(list(meta_geral.topic.subject.students.all()), key = lambda e: e.id)
578 create_excel_file(alunos, itens_da_meta,meta_geral) 597 create_excel_file(alunos, itens_da_meta,meta_geral)
579 - context['goal_file'] = str(meta_geral.slug)+".xls" 598 + context['goal_file'] = str(meta_geral.slug)
580 599
581 return context 600 return context
582 601