zmiana w menu na katalog.edukacjamedialna (compile locale)
[edumed.git] / contact / admin.py
1 from django.contrib import admin
2 from django.forms import ModelForm
3 from .models import Contact
4 from django.utils.translation import ugettext as _
5 from .forms import contact_forms, admin_list_width
6 from django.template import Template
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 .utils import csv_prepare
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     def admin_list(self, obj, nr):
31         try:
32             field_name = contact_forms[obj.form_tag].admin_list[nr]
33         except BaseException, e:
34             return ''
35         else:
36             return Contact.pretty_print(obj.body.get(field_name, ''), for_html=True)
37
38     def __getattr__(self, name):
39         if name.startswith('admin_list_'):
40             nr = int(name[len('admin_list_'):])
41             return lambda obj: self.admin_list(obj, nr)
42         raise AttributeError, name
43
44     def change_view(self, request, object_id, extra_context=None):
45         if object_id:
46             try:
47                 instance = Contact.objects.get(pk=object_id)
48                 assert isinstance(instance.body, dict)
49             except (Contact.DoesNotExist, AssertionError):
50                 pass
51             else:
52                 # Create readonly fields from the body JSON.
53                 body_fields = ['body__%s' % k for k in instance.body.keys()]
54                 attachments = list(instance.attachment_set.all())
55                 body_fields += ['body__%s' % a.tag for a in attachments]
56                 self.readonly_fields.extend(body_fields)
57
58                 # Find the original form.
59                 try:
60                     orig_fields = contact_forms[instance.form_tag]().fields
61                 except KeyError:
62                     orig_fields = {}
63
64                 # Try to preserve the original order.
65                 admin_fields = []
66                 orig_keys = list(orig_fields.keys())
67                 while orig_keys:
68                     key = orig_keys.pop(0)
69                     key = "body__%s" % key
70                     if key in body_fields:
71                         admin_fields.append(key)
72                         body_fields.remove(key)
73                 admin_fields.extend(body_fields)
74
75                 self.fieldsets = [
76                     (None, {'fields': self.fields}),
77                     (_('Body'), {'fields': admin_fields}),
78                 ]
79
80                 # Create field getters for fields and attachments.
81                 for k, v in instance.body.items():
82                     f = (lambda v: lambda self: v)(Contact.pretty_print(v, for_html=True))
83                     f.short_description = orig_fields[k].label if k in orig_fields else _(k)
84                     setattr(self, "body__%s" % k, f)
85
86                 download_link = "<a href='%(url)s'>%(url)s</a>"
87                 for attachment in attachments:
88                     k = attachment.tag
89                     link = mark_safe(download_link % {
90                             'url': attachment.get_absolute_url()})
91                     f = (lambda v: lambda self: v)(link)
92                     f.short_description = orig_fields[k].label if k in orig_fields else _(k)
93                     setattr(self, "body__%s" % k, f)
94         return super(ContactAdmin, self).change_view(request, object_id,
95             extra_context=extra_context)
96
97     def changelist_view(self, request, extra_context=None):
98         context = dict()
99         if 'form_tag' in request.GET:
100             form = contact_forms.get(request.GET['form_tag'])
101             context['extract_types'] = [dict(slug = 'all', label = _('all'))] + [dict(slug = 'contacts', label = _('contacts'))]
102             context['extract_types'] += [type for type in getattr(form, 'extract_types', [])]
103         return super(ContactAdmin, self).changelist_view(request, extra_context = context)
104
105     def get_urls(self):
106         urls = super(ContactAdmin, self).get_urls()
107         return patterns('',
108             url(r'^extract/(?P<form_tag>[\w-]+)/(?P<extract_type_slug>[\w-]+)/$', self.admin_site.admin_view(extract_view), name='contact_extract')
109         ) + super(ContactAdmin, self).get_urls()
110
111
112 def extract_view(request, form_tag, extract_type_slug):
113     toret = u''
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'):
117         raise Http404
118
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')
122     if at_year:
123         q = q.filter(created_at__year = at_year)
124         if at_month:
125             q = q.filter(created_at__month = at_month)
126
127     # Segregate contacts by body key sets
128     for contact in q.all():
129         if extract_type_slug == 'contacts':
130             keys = ['contact']
131         elif extract_type_slug == 'all':
132             keys = contact.body.keys() + ['contact']
133         else:
134             keys = form.get_extract_fields(contact, extract_type_slug)
135         contacts_by_spec.setdefault(tuple(keys), []).append(contact)
136     
137     # Generate list for each body key set
138     for keys, contacts in contacts_by_spec.items():
139         toret += u','.join(keys) + '\n'
140         for contact in contacts:
141             if extract_type_slug == 'contacts':
142                 records = [dict(contact=contact.contact)]
143             elif extract_type_slug == 'all':
144                 records = [dict(contact = contact.contact, **contact.body)]
145             else:
146                 records = form.get_extract_records(keys, contact, extract_type_slug)
147
148             for record in records:
149                 for key in keys:
150                     if key not in record:
151                         record[key] = ''
152                     record[key] = csv_prepare(record[key])
153                 toret += u','.join([record[key] for key in keys]) + '\n'
154         toret += '\n\n'
155
156     response = HttpResponse(toret, content_type = 'text/csv')
157     response['Content-Disposition'] = 'attachment; filename="kontakt.csv"'
158     return response
159
160 admin.site.register(Contact, ContactAdmin)