Commit 75c296d8 authored by Jamesie Pic's avatar Jamesie Pic

Added download as CSV

parent 838ab76f
# coding: utf-8
from django import http
import unicodecsv as csv
class PaginationMixin(object):
......@@ -22,7 +25,7 @@ class PaginationMixin(object):
def get_page_range(self, page):
pages = []
if page.paginator.num_pages != 1:
if page and page.paginator.num_pages != 1:
for i in page.paginator.page_range:
if page.number - 4 < i < page.number + 4:
pages.append(i)
......@@ -57,3 +60,29 @@ class GridListMixin(object):
c = super(GridListMixin, self).get_context_data(**kwargs)
c['grid_list'] = True
return c
class CSVDownloadMixin(object):
def get_paginate_by(self, queryset):
if self.request.GET.get('csv', None) is None:
return super(CSVDownloadMixin, self).get_paginate_by(queryset)
return None
def render_to_csv_response(self, context, **kwargs):
response = http.HttpResponse(content_type='text/csv')
writer = csv.writer(response)
for result in self.get_csv_results(context, **kwargs):
writer.writerow(self.get_csv_row(result))
response['Content-Disposition'] = 'attachment; filename="%s.csv"' % (
self.csv_name)
return response
def render_to_response(self, context, **kwargs):
if self.request.GET.get('csv', None) is None:
return super(CSVDownloadMixin, self).render_to_response(
context, **kwargs)
return self.render_to_csv_response(context, **kwargs)
......@@ -84,6 +84,10 @@
<input type='submit' value='Go' />
</form>
<a href='?csv'>
Download data as CSV
</a>
<div class='pagination-block'>
......
......@@ -84,6 +84,10 @@
<input type='submit' value='Go' />
</form>
<a href='?search=joly&csv'>
Download data as CSV
</a>
<div class='pagination-block'>
......
......@@ -84,6 +84,10 @@
<input type='submit' value='Go' />
</form>
<a href='?csv'>
Download data as CSV
</a>
<div class='pagination-block'>
......
......@@ -84,6 +84,10 @@
<input type='submit' value='Go' />
</form>
<a href='?csv'>
Download data as CSV
</a>
<div class='pagination-block'>
......
......@@ -84,6 +84,10 @@
<input type='submit' value='Go' />
</form>
<a href='?csv'>
Download data as CSV
</a>
<div class='pagination-block'>
......
# Project specific "glue" coupling of all apps
from django.db import models
from core.views import GridListMixin, PaginationMixin
from core.views import GridListMixin, PaginationMixin, CSVDownloadMixin
from representatives import views as representatives_views
from representatives.models import Representative
from representatives_votes import views as representatives_votes_views
......@@ -10,8 +10,28 @@ from representatives_positions.forms import PositionForm
from representatives_recommendations.models import ScoredVote
class RepresentativeList(PaginationMixin, GridListMixin,
representatives_views.RepresentativeList):
class RepresentativeList(
CSVDownloadMixin,
GridListMixin,
PaginationMixin,
representatives_views.RepresentativeList
):
csv_name = 'meps.csv'
def get_csv_results(self, context, **kwargs):
qs = super(RepresentativeList, self).get_queryset()
qs = qs.prefetch_related('email_set')
return [self.add_representative_country_and_main_mandate(r)
for r in qs]
def get_csv_row(self, obj):
return (
obj.full_name,
u', '.join([e.email for e in obj.email_set.all()]),
obj.main_mandate.group.abbreviation,
obj.country,
)
queryset = Representative.objects.filter(
active=True).select_related('score')
......
......@@ -24,6 +24,7 @@ setup(name='political-memory',
'ijson>=2.2,<2.3',
'lesscpy',
'python-dateutil>=2.4,<2.5',
'unicodecsv==0.14.1',
'pytz==2015.7',
'django-suit',
],
......
......@@ -14,6 +14,9 @@
%input{id:"search", type:"text", name:"search"}
%input{type:"submit", value:"Go"}
%a{href:"?{% if request.GET.search %}search={{ request.GET.search }}&{% endif %}csv"}
- trans 'Download data as CSV'
- include 'core/blocks/pagination.html'
- block list
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment