app/soc/logic/cleaning.py
changeset 1287 9b18d612510a
parent 1285 d54f2d80f46e
child 1288 4ef7de6c2b7a
equal deleted inserted replaced
1286:940f06d34787 1287:9b18d612510a
    37 
    37 
    38 DEF_LINK_ID_IN_USE_MSG = ugettext(
    38 DEF_LINK_ID_IN_USE_MSG = ugettext(
    39     'This link ID is already in use, please specify another one')
    39     'This link ID is already in use, please specify another one')
    40 
    40 
    41 
    41 
       
    42 def check_field_is_empty(field_name):
       
    43   """Returns decorator that bypasses cleaning for empty fields.
       
    44   """
       
    45 
       
    46   def decorator(fun):
       
    47     """Decorator that checks if a field is empty if so doesn't do the cleaning.
       
    48 
       
    49     Note Django will capture errors concerning required fields that are empty.
       
    50     """
       
    51     from functools import wraps
       
    52 
       
    53     @wraps(fun)
       
    54     def wrapper(self):
       
    55       field_content = self.cleaned_data.get(field_name)
       
    56 
       
    57       if not field_content:
       
    58         # field has no content so bail out
       
    59         return field_content
       
    60       else:
       
    61         # field has contents
       
    62         return fun(self)
       
    63     return wrapper
       
    64 
       
    65   return decorator
       
    66 
       
    67 
    42 def clean_link_id(field_name):
    68 def clean_link_id(field_name):
    43   """Checks if the field_name value is in a valid link ID format.
    69   """Checks if the field_name value is in a valid link ID format.
    44   """
    70   """
       
    71 
       
    72   @check_field_is_empty(field_name)
    45   def wrapper(self):
    73   def wrapper(self):
    46     # convert to lowercase for user comfort
    74     # convert to lowercase for user comfort
    47     link_id = self.cleaned_data.get(field_name).lower()
    75     link_id = self.cleaned_data.get(field_name).lower()
    48     if not validate.isLinkIdFormatValid(link_id):
    76     if not validate.isLinkIdFormatValid(link_id):
    49       raise forms.ValidationError("This link ID is in wrong format.")
    77       raise forms.ValidationError("This link ID is in wrong format.")
    52 
    80 
    53 
    81 
    54 def clean_scope_path(field_name):
    82 def clean_scope_path(field_name):
    55   """Checks if the field_name value is in a valid scope path format.
    83   """Checks if the field_name value is in a valid scope path format.
    56   """
    84   """
       
    85 
       
    86   @check_field_is_empty(field_name)
    57   def wrapper(self):
    87   def wrapper(self):
    58     # convert to lowercase for user comfort
    88     # convert to lowercase for user comfort
    59     scope_path = self.cleaned_data.get(field_name).lower()
    89     scope_path = self.cleaned_data.get(field_name).lower()
    60     if not validate.isScopePathFormatValid(scope_path):
    90     if not validate.isScopePathFormatValid(scope_path):
    61       raise forms.ValidationError("This scope path is in wrong format.")
    91       raise forms.ValidationError("This scope path is in wrong format.")
    66 def clean_agrees_to_tos(field_name):
    96 def clean_agrees_to_tos(field_name):
    67   """Checks if there is a ToS to see if it is allowed to leave 
    97   """Checks if there is a ToS to see if it is allowed to leave 
    68      the field_name field false.
    98      the field_name field false.
    69   """
    99   """
    70 
   100 
       
   101   @check_field_is_empty(field_name)
    71   def wrapper(self):
   102   def wrapper(self):
    72     agrees_to_tos = self.cleaned_data.get(field_name)
   103     agrees_to_tos = self.cleaned_data.get(field_name)
    73 
   104 
    74     if not site_logic.logic.getToS(site_logic.logic.getSingleton()):
   105     if not site_logic.logic.getToS(site_logic.logic.getSingleton()):
    75       return agrees_to_tos
   106       return agrees_to_tos
    88 
   119 
    89 def clean_existing_user(field_name):
   120 def clean_existing_user(field_name):
    90   """Check if the field_name field is a valid user.
   121   """Check if the field_name field is a valid user.
    91   """
   122   """
    92 
   123 
       
   124   @check_field_is_empty(field_name)
    93   def wrapped(self):
   125   def wrapped(self):
    94     link_id = clean_link_id(field_name)(self)
   126     link_id = clean_link_id(field_name)(self)
    95   
   127   
    96     user_entity = user_logic.logic.getForFields({'link_id': link_id}, 
   128     user_entity = user_logic.logic.getForFields({'link_id': link_id}, 
    97         unique=True)
   129         unique=True)
   107 def clean_user_not_exist(field_name):
   139 def clean_user_not_exist(field_name):
   108   """Check if the field_name value is a valid link_id and a user with the
   140   """Check if the field_name value is a valid link_id and a user with the
   109      link id does not exist.
   141      link id does not exist.
   110   """ 
   142   """ 
   111 
   143 
       
   144   @check_field_is_empty(field_name)
   112   def wrapped(self):
   145   def wrapped(self):
   113     link_id = clean_link_id(field_name)(self)
   146     link_id = clean_link_id(field_name)(self)
   114   
   147   
   115     user_entity = user_logic.logic.getForFields({'link_id': link_id}, 
   148     user_entity = user_logic.logic.getForFields({'link_id': link_id}, 
   116         unique=True)
   149         unique=True)
   126 def clean_users_not_same(field_name):
   159 def clean_users_not_same(field_name):
   127   """Check if the field_name field is a valid user and is not 
   160   """Check if the field_name field is a valid user and is not 
   128      equal to the current user.
   161      equal to the current user.
   129   """
   162   """
   130 
   163 
       
   164   @check_field_is_empty(field_name)
   131   def wrapped(self):
   165   def wrapped(self):
   132     
   166     
   133     clean_user_field = clean_existing_user(field_name)
   167     clean_user_field = clean_existing_user(field_name)
   134     user_entity = clean_user_field(self)
   168     user_entity = clean_user_field(self)
   135     
   169     
   144 
   178 
   145 
   179 
   146 def clean_user_account(field_name):
   180 def clean_user_account(field_name):
   147   """Returns the User with the given field_name value.
   181   """Returns the User with the given field_name value.
   148   """
   182   """
       
   183 
       
   184   @check_field_is_empty(field_name)
   149   def wrapped(self):
   185   def wrapped(self):
   150     email_adress = self.cleaned_data.get(field_name).lower()
   186     email_adress = self.cleaned_data.get(field_name).lower()
   151 
   187 
   152     # get the user account for this email
   188     # get the user account for this email
   153     user_account = users.User(email_adress)
   189     user_account = users.User(email_adress)
   159 def clean_user_account_not_in_use(field_name):
   195 def clean_user_account_not_in_use(field_name):
   160   """Check if the field_name value contains an email 
   196   """Check if the field_name value contains an email 
   161      address that hasn't been used for an existing account.
   197      address that hasn't been used for an existing account.
   162   """ 
   198   """ 
   163 
   199 
       
   200   @check_field_is_empty(field_name)
   164   def wrapped(self):
   201   def wrapped(self):
   165     email_adress = self.cleaned_data.get(field_name).lower()
   202     email_adress = self.cleaned_data.get(field_name).lower()
   166 
   203 
   167     # get the user account for this email and check if it's in use
   204     # get the user account for this email and check if it's in use
   168     user_account = users.User(email_adress)
   205     user_account = users.User(email_adress)
   192 
   229 
   193 def clean_url(field_name):
   230 def clean_url(field_name):
   194   """Clean method for cleaning a field belonging to a LinkProperty.
   231   """Clean method for cleaning a field belonging to a LinkProperty.
   195   """
   232   """
   196 
   233 
       
   234   @check_field_is_empty(field_name)
   197   def wrapped(self):
   235   def wrapped(self):
   198 
   236 
   199     value = self.cleaned_data.get(field_name)
   237     value = self.cleaned_data.get(field_name)
   200 
   238 
   201     # LinkProperty does not accept the empty string so we must return None
   239     # LinkProperty does not accept the empty string so we must return None
   212   
   250   
   213   Raises ValidationError if:
   251   Raises ValidationError if:
   214     -Another User has the given email address as account
   252     -Another User has the given email address as account
   215     -Another User has the given email address in it's FormerAccounts list
   253     -Another User has the given email address in it's FormerAccounts list
   216   """
   254   """
       
   255 
   217   def wrapper(self):
   256   def wrapper(self):
   218     cleaned_data = self.cleaned_data
   257     cleaned_data = self.cleaned_data
   219 
   258 
   220     link_id = cleaned_data.get(link_id_field)
   259     link_id = cleaned_data.get(link_id_field)
   221     user_account = cleaned_data.get(account_field)
   260     user_account = cleaned_data.get(account_field)
   249   
   288   
   250     Raises ValidationError if:
   289     Raises ValidationError if:
   251     -A application with this link id and scope path already exists
   290     -A application with this link id and scope path already exists
   252     -A group with this link id and scope path already exists
   291     -A group with this link id and scope path already exists
   253   """
   292   """
       
   293 
   254   def wrapper(self):
   294   def wrapper(self):
   255       cleaned_data = self.cleaned_data
   295       cleaned_data = self.cleaned_data
   256 
   296 
   257       fields = {}
   297       fields = {}
   258 
   298