views.py 4.42 KB
Newer Older
1
# coding: utf-8
2
3
4

from copy import copy

Jamesie Pic's avatar
Jamesie Pic committed
5
6
7
from django import http

import unicodecsv as csv
8

Jamesie Pic's avatar
Jamesie Pic committed
9

10
11
12
13
14
15
16
17
18
19
20
class ActiveLegislatureMixin(object):
    """
    Mixin for views that can switch between active legislature and all data
    """

    default_active_only = True

    def get(self, *args, **kwargs):
        self.set_active_only()
        return super(ActiveLegislatureMixin, self).get(*args, **kwargs)

21
22
23
24
25
26
27
28
    def override_active_only(self):
        """
        Redefine this method to override active legislature selection
        - return None to enable user choice
        - return True or False to disable user choice and set active state
        """
        return None

29
30
31
32
33
34
35
36
    def set_active_only(self):
        if 'active_only' in self.request.GET:
            self.request.session['active_only'] = \
                self.request.GET['active_only'] == '1'
        elif 'active_only' not in self.request.session:
            self.request.session['active_only'] = self.default_active_only

    def get_active_only(self):
37
38
        overriden = self.override_active_only()
        if overriden is None:
39
40
41
42
            if 'active_only' in self.request.session:
                return self.request.session['active_only']
            else:
                return self.default_active_only
43
44
        else:
            return overriden
45
46
47

    def get_context_data(self, **kwargs):
        c = super(ActiveLegislatureMixin, self).get_context_data(**kwargs)
48
49
        if self.override_active_only() is None:
            c['active_only'] = self.get_active_only()
50
51
52
        return c


Jamesie Pic's avatar
Jamesie Pic committed
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
class PaginationMixin(object):
    pagination_limits = (12, 24, 48, 96)

    def get(self, *args, **kwargs):
        self.set_paginate_by()
        return super(PaginationMixin, self).get(*args, **kwargs)

    def set_paginate_by(self):
        if 'paginate_by' in self.request.GET:
            self.request.session['paginate_by'] = \
                self.request.GET['paginate_by']

        elif 'paginate_by' not in self.request.session:
            self.request.session['paginate_by'] = 12

    def get_paginate_by(self, queryset):
        return self.request.session['paginate_by']

    def get_page_range(self, page):
        pages = []

Jamesie Pic's avatar
Jamesie Pic committed
74
        if page and page.paginator.num_pages != 1:
Jamesie Pic's avatar
Jamesie Pic committed
75
76
77
78
79
80
81
82
83
84
85
            for i in page.paginator.page_range:
                if page.number - 4 < i < page.number + 4:
                    pages.append(i)

        return pages

    def get_context_data(self, **kwargs):
        c = super(PaginationMixin, self).get_context_data(**kwargs)
        c['pagination_limits'] = self.pagination_limits
        c['paginate_by'] = self.request.session['paginate_by']
        c['page_range'] = self.get_page_range(c['page_obj'])
86
87
88
        c['queries'] = copy(self.request.GET)
        if 'page' in c['queries']:
            del c['queries']['page']
Jamesie Pic's avatar
Jamesie Pic committed
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
        return c


class GridListMixin(object):
    def set_session_display(self):
        if self.request.GET.get('display') in ('grid', 'list'):
            self.request.session['display'] = self.request.GET.get('display')

        if 'display' not in self.request.session:
            self.request.session['display'] = 'grid'

    def get(self, *args, **kwargs):
        self.set_session_display()
        return super(GridListMixin, self).get(*args, **kwargs)

    def get_template_names(self):
        return [t.replace('_list', '_%s' % self.request.session['display'])
                for t in super(GridListMixin, self).get_template_names()]

    def get_context_data(self, **kwargs):
        c = super(GridListMixin, self).get_context_data(**kwargs)
        c['grid_list'] = True
        return c
Jamesie Pic's avatar
Jamesie Pic committed
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137


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)