app/soc/views/models/project_survey.py
changeset 2494 c312f1b1939c
parent 2478 985fd974e095
child 2498 fd51f2159bff
equal deleted inserted replaced
2493:0aabd2d76606 2494:c312f1b1939c
    22   ]
    22   ]
    23 
    23 
    24 
    24 
    25 from soc.logic import dicts
    25 from soc.logic import dicts
    26 from soc.logic.models.survey import project_logic as project_survey_logic
    26 from soc.logic.models.survey import project_logic as project_survey_logic
       
    27 from soc.logic.models.user import logic as user_logic
    27 from soc.views.helper import access
    28 from soc.views.helper import access
    28 from soc.views.helper import decorators
    29 from soc.views.helper import decorators
       
    30 from soc.views.helper import lists
       
    31 from soc.views.helper import redirects
    29 from soc.views.models import survey
    32 from soc.views.models import survey
    30 
    33 
    31 
    34 
    32 class View(survey.View):
    35 class View(survey.View):
    33   """View methods for the ProjectSurvey model.
    36   """View methods for the ProjectSurvey model.
    42     """
    45     """
    43 
    46 
    44     rights = access.Checker(params)
    47     rights = access.Checker(params)
    45     rights['any_access'] = ['allow']
    48     rights['any_access'] = ['allow']
    46     rights['show'] = [('checkIsSurveyReadable', project_survey_logic)]
    49     rights['show'] = [('checkIsSurveyReadable', project_survey_logic)]
    47     rights['create'] = ['checkIsUser']
    50     rights['create'] = ['checkIsDeveloper'] # TODO(ljvderijk) proper access check
    48     rights['edit'] = [('checkIsSurveyWritable', project_survey_logic)]
    51     rights['edit'] = [('checkIsSurveyWritable', project_survey_logic)]
    49     rights['delete'] = [('checkIsSurveyWritable', project_survey_logic)]
    52     rights['delete'] = [('checkIsSurveyWritable', project_survey_logic)]
    50     rights['list'] = ['checkDocumentList']
    53     rights['list'] = ['checkDocumentList']
    51     rights['pick'] = ['checkDocumentPick']
    54     rights['take'] = ['checkIsDeveloper'] # TODO(ljvderijk) add Project check
    52 
    55 
    53     new_params = {}
    56     new_params = {}
    54     new_params['logic'] = project_survey_logic
    57     new_params['logic'] = project_survey_logic
    55     new_params['rights'] = rights
    58     new_params['rights'] = rights
    56 
    59 
    60 
    63 
    61     params = dicts.merge(params, new_params)
    64     params = dicts.merge(params, new_params)
    62 
    65 
    63     super(View, self).__init__(params=params)
    66     super(View, self).__init__(params=params)
    64 
    67 
       
    68   @decorators.merge_params
       
    69   @decorators.check_access
       
    70   def take(self, request, access_type, page_name=None,
       
    71            params=None, **kwargs):
       
    72     """View for taking a Survey.
       
    73 
       
    74     For Args see base.View().public().
       
    75     """
       
    76 
       
    77     from soc.logic.models.student import logic as student_logic
       
    78 
       
    79     survey_logic = params['logic']
       
    80     record_logic = survey_logic.getRecordLogic()
       
    81 
       
    82     try:
       
    83       entity = self._logic.getFromKeyFieldsOr404(kwargs)
       
    84     except out_of_band.Error, error:
       
    85       return responses.errorResponse(
       
    86           error, request, template=params['error_public'])
       
    87 
       
    88     get_dict = request.GET
       
    89 
       
    90     if not 'project' in get_dict:
       
    91       user_entity = user_logic.getForCurrentAccount()
       
    92 
       
    93       fields = {'user': user_entity,
       
    94                 'scope': survey_logic.getScope(entity),
       
    95                 'status': 'active'}
       
    96 
       
    97       student_entity = student_logic.getForFields(fields, unique=True)
       
    98 
       
    99       # TODO(ljvderijk) transform StudentProject to handle multiple surveys
       
   100       fields = {'student': student_entity,
       
   101                 'status': 'accepted'}
       
   102 
       
   103       # show project selection screen
       
   104       return self._selectProjects(request, page_name, params, entity, fields)
       
   105 
       
   106     return super(View, self).take(request, 'any_access', page_name=page_name,
       
   107                                   params=params, **kwargs)
       
   108 
       
   109   def _takeGet(self, request, template, context, params, entity, record,
       
   110               **kwargs):
       
   111     """Hooking into the view for the take's page GET request.
       
   112 
       
   113     For params see survey.View._takeGet().
       
   114     """
       
   115 
       
   116     # the form action should contain the requested project
       
   117     context['form_action'] = "?project=%s" %(request.GET['project'])
       
   118 
       
   119   def _takePost(self, request, params, entity, record, properties):
       
   120     """Hook into the view for the take's page POST request.
       
   121 
       
   122     This is used to ensure the right StudentProject gets stored
       
   123 
       
   124     For params see survey.View._takePost().
       
   125     """
       
   126 
       
   127     from soc.logic.models.student_project import logic as student_project_logic
       
   128 
       
   129     # retrieve the project using the key name in the GET param
       
   130     get_dict = request.GET
       
   131     project_entity = student_project_logic.getFromKeyName(get_dict['project'])
       
   132 
       
   133     # update the properties that will be stored with the referenced project
       
   134     properties.update(project=project_entity)
       
   135 
       
   136   def _selectProjects(self, request, page_name, params, survey, fields):
       
   137     """Shows a view upon which a User can select a Student Project to fill in
       
   138     the ProjectSurvey for.
       
   139 
       
   140     Args:
       
   141       survey: a Survey entity
       
   142       fields: the filter to use on the Project List.
       
   143       rest: see base.View.public()
       
   144     """
       
   145 
       
   146     from soc.views.models.student_project import view as student_project_view
       
   147 
       
   148     student_project_params = student_project_view.getParams().copy()
       
   149 
       
   150     redirect_dict = {'survey': survey,
       
   151                      'params': params}
       
   152 
       
   153     student_project_params['list_action'] = (
       
   154         redirects.getTakeProjectSurveyRedirect, redirect_dict)
       
   155     student_project_params['list_description'] = (
       
   156         "Select a %s for which to fill in the %s named %s" %(
       
   157             student_project_params['name'], params['name'], survey.title))
       
   158 
       
   159     content = lists.getListContent(request, student_project_params, fields)
       
   160     contents = [content]
       
   161 
       
   162     return self._list(request, student_project_params, contents, page_name)
       
   163 
    65 
   164 
    66 view = View()
   165 view = View()
    67 
   166 
    68 admin = decorators.view(view.admin)
       
    69 create = decorators.view(view.create)
   167 create = decorators.view(view.create)
    70 edit = decorators.view(view.edit)
   168 edit = decorators.view(view.edit)
    71 delete = decorators.view(view.delete)
   169 delete = decorators.view(view.delete)
    72 list = decorators.view(view.list)
   170 list = decorators.view(view.list)
    73 public = decorators.view(view.public)
   171 public = decorators.view(view.public)
    74 export = decorators.view(view.export)
   172 take = decorators.view(view.take)
    75 pick = decorators.view(view.pick)
       
    76 results = decorators.view(view.viewResults)
       
    77 json = decorators.view(view.exportSerialized)