def post(request): body_unicode = request.body.decode('utf-8') body = json.loads(body_unicode) phoneNumber=body['phoneNumber'] sessionId=body['sessionId'] serviceCode=body['serviceCode'] text=body['text'] ussd_request=UssdRequest(sessionId, phoneNumber, text, 'en', journey_name="myownscreen", journey_version=None) ussd_engine=UssdEngine(ussd_request) ussd_response=ussd_engine.ussd_dispatcher() ussd_response_text=ussd_response.dumps() if ussd_response.status: http_text = 'CON' + ' ' + ussd_response_text phoneresponse = HttpResponse(http_text) else: http_text = 'END' + ' ' + ussd_response_text phoneresponse = HttpResponse(http_text) return phoneresponse
def test_saving_journeys_thats_still_in_edit_mode(self): sample_journey = { "initial_screen": { "type": "initial_screen", "next_screen": "end_screen", "default_language": "en" }, "end_screen": { "type": "quit_screen", "text": "end screen" } } self.driver.save(name="journey_a", journey=sample_journey, version="0.0.1") self.assertEqual(sample_journey, self.driver.get('journey_a', '0.0.1')) sample_journey_edit_mode = { "initial_screen": { "type": "initial_screen", "next_screen": "end_screen", "default_language": "en" }, } # confirm its invalid is_valid, errors = UssdEngine.validate_ussd_journey( sample_journey_edit_mode) self.assertFalse(is_valid) # but we can still save it under edit mode self.driver.save(name='journey_a', journey=sample_journey_edit_mode, edit_mode=True) # stills get the valid one that was saved. self.assertEqual(sample_journey, self.driver.get('journey_a')) # getting the one use edit mode self.assertEqual(sample_journey_edit_mode, self.driver.get('journey_a', edit_mode=True)) sample_journey_edit_mode['invalid_screen'] = "invalid_screen" # and we can still save it again self.driver.save(name='journey_a', journey=sample_journey_edit_mode, edit_mode=True) self.assertEqual(sample_journey_edit_mode, self.driver.get('journey_a', edit_mode=True))
def validate_yaml(value): try: journey = read_journey(value['journeys']) is_valid, errors = UssdEngine.validate_ussd_journey(journey) # validate the existance and form of a custom defined initialize_survey screen # if is_valid: # is_valid, errors = validate_ussd_journey(journey) except Exception as ex: logger.error(ex) raise serializers.ValidationError( {"journeys": ["The yaml file is invalid."]}) if not is_valid: raise serializers.ValidationError({"journeys": [json.dumps(errors)]})
def send(self, ussd_input, raw=False): payload = { "session_id": self.session_id, "ussd_input": ussd_input, "phone_number": self.phone_number, "language": self.language, } payload.update(self.extra_payload) ussd_request = UssdRequest(**payload) response = UssdEngine(ussd_request).ussd_dispatcher() if raw: return response return str(response)
def _test_ussd_validation(self, version_to_validate, expected_validation, expected_errors): if self.validate_ussd: ussd_screens = self.journey_store.get(self.journey_name, version_to_validate) is_valid, error_message = UssdEngine.validate_ussd_journey( ussd_screens) self.assertEqual(is_valid, expected_validation, error_message) for key, value in expected_errors.items(): args = (value, error_message[key], key) if isinstance(value, dict): self.assertDictEqual(*args) else: self.assertEqual(*args) self.assertDictEqual(error_message, expected_errors)
def save(self, name: str, journey: dict, version=None, edit_mode=False): # version and editor mode should not be false if not (version or edit_mode): raise ValidationError( "version is required if its not in editor mode") if edit_mode: version = self.edit_mode_version # check if this version already exists if self.get(name, version, propagate_error=False) is not None: if not edit_mode: raise ValidationError("journey already exists") # validate if its not in editing mode. if not edit_mode: from ussd.core import UssdEngine is_valid, errors = UssdEngine.validate_ussd_journey(journey) if not is_valid: raise ValidationError(errors, "journey") # now create journey return self._save(name, journey, version)