X-Git-Url: https://git.mdrn.pl/edumed.git/blobdiff_plain/0b9065ff097553c60578c2b7f61cb0fee8dddd5e..a914aa2c68ea9b734510a210503b85d1f3869581:/contact/admin.py?ds=inline diff --git a/contact/admin.py b/contact/admin.py index 8d5c5a3..0a96792 100644 --- a/contact/admin.py +++ b/contact/admin.py @@ -1,13 +1,15 @@ # -*- coding: utf-8 -*- +import csv + from django.contrib import admin -from .models import Contact from django.utils.translation import ugettext as _ -from .forms import contact_forms, admin_list_width from django.utils.safestring import mark_safe from django.conf.urls import patterns, url from django.http import HttpResponse, Http404 -from .utils import csv_prepare +from edumed.utils import UnicodeCSVWriter +from .forms import contact_forms, admin_list_width +from .models import Contact class ContactAdminMeta(admin.ModelAdmin.__class__): @@ -114,7 +116,6 @@ class ContactAdmin(admin.ModelAdmin): def extract_view(request, form_tag, extract_type_slug): - toret = u'' contacts_by_spec = dict() form = contact_forms.get(form_tag) if form is None and extract_type_slug not in ('contacts', 'all'): @@ -137,10 +138,13 @@ def extract_view(request, form_tag, extract_type_slug): else: keys = form.get_extract_fields(contact, extract_type_slug) contacts_by_spec.setdefault(tuple(keys), []).append(contact) - + + response = HttpResponse(content_type='text/csv') + csv_writer = UnicodeCSVWriter(response) + # Generate list for each body key set for keys, contacts in contacts_by_spec.items(): - toret += u','.join(keys) + '\n' + csv_writer.writerow(keys) for contact in contacts: if extract_type_slug == 'contacts': records = [dict(contact=contact.contact)] @@ -153,11 +157,14 @@ def extract_view(request, form_tag, extract_type_slug): for key in keys: if key not in record: record[key] = '' - record[key] = csv_prepare(record[key]) - toret += u','.join([record[key] for key in keys]) + '\n' - toret += '\n\n' + if isinstance(record[key], bool): + record[key] = 'tak' if record[key] else 'nie' + if isinstance(record[key], (list, tuple)): + record[key] = ', '.join(record[key]) + + csv_writer.writerow([record[key] for key in keys]) + csv_writer.writerow([]) - response = HttpResponse(toret, content_type='text/csv') response['Content-Disposition'] = 'attachment; filename="kontakt.csv"' return response