app/soc/views/models/role.py
changeset 1084 9c4221f7b747
parent 1080 d533408811ba
child 1085 0afbdd0905ef
equal deleted inserted replaced
1083:b8018d7a9f23 1084:9c4221f7b747
    46 class View(base.View):
    46 class View(base.View):
    47   """Views for all entities that inherit from Role.
    47   """Views for all entities that inherit from Role.
    48 
    48 
    49   All views that only Role entities have are defined in this subclass.
    49   All views that only Role entities have are defined in this subclass.
    50   """
    50   """
    51   
    51 
    52   DEF_INVITE_INSTRUCTION_MSG_FMT = ugettext(
    52   DEF_INVITE_INSTRUCTION_MSG_FMT = ugettext(
    53       'Please use this form to invite someone to become a %(name)s.')
    53       'Please use this form to invite someone to become a %(name)s.')
       
    54 
       
    55   DEF_REQUEST_INSTRUCTION_MSG_FMT = ugettext(
       
    56       'Please use this form to request to become a %(name)s')
       
    57 
       
    58   DEF_INVITE_ERROR_MSG_FMT = ugettext(
       
    59       'This user can not receive an invite to become a %(name)s. <br/>'
       
    60       'Please make sure there is no outstanding invite or request and '
       
    61       'be sure that this user is not a %(name)s.')
       
    62 
       
    63   DEF_REQUEST_ERROR_MSG_FMT = ugettext(
       
    64       'You can not request to become a %(name)s. <br/>'
       
    65       'Please make sure there is no outstanding invite or request and '
       
    66       'be sure that you are not a %(name)s already.')
       
    67 
    54 
    68 
    55   def __init__(self, params=None):
    69   def __init__(self, params=None):
    56     """
    70     """
    57 
    71 
    58     Args:
    72     Args:
   124     """
   138     """
   125 
   139 
   126     # get the context for this webpage
   140     # get the context for this webpage
   127     context = responses.getUniversalContext(request)
   141     context = responses.getUniversalContext(request)
   128     context['page_name'] = page_name
   142     context['page_name'] = page_name
       
   143     context['instruction_message'] = (self.DEF_INVITE_INSTRUCTION_MSG_FMT %
       
   144         params)
   129 
   145 
   130     if request.method == 'POST':
   146     if request.method == 'POST':
   131       return self.invitePost(request, context, params, **kwargs)
   147       return self.invitePost(request, context, params, **kwargs)
   132     else:
   148     else:
   133       # request.method == 'GET'
   149       # request.method == 'GET'
   151     request_params = request_view.view.getParams()
   167     request_params = request_view.view.getParams()
   152     form = request_params['invite_form'](initial=fields)
   168     form = request_params['invite_form'](initial=fields)
   153 
   169 
   154     # construct the appropriate response
   170     # construct the appropriate response
   155     return super(View, self)._constructResponse(request, entity=None,
   171     return super(View, self)._constructResponse(request, entity=None,
   156         context=context, form=form, params=params)
   172         context=context, form=form, params=request_params)
   157 
   173 
   158   def invitePost(self, request, context, params, **kwargs):
   174   def invitePost(self, request, context, params, **kwargs):
   159     """Handles the POST request concerning the view that creates an invite
   175     """Handles the POST request concerning the view that creates an invite
   160     for attaining a certain Role.
   176     for attaining a certain Role.
   161 
   177 
   175       return self._constructResponse(request, entity=None, context=context,
   191       return self._constructResponse(request, entity=None, context=context,
   176           form=form, params=params)
   192           form=form, params=params)
   177 
   193 
   178     # collect the cleaned data from the valid form
   194     # collect the cleaned data from the valid form
   179     key_name, form_fields = soc.views.helper.forms.collectCleanedFields(form)
   195     key_name, form_fields = soc.views.helper.forms.collectCleanedFields(form)
   180     
   196 
   181     # get the group entity for which this request is via the scope_path
   197     # get the group entity for which this request is via the scope_path
   182     group = self._logic.getGroupEntityFromScopePath(params['group_logic'],
   198     group = self._logic.getGroupEntityFromScopePath(params['group_logic'],
   183          kwargs['scope_path'])
   199          kwargs['scope_path'])
   184 
   200 
   185     # get the request scope path
   201     # get the request scope path
   191         'scope_path': request_scope_path,
   207         'scope_path': request_scope_path,
   192         'role': params['module_name'],
   208         'role': params['module_name'],
   193         'role_verbose': params['name'],
   209         'role_verbose': params['name'],
   194         'state': 'group_accepted'}
   210         'state': 'group_accepted'}
   195 
   211 
       
   212     if not self._isValidNewRequest(request_fields, params):
       
   213       # not a valid invite
       
   214       context['error_message'] = self.DEF_INVITE_ERROR_MSG_FMT % (
       
   215           params)
       
   216       return self.inviteGet(request, context, params, **kwargs)
       
   217 
   196     # extract the key_name for the new request entity
   218     # extract the key_name for the new request entity
   197     key_fields = request_logic.logic.getKeyFieldsFromDict(request_fields)
   219     key_fields = request_logic.logic.getKeyFieldsFromDict(request_fields)
   198     key_name = request_logic.logic.getKeyNameForFields(key_fields)
   220     key_name = request_logic.logic.getKeyNameForFields(key_fields)
   199 
   221 
   200     # create the request entity
   222     # create the request entity
   300     self._acceptInvitePost(fields, request, context, params, **kwargs)
   322     self._acceptInvitePost(fields, request, context, params, **kwargs)
   301 
   323 
   302     group_logic = params['group_logic']
   324     group_logic = params['group_logic']
   303     group_entity = group_logic.getFromKeyName(fields['scope_path'])
   325     group_entity = group_logic.getFromKeyName(fields['scope_path'])
   304     fields['scope'] = group_entity
   326     fields['scope'] = group_entity
   305     
   327 
   306     # make sure that this role becomes active once more in case this user
   328     # make sure that this role becomes active once more in case this user
   307     # has been reinvited
   329     # has been reinvited
   308     fields ['state'] = 'active'
   330     fields ['state'] = 'active'
   309 
   331 
   310     # get the key_name for the new entity
   332     # get the key_name for the new entity
   401     """
   423     """
   402 
   424 
   403     # get the context for this webpage
   425     # get the context for this webpage
   404     context = responses.getUniversalContext(request)
   426     context = responses.getUniversalContext(request)
   405     context['page_name'] = page_name
   427     context['page_name'] = page_name
       
   428     context['instruction_message'] = (self.DEF_REQUEST_INSTRUCTION_MSG_FMT %
       
   429         params)
   406 
   430 
   407     if request.method == 'POST':
   431     if request.method == 'POST':
   408       return self.requestPost(request, context, params, **kwargs)
   432       return self.requestPost(request, context, params, **kwargs)
   409     else:
   433     else:
   410       # request.method == 'GET'
   434       # request.method == 'GET'
   431     request_params = request_view.view.getParams()
   455     request_params = request_view.view.getParams()
   432     form = request_params['request_form'](initial=fields)
   456     form = request_params['request_form'](initial=fields)
   433 
   457 
   434     # construct the appropriate response
   458     # construct the appropriate response
   435     return super(View, self)._constructResponse(request, entity=None,
   459     return super(View, self)._constructResponse(request, entity=None,
   436         context=context, form=form, params=params)
   460         context=context, form=form, params=request_params)
   437 
   461 
   438   def requestPost(self, request, context, params, **kwargs):
   462   def requestPost(self, request, context, params, **kwargs):
   439     """Handles the POST request concerning the creation of a request
   463     """Handles the POST request concerning the creation of a request
   440     to attain a role.
   464     to attain a role.
   441 
   465 
   468         'scope' : group,
   492         'scope' : group,
   469         'scope_path' : request_scope_path,
   493         'scope_path' : request_scope_path,
   470         'role' : params['module_name'],
   494         'role' : params['module_name'],
   471         'role_verbose' : params['name'],
   495         'role_verbose' : params['name'],
   472         'state' : 'new'}
   496         'state' : 'new'}
       
   497 
       
   498     if self._isValidNewRequest(request_fields, params):
       
   499       # not a valid request
       
   500       context['error_message'] = self.DEF_REQUEST_ERROR_MSG_FMT % (
       
   501           params)
       
   502       return self.requestGet(request, context, params, **kwargs)
   473 
   503 
   474     # extract the key_name for the new request entity
   504     # extract the key_name for the new request entity
   475     key_fields = request_logic.logic.getKeyFieldsFromDict(request_fields)
   505     key_fields = request_logic.logic.getKeyFieldsFromDict(request_fields)
   476     key_name = request_logic.logic.getKeyNameForFields(key_fields)
   506     key_name = request_logic.logic.getKeyNameForFields(key_fields)
   477 
   507 
   535 
   565 
   536     #display the request processing page using the appropriate template
   566     #display the request processing page using the appropriate template
   537     template = request_view.view.getParams()['request_processing_template']
   567     template = request_view.view.getParams()['request_processing_template']
   538 
   568 
   539     return responses.respond(request, template, context=context)
   569     return responses.respond(request, template, context=context)
       
   570 
       
   571   def _isValidNewRequest(self, request_fields, params):
       
   572     """Checks if this is a valid Request object to make.
       
   573 
       
   574     Args:
       
   575     request_fields: dict containing the fields for the new request entity.
       
   576     params: parameters for the current view
       
   577     """
       
   578     fields = request_fields.copy()
       
   579     fields['state'] = ['new', 'group_accepted', 'ignored']
       
   580 
       
   581     request_entity = request_logic.logic.getForFields(fields, unique=True)
       
   582     
       
   583     if request_entity:
       
   584       # already outstanding request
       
   585       return False
       
   586 
       
   587     # check if the role already exists
       
   588     fields = {'scope' : request_fields['scope'],
       
   589         'link_id': request_fields['link_id'],
       
   590         'state': ['active','inactive'],
       
   591         }
       
   592 
       
   593     role_entity = params['logic'].getForFields(fields, unique=True)
       
   594 
       
   595     if role_entity:
       
   596       # already has this role
       
   597       return False
       
   598 
       
   599     # no oustanding request or a valid role
       
   600     return True