Skip to content

Commit

Permalink
#256 firstvariont of exporting the task lit to excel
Browse files Browse the repository at this point in the history
  • Loading branch information
yn-coder committed Apr 20, 2021
1 parent 20f4690 commit 02fbdff
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
1 change: 1 addition & 0 deletions project/templates/project/project.html
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ <h2>{% trans "Milestones" %}{% if user_can_admin %} <a class="btn btn-default" h
<h2>{% trans "Reports" %}</h2>

<p><a href="{% url 'project:project_view_report_all_tasks' project.id %}" target="_blank">Download all tasks report (new window)</a></p>
<p><a href="{% url 'project:project_view_report_all_tasks_xls' project.id %}" target="_blank">Download all tasks report (as Excel)</a></p>

{% endif %}

Expand Down
2 changes: 2 additions & 0 deletions project/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
url(r'^project/(?P<project_id>\w+)/members/$', views.project_view_members, name='project_view_members'),
url(r'^project/(?P<project_id>\w+)/milestones/$', views.project_view_milestones, name='project_view_milestones'),
url(r'^project/(?P<project_id>\w+)/reports/$', views.project_view_reports, name='project_view_reports'),

url(r'^project/(?P<project_id>\w+)/report_all_tasks/$', views.project_view_report_all_tasks, name='project_view_report_all_tasks'),
url(r'^project/(?P<project_id>\w+)/report_all_tasks_xls/$', views.project_view_report_all_tasks_xls.as_view(), name='project_view_report_all_tasks_xls'),
# repo urls
url(r'^project/(?P<project_id>\w+)/files/$', views.project_view_files, name='project_view_files'),
url(r'^project/(?P<project_id>\w+)/files/commit/(?P<rev_id>\w+)/$', views.project_view_file_commit_view, name='project_view_file_commit_view'),
Expand Down
65 changes: 64 additions & 1 deletion project/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
from django.urls import reverse
from django.db.models import Count

from django.http import HttpResponseForbidden
from django.http import HttpResponseForbidden, HttpResponse

from django.utils.html import strip_tags

from account.mixins import LoginRequiredMixin
from django.views.generic import View
from django.views.generic.edit import CreateView, UpdateView

from django.contrib.auth.mixins import PermissionRequiredMixin
Expand Down Expand Up @@ -465,6 +467,67 @@ def project_view_report_all_tasks(request, project_id ):
return render( request, 'project/project_report_all_tasks.html',
context_dict )

class project_view_report_all_tasks_xls(View):

def get(self, request, project_id):
context = RequestContext(request)
project = get_object_or_404( Project, pk=project_id)

if not project.can_view( request.user ):
raise Http404()

import io
import xlsxwriter

# Create an in-memory output file for the new workbook.
output = io.BytesIO()

# Even though the final file will be in memory the module uses temp
# files during assembly for efficiency. To avoid this on servers that
# don't allow temp files, for example the Google APP Engine, set the
# 'in_memory' Workbook() constructor option as shown in the docs.
workbook = xlsxwriter.Workbook(output)
worksheet = workbook.add_worksheet()

# Get data to write to the spreadsheet.
data = project.project2tasks.all().order_by('created_at')

# Write data.
fields = { 'id', 'created_at', 'fullname', 'holder', 'state', 'detailed_state', 'milestone', 'finished_fact_at', 'important', 'kind', 'sub_project' }

#Task._meta.get_fields()
row_num = 0
col_num = 0
for field in fields:
cell_data = field
worksheet.write(row_num, col_num, cell_data)
col_num = col_num + 1

row_num = 1
for task in data:
col_num = 0
for field in fields:
cell_data = getattr(task, field )
worksheet.write(row_num, col_num, str( cell_data) )
col_num = col_num + 1
row_num = row_num + 1

# Close the workbook before sending the data.
workbook.close()

# Rewind the buffer.
output.seek(0)

# Set up the Http response.
filename = project_id + '.xlsx'
response = HttpResponse(
output,
content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
)
response['Content-Disposition'] = 'attachment; filename=%s' % filename

return response

def project_view_file_commit_view(request, project_id, rev_id):
context = RequestContext(request)
project = get_object_or_404( Project, pk=project_id)
Expand Down

0 comments on commit 02fbdff

Please sign in to comment.