diff sat_website/forms.py @ 60:0d20fb28c32e

many small changes: - set opacity 0.85 to libervia screenshot on the main page - remove videos from the repository (stored on external ftp) - more details in the README concerning the i18n - settings can be overriden by an external file (for the stats) - add in basic features.html a button to experimental features - add a link to the SàT MUC in downloads section - add link to bugzilla + improve other links in dev/community - add + symbol for projects using standard blogging system - add ~ symbol for projects being no communication tool - save subscription form results to a text file - update press.py and links.py
author souliane <souliane@mailoo.org>
date Fri, 15 May 2015 17:15:40 +0200
parents bfa8009f0769
children 26353615cc2e
line wrap: on
line diff
--- a/sat_website/forms.py	Thu May 14 23:42:25 2015 +0200
+++ b/sat_website/forms.py	Fri May 15 17:15:40 2015 +0200
@@ -28,6 +28,7 @@
 from django.conf import settings
 from collections import OrderedDict
 from email import email
+import unicodecsv
 import utils
 import re
 
@@ -146,8 +147,8 @@
     def results(self, user_readable=True):
         """Get the results submitted by the user as a list.
         
-        Keep the pertinent fields and filter out the inappropriate ones.
-        @param user_readable: (bool) set to True to prefer the field labels to their names
+        @param user_readable: (bool) set to True to prefer the fields' labels
+            to their names, also filter out the empty fields.
         @return: list of couple (name, value) or (label, value)
         """
         if not self.is_valid():
@@ -155,11 +156,13 @@
         results = []
         for field in self:
             if isinstance(field.field, Section):
-                continue
-            if field.name in ('email_confirmation', 'agreement_confirmation') or not field.value():
-                continue
-            if field.name == "payment_method" and self['subscription_amount'].value() == "0":
-                continue
+                continue  # filter out section fields
+            if field.name in ('email_confirmation', 'agreement_confirmation'):
+                continue  # filter out confirmation fields
+            if user_readable and not field.value():
+                continue  # filter out empty value
+            if user_readable and field.name == "payment_method" and self['subscription_amount'].value() == "0":
+                continue  # filter out fields that are not pertinent for the user
             key = field.name
             if isinstance(field.field, BooleanField) and user_readable:
                 value = ugettext(u"yes") if field.value() else ugettext(u"no")
@@ -255,6 +258,12 @@
 
         return MSG.format(**data)
 
+    def writeResultToCSV(self):
+        result = [unicode(value) for key, value in self.results(False)]
+        with open(settings.ASSO_SUBSCR_CSV, 'a+') as csvfile:
+            writer = unicodecsv.UnicodeWriter(csvfile, delimiter=';')
+            writer.writerow(result)
+
     def process_submitted_data(self):
         """Send emails to the subscriber and the admins."""
         if not self.is_valid():
@@ -263,3 +272,5 @@
         send_mail(_(u'Subscription to Salut à Toi'), self.prepareResultForUser(), settings.EMAIL_BACKEND, [self['email'].value()], fail_silently=False)
         # send email to admins
         send_mail(_(u'Subscription to Salut à Toi'), self.prepareResultForAdmin(), settings.EMAIL_BACKEND, [email for name, email in settings.ADMINS], fail_silently=False)
+        # save to a CSV file
+        self.writeResultToCSV()