Ejemplo n.º 1
0
def process_create_data_sender_form(dbm, form, org_id):
    message = None
    data_sender_id = None

    if form.is_valid():
        try:
            organization = Organization.objects.get(org_id=org_id)
            if organization.in_trial_mode:
                add_data_sender_to_trial_organization(form.cleaned_data["telephone_number"], org_id)
            web_player = WebPlayer(dbm, LocationBridge(location_tree=get_location_tree(), get_loc_hierarchy=get_location_hierarchy))
            reporter_id = form.cleaned_data["short_code"] if form.cleaned_data != "" else None
            request = Request(message=_get_data(form.cleaned_data, organization.country_name(), reporter_id),
                transportInfo=TransportInfo(transport='web', source='web', destination='mangrove'))

            response = web_player.accept(request, logger=websubmission_logger)
            if response.success:
                data_sender_id = response.short_code
                message = get_success_msg_for_registration_using(response, "web")
            else:
                form.update_errors(response.errors)

        except MangroveException as exception:
            message = exception.message

    return data_sender_id,message
Ejemplo n.º 2
0
def _validate_post_data(dbm, request):
    form = ReporterRegistrationForm(request.POST)
    message = None
    success = False
    form_errors = []
    form_errors.extend(form.non_field_errors())
    if form.is_valid():
        form_errors = []
        form_data = {
            k: v
            for (k, v) in form.cleaned_data.items() if not is_empty(v)
        }
        try:
            entered_telephone_number = form_data.get("telephone_number")
            tel_number = _get_telephone_number(entered_telephone_number)
            if not helper.unique(dbm, tel_number):
                raise MultipleReportersForANumberException(
                    entered_telephone_number)

            web_player = WebPlayer(dbm, SubmissionHandler(dbm))
            response = web_player.accept(
                Request(message=_get_data(form_data),
                        transportInfo=TransportInfo(transport='web',
                                                    source='web',
                                                    destination='mangrove')))
            message = get_success_msg_for_registration_using(response, "web")
            success = True
        except MangroveException as exception:
            form_errors.append(exception.message)
            success = False
    return form, form_errors, message, success
Ejemplo n.º 3
0
def submit(request):
    dbm = get_database_manager(request)
    post = _get_submission(request.POST)
    success = True
    try:
        web_player = WebPlayer(dbm, SubmissionHandler(dbm))
        message = {
            k: v
            for (k, v) in post.get('message').items() if not is_empty(v)
        }
        if message.get(LOCATION_TYPE_FIELD_CODE) is not None:
            message[LOCATION_TYPE_FIELD_CODE] += COUNTRY
        request = Request(message=message,
                          transportInfo=TransportInfo(
                              transport=post.get('transport'),
                              source=post.get('source'),
                              destination=post.get('destination')))
        response = web_player.accept(request)
        if response.success:
            message = get_success_msg_for_registration_using(response, "web")
        else:
            message = get_submission_error_message_for(response.errors)
        entity_id = response.datarecord_id
    except MangroveException as exception:
        message = get_exception_message_for(exception=exception, channel="web")
        success = False
        entity_id = None
    return HttpResponse(
        json.dumps({
            'success': success,
            'message': message,
            'entity_id': entity_id
        }))
Ejemplo n.º 4
0
def _validate_post_data(dbm, request):
    form = ReporterRegistrationForm(request.POST)
    message = None
    success = False
    form_errors = []
    form_errors.extend(form.non_field_errors())
    if form.is_valid():
        form_errors = []
        form_data = {k: v for (k, v) in form.cleaned_data.items() if not is_empty(v)}
        try:
            entered_telephone_number = form_data.get("telephone_number")
            tel_number = _get_telephone_number(entered_telephone_number)
            if not helper.unique(dbm, tel_number):
                raise MultipleReportersForANumberException(entered_telephone_number)

            web_player = WebPlayer(dbm, SubmissionHandler(dbm))
            response = web_player.accept(
                Request(message=_get_data(form_data),
                        transportInfo=TransportInfo(transport='web', source='web', destination='mangrove')))
            message = get_success_msg_for_registration_using(response, "web")
            success = True
        except MangroveException as exception:
            form_errors.append(exception.message)
            success = False
    return form, form_errors, message, success
Ejemplo n.º 5
0
 def test_should_format_success_message_for_registration_with_short_code(
         self):
     expected_message = get_registration_success_message() % "ID is: REP1"
     form_submission_mock = Mock()
     form_submission_mock.cleaned_data = {'name': 'tester'}
     form_submission_mock.short_code = "REP1"
     response = create_response_from_form_submission(
         reporters=[],
         submission_id=123,
         form_submission=form_submission_mock)
     message = get_success_msg_for_registration_using(response, "web")
     self.assertEqual(expected_message, message)
Ejemplo n.º 6
0
 def test_should_format_success_message_for_registration_with_short_code(
         self):
     expected_message = success_messages[
         REGISTRATION] % "Unique identification number(ID) is: REP1"
     submission_response = SubmissionResponse(
         success=True,
         submission_id=123,
         errors={},
         processed_data={'name': 'tester'},
         short_code="REP1")
     response = Response(reporters=[],
                         submission_response=submission_response)
     message = get_success_msg_for_registration_using(response, "web")
     self.assertEqual(expected_message, message)
Ejemplo n.º 7
0
 def test_should_format_success_message_for_registration_with_short_code(
         self):
     expected_message = u'Registration successful. ID is: REP1'
     form_submission_mock = Mock()
     form_submission_mock.cleaned_data = {'name': 'tester'}
     form_submission_mock.short_code = "REP1"
     form_submission_mock.entity_type = ["subject"]
     response = create_response_from_form_submission(
         reporters=[{
             'name': 'mino rakoto'
         }],
         submission_id=123,
         form_submission=form_submission_mock)
     message = get_success_msg_for_registration_using(response, "web")
     self.assertEqual(expected_message, message)
Ejemplo n.º 8
0
def submit(request):
    dbm = get_database_manager(request.user)
    post = json.loads(request.POST['data'])
    success = True
    try:
        web_player = WebPlayer(
            dbm,
            LocationBridge(location_tree=get_location_tree(),
                           get_loc_hierarchy=get_location_hierarchy))
        message = post['message']
        message[LOCATION_TYPE_FIELD_CODE] = get_country_appended_location(
            message.get(LOCATION_TYPE_FIELD_CODE),
            get_organization_country(request))
        request = Request(message=message,
                          transportInfo=TransportInfo(
                              transport=post.get('transport'),
                              source=post.get('source'),
                              destination=post.get('destination')))
        response = web_player.accept(request)
        if response.success:
            message = get_success_msg_for_registration_using(response, "web")
        else:
            message = get_submission_error_message_for(response.errors)
        entity_id = response.datarecord_id
    except DataObjectAlreadyExists as exception:
        message = _(
            "Entity with Unique Identification Number (ID) = %s already exists."
        ) % exception.data[1]
        success, entity_id = False, None
    except MangroveException as exception:
        message = get_exception_message_for(exception=exception, channel="web")
        message = _("Please add subject type and then add a subject"
                    ) if message == "t should be present" else message
        success = False
        entity_id = None
    return HttpResponse(
        json.dumps({
            'success': success,
            'message': message,
            'entity_id': entity_id
        }))
Ejemplo n.º 9
0
def submit(request):
    dbm = get_database_manager(request)
    post = _get_submission(request.POST)
    success = True
    try:
        web_player = WebPlayer(dbm, SubmissionHandler(dbm))
        message = {k: v for (k, v) in post.get('message').items() if not is_empty(v)}
        if message.get(LOCATION_TYPE_FIELD_CODE) is not None:
            message[LOCATION_TYPE_FIELD_CODE]+= COUNTRY
        request = Request(message=message,
                          transportInfo=TransportInfo(transport=post.get('transport'), source=post.get('source'),
                                                      destination=post.get('destination')))
        response = web_player.accept(request)
        if response.success:
            message = get_success_msg_for_registration_using(response, "web")
        else:
            message = get_submission_error_message_for(response.errors)
        entity_id = response.datarecord_id
    except MangroveException as exception:
        message = get_exception_message_for(exception=exception, channel="web")
        success = False
        entity_id = None
    return HttpResponse(json.dumps({'success': success, 'message': message, 'entity_id': entity_id}))
Ejemplo n.º 10
0
 def test_should_format_success_message_for_registration_with_short_code(self):
     expected_message = success_messages[REGISTRATION] % "Unique identification number(ID) is: REP1"
     submission_response = SubmissionResponse(success=True, submission_id=123, errors={}, processed_data={'name':'tester'}, short_code="REP1")
     response = Response(reporters=[], submission_response=submission_response)
     message = get_success_msg_for_registration_using(response, "web")
     self.assertEqual(expected_message, message)