Permalink
Browse files

Pooja/Ashwin | #1913 | Editing of successful survey response implemented

  • Loading branch information...
lakshmirp committed Mar 26, 2013
1 parent eb980b7 commit 2ec2bd8faff7419760dce11ced39c6887fd91896
@@ -0,0 +1,8 @@
+function(doc) {
+ var isNotNull = function(o) {
+ return !((o === undefined) || (o == null));
+ };
+ if (doc.document_type == 'SurveyResponse' && isNotNull(doc.form_code)) {
+ emit(doc._id,doc);
+ }
+}
@@ -58,6 +58,10 @@ def form_model_revision(self):
def values(self):
return self._doc.values
+ @values.setter
+ def values(self, values):
+ self._doc.values = values
+
@property
def errors(self):
return self._doc.error_message
@@ -87,12 +91,14 @@ def delete(self):
data_record.delete()
super(SurveyResponse, self).delete()
- def update(self, status, errors, entity_question_code, entity_short_code, data_record_id=None, is_test_mode=False):
+ def update(self, status, errors, entity_question_code, entity_short_code, values=None,data_record_id=None, is_test_mode=False):
self.set_entity(entity_question_code, entity_short_code)
self._doc.status = status
self._doc.data_record_id = data_record_id
self._doc.error_message = self._to_string(errors)
self._doc.test = is_test_mode
+ if values:
+ self.values = values
self.save()
def update_form_model_revision(self, form_model_revision):
@@ -18,6 +18,12 @@ def add_survey_response(self, request, logger=None):
def _parse(self, message):
return WebParser().parse(message)
+ def edit_survey_response(self,request,survey_response_id,logger=None):
+ assert request is not None
+ form_code, values = self._parse(request.message)
+ service = SurveyResponseService(self.dbm, logger)
+ return service.edit_survey(form_code, values, [],request.transport, request.message,survey_response_id)
+
class SMSPlayerV2(object):
def __init__(self, dbm, post_sms_parser_processors):
@@ -98,7 +98,6 @@ def test_survey_response_is_saved(self):
submission = Submission.get(self.manager, response.survey_response_id)
self.assertDictEqual({'Q1': 'name', 'Q3': 'a', 'Q2': '80', 'ID': '1'}, submission.values)
- self.assertDictEqual({'Q1': 'name', 'Q3': 'a', 'Q2': '80', 'ID': '1'}, submission.values)
self.assertEqual(test_data.form_model.revision, submission.form_model_revision)
self.assertEqual(test_data.entity1.short_code, submission.get_entity_short_code('ID'))
self.assertEqual(True, submission.status)
@@ -112,3 +111,31 @@ def test_survey_response_is_saved(self):
self.assertEqual(True, survey_response.status)
self.assertIsNotNone(survey_response.data_record)
+ def test_survey_response_is_edited_and_new_submission_is_created(self):
+ test_data = TestData(self.manager)
+ survey_response_service = SurveyResponseService(self.manager)
+
+ values = {'ID': test_data.entity1.short_code, 'Q1': 'name', 'Q2': '80', 'Q3': 'a'}
+ transport_info = TransportInfo('web', 'src', 'dest')
+ request = Request(values, transport_info)
+
+ saved_response = survey_response_service.save_survey('CL1', values, [], transport_info,request.message)
+ self.assertDictEqual(OrderedDict([('Q1', 'name'), ('Q3', ['RED']), ('Q2', 80), ('ID', u'1')]),
+ saved_response.processed_data)
+
+ new_values = {'ID': test_data.entity1.short_code, 'Q1': 'new_name', 'Q2': '430', 'Q3': 'b'}
+ edited_response = survey_response_service.edit_survey('CL1', new_values, [], transport_info,request.message,saved_response.survey_response_id)
+ self.assertTrue(edited_response.success)
+ self.assertEqual(0, edited_response.errors.__len__())
+ self.assertIsNotNone(edited_response.datarecord_id)
+ self.assertIsNotNone(edited_response.survey_response_id)
+ self.assertEqual(test_data.entity_type, edited_response.entity_type)
+ self.assertEqual('CL1', edited_response.form_code)
+ self.assertEqual('1', edited_response.short_code)
+ self.assertDictEqual(OrderedDict([('Q1', 'new_name'), ('Q3', ['YELLOW']), ('Q2', 430), ('ID', u'1')]),
+ edited_response.processed_data)
+
+ submission = Submission.get(self.manager, edited_response.submission_id)
+ self.assertNotEquals(saved_response.submission_id,edited_response.submission_id)
+ self.assertIsNotNone(submission.form_model_revision)
+ self.assertDictEqual({'Q1': 'new_name', 'Q3': 'b', 'Q2': '430', 'ID': '1'}, submission.values)
@@ -5,7 +5,7 @@
from mangrove.transport.player.parser import WebParser
from mangrove.transport.contract.submission import Submission
from mangrove.transport.contract.response import Response
-from mangrove.transport.repository.survey_responses import SurveyResponse
+from mangrove.transport.repository.survey_responses import SurveyResponse, get_survey_response_by_id
class SurveyResponseService(object):
PARSERS = {'web': WebParser()}
@@ -27,7 +27,10 @@ def _create_survey_response(self, transport_info, form_code, values):
def save_survey(self, form_code, values, reporter_names, transport_info, message):
submission = self._create_submission_log(transport_info, form_code, copy(values))
survey_response = self._create_survey_response(transport_info, form_code, copy(values))
+ return self.update_survey_response_and_submission(submission,survey_response,form_code,values,reporter_names,transport_info,message)
+
+ def update_survey_response_and_submission(self,submission,survey_response,form_code,values, reporter_names, transport_info, message):
form_model = get_form_model_by_code(self.dbm, form_code)
submission.update_form_model_revision(form_model.revision)
survey_response.update_form_model_revision(form_model.revision)
@@ -43,13 +46,13 @@ def save_survey(self, form_code, values, reporter_names, transport_info, message
submission.update(form_submission.saved, form_submission.errors, form_model.entity_question.code,
form_submission.short_code, form_submission.data_record_id,
form_model.is_in_test_mode())
- survey_response.update(form_submission.saved, form_submission.errors, form_model.entity_question.code,
- form_submission.short_code, form_submission.data_record_id,
- form_model.is_in_test_mode())
+
+ survey_response.update(form_submission.saved, form_submission.errors, form_model.entity_question.code,form_submission.short_code,values,
+ form_submission.data_record_id,form_model.is_in_test_mode())
self.log_request(form_submission.saved, transport_info.source, message)
- return Response(reporter_names, None, survey_response.uuid, form_submission.saved,
+ return Response(reporter_names, submission.uuid, survey_response.uuid, form_submission.saved,
form_submission.errors,
form_submission.data_record_id,
form_submission.short_code, form_submission.cleaned_data, form_submission.is_registration,
@@ -66,49 +69,16 @@ def log_request(self, status, source, message):
log_entry += "status: True" if status else "status: False"
self.logger.info(log_entry)
-
- # def save_survey_response(self, request):
- # self.parser = SurveyResponseService.PARSERS.get(request.transport.transport)
- # assert request is not None
- # form_code, values = self.parser.parse(request.message)
- #
- # submission = self._create_submission_log(request.transport, form_code, copy(values))
- # form_model = get_form_model_by_code(self.dbm, form_code)
- # submission.update_form_model_revision(form_model.revision)
- #
- # if form_model.is_inactive():
- # raise InactiveFormModelException(form_model.form_code)
- #
- # try:
- # form_model.bind(values)
- # cleaned_data, errors = form_model.validate_submission(values=values)
- # form_submission = self.save(form_model, cleaned_data, errors)
- #
- # submission.values[form_model.entity_question.code] = form_submission.short_code
- # submission.update(form_submission.saved, form_submission.errors, form_submission.data_record_id,
- # form_model.is_in_test_mode())
- #
- # self.log_request(form_submission, request)
- #
- # return Response([], submission.uuid, form_submission.saved, form_submission.errors,
- # form_submission.data_record_id,
- # form_submission.short_code, form_submission.cleaned_data, form_submission.is_registration,
- # form_submission.entity_type,
- # form_submission.form_model.form_code)
- # except (MangroveException, FormModelDoesNotExistsException) as exception:
- # submission.update(status=False, errors=exception.message, is_test_mode=form_model.is_in_test_mode())
- # raise
- #
- #
-
def save(self, form_model, cleaned_data, errors):
form_submission = DataFormSubmission(form_model, cleaned_data, errors)
if form_submission.is_valid:
form_submission.save(self.dbm)
return form_submission
- def edit_survey_response(self):
- pass
+ def edit_survey(self, form_code, values, reporter_names, transport_info, message,survey_response_id):
+ submission = self._create_submission_log(transport_info, form_code, copy(values))
+ survey_response = get_survey_response_by_id(self.dbm, survey_response_id)
+ return self.update_survey_response_and_submission(submission,survey_response,form_code,values,reporter_names,transport_info,message)
def delete_survey_response(self):
pass

0 comments on commit 2ec2bd8

Please sign in to comment.