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 url
8 from django.http import HttpResponse, Http404
10 from wolnelektury.utils import UnicodeCSVWriter
11 from .forms import contact_forms, admin_list_width
12 from .models import Contact
15 class ContactAdminMeta(admin.ModelAdmin.__class__):
16 def __getattr__(cls, name):
17 if name.startswith('admin_list_'):
18 return lambda self: ""
19 raise AttributeError(name)
22 class ContactAdmin(admin.ModelAdmin):
23 __metaclass__ = ContactAdminMeta
24 date_hierarchy = 'created_at'
25 list_display = ['created_at', 'contact', 'form_tag'] + \
26 ["admin_list_%d" % i for i in range(admin_list_width)]
27 fields = ['form_tag', 'created_at', 'contact', 'ip']
28 readonly_fields = ['form_tag', 'created_at', 'contact', 'ip']
29 list_filter = ['form_tag']
32 def admin_list(obj, nr):
34 field_name = contact_forms[obj.form_tag].admin_list[nr]
38 return Contact.pretty_print(obj.get_body_json().get(field_name, ''), for_html=True)
40 def __getattr__(self, name):
41 if name.startswith('admin_list_'):
42 nr = int(name[len('admin_list_'):])
43 return lambda obj: self.admin_list(obj, nr)
44 raise AttributeError(name)
46 def change_view(self, request, object_id, form_url='', extra_context=None):
49 instance = Contact.objects.get(pk=object_id)
50 body = instance.get_body_json()
51 assert isinstance(body, dict)
52 except (Contact.DoesNotExist, AssertionError):
55 # Create readonly fields from the body JSON.
56 attachments = list(instance.attachment_set.all())
57 body_keys = body.keys() + [a.tag for a in attachments]
59 # Find the original form.
61 orig_fields = contact_forms[instance.form_tag]().fields
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]
71 self.readonly_fields.extend(admin_fields)
74 (None, {'fields': self.fields}),
75 (_('Body'), {'fields': admin_fields}),
78 # Create field getters for fields and attachments.
79 def attach_getter(key, value):
82 f.short_description = orig_fields[key].label if key in orig_fields else _(key)
83 setattr(self, "body__%s" % key, f)
85 for k, v in body.items():
86 attach_getter(k, Contact.pretty_print(v, for_html=True))
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)
96 def changelist_view(self, request, extra_context=None):
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)
108 url(r'^extract/(?P<form_tag>[\w-]+)/(?P<extract_type_slug>[\w-]+)/$',
109 self.admin_site.admin_view(extract_view), name='contact_extract')
110 ] + super(ContactAdmin, self).get_urls()
113 def extract_view(request, form_tag, extract_type_slug):
114 contacts_by_spec = dict()
115 form = contact_forms.get(form_tag)
116 if form is None and extract_type_slug not in ('contacts', 'all'):
119 q = Contact.objects.filter(form_tag=form_tag)
120 at_year = request.GET.get('created_at__year')
121 at_month = request.GET.get('created_at__month')
123 q = q.filter(created_at__year=at_year)
125 q = q.filter(created_at__month=at_month)
127 # Segregate contacts by body key sets
129 orig_keys = list(form().fields.keys())
132 for contact in q.all():
133 if extract_type_slug == 'contacts':
135 elif extract_type_slug == 'all':
136 keys = contact.get_body_json().keys() + ['contact']
137 keys = [key for key in orig_keys if key in keys] + [key for key in keys if key not in orig_keys]
139 keys = form.get_extract_fields(contact, extract_type_slug)
140 contacts_by_spec.setdefault(tuple(keys), []).append(contact)
142 response = HttpResponse(content_type='text/csv')
143 csv_writer = UnicodeCSVWriter(response)
145 # Generate list for each body key set
146 for keys, contacts in contacts_by_spec.items():
147 csv_writer.writerow(keys)
148 for contact in contacts:
149 if extract_type_slug == 'contacts':
150 records = [dict(contact=contact.contact)]
151 elif extract_type_slug == 'all':
152 records = [dict(contact=contact.contact, **contact.get_body_json())]
154 records = form.get_extract_records(keys, contact, extract_type_slug)
156 for record in records:
158 if key not in record:
160 if isinstance(record[key], str):
162 elif isinstance(record[key], bool):
163 record[key] = 'tak' if record[key] else 'nie'
164 elif isinstance(record[key], (list, tuple)) and all(isinstance(v, str) for v in record[key]):
165 record[key] = ', '.join(record[key])
167 record[key] = json.dumps(record[key])
169 csv_writer.writerow([record[key] for key in keys])
170 csv_writer.writerow([])
172 response['Content-Disposition'] = 'attachment; filename="kontakt.csv"'
175 admin.site.register(Contact, ContactAdmin)