new question
[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
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[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[form_tag]
116
117     q = Contact.objects.filter(form_tag = form_tag)
118     at_year = request.GET.get('created_at__year')
119     at_month = request.GET.get('created_at__month')
120     if at_year:
121         q = q.filter(created_at__year = at_year)
122         if at_month:
123             q = q.filter(created_at__month = at_month)
124
125     # Segregate contacts by body key sets
126     for contact in q.all():
127         if extract_type_slug == 'contacts':
128             keys = ['contact']
129         elif extract_type_slug == 'all':
130             keys = contact.body.keys() + ['contact']
131         else:
132             keys = form.get_extract_fields(contact, extract_type_slug)
133         contacts_by_spec.setdefault(tuple(keys), []).append(contact)
134     
135     # Generate list for each body key set
136     for keys, contacts in contacts_by_spec.items():
137         toret += u','.join(keys) + '\n'
138         for contact in contacts:
139             if extract_type_slug == 'contacts':
140                 records = [dict(contact=contact.contact)]
141             elif extract_type_slug == 'all':
142                 records = [dict(contact = contact.contact, **contact.body)]
143             else:
144                 records = form.get_extract_records(keys, contact, extract_type_slug)
145
146             for record in records:
147                 for key in keys:
148                     if key not in record:
149                         record[key] = ''
150                     record[key] = csv_prepare(record[key])
151                 toret += u','.join([record[key] for key in keys]) + '\n'
152         toret += '\n\n'
153
154     response = HttpResponse(toret, content_type = 'text/csv')
155     response['Content-Disposition'] = 'attachment; filename="kontakt.csv"'
156     return response
157
158 admin.site.register(Contact, ContactAdmin)