Implemented simple group sorted using numerical ordering
authorSverre Rabbelier <srabbelier@gmail.com>
Tue, 17 Feb 2009 16:36:49 +0000
changeset 1371 2d97dbbb4d99
parent 1370 8d684e64fbbe
child 1372 a9b7f64d4813
Implemented simple group sorted using numerical ordering See role.py for some example usages. Note that the reason ToS does not appear seperately is due to the fact that it is defined as a regular field which does not take the group field from the one defined in the model. Patch by: Sverre Rabbelier
app/soc/models/role.py
app/soc/templates/soc/templatetags/_as_table.html
app/soc/views/helper/templatetags/forms_helpers.py
--- a/app/soc/models/role.py	Tue Feb 17 14:25:40 2009 +0000
+++ b/app/soc/models/role.py	Tue Feb 17 16:36:49 2009 +0000
@@ -174,7 +174,7 @@
       verbose_name=ugettext('Street address'))
   res_street.help_text = ugettext(
       'street number and name, lower ASCII characters only')
-  res_street.group=ugettext("Contact Info")
+  res_street.group=ugettext("2. Contact Info")
 
   #: Required field containing residence address city; kept private.
   #: Residence city can only be lower ASCII, not UTF-8 text, because it
@@ -182,7 +182,7 @@
   res_city = db.StringProperty(required=True,
       verbose_name=ugettext('City'))
   res_city.help_text = ugettext('lower ASCII characters only')
-  res_city.group=ugettext("Contact Info")
+  res_city.group=ugettext("2. Contact Info")
 
   #: Optional field containing residence address state or province; kept
   #: private.  Residence state/province can only be lower ASCII, not UTF-8
@@ -192,14 +192,14 @@
   res_state.help_text = ugettext(
       'optional if country/territory does not have states or provinces, '
       'lower ASCII characters only')
-  res_state.group=ugettext("Contact Info")
+  res_state.group=ugettext("2. Contact Info")
 
   #: Required field containing residence address country or territory; kept
   #: private.
   res_country = db.StringProperty(required=True,
       verbose_name=ugettext('Country/Territory'),
       choices=countries.COUNTRIES_AND_TERRITORIES)
-  res_country.group=ugettext("Contact Info")
+  res_country.group=ugettext("2. Contact Info")
 
   #: Required field containing residence address postal code (ZIP code in
   #: the United States); kept private.  Residence postal code can only be
@@ -207,7 +207,14 @@
   res_postalcode = db.StringProperty(required=True,
       verbose_name=ugettext('ZIP/Postal Code'))
   res_postalcode.help_text = ugettext('lower ASCII characters only')
-  res_postalcode.group=ugettext("Contact Info")
+  res_postalcode.group=ugettext("2. Contact Info")
+
+  #: field storing whether the User has agreed to publish his location
+  publish_location = db.BooleanProperty(required=False, default=False,
+      verbose_name=ugettext('Publish my location'))
+  publish_location.help_text = ugettext(
+      'Indicates whether the user agreed to publish location.')
+  publish_location.group=ugettext("3. Contact Info")
 
   #: Optional field containing a separate shipping street address; kept
   #: private.  If shipping address is not present in its entirety, the
@@ -218,7 +225,7 @@
       verbose_name=ugettext('Shipping Street address'))
   ship_street.help_text = ugettext(
       'street number and name, lower ASCII characters only')
-  ship_street.group=ugettext("Shipping Info")
+  ship_street.group=ugettext("4. Shipping Info")
 
   #: Optional field containing shipping address city; kept private.
   #: Shipping city can only be lower ASCII, not UTF-8 text, because, if
@@ -226,7 +233,7 @@
   ship_city = db.StringProperty(
       verbose_name=ugettext('Shipping City'))
   ship_city.help_text = ugettext('lower ASCII characters only')
-  ship_city.group=ugettext("Shipping Info")
+  ship_city.group=ugettext("4. Shipping Info")
 
   #: Optional field containing shipping address state or province; kept
   #: private.  Shipping state/province can only be lower ASCII, not UTF-8
@@ -236,14 +243,14 @@
   ship_state.help_text = ugettext(
       'optional if country/territory does not have states or provinces, '
       'lower ASCII characters only')
-  ship_state.group=ugettext("Shipping Info")
+  ship_state.group=ugettext("4. Shipping Info")
 
   #: Optional field containing shipping address country or territory; kept
   #: private.
   ship_country = db.StringProperty(
       verbose_name=ugettext('Shipping Country/Territory'),
       choices=countries.COUNTRIES_AND_TERRITORIES)
-  ship_country.group=ugettext("Shipping Info")
+  ship_country.group=ugettext("4. Shipping Info")
 
   #: Optional field containing shipping address postal code (ZIP code in
   #: the United States); kept private.  Shipping postal code can only be
@@ -252,7 +259,7 @@
   ship_postalcode = db.StringProperty(
       verbose_name=ugettext('Shipping ZIP/Postal Code'))
   ship_postalcode.help_text = ugettext('lower ASCII characters only')
-  ship_postalcode.group=ugettext("Shipping Info")
+  ship_postalcode.group=ugettext("4. Shipping Info")
 
   #: Required field containing a phone number that will be supplied
   #: to shippers; kept private.
@@ -261,7 +268,7 @@
       verbose_name=ugettext('Phone Number'))
   phone.help_text = ugettext(
       'include complete international calling number with country code')
-  phone.group=ugettext("Shipping Info")
+  phone.group=ugettext("4. Shipping Info")
   
 
   #====================================================================
@@ -275,20 +282,20 @@
       verbose_name=ugettext('Birth Date'))
   birth_date.help_text = ugettext(
       'required for determining program eligibility')
-  birth_date.group=ugettext("Private Info")
+  birth_date.group=ugettext("5. Private Info")
 
   #: Optional field indicating choice of t-shirt, from XXS to XXXL;
   #: kept private.
   tshirt_size = db.StringProperty(
       verbose_name=ugettext('T-shirt Size'),
       choices=('XXS', 'XS', 'S', 'M', 'L', 'XL', 'XXL', 'XXXL'))
-  tshirt_size.group=ugettext("Private Info")
+  tshirt_size.group=ugettext("5. Private Info")
 
   #: Optional field indicating choice of t-shirt fit; kept private.
   tshirt_style = db.StringProperty(
       verbose_name=ugettext('T-shirt Style'),
       choices=('male', 'female'))
-  tshirt_style.group=ugettext("Private Info")
+  tshirt_style.group=ugettext("5. Private Info")
 
   #: field storing wheter the User has agreed to the site-wide Terms of Service.
   #: (Not a required field because the Terms of Service might not be present
@@ -297,7 +304,7 @@
       verbose_name=ugettext('I Agree to the Terms of Service'))
   agreed_to_tos.help_text = ugettext(
       'Indicates whether the user agreed to this role Terms of Service.')
-  agreed_to_tos.group=ugettext("Terms of Service")
+  agreed_to_tos.group=ugettext("6. Terms of Service")
 
   #: field storing when the User has agreed to the site-wide Terms of Service.
   #: (Not a required field because the Terms of Service might not be present
@@ -306,7 +313,7 @@
       verbose_name=ugettext('Has agreed to the Terms of Service on'))
   agreed_to_tos_on.help_text = ugettext(
       'Indicates when the user agreed to this role Terms of Service.')
-  agreed_to_tos.group=ugettext("Terms of Service")
+  agreed_to_tos.group=ugettext("6. Terms of Service")
 
   #: field storing the status of this role
   #: Active means that this role can exercise all it's privileges.
@@ -321,12 +328,6 @@
   status.help_text = ugettext(
       'Indicates the status of the role concerning which privileges may be used.')
 
-  #: field storing whether the User has agreed to publish his location
-  publish_location = db.BooleanProperty(required=False, default=False,
-      verbose_name=ugettext('Publish my location'))
-  publish_location.help_text = ugettext(
-      'Indicates whether the user agreed to publish location.')
-
   def name(self):
     """Property as 'name' for use in common templates.
     """
--- a/app/soc/templates/soc/templatetags/_as_table.html	Tue Feb 17 14:25:40 2009 +0000
+++ b/app/soc/templates/soc/templatetags/_as_table.html	Tue Feb 17 16:36:49 2009 +0000
@@ -26,31 +26,18 @@
 </ul>
 {% endif %}
 
-{% regroup fields by group as grouped_fields %}
-
-{% for grouped_field in grouped_fields %}
-  {% ifequal grouped_field.grouper 'main' %}
-    <tr><td class="grouptitle">General</td></tr>
-    {% for item in grouped_field.list %}
-      {% block main_fields_loop %}
-      {% as_table_row item %}
-      {% endblock %}
-    {% endfor %}
-  {% endifequal %}
-{% endfor %}
-
-{% for grouped_field in grouped_fields %}
-  {% ifnotequal grouped_field.grouper 'main' %}
+{% for group_name, fields in groups %}
   <tr><td>&nbsp;</td></tr>
-  <tr><td class="grouptitle">{{ grouped_field.grouper }}</td></tr>
-  {% for item in grouped_field.list %}
+  {% if group_name %}
+  <tr><td class="grouptitle">{{ group_name }}</td></tr>
+  {% endif %}
+  {% for item in fields %}
     {% block grouped_fields_loop %}
       {% as_table_row item %}
     {% endblock %}
   {% endfor %}
-  {% endifnotequal %}
 {% endfor %}
 
-{% for field in  hidden_fields %}
+{% for field in hidden_fields %}
   {{ field }}
 {% endfor %}
--- a/app/soc/views/helper/templatetags/forms_helpers.py	Tue Feb 17 14:25:40 2009 +0000
+++ b/app/soc/views/helper/templatetags/forms_helpers.py	Tue Feb 17 16:36:49 2009 +0000
@@ -24,6 +24,9 @@
   ]
 
 
+import operator
+import re
+
 from google.appengine.ext import db
 
 from django import template
@@ -31,6 +34,7 @@
 from django.utils.encoding import force_unicode
 from django.utils.html import escape
 
+from soc.logic import dicts
 from soc.views.helper import widgets
 
 
@@ -174,8 +178,8 @@
     # If the field is hidden we display it elsewhere
     if not bf.is_hidden:
       example_text = ''
-      group = 'main'
-      
+      group = '0. '
+
       if hasattr(field, 'group'):
         group = field.group
 
@@ -198,10 +202,14 @@
         item = (name, force_unicode(e))
         hidden_fields_errors.append(item)
 
+  grouped = dicts.groupby(fields, 'group')
+  rexp = re.compile(r"\d+. ")
+  fields = [(rexp.sub('',key), grouped[key]) for key in sorted(grouped)]
+
   context.update({
       'top_errors': form.non_field_errors() or '',
       'hidden_field_errors': hidden_fields_errors or '',
-      'fields':  sorted(fields, key=lambda x: x.get('group')) or '',
+      'groups': fields if fields else '',
       'hidden_fields': hidden_fields or '',
       })