Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Anthony
memopol
Commits
75c296d8
Commit
75c296d8
authored
Jan 26, 2016
by
Jamesie Pic
Browse files
Added download as CSV
parent
838ab76f
Changes
9
Hide whitespace changes
Inline
Side-by-side
core/views.py
View file @
75c296d8
# 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
)
memopol/tests/response_fixtures/RepresentativeListTest.test_page1_paginateby12_displaylist/content
View file @
75c296d8
...
...
@@ -84,6 +84,10 @@
<input
type=
'submit'
value=
'Go'
/>
</form>
<a
href=
'?csv'
>
Download data as CSV
</a>
<div
class=
'pagination-block'
>
...
...
memopol/tests/response_fixtures/RepresentativeListTest.test_page1_paginateby12_displaylist_searchjoly/content
View file @
75c296d8
...
...
@@ -84,6 +84,10 @@
<input
type=
'submit'
value=
'Go'
/>
</form>
<a
href=
'?search=joly&csv'
>
Download data as CSV
</a>
<div
class=
'pagination-block'
>
...
...
memopol/tests/response_fixtures/RepresentativeListTest.test_page1_paginateby24_displaygrid/content
View file @
75c296d8
...
...
@@ -84,6 +84,10 @@
<input
type=
'submit'
value=
'Go'
/>
</form>
<a
href=
'?csv'
>
Download data as CSV
</a>
<div
class=
'pagination-block'
>
...
...
memopol/tests/response_fixtures/RepresentativeListTest.test_page2_paginateby12_displaylist/content
View file @
75c296d8
...
...
@@ -84,6 +84,10 @@
<input
type=
'submit'
value=
'Go'
/>
</form>
<a
href=
'?csv'
>
Download data as CSV
</a>
<div
class=
'pagination-block'
>
...
...
memopol/tests/response_fixtures/RepresentativeListTest.test_page2_paginateby24_displaylist/content
View file @
75c296d8
...
...
@@ -84,6 +84,10 @@
<input
type=
'submit'
value=
'Go'
/>
</form>
<a
href=
'?csv'
>
Download data as CSV
</a>
<div
class=
'pagination-block'
>
...
...
memopol/views.py
View file @
75c296d8
# 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'
)
...
...
setup.py
View file @
75c296d8
...
...
@@ -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'
,
],
...
...
templates/representatives/representative_list.haml
View file @
75c296d8
...
...
@@ -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
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment