1 # -*- coding: utf-8 -*-
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
10 from .forms import contact_forms, admin_list_width
11 from .models import Contact
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)
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']
31 def admin_list(obj, nr):
33 field_name = contact_forms[obj.form_tag].admin_list[nr]
37 return Contact.pretty_print(obj.body.get(field_name, ''), for_html=True)
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)
45 def change_view(self, request, object_id, form_url='', extra_context=None):
48 instance = Contact.objects.get(pk=object_id)
49 assert isinstance(instance.body, dict)
50 except (Contact.DoesNotExist, AssertionError):
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)
59 # Find the original form.
61 orig_fields = contact_forms[instance.form_tag]().fields
65 # Try to preserve the original order.
67 orig_keys = list(orig_fields.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)
77 (None, {'fields': self.fields}),
78 (_('Body'), {'fields': admin_fields}),
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)
87 download_link = "<a href='%(url)s'>%(url)s</a>"
88 for attachment in attachments:
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)
98 def changelist_view(self, request, extra_context=None):
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)
109 # urls = super(ContactAdmin, self).get_urls()
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()
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'):
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')
127 q = q.filter(created_at__year=at_year)
129 q = q.filter(created_at__month=at_month)
131 # Segregate contacts by body key sets
132 for contact in q.all():
133 if extract_type_slug == 'contacts':
135 elif extract_type_slug == 'all':
136 keys = contact.body.keys() + ['contact']
138 keys = form.get_extract_fields(contact, extract_type_slug)
139 contacts_by_spec.setdefault(tuple(keys), []).append(contact)
141 response = HttpResponse(content_type='text/csv')
142 csv_writer = csv.writer(response)
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)]
153 records = form.get_extract_records(keys, contact, extract_type_slug)
155 for record in records:
157 if key not in record:
159 csv_writer.writerow([record[key].encode('utf-8') for key in keys])
160 csv_writer.writerow([])
162 response['Content-Disposition'] = 'attachment; filename="kontakt.csv"'
165 admin.site.register(Contact, ContactAdmin)