app/soc/logic/cleaning.py
changeset 1333 c0ff6fc3192e
parent 1320 a5913d46e97e
child 1354 aba2beea6dfa
equal deleted inserted replaced
1332:6655b1e89adb 1333:c0ff6fc3192e
   127   """
   127   """
   128 
   128 
   129   @check_field_is_empty(field_name)
   129   @check_field_is_empty(field_name)
   130   def wrapped(self):
   130   def wrapped(self):
   131     link_id = clean_link_id(field_name)(self)
   131     link_id = clean_link_id(field_name)(self)
   132   
   132 
   133     user_entity = user_logic.logic.getForFields({'link_id': link_id}, 
   133     user_entity = user_logic.logic.getForFields({'link_id': link_id}, 
   134         unique=True)
   134         unique=True)
   135   
   135 
   136     if not user_entity:
   136     if not user_entity:
   137       # user does not exist
   137       # user does not exist
   138       raise forms.ValidationError("This user does not exist.")
   138       raise forms.ValidationError("This user does not exist.")
   139   
   139 
       
   140     return user_entity
       
   141   return wrapped
       
   142 
       
   143 
       
   144 def clean_user_is_current(field_name):
       
   145   """Check if the field_name value is a valid link_id and resembles the 
       
   146      current user.
       
   147   """
       
   148 
       
   149   @check_field_is_empty(field_name)
       
   150   def wrapped(self):
       
   151     link_id = clean_link_id(field_name)(self)
       
   152 
       
   153     user_entity = user_logic.logic.getForCurrentAccount()
       
   154 
       
   155     if not user_entity or user_entity.link_id != link_id:
       
   156       # this user is not the current user
       
   157       raise forms.ValidationError("This user is not you.")
       
   158 
   140     return user_entity
   159     return user_entity
   141   return wrapped
   160   return wrapped
   142 
   161 
   143 
   162 
   144 def clean_user_not_exist(field_name):
   163 def clean_user_not_exist(field_name):
   147   """ 
   166   """ 
   148 
   167 
   149   @check_field_is_empty(field_name)
   168   @check_field_is_empty(field_name)
   150   def wrapped(self):
   169   def wrapped(self):
   151     link_id = clean_link_id(field_name)(self)
   170     link_id = clean_link_id(field_name)(self)
   152   
   171 
   153     user_entity = user_logic.logic.getForFields({'link_id': link_id}, 
   172     user_entity = user_logic.logic.getForFields({'link_id': link_id}, 
   154         unique=True)
   173         unique=True)
   155   
   174 
   156     if user_entity:
   175     if user_entity:
   157       # user exists already
   176       # user exists already
   158       raise forms.ValidationError("There is already a user with this link id.")
   177       raise forms.ValidationError("There is already a user with this link id.")
   159   
   178 
   160     return link_id
   179     return link_id
   161   return wrapped
   180   return wrapped
   162 
   181 
   163 
   182 
   164 def clean_users_not_same(field_name):
   183 def clean_users_not_same(field_name):
   166      equal to the current user.
   185      equal to the current user.
   167   """
   186   """
   168 
   187 
   169   @check_field_is_empty(field_name)
   188   @check_field_is_empty(field_name)
   170   def wrapped(self):
   189   def wrapped(self):
   171     
   190 
   172     clean_user_field = clean_existing_user(field_name)
   191     clean_user_field = clean_existing_user(field_name)
   173     user_entity = clean_user_field(self)
   192     user_entity = clean_user_field(self)
   174     
   193 
   175     current_user_entity = user_logic.logic.getForCurrentAccount()
   194     current_user_entity = user_logic.logic.getForCurrentAccount()
   176     
   195 
   177     if user_entity.key() == current_user_entity.key():
   196     if user_entity.key() == current_user_entity.key():
   178       # users are equal
   197       # users are equal
   179       raise forms.ValidationError("You cannot enter yourself here.")
   198       raise forms.ValidationError("You cannot enter yourself here.")
   180     
   199 
   181     return user_entity
   200     return user_entity
   182   return wrapped
   201   return wrapped
   183 
   202 
   184 
   203 
   185 def clean_user_account(field_name):
   204 def clean_user_account(field_name):