def createPost(self, request, context, params): """See editPost. Handles the creation of new entities. """ form = params['create_form'](request.POST) if not form.is_valid(): return self._constructResponse(request, None, context, form, params) _, fields = forms.collectCleanedFields(form) self._editPost(request, None, fields) logic = params['logic'] entity = logic.updateOrCreateFromFields(fields) self._postCreate(request, entity) page_params = params['edit_params'] params['suffix'] = entity.key().id_or_name() request.path = params['edit_redirect'] % params return helper.responses.redirectToChangedSuffix(request, None, params=page_params)
def createPost(self, request, context, params): """See editPost. Handles the creation of new entities. """ form = params['create_form'](request.POST) if not form.is_valid(): return self._constructResponse(request, None, context, form, params) _, fields = forms.collectCleanedFields(form) self._editPost(request, None, fields) logic = params['logic'] entity = logic.updateOrCreateFromFields(fields) self._postCreate(request, entity) page_params = params['edit_params'] params['suffix'] = entity.key().id_or_name() request.path = params['edit_redirect'] % params return helper.responses.redirectToChangedSuffix( request, None, params=page_params)
def _editRecordPost(self, request, params, context, template, record_entity): """Handles the POST request for editing a GradingRecord. Args: request: a Django Request object params: the params for this view context: the context for the webpage template: the location of the template used for this view record_entity: a GradingRecord entity """ from google.appengine.api.labs import taskqueue from soc.modules.gsoc.logic.models.student_project import logic as \ student_project_logic survey_logic = params['logic'] record_logic = survey_logic.getRecordLogic() post_dict = request.POST form = params['record_edit_form'](post_dict) if not form.is_valid(): return self._constructResponse(request, record_entity, context, form, params) _, fields = forms_helper.collectCleanedFields(form) record_entity = record_logic.updateEntityProperties( record_entity, fields) if 'save_update' in post_dict: # also update the accompanying StudentProject student_project_logic.updateProjectsForGradingRecords( [record_entity]) elif 'save_update_mail' in post_dict: # update the StudentProject and send an email about the result student_project_logic.updateProjectsForGradingRecords( [record_entity]) # pass along these params as POST to the new task task_params = {'record_key': record_entity.key().id_or_name()} task_url = '/tasks/grading_survey_group/mail_result' mail_task = taskqueue.Task(params=task_params, url=task_url) mail_task.add('mail') # Redirect to the same page redirect = request.META['HTTP_REFERER'] return http.HttpResponseRedirect(redirect)
def editPost(self, request, entity, context, params=None): """Processes POST requests for the specified entity. Params usage: The params dictionary is passed to _constructResponse when the form is not valid (see edit_form and create_form below). See the docstring of _constructResponse on how it uses it. edit_form: The edit_form value is used as form when there is an existing entity. It is provided with with the request.POST dictionary on construction. The collectCleanedFields method is called with the newly constructed form. If the form is not valid, it is passed as argument to _constructResponse. create_form: The create_form value is used in a similar way to edit_form, only it is used when there is no existing entity. edit_redirect: The edit_redirect value is used as the first part of the url if the form was valid. The last part of the url is created using the .key().id_or_name() method of the entity. edit_params: The edit_params dictionary is used as argument to redirectToChangedSuffix, it will be appended to the url in the standard ?key=value format. Args: request: a django request object entity: the entity that will be modified or created, may be None context: the context dictionary that will be provided to Django params: required, a dict with params for this View """ logic = params['logic'] form = params['edit_form'](request.POST) if not form.is_valid(): return self._constructResponse(request, entity, context, form, params) _, fields = forms.collectCleanedFields(form) self._editPost(request, entity, fields) entity = logic.updateEntityProperties(entity, fields) page_params = params['edit_params'] params['suffix'] = entity.key().id_or_name() request.path = params['edit_redirect'] % params return helper.responses.redirectToChangedSuffix(request, None, params=page_params)
def editPost(self, request, entity, context, params=None): """Processes POST requests for the specified entity. Params usage: The params dictionary is passed to _constructResponse when the form is not valid (see edit_form and create_form below). See the docstring of _constructResponse on how it uses it. edit_form: The edit_form value is used as form when there is an existing entity. It is provided with with the request.POST dictionary on construction. The collectCleanedFields method is called with the newly constructed form. If the form is not valid, it is passed as argument to _constructResponse. create_form: The create_form value is used in a similar way to edit_form, only it is used when there is no existing entity. edit_redirect: The edit_redirect value is used as the first part of the url if the form was valid. The last part of the url is created using the .key().id_or_name() method of the entity. edit_params: The edit_params dictionary is used as argument to redirectToChangedSuffix, it will be appended to the url in the standard ?key=value format. Args: request: a django request object entity: the entity that will be modified or created, may be None context: the context dictionary that will be provided to Django params: required, a dict with params for this View """ logic = params['logic'] form = params['edit_form'](request.POST) if not form.is_valid(): return self._constructResponse(request, entity, context, form, params) _, fields = forms.collectCleanedFields(form) self._editPost(request, entity, fields) entity = logic.updateEntityProperties(entity, fields) page_params = params['edit_params'] params['suffix'] = entity.key().id_or_name() request.path = params['edit_redirect'] % params return helper.responses.redirectToChangedSuffix( request, None, params=page_params)
def _manageSetMentor(self, request, template, context, params, entity, form): """Handles the POST request for changing a Projects's mentor. Args: template: the template used for this view entity: the student project entity form: instance of the form used to set the mentor rest: see base.View.public() """ if not form.is_valid(): context['mentor_edit_form'] = form # add an a fresh additional mentors form context['additional_mentor_form'] = params[ 'additional_mentor_form']() return responses.respond(request, template, context) _, fields = forms_helper.collectCleanedFields(form) # get the mentor from the form fields = { 'link_id': fields['mentor_id'], 'scope': entity.scope, 'status': 'active' } mentor = mentor_logic.getForFields(fields, unique=True) # update the project with the assigned mentor fields = {'mentor': mentor} additional_mentors = entity.additional_mentors # pylint: disable=E1103 if additional_mentors and mentor.key() in additional_mentors: # remove the mentor that is now becoming the primary mentor additional_mentors.remove(mentor.key()) fields['additional_mentors'] = additional_mentors # update the project with the new mentor and possible # new set of additional mentors project_logic.updateEntityProperties(entity, fields) # redirect to the same page redirect = request.path return http.HttpResponseRedirect(redirect)
def _editRecordPost(self, request, params, context, template, record_entity): """Handles the POST request for editing a GradingRecord. Args: request: a Django Request object params: the params for this view context: the context for the webpage template: the location of the template used for this view record_entity: a GradingRecord entity """ from google.appengine.api import taskqueue from soc.modules.gsoc.logic.models.student_project import logic as \ student_project_logic survey_logic = params['logic'] record_logic = survey_logic.getRecordLogic() post_dict = request.POST form = params['record_edit_form'](post_dict) if not form.is_valid(): return self._constructResponse(request, record_entity, context, form, params) _, fields = forms_helper.collectCleanedFields(form) record_entity = record_logic.updateEntityProperties(record_entity, fields) if 'save_update' in post_dict: # also update the accompanying StudentProject student_project_logic.updateProjectsForGradingRecords([record_entity]) elif 'save_update_mail' in post_dict: # update the StudentProject and send an email about the result student_project_logic.updateProjectsForGradingRecords([record_entity]) # pass along these params as POST to the new task task_params = {'record_key': record_entity.key().id_or_name()} task_url = '/tasks/grading_survey_group/mail_result' mail_task = taskqueue.Task(params=task_params, url=task_url) mail_task.add('mail') # Redirect to the same page redirect = request.META['HTTP_REFERER'] return http.HttpResponseRedirect(redirect)
def stEditPost(self, request, context, params, entity, **kwargs): """Handles the POST request for the student's edit page. Args: entity: the student project entity rest: see base.View.public() """ form = params['student_edit_form'](request.POST) if not form.is_valid(): return self._constructResponse(request, entity, context, form, params) _, fields = forms_helper.collectCleanedFields(form) project_logic.updateEntityProperties(entity, fields) return self.stEditGet(request, context, params, entity, **kwargs)
def _manageSetMentor(self, request, template, context, params, entity, form): """Handles the POST request for changing a Projects's mentor. Args: template: the template used for this view entity: the student project entity form: instance of the form used to set the mentor rest: see base.View.public() """ if not form.is_valid(): context['mentor_edit_form'] = form # add an a fresh additional mentors form context['additional_mentor_form'] = params['additional_mentor_form']() return responses.respond(request, template, context) _, fields = forms_helper.collectCleanedFields(form) # get the mentor from the form fields = {'link_id': fields['mentor_id'], 'scope': entity.scope, 'status': 'active'} mentor = mentor_logic.logic.getForFields(fields, unique=True) # update the project with the assigned mentor fields = {'mentor': mentor} additional_mentors = entity.additional_mentors # pylint: disable-msg=E1103 if additional_mentors and mentor.key() in additional_mentors: # remove the mentor that is now becoming the primary mentor additional_mentors.remove(mentor.key()) fields['additional_mentors'] = additional_mentors # update the project with the new mentor and possible # new set of additional mentors project_logic.updateEntityProperties(entity, fields) # redirect to the same page redirect = request.path return http.HttpResponseRedirect(redirect)
def takePost(self, request, template, context, params, survey_form, entity, record, **kwargs): """Handles the POST request for the Survey's take page. Args: template: the template used for this view survey_form: instance of SurveyTakeForm entity: the Survey entity record: a SurveyRecord entity rest: see base.View.public() """ survey_logic = params['logic'] record_logic = survey_logic.getRecordLogic() if not survey_form.is_valid(): # show the form errors return self._constructResponse(request, entity=entity, context=context, form=survey_form, params=params, template=template) # retrieve the data from the form _, properties = forms_helper.collectCleanedFields(survey_form) # add the required SurveyRecord properties properties['user'] = user_logic.getCurrentUser() properties['survey'] = entity properties['modified'] = db.DateTimeProperty.now() # call the hook method before updating the SurveyRecord self._takePost(request, params, entity, record, properties) # update the record entity if any and clear all dynamic properties record = record_logic.updateOrCreateFromFields(record, properties, clear_dynamic=True) # get the path to redirect the user to path = self._getRedirectOnSuccessfulTake(request, params, entity, record) return http.HttpResponseRedirect(path)
def _manageAddAdditionalMentor(self, request, template, context, params, entity, form): """Handles the POST request for changing a Projects's additional mentors. Args: template: the template used for this view entity: the student project entity form: instance of the form used to add an additional mentor rest: see base.View.public() """ if not form.is_valid(): context['additional_mentor_form'] = form # add a fresh edit mentor form initial = {'mentor_id': entity.mentor.link_id} context['mentor_edit_form'] = params['mentor_edit_form']( initial=initial) return responses.respond(request, template, context) _, fields = forms_helper.collectCleanedFields(form) # get the mentor from the form fields = { 'link_id': fields['mentor_id'], 'scope': entity.scope, 'status': 'active' } mentor = mentor_logic.getForFields(fields, unique=True) # add this mentor to the additional mentors if not entity.additional_mentors: additional_mentors = [mentor.key()] else: additional_mentors = entity.additional_mentors additional_mentors.append(mentor.key()) fields = {'additional_mentors': additional_mentors} project_logic.updateEntityProperties(entity, fields) # redirect to the same page redirect = request.path return http.HttpResponseRedirect(redirect)
def _manageAddAdditionalMentor(self, request, template, context, params, entity, form): """Handles the POST request for changing a Projects's additional mentors. Args: template: the template used for this view entity: the student project entity form: instance of the form used to add an additional mentor rest: see base.View.public() """ if not form.is_valid(): context['additional_mentor_form'] = form # add a fresh edit mentor form initial = {'mentor_id': entity.mentor.link_id} context['mentor_edit_form'] = params['mentor_edit_form'](initial=initial) return responses.respond(request, template, context) _, fields = forms_helper.collectCleanedFields(form) # get the mentor from the form fields = {'link_id': fields['mentor_id'], 'scope': entity.scope, 'status': 'active'} mentor = mentor_logic.logic.getForFields(fields, unique=True) # add this mentor to the additional mentors if not entity.additional_mentors: additional_mentors = [mentor.key()] else: additional_mentors = entity.additional_mentors additional_mentors.append(mentor.key()) fields = {'additional_mentors': additional_mentors} project_logic.updateEntityProperties(entity, fields) # redirect to the same page redirect = request.path return http.HttpResponseRedirect(redirect)
def takePost(self, request, template, context, params, survey_form, entity, record, **kwargs): """Handles the POST request for the Survey's take page. Args: template: the template used for this view survey_form: instance of SurveyTakeForm entity: the Survey entity record: a SurveyRecord entity rest: see base.View.public() """ survey_logic = params['logic'] record_logic = survey_logic.getRecordLogic() if not survey_form.is_valid(): # show the form errors return self._constructResponse(request, entity=entity, context=context, form=survey_form, params=params, template=template) # retrieve the data from the form _, properties = forms_helper.collectCleanedFields(survey_form) # add the required SurveyRecord properties properties['user'] = user_logic.getForCurrentAccount() properties['survey'] = entity # call the hook method before updating the SurveyRecord self._takePost(request, params, entity, record, properties) # update the record entity if any and clear all dynamic properties record_logic.updateOrCreateFromFields(record, properties, clear_dynamic=True) # TODO: add notice to page that the response has been saved successfully # redirect to the same page for now redirect = request.path return http.HttpResponseRedirect(redirect)