app/soc/views/models/user_self.py
changeset 860 cfb57fe35d3c
parent 858 e79e7a22326f
child 876 0c1329d4b514
equal deleted inserted replaced
859:b93fad44b70a 860:cfb57fe35d3c
    32 from django.utils.translation import ugettext_lazy
    32 from django.utils.translation import ugettext_lazy
    33 
    33 
    34 from soc.logic import dicts
    34 from soc.logic import dicts
    35 from soc.logic import validate
    35 from soc.logic import validate
    36 from soc.logic import models as model_logic
    36 from soc.logic import models as model_logic
    37 from soc.logic.models import user as user_logic
    37 from soc.logic.models.site import logic as site_logic
       
    38 from soc.logic.models.user import logic as user_logic
    38 from soc.views import helper
    39 from soc.views import helper
    39 from soc.views import out_of_band
    40 from soc.views import out_of_band
    40 from soc.views.helper import access
    41 from soc.views.helper import access
    41 from soc.views.models import base
    42 from soc.views.models import base
    42 
    43 
    60   def clean_link_id(self):
    61   def clean_link_id(self):
    61     link_id = self.cleaned_data.get('link_id').lower()
    62     link_id = self.cleaned_data.get('link_id').lower()
    62     if not validate.isLinkIdFormatValid(link_id):
    63     if not validate.isLinkIdFormatValid(link_id):
    63       raise forms.ValidationError("This link ID is in wrong format.")
    64       raise forms.ValidationError("This link ID is in wrong format.")
    64 
    65 
    65     user = user_logic.logic.getForFields({'link_id': link_id},
    66     user = user_logic.getForFields({'link_id': link_id}, unique=True)
    66                                           unique=True)
       
    67 
    67 
    68     # Get the currently logged in user account
    68     # Get the currently logged in user account
    69     current_account = users.get_current_user()
    69     current_account = users.get_current_user()
    70 
    70 
    71     if user:
    71     if user:
    72       if current_account != user.account:
    72       if current_account != user.account:
    73         raise forms.ValidationError("This link ID is already in use.")
    73         raise forms.ValidationError("This link ID is already in use.")
    74 
    74 
    75     return link_id
    75     return link_id
       
    76 
       
    77   def clean_agrees_to_tos(self):
       
    78     agrees_to_tos = self.cleaned_data.get('agrees_to_tos')
       
    79 
       
    80     if not site_logic.getToS(site_logic.getSingleton()):
       
    81       return agrees_to_tos
       
    82 
       
    83     # Site settings specify a site-wide ToS, so agreement is *required*
       
    84     if agrees_to_tos:
       
    85       return True
       
    86 
       
    87     raise forms.ValidationError(
       
    88         'The site-wide Terms of Service must be accepted to participate'
       
    89         ' on this site.')
    76 
    90 
    77 
    91 
    78 class View(base.View):
    92 class View(base.View):
    79   """View methods for the User model.
    93   """View methods for the User model.
    80   """
    94   """
   105     rights['signIn'] = [access.checkNotLoggedIn]
   119     rights['signIn'] = [access.checkNotLoggedIn]
   106     rights['notification'] = [access.checkIsUser]
   120     rights['notification'] = [access.checkIsUser]
   107 
   121 
   108     new_params = {}
   122     new_params = {}
   109     new_params['rights'] = rights
   123     new_params['rights'] = rights
   110     new_params['logic'] = user_logic.logic
   124     new_params['logic'] = user_logic
   111 
   125 
   112     new_params['name'] = "User"
   126     new_params['name'] = "User"
   113     new_params['module_name'] = "user_self"
   127     new_params['module_name'] = "user_self"
   114     new_params['url_name'] = "user"
   128     new_params['url_name'] = "user"
   115 
   129 
   162           template=self.EDIT_SELF_TMPL)
   176           template=self.EDIT_SELF_TMPL)
   163 
   177 
   164     account = users.get_current_user()
   178     account = users.get_current_user()
   165     properties = {'account': account}
   179     properties = {'account': account}
   166 
   180 
   167     user = user_logic.logic.getForFields(properties, unique=True)
   181     user = user_logic.getForFields(properties, unique=True)
   168 
   182 
   169     # create default template context for use with any templates
   183     # create default template context for use with any templates
   170     context = helper.responses.getUniversalContext(request)
   184     context = helper.responses.getUniversalContext(request)
   171 
   185 
   172     if request.method == 'POST':
   186     if request.method == 'POST':
   181           'agrees_to_tos': form.cleaned_data.get('agrees_to_tos'),
   195           'agrees_to_tos': form.cleaned_data.get('agrees_to_tos'),
   182         }
   196         }
   183 
   197 
   184         # check if user account is not in former_accounts
   198         # check if user account is not in former_accounts
   185         # if it is show error message that account is invalid
   199         # if it is show error message that account is invalid
   186         if user_logic.logic.isFormerAccount(account):
   200         if user_logic.isFormerAccount(account):
   187           msg = self.DEF_USER_ACCOUNT_INVALID_MSG_FMT % {
   201           msg = self.DEF_USER_ACCOUNT_INVALID_MSG_FMT % {
   188             'email': account.email()}
   202             'email': account.email()}
   189           error = out_of_band.Error(msg)
   203           error = out_of_band.Error(msg)
   190           return helper.responses.errorResponse(
   204           return helper.responses.errorResponse(
   191               error, request, template=self.EDIT_SELF_TMPL, context=context)
   205               error, request, template=self.EDIT_SELF_TMPL, context=context)
   192 
   206 
   193         user = user_logic.logic.updateOrCreateFromFields(
   207         user = user_logic.updateOrCreateFromFields(
   194             properties, {'link_id': new_link_id})
   208             properties, {'link_id': new_link_id})
   195 
   209 
   196         # redirect to /user/profile?s=0
   210         # redirect to /user/profile?s=0
   197         # (causes 'Profile saved' message to be displayed)
   211         # (causes 'Profile saved' message to be displayed)
   198         return helper.responses.redirectToChangedSuffix(
   212         return helper.responses.redirectToChangedSuffix(
   251     """See base.View.getSidebarMenus().
   265     """See base.View.getSidebarMenus().
   252     """
   266     """
   253 
   267 
   254     link_title = ugettext_lazy('Notifications')
   268     link_title = ugettext_lazy('Notifications')
   255 
   269 
   256     user = user_logic.logic.getForCurrentAccount()
   270     user = user_logic.getForCurrentAccount()
   257 
   271 
   258     filter = {
   272     filter = {
   259         'scope': user,
   273         'scope': user,
   260         'unread': True,
   274         'unread': True,
   261         }
   275         }