X-Git-Url: https://git.mdrn.pl/prawokultury.git/blobdiff_plain/a55d79e4c27c893841712145e38d4a4a61e04b20..295dde537cc113a8261a25278c42ae5dad53784e:/contact/admin.py diff --git a/contact/admin.py b/contact/admin.py index 8bd6341..90bd60d 100644 --- a/contact/admin.py +++ b/contact/admin.py @@ -2,16 +2,46 @@ from django.contrib import admin from django.forms import ModelForm from .models import Contact from django.utils.translation import ugettext as _ -from .forms import contact_forms +from .forms import contact_forms, admin_list_width from django.template import Template from django.utils.safestring import mark_safe +from django.conf.urls import patterns, url +from django.http import HttpResponse, Http404 + +from .utils import deunicode +from StringIO import StringIO +from csv import writer + + +class ContactAdminMeta(admin.ModelAdmin.__class__): + def __getattr__(cls, name): + if name.startswith('admin_list_'): + return lambda self: "" + raise AttributeError, name class ContactAdmin(admin.ModelAdmin): + __metaclass__ = ContactAdminMeta date_hierarchy = 'created_at' - list_display = ('created_at', 'contact', 'form_tag') - fields = ['form_tag', 'created_at', 'contact', 'ip'] - readonly_fields = ['form_tag', 'created_at', 'contact', 'ip'] + list_display = ['created_at', 'contact', 'form_tag'] + \ + ["admin_list_%d" % i for i in range(admin_list_width)] + fields = ['form_tag', 'created_at', 'contact', 'ip', 'key'] + readonly_fields = ['form_tag', 'created_at', 'contact', 'ip', 'key'] + list_filter = ['form_tag'] + + def admin_list(self, obj, nr): + try: + field_name = contact_forms[obj.form_tag].admin_list[nr] + except BaseException, e: + return '' + else: + return obj.body.get(field_name, '') + + def __getattr__(self, name): + if name.startswith('admin_list_'): + nr = int(name[len('admin_list_'):]) + return lambda obj: self.admin_list(obj, nr) + raise AttributeError, name def change_view(self, request, object_id, extra_context=None): if object_id: @@ -66,5 +96,70 @@ class ContactAdmin(admin.ModelAdmin): return super(ContactAdmin, self).change_view(request, object_id, extra_context=extra_context) + def changelist_view(self, request, extra_context=None): + context = dict() + if 'form_tag' in request.GET: + form = contact_forms.get(request.GET['form_tag']) + context['extract_types'] = [dict(slug = 'all', label = _('all'))] + [dict(slug = 'contacts', label = _('contacts'))] + context['extract_types'] += [type for type in getattr(form, 'extract_types', [])] + return super(ContactAdmin, self).changelist_view(request, extra_context = context) + + def get_urls(self): + urls = super(ContactAdmin, self).get_urls() + return patterns('', + url(r'^extract/(?P[\w-]+)/(?P[\w-]+)/$', self.admin_site.admin_view(extract_view), name='contact_extract') + ) + super(ContactAdmin, self).get_urls() + + +def extract_view(request, form_tag, extract_type_slug): + retval = StringIO() + toret = writer(retval) + contacts_by_spec = dict() + try: + form = [f for f in contact_forms.values() if f.save_as_tag == form_tag][0] + except IndexError: + form = None + if form is None and extract_type_slug not in ('contacts', 'all'): + raise Http404 + + q = Contact.objects.filter(form_tag = form_tag) + at_year = request.GET.get('created_at__year') + at_month = request.GET.get('created_at__month') + if at_year: + q = q.filter(created_at__year = at_year) + if at_month: + q = q.filter(created_at__month = at_month) + + if extract_type_slug == 'contacts': + keys = set(['contact']) + else: + keys = set(['contact', 'ip', 'created_at']) + for contact in q.all(): + if extract_type_slug == 'all': + keys.update(contact.body.keys()) + else: + keys.update(form.get_extract_fields(contact, extract_type_slug)) + + keys = sorted(keys) + key_labels = {f.name: f.verbose_name.encode('utf-8') for f in Contact._meta.fields} + if form is not None: + fi = form() + key_labels.update({k: v.label.encode('utf-8') for (k, v) in fi.fields.items()}) + + toret.writerow([key_labels.get(key, key) for key in keys]) + for contact in q.all(): + if extract_type_slug == 'contacts': + records = [dict(contact=contact.contact)] + elif extract_type_slug == 'all': + records = [dict(contact = contact.contact, ip=contact.ip, created_at=contact.created_at, **contact.body)] + else: + records = form.get_extract_records(keys, contact, extract_type_slug) + + for record in records: + toret.writerow([deunicode(record.get(key, '-')) for key in keys]) + + response = HttpResponse(retval.getvalue(), content_type = 'text/csv') + response['Content-Disposition'] = 'attachment; filename="%s.csv"' % form_tag + return response admin.site.register(Contact, ContactAdmin)