e9e10a0139434b3baaa6011421eba28c8565eb1e
[edumed.git] / contact / admin.py
1 # -*- coding: utf-8 -*-
2 import csv
3
4 from django.contrib import admin
5 from django.utils.translation import ugettext as _
6 from django.utils.safestring import mark_safe
7 from django.conf.urls import patterns, url
8 from django.http import HttpResponse, Http404
9
10 from .forms import contact_forms, admin_list_width
11 from .models import Contact
12
13
14 class ContactAdminMeta(admin.ModelAdmin.__class__):
15     def __getattr__(cls, name):
16         if name.startswith('admin_list_'):
17             return lambda self: ""
18         raise AttributeError(name)
19
20
21 class ContactAdmin(admin.ModelAdmin):
22     __metaclass__ = ContactAdminMeta
23     date_hierarchy = 'created_at'
24     list_display = ['created_at', 'contact', 'form_tag'] + \
25         ["admin_list_%d" % i for i in range(admin_list_width)]
26     fields = ['form_tag', 'created_at', 'contact', 'ip']
27     readonly_fields = ['form_tag', 'created_at', 'contact', 'ip']
28     list_filter = ['form_tag']
29
30     @staticmethod
31     def admin_list(obj, nr):
32         try:
33             field_name = contact_forms[obj.form_tag].admin_list[nr]
34         except BaseException:
35             return ''
36         else:
37             return Contact.pretty_print(obj.body.get(field_name, ''), for_html=True)
38
39     def __getattr__(self, name):
40         if name.startswith('admin_list_'):
41             nr = int(name[len('admin_list_'):])
42             return lambda obj: self.admin_list(obj, nr)
43         raise AttributeError(name)
44
45     def change_view(self, request, object_id, form_url='', extra_context=None):
46         if object_id:
47             try:
48                 instance = Contact.objects.get(pk=object_id)
49                 assert isinstance(instance.body, dict)
50             except (Contact.DoesNotExist, AssertionError):
51                 pass
52             else:
53                 # Create readonly fields from the body JSON.
54                 body_fields = ['body__%s' % k for k in instance.body.keys()]
55                 attachments = list(instance.attachment_set.all())
56                 body_fields += ['body__%s' % a.tag for a in attachments]
57                 self.readonly_fields.extend(body_fields)
58
59                 # Find the original form.
60                 try:
61                     orig_fields = contact_forms[instance.form_tag]().fields
62                 except KeyError:
63                     orig_fields = {}
64
65                 # Try to preserve the original order.
66                 admin_fields = []
67                 orig_keys = list(orig_fields.keys())
68                 while orig_keys:
69                     key = orig_keys.pop(0)
70                     key = "body__%s" % key
71                     if key in body_fields:
72                         admin_fields.append(key)
73                         body_fields.remove(key)
74                 admin_fields.extend(body_fields)
75
76                 self.fieldsets = [
77                     (None, {'fields': self.fields}),
78                     (_('Body'), {'fields': admin_fields}),
79                 ]
80
81                 # Create field getters for fields and attachments.
82                 for k, v in instance.body.items():
83                     f = (lambda v: lambda self: v)(Contact.pretty_print(v, for_html=True))
84                     f.short_description = orig_fields[k].label if k in orig_fields else _(k)
85                     setattr(self, "body__%s" % k, f)
86
87                 download_link = "<a href='%(url)s'>%(url)s</a>"
88                 for attachment in attachments:
89                     k = attachment.tag
90                     link = mark_safe(download_link % {
91                             'url': attachment.get_absolute_url()})
92                     f = (lambda v: lambda self: v)(link)
93                     f.short_description = orig_fields[k].label if k in orig_fields else _(k)
94                     setattr(self, "body__%s" % k, f)
95         return super(ContactAdmin, self).change_view(
96             request, object_id, form_url=form_url, extra_context=extra_context)
97
98     def changelist_view(self, request, extra_context=None):
99         context = dict()
100         if 'form_tag' in request.GET:
101             form = contact_forms.get(request.GET['form_tag'])
102             context['extract_types'] = [
103                 {'slug': 'all', 'label': _('all')},
104                 {'slug': 'contacts', 'label': _('contacts')}]
105             context['extract_types'] += [type for type in getattr(form, 'extract_types', [])]
106         return super(ContactAdmin, self).changelist_view(request, extra_context=context)
107
108     def get_urls(self):
109         # urls = super(ContactAdmin, self).get_urls()
110         return patterns(
111             '',
112             url(r'^extract/(?P<form_tag>[\w-]+)/(?P<extract_type_slug>[\w-]+)/$',
113                 self.admin_site.admin_view(extract_view), name='contact_extract')
114         ) + super(ContactAdmin, self).get_urls()
115
116
117 def extract_view(request, form_tag, extract_type_slug):
118     contacts_by_spec = dict()
119     form = contact_forms.get(form_tag)
120     if form is None and extract_type_slug not in ('contacts', 'all'):
121         raise Http404
122
123     q = Contact.objects.filter(form_tag=form_tag)
124     at_year = request.GET.get('created_at__year')
125     at_month = request.GET.get('created_at__month')
126     if at_year:
127         q = q.filter(created_at__year=at_year)
128         if at_month:
129             q = q.filter(created_at__month=at_month)
130
131     # Segregate contacts by body key sets
132     for contact in q.all():
133         if extract_type_slug == 'contacts':
134             keys = ['contact']
135         elif extract_type_slug == 'all':
136             keys = contact.body.keys() + ['contact']
137         else:
138             keys = form.get_extract_fields(contact, extract_type_slug)
139         contacts_by_spec.setdefault(tuple(keys), []).append(contact)
140
141     response = HttpResponse(content_type='text/csv')
142     csv_writer = csv.writer(response)
143
144     # Generate list for each body key set
145     for keys, contacts in contacts_by_spec.items():
146         csv_writer.writerow(keys)
147         for contact in contacts:
148             if extract_type_slug == 'contacts':
149                 records = [dict(contact=contact.contact)]
150             elif extract_type_slug == 'all':
151                 records = [dict(contact=contact.contact, **contact.body)]
152             else:
153                 records = form.get_extract_records(keys, contact, extract_type_slug)
154
155             for record in records:
156                 for key in keys:
157                     if key not in record:
158                         record[key] = ''
159                 csv_writer.writerow([record[key].encode('utf-8') for key in keys])
160         csv_writer.writerow([])
161
162     response['Content-Disposition'] = 'attachment; filename="kontakt.csv"'
163     return response
164
165 admin.site.register(Contact, ContactAdmin)