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