app/soc/views/models/role.py
changeset 962 606871fda11c
parent 959 b6e8750c9407
child 963 24c0a9da6360
equal deleted inserted replaced
961:36a90d0e9211 962:606871fda11c
    26 from django import http
    26 from django import http
    27 from django.utils.translation import ugettext_lazy
    27 from django.utils.translation import ugettext_lazy
    28 
    28 
    29 from soc.logic import dicts
    29 from soc.logic import dicts
    30 from soc.logic.models import request as request_logic
    30 from soc.logic.models import request as request_logic
       
    31 from soc.logic.models import user as user_logic
       
    32 from soc.logic.helper import notifications as notifications_helper
    31 from soc.logic.helper import request as request_helper
    33 from soc.logic.helper import request as request_helper
    32 from soc.views.helper import decorators
    34 from soc.views.helper import decorators
    33 from soc.views.helper import redirects
    35 from soc.views.helper import redirects
    34 from soc.views.helper import responses
    36 from soc.views.helper import responses
    35 from soc.views.models import base
    37 from soc.views.models import base
    56     Args:
    58     Args:
    57       params: This dictionary should be filled with the parameters
    59       params: This dictionary should be filled with the parameters
    58     """
    60     """
    59 
    61 
    60     new_params = {}
    62     new_params = {}
    61     # TODO(ljvderijk) add request and process_request
    63     # TODO(ljvderijk) parameterize these patterns
    62     patterns = [(r'^%(url_name)s/(?P<access_type>invite)/%(scope)s$',
    64     patterns = [(r'^%(url_name)s/(?P<access_type>invite)/%(scope)s$',
    63         'soc.views.models.%(module_name)s.invite',
    65         'soc.views.models.%(module_name)s.invite',
    64         'Create invite for %(name_plural)s'),
    66         'Create invite for %(name)s'),
    65         (r'^%(url_name)s/(?P<access_type>accept_invite)/%(scope)s/%(lnp)s$',
    67         (r'^%(url_name)s/(?P<access_type>accept_invite)/%(scope)s/%(lnp)s$',
    66         'soc.views.models.%(module_name)s.accept_invite',
    68         'soc.views.models.%(module_name)s.accept_invite',
    67         'Accept invite for %(name_plural)s')]
    69         'Accept invite for %(name)s'),
       
    70         (r'^%(url_name)s/(?P<access_type>request)/%(scope)s$',
       
    71         'soc.views.models.%(module_name)s.request',
       
    72         'Create a Request to become %(name)s'),
       
    73         (r'^%(url_name)s/(?P<access_type>process_request)/%(scope)s/%(lnp)s$',
       
    74         'soc.views.models.%(module_name)s.process_request',
       
    75         'Process request for %(name)s')]
    68 
    76 
    69     new_params['extra_django_patterns'] = patterns
    77     new_params['extra_django_patterns'] = patterns
    70     new_params['scope_redirect'] = redirects.getInviteRedirect
    78     new_params['scope_redirect'] = redirects.getInviteRedirect
    71 
    79 
    72     params = dicts.merge(params, new_params)
    80     params = dicts.merge(params, new_params)
   111     # set the role to the right name
   119     # set the role to the right name
   112     fields = {'role': '%(module_name)s' % (params)}
   120     fields = {'role': '%(module_name)s' % (params)}
   113 
   121 
   114     # get the request view parameters and initialize the create form
   122     # get the request view parameters and initialize the create form
   115     request_params = request_view.view.getParams()
   123     request_params = request_view.view.getParams()
   116     form = request_params['create_form'](initial=fields)
   124     form = request_params['invite_form'](initial=fields)
   117 
   125 
   118     # construct the appropriate response
   126     # construct the appropriate response
   119     return super(View, self)._constructResponse(request, entity=None,
   127     return super(View, self)._constructResponse(request, entity=None,
   120         context=context, form=form, params=params)
   128         context=context, form=form, params=params)
   121 
   129 
   130       kwargs: the Key Fields for the specified entity
   138       kwargs: the Key Fields for the specified entity
   131     """
   139     """
   132 
   140 
   133     # get the request view parameters and populate the form using POST data
   141     # get the request view parameters and populate the form using POST data
   134     request_params = request_view.view.getParams()
   142     request_params = request_view.view.getParams()
   135     form = request_params['create_form'](request.POST)
   143     form = request_params['invite_form'](request.POST)
   136 
   144 
   137     if not form.is_valid():
   145     if not form.is_valid():
   138       # return the invalid form response
   146       # return the invalid form response
   139       return self._constructResponse(request, entity=None, context=context,
   147       return self._constructResponse(request, entity=None, context=context,
   140           form=form, params=params)
   148           form=form, params=params)
   141 
   149 
   142     # collect the cleaned data from the valid form
   150     # collect the cleaned data from the valid form
   143     key_name, form_fields = soc.views.helper.forms.collectCleanedFields(form)
   151     key_name, form_fields = soc.views.helper.forms.collectCleanedFields(form)
   144     
   152     
   145     # get the group entity for which this request is via the scope_path
   153     # get the group entity for which this request is via the scope_path
   146     group_key_fields = kwargs['scope_path'].rsplit('/', 1)
   154     group = self._getGroupEntityFromScopePath(params['group_logic'],
   147     
   155          kwargs['scope_path'])
   148     if len(group_key_fields) == 1:
   156 
   149       # there is only a link_id
   157     # get the request scope path
   150       fields = {'link_id': group_key_fields[0]}
   158     request_scope_path = self._getRequestScopePathFromGroup(group)
   151     else:
       
   152       # there is a scope_path and link_id
       
   153       fields = {'scope_path': group_key_fields[0],
       
   154                 'link_id': group_key_fields[1]}
       
   155     
       
   156     group = params['group_logic'].getForFields(fields, unique=True)
       
   157     
       
   158     if group.scope_path:
       
   159       request_scope_path = '%s/%s' % [group.scope_path, group.link_id]
       
   160     else:
       
   161       request_scope_path = group.link_id
       
   162 
   159 
   163     # create the fields for the new request entity
   160     # create the fields for the new request entity
   164     request_fields = {'link_id': form_fields['link_id'].link_id,
   161     request_fields = {'link_id': form_fields['link_id'].link_id,
   165         'scope': group,
   162         'scope': group,
   166         'scope_path': request_scope_path,
   163         'scope_path': request_scope_path,
   173     key_name = request_logic.logic.getKeyNameForFields(key_fields)
   170     key_name = request_logic.logic.getKeyNameForFields(key_fields)
   174 
   171 
   175     # create the request entity
   172     # create the request entity
   176     entity = request_logic.logic.updateOrCreateFromKeyName(request_fields, 
   173     entity = request_logic.logic.updateOrCreateFromKeyName(request_fields, 
   177         key_name)
   174         key_name)
   178     
   175 
       
   176     # send out an invite notification
       
   177     notifications_helper.sendInviteNotification(entity)
       
   178 
   179     # TODO(ljvderijk) redirect to a more useful place like the group homepage
   179     # TODO(ljvderijk) redirect to a more useful place like the group homepage
   180     return http.HttpResponseRedirect('/')
   180     return http.HttpResponseRedirect('/')
       
   181 
       
   182   def _getGroupEntityFromScopePath(self, group_logic, scope_path):
       
   183     """Returns a group entity by using the given scope_path that's in kwargs.
       
   184     
       
   185     Args:
       
   186       group_logic: logic for the group which should be retrieved
       
   187       kwargs: the Key Fields for the specified entity
       
   188     """
       
   189     group_key_fields = scope_path.rsplit('/',1)
       
   190 
       
   191     if len(group_key_fields) == 1:
       
   192       # there is only a link_id
       
   193       fields = {'link_id' : group_key_fields[0]}
       
   194     else:
       
   195       # there is a scope_path and link_id
       
   196       fields = {'scope_path' : group_key_fields[0],
       
   197                 'link_id' : group_key_fields[1]}
       
   198 
       
   199     group = group_logic.getForFields(fields, unique=True)
       
   200 
       
   201     return group
       
   202 
       
   203   def _getRequestScopePathFromGroup(self, group_entity):
       
   204     """Returns the scope_path that should be put in a request for a given group.
       
   205 
       
   206     Args:
       
   207       group_entity: The group entity for which the request scope_path should
       
   208                     be returned.
       
   209     """
       
   210 
       
   211     if group_entity.scope_path:
       
   212       request_scope_path = '%s/%s' % [
       
   213           group_entity.scope_path, group_entity.link_id]
       
   214     else:
       
   215       request_scope_path = group_entity.link_id
       
   216 
       
   217     return request_scope_path
       
   218 
   181 
   219 
   182   @decorators.merge_params
   220   @decorators.merge_params
   183   @decorators.check_access
   221   @decorators.check_access
   184   def acceptInvite(self, request, access_type,
   222   def acceptInvite(self, request, access_type,
   185                    page_name=None, params=None, **kwargs):
   223                    page_name=None, params=None, **kwargs):
   279       context: dictionary containing the context for this view
   317       context: dictionary containing the context for this view
   280       params: a dict with params for this View
   318       params: a dict with params for this View
   281       kwargs: the Key Fields for the specified entity
   319       kwargs: the Key Fields for the specified entity
   282     """
   320     """
   283     pass
   321     pass
       
   322 
       
   323 
       
   324   @decorators.merge_params
       
   325   @decorators.check_access
       
   326   def request(self, request, access_type,
       
   327                    page_name=None, params=None, **kwargs):
       
   328     """Handles the GET request concerning the view that creates a request
       
   329     for attaining a certain Role.
       
   330 
       
   331     Args:
       
   332       request: the standard Django HTTP request object
       
   333       page_name: the page name displayed in templates as page and header title
       
   334       params: a dict with params for this View
       
   335       kwargs: the Key Fields for the specified entity
       
   336     """
       
   337 
       
   338     # get the context for this webpage
       
   339     context = responses.getUniversalContext(request)
       
   340     context['page_name'] = page_name
       
   341 
       
   342     if request.method == 'POST':
       
   343       return self.requestPost(request, context, params, **kwargs)
       
   344     else:
       
   345       # request.method == 'GET'
       
   346       return self.requestGet(request, context, params, **kwargs)
       
   347 
       
   348   def requestGet(self, request, context, params, **kwargs):
       
   349     """Handles the GET request concerning the creation of a request
       
   350     to attain a role.
       
   351 
       
   352     Args:
       
   353       request: the standard Django HTTP request object
       
   354       context: dictionary containing the context for this view
       
   355       params: a dict with params for this View
       
   356       kwargs: the Key Fields for the specified entity
       
   357     """
       
   358 
       
   359     # set right fields for the request form
       
   360     user_entity = user_logic.logic.getForCurrentAccount()
       
   361     fields = {'link_id' : user_entity.link_id,
       
   362               'role' : '%(module_name)s' %(params),
       
   363               'group_id' : kwargs['scope_path']}
       
   364 
       
   365     # get the request view parameters and initialize the create form
       
   366     request_params = request_view.view.getParams()
       
   367     form = request_params['request_form'](initial=fields)
       
   368 
       
   369     # construct the appropriate response
       
   370     return super(View, self)._constructResponse(request, entity=None,
       
   371         context=context, form=form, params=params)
       
   372 
       
   373   def requestPost(self, request, context, params, **kwargs):
       
   374     """Handles the POST request concerning the creation of a request
       
   375     to attain a role.
       
   376 
       
   377     Args:
       
   378       request: the standard Django HTTP request object
       
   379       context: dictionary containing the context for this view
       
   380       params: a dict with params for this View
       
   381       kwargs: the Key Fields for the specified entity
       
   382     """
       
   383 
       
   384     # get the request view parameters and populate the form using POST data
       
   385     request_params = request_view.view.getParams()
       
   386     form = request_params['invite_form'](request.POST)
       
   387 
       
   388     if not form.is_valid():
       
   389       # return the invalid form response
       
   390       return self._constructResponse(request, entity=None, context=context,
       
   391           form=form, params=params)
       
   392 
       
   393     # get the group entity for which this request is via the scope_path
       
   394     group = self._getGroupEntityFromScopePath(params['group_logic'],
       
   395          kwargs['scope_path'])
       
   396 
       
   397     # get the request scope path
       
   398     request_scope_path = self._getRequestScopePathFromGroup(group)
       
   399 
       
   400     # defensively set the fields we need for this request and set state to new
       
   401     user_entity = user_logic.logic.getForCurrentAccount()
       
   402     request_fields = {'link_id' : user_entity.link_id,
       
   403         'scope' : group,
       
   404         'scope_path' : request_scope_path,
       
   405         'role' : params['module_name'],
       
   406         'role_verbose' : params['name'],
       
   407         'state' : 'new'}
       
   408 
       
   409     # extract the key_name for the new request entity
       
   410     key_fields = request_logic.logic.getKeyFieldsFromDict(request_fields)
       
   411     key_name = request_logic.logic.getKeyNameForFields(key_fields)
       
   412 
       
   413     # create the request entity
       
   414     entity = request_logic.logic.updateOrCreateFromKeyName(request_fields, key_name)
       
   415 
       
   416     # TODO(ljvderijk) send out a message to alert the users able to process this request
       
   417 
       
   418     # redirect to roles overview
       
   419     return http.HttpResponseRedirect('/user/roles')
       
   420 
       
   421 
       
   422   @decorators.merge_params
       
   423   @decorators.check_access
       
   424   def processRequest(self, request, access_type,
       
   425                    page_name=None, params=None, **kwargs):
       
   426     """Creates the page upon which a request can be processed.
       
   427 
       
   428     Args:
       
   429       request: the standard Django HTTP request object
       
   430       access_type : the name of the access type which should be checked
       
   431       page_name: the page name displayed in templates as page and header title
       
   432       params: a dict with params for this View
       
   433       kwargs: the Key Fields for the specified entity
       
   434     """
       
   435 
       
   436     # get the context for this webpage
       
   437     context = responses.getUniversalContext(request)
       
   438     context['page_name'] = page_name
       
   439 
       
   440     # get the request entity using the information from kwargs
       
   441     fields = {'link_id': kwargs['link_id'],
       
   442         'scope_path': kwargs['scope_path'],
       
   443         'role': params['module_name']}
       
   444     request_entity = request_logic.logic.getForFields(fields, unique=True)
       
   445     
       
   446     get_dict = request.GET
       
   447     
       
   448     if 'status' in get_dict.keys():
       
   449       if get_dict['status'] in ['group_accepted', 'rejected', 'ignored']:
       
   450         # update the request_entity and redirect away from this page
       
   451         request_state = get_dict['status']
       
   452         request_logic.logic.updateModelProperties(request_entity, {
       
   453             'state': get_dict['status']})
       
   454 
       
   455         if request_state == 'group_accepted':
       
   456           notifications_helper.sendInviteNotification(request_entity)
       
   457 
       
   458         # TODO(ljvderijk) redirect to group requests overview
       
   459         return http.HttpResponseRedirect('/')
       
   460 
       
   461     # put the entity in the context
       
   462     context['entity'] = request_entity
       
   463     context['module_name'] = params['module_name']
       
   464 
       
   465     #display the request processing page using the appropriate template
       
   466     template = request_view.view.getParams()['request_processing_template']
       
   467 
       
   468     return responses.respond(request, template, context=context)