All address fields for roles and groups can now only use characters that can successfully be converted to ASCII.
authorLennard de Rijk <ljvderijk@gmail.com>
Sat, 07 Mar 2009 19:17:02 +0000
changeset 1722 7f285e96cb17
parent 1721 9acf4fe1b9bb
child 1723 259bba384c60
All address fields for roles and groups can now only use characters that can successfully be converted to ASCII. Has to do with Issue 243. Patch by: Lennard de Rijk Reviewed by: to-be-reviewed
app/soc/logic/cleaning.py
app/soc/views/models/group.py
app/soc/views/models/role.py
--- a/app/soc/logic/cleaning.py	Sat Mar 07 18:16:54 2009 +0000
+++ b/app/soc/logic/cleaning.py	Sat Mar 07 19:17:02 2009 +0000
@@ -277,6 +277,26 @@
   return wrapped
 
 
+def clean_ascii_only(field_name):
+  """Clean method for cleaning a field that may only contain ASCII-characters.
+  """
+  @check_field_is_empty(field_name)
+  def wrapper(self):
+    """Decorator wrapper method.
+    """
+    value = self.cleaned_data.get(field_name)
+
+    try:
+      # encode to ASCII
+      value = value.encode("ascii")
+    except UnicodeEncodeError:
+      # can not encode as ASCII
+      raise forms.ValidationError("Only ASCII characters are allowed")
+
+    return value
+  return wrapper
+
+
 def clean_feed_url(self):
   """Clean method for cleaning feed url.
   """
@@ -316,6 +336,7 @@
 
   return wrapped
 
+
 def clean_url(field_name):
   """Clean method for cleaning a field belonging to a LinkProperty.
   """
--- a/app/soc/views/models/group.py	Sat Mar 07 18:16:54 2009 +0000
+++ b/app/soc/views/models/group.py	Sat Mar 07 19:17:02 2009 +0000
@@ -27,6 +27,7 @@
 from django import http
 from django.utils.translation import ugettext
 
+from soc.logic import cleaning
 from soc.logic import dicts
 from soc.logic.models import user as user_logic
 from soc.views.helper import decorators
@@ -89,6 +90,18 @@
     new_params['list_row'] = 'soc/group/list/row.html'
     new_params['list_heading'] = 'soc/group/list/heading.html'
 
+    new_params['create_extra_dynaproperties'] = {
+       'clean_contact_street': cleaning.clean_ascii_only('contact_street'),
+       'clean_contact_city': cleaning.clean_ascii_only('contact_city'),
+       'clean_contact_state': cleaning.clean_ascii_only('contact_state'),
+       'clean_contact_postalcode': cleaning.clean_ascii_only('contact_postalcode'),
+       'clean_shipping_street': cleaning.clean_ascii_only('shipping_street'),
+       'clean_shipping_city': cleaning.clean_ascii_only('shipping_city'),
+       'clean_shipping_state': cleaning.clean_ascii_only('shipping_state'),
+       'clean_shipping_postalcode': cleaning.clean_ascii_only('shipping_postalcode'),
+       }
+
+
     new_params['role_views'] = {}
 
     params = dicts.merge(params, new_params, sub_merge=True)
--- a/app/soc/views/models/role.py	Sat Mar 07 18:16:54 2009 +0000
+++ b/app/soc/views/models/role.py	Sat Mar 07 19:17:02 2009 +0000
@@ -139,6 +139,14 @@
        'longitude': forms.fields.FloatField(widget=forms.HiddenInput,
                                             required=False),
        'clean_link_id': cleaning.clean_existing_user('link_id'),
+       'clean_res_street': cleaning.clean_ascii_only('res_street'),
+       'clean_res_city': cleaning.clean_ascii_only('res_city'),
+       'clean_res_state': cleaning.clean_ascii_only('res_state'),
+       'clean_res_postalcode': cleaning.clean_ascii_only('res_postalcode'),
+       'clean_ship_street': cleaning.clean_ascii_only('ship_street'),
+       'clean_ship_city': cleaning.clean_ascii_only('ship_city'),
+       'clean_ship_state': cleaning.clean_ascii_only('ship_state'),
+       'clean_ship_postalcode': cleaning.clean_ascii_only('ship_postalcode'),
        'clean_home_page': cleaning.clean_url('home_page'),
        'clean_blog': cleaning.clean_url('blog'),
        'clean_photo_url': cleaning.clean_url('photo_url'),
@@ -148,6 +156,8 @@
 
     new_params['extra_dynaexclude'] = ['user', 'status', 'agreed_to_tos_on']
 
+    new_params['disallow_last_resign'] = False
+
     params = dicts.merge(params, new_params, sub_merge=True)
 
     super(View, self).__init__(params=params)
@@ -426,12 +436,29 @@
     resign = get_dict.get('resign')
 
     if resign == 'true':
-      # change the status of this role_entity to invalid
-      fields = {'status': 'invalid'}
-      logic.updateEntityProperties(role_entity, fields)
+
+      if params.get('disallow_last_resign'):
+        # check if the current role is the last for this scope
+        fields = {'scope': role_entity.scope,
+            'status': 'active'}
+        roles = logic.getForFields(fields, limit=2)
+
+        # if there is more then one left we can safely resign
+        resign = len(roles) > 1
+      else:
+        resign = True
 
-      # redirect to the roles listing
-      return http.HttpResponseRedirect(redirect)
+      if resign:
+        # change the status of this role_entity to invalid
+        fields = {'status': 'invalid'}
+        logic.updateEntityProperties(role_entity, fields)
+
+        # redirect to the roles listing
+        return http.HttpResponseRedirect(redirect)
+      else:
+        # show error to the user
+        context['not_allowed_to_resign'] = ugettext("This user can't be "
+            "resigned, please make sure it's not the last %(name)s." % params)
 
     # set the appropriate context
     context['entity'] = role_entity