From 143ec783886ff876d371dc74370fbb037166cb9c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Aleksander=20=C5=81ukasz?= Date: Tue, 5 Nov 2013 09:22:51 +0100 Subject: [PATCH] Extracting contact forms data to pseudo csv format --- contact/admin.py | 64 ++++++++++++++++++ contact/locale/pl/LC_MESSAGES/django.mo | Bin 1537 -> 1657 bytes contact/locale/pl/LC_MESSAGES/django.po | 55 +++++++++------ .../admin/contact/contact/change_list.html | 18 +++++ contact/utils.py | 8 +++ edumed/contact_forms.py | 31 +++++++++ edumed/locale/pl/LC_MESSAGES/django.mo | Bin 1832 -> 1893 bytes edumed/locale/pl/LC_MESSAGES/django.po | 35 +++++----- 8 files changed, 173 insertions(+), 38 deletions(-) create mode 100644 contact/templates/admin/contact/contact/change_list.html create mode 100644 contact/utils.py diff --git a/contact/admin.py b/contact/admin.py index 33b1963..f849f95 100644 --- a/contact/admin.py +++ b/contact/admin.py @@ -5,6 +5,10 @@ from django.utils.translation import ugettext as _ 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 + +from .utils import csv_prepare class ContactAdminMeta(admin.ModelAdmin.__class__): @@ -90,5 +94,65 @@ 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[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): + toret = u'' + contacts_by_spec = dict() + form = contact_forms[form_tag] + + 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) + + # Segregate contacts by body key sets + for contact in q.all(): + if extract_type_slug == 'contacts': + keys = ['contact'] + elif extract_type_slug == 'all': + keys = contact.body.keys() + ['contact'] + else: + keys = form.get_extract_fields(contact, extract_type_slug) + contacts_by_spec.setdefault(tuple(keys), []).append(contact) + + # Generate list for each body key set + for keys, contacts in contacts_by_spec.items(): + toret += u','.join(keys) + '\n' + for contact in contacts: + if extract_type_slug == 'contacts': + records = [dict(contact=contact.contact)] + elif extract_type_slug == 'all': + records = [dict(contact = contact.contact, **contact.body)] + else: + records = form.get_extract_records(keys, contact, extract_type_slug) + + for record in records: + 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' + + response = HttpResponse(toret, content_type = 'text/csv') + response['Content-Disposition'] = 'attachment; filename="kontakt.csv"' + return response admin.site.register(Contact, ContactAdmin) diff --git a/contact/locale/pl/LC_MESSAGES/django.mo b/contact/locale/pl/LC_MESSAGES/django.mo index 985b0c0f584c959cdaab7dec71ab592201cc2b00..2b2e6e9a44225b8271f668e7eed6d5f0c2db9629 100644 GIT binary patch delta 590 zcmYk&zb`{k6u|NO+E@gk^o4d1B2)*7*lY|{EU&zzeF-&ax@cl8gZMLQ zG?1vo(nVMVi&$)85sUBX4{?+8KKI;r@3}upo^Ma_#iyStnx6;~7et6y*7(q_P{(WZ z;4OCIJ$B<0Ch#3QFyv6G3nLiC2^_^WoW(Lav4+$wsyjMTu#RqgZ2nNM*iZh3efZVN zovfC__)*q9P-PIGQPzL9{J|M=-PN2ok3Hl|n8a0_Vt;i;XOV$AN&!FDkix;15tQ+9 zl$*s-4m5|d@d^%M202&bA2jB2=EmzuF1MB@Fa6in>9KEPLm8A4$}|6OUJ`j~d5r<0 zm+%pCDE3rp!P+dSq_u0OGj@2}%I&DszMUhf9BUtebT+FtWJabrqiw!YavXUlqlOWU z#luEy+DMGWjOwuSP;;luJOxa@IrBqr cu4DbC*ZB=5z0lUSo-dzTQq?(Co0Rfve~vymS^xk5 diff --git a/contact/locale/pl/LC_MESSAGES/django.po b/contact/locale/pl/LC_MESSAGES/django.po index 7805958..35e33a3 100644 --- a/contact/locale/pl/LC_MESSAGES/django.po +++ b/contact/locale/pl/LC_MESSAGES/django.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2012-10-10 13:12+0200\n" +"POT-Creation-Date: 2013-11-05 10:16+0100\n" "PO-Revision-Date: 2012-10-10 13:12+0100\n" "Last-Translator: Radek Czajka \n" "Language-Team: LANGUAGE \n" @@ -15,50 +15,62 @@ msgstr "" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" -"Plural-Forms: nplurals=3; plural=(n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2)\n" +"Plural-Forms: nplurals=3; plural=(n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 " +"|| n%100>=20) ? 1 : 2)\n" -#: admin.py:49 +#: admin.py:77 msgid "Body" msgstr "Treść" -#: models.py:10 +#: admin.py:101 +msgid "all" +msgstr "wszystko" + +#: admin.py:101 +msgid "contacts" +msgstr "kontakty" + +#: forms.py:29 +msgid "Contact form" +msgstr "Formularz kontaktowy" + +#: forms.py:30 +msgid "Submit" +msgstr "Wyślij" + +#: models.py:11 msgid "submission date" msgstr "data wysłania" -#: models.py:11 +#: models.py:12 msgid "IP address" msgstr "adres IP" -#: models.py:12 +#: models.py:13 msgid "contact" msgstr "kontakt" -#: models.py:13 +#: models.py:14 msgid "form" msgstr "formularz" -#: models.py:14 +#: models.py:15 msgid "body" msgstr "treść" -#: models.py:18 +#: models.py:29 msgid "submitted form" msgstr "przysłany formularz" -#: models.py:19 +#: models.py:30 msgid "submitted forms" msgstr "przysłane formularze" -#: templates/contact/form.html:5 -msgid "Contact form" -msgstr "Formularz kontaktowy" +#: templates/admin/contact/contact/change_list.html:12 +msgid "Extract" +msgstr "Ekstrakt" -#: templates/contact/form.html:14 -msgid "Submit" -msgstr "Wyślij" - -#: templates/contact/mail_body.txt:2 -#: templates/contact/mail_subject.txt:1 +#: templates/contact/mail_body.txt:2 templates/contact/mail_subject.txt:1 #, python-format msgid "Thank you for contacting us at %(site_name)s." msgstr "Dziękujemy za skontaktowanie się z nami na stronie %(site_name)s." @@ -71,11 +83,10 @@ msgstr "Twoje zgłoszenie zostało przekazane osobie koordynującej projekt." msgid "Message sent automatically. Please do not reply to it." msgstr "Wiadomość wysłana automatycznie, prosimy nie odpowiadać." -#: templates/contact/thanks.html:5 +#: templates/contact/thanks.html:4 templates/contact/thanks.html.py:8 msgid "Thank you" msgstr "Dziękujemy" -#: templates/contact/thanks.html:8 +#: templates/contact/thanks.html:11 msgid "Thank you for submitting the contact form." msgstr "Dziękujemy za wypełnienie formularza kontaktowego." - diff --git a/contact/templates/admin/contact/contact/change_list.html b/contact/templates/admin/contact/contact/change_list.html new file mode 100644 index 0000000..3b47c97 --- /dev/null +++ b/contact/templates/admin/contact/contact/change_list.html @@ -0,0 +1,18 @@ +{% extends "admin/change_list.html" %} +{% load i18n %} +{% load admin_urls %} + + +{% block object-tools-items %} + {{block.super}} + {% if request.GET.form_tag %} + {% for extract_type in extract_types %} +
  • + + {% trans 'Extract' %}: {{extract_type.label}} + +
  • + {% endfor %} + {% endif %} +{% endblock %} + diff --git a/contact/utils.py b/contact/utils.py new file mode 100644 index 0000000..ce37833 --- /dev/null +++ b/contact/utils.py @@ -0,0 +1,8 @@ +def csv_escape(string): + return '"' + string.replace('\r\n', ' ').replace('\n', ' ').replace('"', '\"') + '"' + +def csv_prepare(obj): + to_escape = obj + if not isinstance(obj, unicode): + to_escape = str(to_escape) + return csv_escape(to_escape) \ No newline at end of file diff --git a/edumed/contact_forms.py b/edumed/contact_forms.py index 507c6d1..8d3131e 100644 --- a/edumed/contact_forms.py +++ b/edumed/contact_forms.py @@ -128,6 +128,37 @@ class WTEMForm(ContactForm): required=False ) + extract_types = (dict(slug='extended', label=_('extended')),) + + @staticmethod + def get_extract_fields(contact, extract_type_slug): + fields = contact.body.keys() + fields.pop(fields.index('student')) + fields.extend(['contact', 'student_first_name', 'student_last_name', 'student_email']) + return fields + + @staticmethod + def get_extract_records(keys, contact, extract_type_slug): + toret = [dict()] + for field_name in keys: + if field_name.startswith('student_'): + continue + if field_name == 'contact': + val = contact.contact + else: + val = contact.body[field_name] + toret[0][field_name] = val + + current = toret[0] + for student in contact.body['student']: + for attr in ('first_name', 'last_name', 'email'): + current['student_' + attr] = student[attr] + if not current in toret: + toret.append(current) + current = dict() + return toret + + class MILForm(ContactForm): form_tag = 'mil' form_title = _('Share your thoughts on the "Media and information literacy competencies catalogue"') diff --git a/edumed/locale/pl/LC_MESSAGES/django.mo b/edumed/locale/pl/LC_MESSAGES/django.mo index 455c60e7f7d5417474656a0584b7f960be22f6ca..2417c1bab9747395c852e688a516f285317053a0 100644 GIT binary patch delta 487 zcmYk&ze@sf7{~GF`f6F7R~@v3MT5{#(Ou;tPA)-XAk`XotA;k%ElvgvE`_x^Iy5Bi zK@hqGE^Se?1pNX10lgnH>4E27_k;UBpYONrd^)vvH`Wx&rBn1By-epUF4R5R*uf+| z;1oXNB)*Kkzv3*gO1|A2B?8fQG-@-4(m9BP1HawT*S`EKJGGK-nh{PTw>`0OSpw=n8icP;T2xu z05!ldKaodbv_9)lJ)ytSHUAg89d2X+b&j66c(fjmCS~Kp(zIk67xTttx^?TrN`%$3 muo#5Fbzllff78pX`d-%GUibWN&zu;$T#71DSdL1?bNK;#7%s2? delta 425 zcmXxgKTpCy7{~D|#~&3aLSjgaAx<(-LqQuzW8z5SXqZhjGV>DZNM}MP;?#iwroI8# zeG3{*WZ(_x!r=F5>yte9X>+-|-`&<+nBGS%J5yAZKBGgrPFn^8HNg`4SjGSg7-Alm z>3oDm{?~YnZ`iA;#=jh-!-r=cMYQy3Y$^p$>;uT7e zTay$5$jrTylVfA!B?y zSuZ6o^j~ZfzbdzkOI}L?Nk|DUr4JaSv(d`\n" "Language-Team: LANGUAGE \n" @@ -18,45 +18,48 @@ msgstr "" "Plural-Forms: nplurals=3; plural=(n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 " "|| n%100>=20) ? 1 : 2)\n" -#: contact_forms.py:133 +#: contact_forms.py:131 +msgid "extended" +msgstr "rozszerzony" + +#: contact_forms.py:160 msgid "" "Share your thoughts on the \"Media and information literacy competencies " "catalogue\"" -msgstr "" -" Podziel się uwagami na temat Katalogu Kompetencji " +msgstr " Podziel się uwagami na temat Katalogu Kompetencji " -#: contact_forms.py:134 +#: contact_forms.py:161 msgid "Submit" msgstr "Zgłoś" -#: contact_forms.py:138 +#: contact_forms.py:165 msgid "Name and Surname" msgstr "Imię i nazwisko" -#: contact_forms.py:139 +#: contact_forms.py:166 msgid "E-mail" msgstr "Adres e-mail" -#: contact_forms.py:141 +#: contact_forms.py:168 msgid "Institution" msgstr "Instytucja" -#: contact_forms.py:144 +#: contact_forms.py:171 msgid "What do you think about the proposed educational stages classification?" msgstr "Co sądzisz o zaproponowanym podziale na etapy edukacyjne?" -#: contact_forms.py:151 +#: contact_forms.py:178 msgid "What do you think about the proposed thematic fields?" msgstr "Co sądzisz o zaproponowanym podziale na obszary tematyczne?" -#: contact_forms.py:158 +#: contact_forms.py:185 msgid "" "What important areas of media and information literacy have been left out?" msgstr "" "Jakie ważne obszary edukacji medialnej i informacyjnej zostały Twoim zdaniem " "niewystarczająco pogłębione lub pominięto je w ogóle?" -#: contact_forms.py:165 +#: contact_forms.py:192 msgid "Other suggestions and comments" msgstr "Inne uwagi i sugestie" @@ -77,19 +80,19 @@ msgstr "Strona której szukasz nie została znaleziona." msgid "Forum search" msgstr "Szukaj na forum" -#: templates/base_mil.html:8 +#: templates/base_mil.html:9 msgid "Consultations" msgstr "Konsultacje" -#: templates/base_mil.html:9 +#: templates/base_mil.html:10 msgid "Competencies" msgstr "Kompetencje" -#: templates/base_mil.html:10 +#: templates/base_mil.html:11 msgid "Take Part" msgstr "Weź udział" -#: templates/base_mil.html:14 +#: templates/base_mil.html:15 msgid "Contact" msgstr "Kontakt" -- 2.20.1