app/soc/logic/cleaning.py
changeset 1680 fbf8101f6eff
parent 1621 106a5a7834d0
child 1681 b0e10552f5c2
equal deleted inserted replaced
1679:2b28763da7a4 1680:fbf8101f6eff
    32 from django.forms.util import ErrorList
    32 from django.forms.util import ErrorList
    33 from django.utils.translation import ugettext
    33 from django.utils.translation import ugettext
    34 
    34 
    35 from soc.logic import rights as rights_logic
    35 from soc.logic import rights as rights_logic
    36 from soc.logic import validate
    36 from soc.logic import validate
    37 from soc.logic.models import site as site_logic
    37 from soc.logic.models.site import logic as site_logic
    38 from soc.logic.models import user as user_logic
    38 from soc.logic.models.user import logic as user_logic
    39 from soc.models import document as document_model
    39 from soc.models import document as document_model
    40 
    40 
    41 
    41 
    42 DEF_LINK_ID_IN_USE_MSG = ugettext(
    42 DEF_LINK_ID_IN_USE_MSG = ugettext(
    43     'This link ID is already in use, please specify another one')
    43     'This link ID is already in use, please specify another one')
   130   def wrapper(self):
   130   def wrapper(self):
   131     """Decorator wrapper method.
   131     """Decorator wrapper method.
   132     """
   132     """
   133     agrees_to_tos = self.cleaned_data.get(field_name)
   133     agrees_to_tos = self.cleaned_data.get(field_name)
   134 
   134 
   135     if not site_logic.logic.getToS(site_logic.logic.getSingleton()):
   135     if not site_logic.getToS(site_logic.logic.getSingleton()):
   136       return agrees_to_tos
   136       return agrees_to_tos
   137 
   137 
   138     # Site settings specify a site-wide ToS, so agreement is *required*
   138     # Site settings specify a site-wide ToS, so agreement is *required*
   139     if agrees_to_tos:
   139     if agrees_to_tos:
   140       return True
   140       return True
   155   def wrapped(self):
   155   def wrapped(self):
   156     """Decorator wrapper method.
   156     """Decorator wrapper method.
   157     """
   157     """
   158     link_id = clean_link_id(field_name)(self)
   158     link_id = clean_link_id(field_name)(self)
   159 
   159 
   160     user_entity = user_logic.logic.getForFields({'link_id': link_id}, 
   160     user_entity = user_logic.getForFields({'link_id': link_id},
   161         unique=True)
   161         unique=True)
   162 
   162 
   163     if not user_entity:
   163     if not user_entity:
   164       # user does not exist
   164       # user does not exist
   165       raise forms.ValidationError("This user does not exist.")
   165       raise forms.ValidationError("This user does not exist.")
   177   def wrapped(self):
   177   def wrapped(self):
   178     """Decorator wrapper method.
   178     """Decorator wrapper method.
   179     """
   179     """
   180     link_id = clean_link_id(field_name)(self)
   180     link_id = clean_link_id(field_name)(self)
   181 
   181 
   182     user_entity = user_logic.logic.getForCurrentAccount()
   182     user_entity = user_logic.getForCurrentAccount()
   183 
   183 
   184     if not user_entity or user_entity.link_id != link_id:
   184     if not user_entity or user_entity.link_id != link_id:
   185       # this user is not the current user
   185       # this user is not the current user
   186       raise forms.ValidationError("This user is not you.")
   186       raise forms.ValidationError("This user is not you.")
   187 
   187 
   198   def wrapped(self):
   198   def wrapped(self):
   199     """Decorator wrapper method.
   199     """Decorator wrapper method.
   200     """
   200     """
   201     link_id = clean_link_id(field_name)(self)
   201     link_id = clean_link_id(field_name)(self)
   202 
   202 
   203     user_entity = user_logic.logic.getForFields({'link_id': link_id}, 
   203     user_entity = user_logic.getForFields({'link_id': link_id},
   204         unique=True)
   204         unique=True)
   205 
   205 
   206     if user_entity:
   206     if user_entity:
   207       # user exists already
   207       # user exists already
   208       raise forms.ValidationError("There is already a user with this link id.")
   208       raise forms.ValidationError("There is already a user with this link id.")
   221     """Decorator wrapper method.
   221     """Decorator wrapper method.
   222     """
   222     """
   223     clean_user_field = clean_existing_user(field_name)
   223     clean_user_field = clean_existing_user(field_name)
   224     user_entity = clean_user_field(self)
   224     user_entity = clean_user_field(self)
   225 
   225 
   226     current_user_entity = user_logic.logic.getForCurrentAccount()
   226     current_user_entity = user_logic.getForCurrentAccount()
   227 
   227 
   228     if user_entity.key() == current_user_entity.key():
   228     if user_entity.key() == current_user_entity.key():
   229       # users are equal
   229       # users are equal
   230       raise forms.ValidationError("You cannot enter yourself here.")
   230       raise forms.ValidationError("You cannot enter yourself here.")
   231 
   231 
   260 
   260 
   261     # get the user account for this email and check if it's in use
   261     # get the user account for this email and check if it's in use
   262     user_account = users.User(email_adress)
   262     user_account = users.User(email_adress)
   263 
   263 
   264     fields = {'account': user_account}
   264     fields = {'account': user_account}
   265     user_entity = user_logic.logic.getForFields(fields, unique=True)
   265     user_entity = user_logic.getForFields(fields, unique=True)
   266 
   266 
   267     if user_entity or user_logic.logic.isFormerAccount(user_account):
   267     if user_entity or user_logic.isFormerAccount(user_account):
   268       raise forms.ValidationError("There is already a user "
   268       raise forms.ValidationError("There is already a user "
   269           "with this email adress.")
   269           "with this email adress.")
   270 
   270 
   271     return user_account
   271     return user_account
   272   return wrapped
   272   return wrapped
   333 
   333 
   334     # if both fields were valid do this check
   334     # if both fields were valid do this check
   335     if link_id and user_account:
   335     if link_id and user_account:
   336       # get the user from the link_id in the form
   336       # get the user from the link_id in the form
   337       fields = {'link_id': link_id}
   337       fields = {'link_id': link_id}
   338       user_entity = user_logic.logic.getForFields(fields, unique=True)
   338       user_entity = user_logic.getForFields(fields, unique=True)
   339 
   339 
   340       # if it's not the user's current account
   340       # if it's not the user's current account
   341       if user_entity.account != user_account:
   341       if user_entity.account != user_account:
   342 
   342 
   343         # get the user having the given account
   343         # get the user having the given account
   344         fields = {'account': user_account}
   344         fields = {'account': user_account}
   345         user_from_account_entity = user_logic.logic.getForFields(fields, 
   345         user_from_account_entity = user_logic.getForFields(fields,
   346             unique=True)
   346             unique=True)
   347 
   347 
   348         # if there is a user with the given account or it's a former account
   348         # if there is a user with the given account or it's a former account
   349         if user_from_account_entity or \
   349         if user_from_account_entity or \
   350             user_logic.logic.isFormerAccount(user_account):
   350             user_logic.isFormerAccount(user_account):
   351           # raise an error because this email address can't be used
   351           # raise an error because this email address can't be used
   352           raise forms.ValidationError("There is already a user with "
   352           raise forms.ValidationError("There is already a user with "
   353               "this email address.")
   353               "this email address.")
   354 
   354 
   355     return cleaned_data
   355     return cleaned_data
   383 
   383 
   384       # get the application
   384       # get the application
   385       group_app_entity = group_app_logic.logic.getForFields(fields, unique=True)
   385       group_app_entity = group_app_logic.logic.getForFields(fields, unique=True)
   386 
   386 
   387       # get the current user
   387       # get the current user
   388       user_entity = user_logic.logic.getForCurrentAccount()
   388       user_entity = user_logic.getForCurrentAccount()
   389 
   389 
   390       # make sure it's not the applicant creating the new group
   390       # make sure it's not the applicant creating the new group
   391       if group_app_entity and (
   391       if group_app_entity and (
   392           group_app_entity.applicant.key() != user_entity.key()):
   392           group_app_entity.applicant.key() != user_entity.key()):
   393         # add the error message to the link id field
   393         # add the error message to the link id field
   489   scope_path = self.cleaned_data['scope_path']
   489   scope_path = self.cleaned_data['scope_path']
   490 
   490 
   491   params = view.getParams()
   491   params = view.getParams()
   492   rights = params['rights']
   492   rights = params['rights']
   493 
   493 
   494   user = user_logic.logic.getForCurrentAccount()
   494   user = user_logic.getForCurrentAccount()
   495 
   495 
   496   rights.setCurrentUser(user.account, user)
   496   rights.setCurrentUser(user.account, user)
   497   checker = rights_logic.Checker(prefix)
   497   checker = rights_logic.Checker(prefix)
   498 
   498 
   499   roles = checker.getMembership(access_level)
   499   roles = checker.getMembership(access_level)