Beispiel #1
0
    def json_post(self):
        payload = self.request.body
        if not payload:
            return HTTPBadRequest(comment='Missing JSON Payload')

        try:
            Submission.create_from_json(payload)
        except SubmissionHandlerError:
            return Response(
                'Accepted pending manual matching process', status=202)
        else:
            return Response('Saved', status=201)
Beispiel #2
0
    def test_raises_submission_handler_error_if_invalid_json(self):
        # check current counts
        count = Submission.count()
        clinic_count = Clinic.count()
        payload = self.clinic_registrations[1]
        self.assertRaises(SubmissionHandlerError, Submission.create_from_json,
                          payload)
        submission = Submission.newest()
        self.assertEqual(Submission.count(), count + 1)
        self.assertEqual(submission.raw_data, json.loads(payload))

        # check that a clinic record was still created
        self.assertEqual(Clinic.count(), clinic_count + 1)
Beispiel #3
0
 def test_submission_created_when_submission_handler_errors(self):
     self.setup_test_data()
     count = Submission.count()
     clinic_submission_count = ClinicSubmission.count()
     url = self.request.route_path('submissions', traverse=())
     payload = self.submissions[5]
     headers = self._login_user('manager_b')
     response = self.testapp.post(url, payload, headers=headers)
     self.assertEqual(response.status_code, 202)
     # check that a submission was created
     self.assertEqual(Submission.count(), count + 1)
     # check that a clinic submission was NOT created
     self.assertEqual(ClinicSubmission.count(), clinic_submission_count)
Beispiel #4
0
def parse_municipalities_from_submissions():
    submissions = Submission.all()
    for submission in submissions:
        try:
            with transaction.manager:
                clinic_code = submission.raw_data[constants.CLINIC_IDENTIFIER]
                clinic = Clinic.get(Clinic.code == clinic_code)

                state_name = submission.raw_data[constants.STATE_IDENTIFIER]

                municipality_name = submission.raw_data[
                    constants.MUNICIPALITY_IDENTIFIER]

                state_params = {'name': state_name}
                state = State.get_or_create(State.name == state_name,
                                            **state_params)

                municipality_params = {'name': municipality_name}
                municipality = Municipality.get_or_create(
                    Municipality.name == municipality_name,
                    **municipality_params)

                municipality.state = state

                if clinic.municipality is None:
                    clinic.municipality = municipality
                    DBSession.add_all([municipality, clinic])

        except (NoResultFound, KeyError):
            pass
Beispiel #5
0
    def test_save_submission_with_invalid_clinic_id(self):
        clinic_submissions_count = ClinicSubmission.count()
        payload = self.submissions[1]

        submission = Submission(raw_data=json.loads(payload))
        self.assertRaises(ClinicNotFound,
                          ClinicReportHandler(submission).handle_submission)
        self.assertEqual(clinic_submissions_count, ClinicSubmission.count())
Beispiel #6
0
    def test_create_submission_from_json(self):
        # create clinic with matching id
        clinic_code = "1A2B"
        clinic = Clinic(code=clinic_code, name="Clinic A")
        DBSession.add(clinic)

        # check current counts
        count = Submission.count()
        clinic_submission_count = ClinicSubmission.count()
        payload = self.submissions[0]
        Submission.create_from_json(payload)
        submission = Submission.newest()
        self.assertEqual(Submission.count(), count + 1)
        self.assertEqual(submission.raw_data, json.loads(payload))

        # check that a clinic_submission record was created
        self.assertEqual(ClinicSubmission.count(),
                         clinic_submission_count + 17)
Beispiel #7
0
 def test_raise_multiple_submission_handlers_error_if_multiple_handlers(
         self):
     payload = self.submissions[0]
     handler_mapping = (
         (ClinicReportHandler, [constants.ADOLESCENT_CLIENT]),
         (ClinicRegistrationHandler, [constants.ADOLESCENT_CLIENT]),
     )
     submission = Submission(raw_data=json.loads(payload))
     self.assertRaises(MultipleSubmissionHandlersError,
                       determine_handler_class, submission, handler_mapping)
Beispiel #8
0
    def test_check_if_submission_exists(self):
        # create a clinic with matching id
        clinic_code = "1A2B"
        clinic = Clinic(code=clinic_code, name="Clinic A")
        DBSession.add(clinic)

        # check current counts
        count = Submission.count()
        clinic_submission_count = ClinicSubmission.count()
        _submission = self.submissions[1]

        if type(_submission) is not dict:
            payload = json.loads(_submission)

        if Submission.exists(payload):
            submission = Submission.newest()
            self.assertEqual(Submission.count(), count)
            self.assertEqual(ClinicSubmission.count(), clinic_submission_count)
            self.assertIsInstance(submission, Submission)
Beispiel #9
0
    def test_creates_unassigned_clinic_if_user_doesnt_exist(self):
        payload = self.clinic_registrations[0]
        raw_data = json.loads(payload)
        count = Clinic.count()
        submission = Submission(raw_data=raw_data)
        self.assertRaises(
            UserNotFound,
            ClinicRegistrationHandler(submission).handle_submission)

        # check that a clinic_submission record was NOT created
        self.assertEqual(Clinic.count(), count + 1)
Beispiel #10
0
    def test_save_submission_with_valid_clinic_id(self):
        # create clinic with matching id
        clinic_code = "1A2B"
        clinic = Clinic(code=clinic_code, name="Clinic A")
        DBSession.add(clinic)

        payload = self.submissions[0]
        submission = Submission(raw_data=json.loads(payload))
        count = ClinicSubmission.count()
        ClinicReportHandler(submission).handle_submission()

        # check that a clinic_submission record was created
        self.assertEqual(ClinicSubmission.count(), count + 17)
Beispiel #11
0
    def test_save_submission_with_multiple_characteristics(self):
        # create clinic with matching id
        clinic_code = "1A2B"
        clinic = Clinic(code=clinic_code, name="Clinic A")
        DBSession.add(clinic)

        # check current counts
        count = ClinicSubmission.count()
        payload = self.submissions[2]
        submission = Submission(raw_data=json.loads(payload))
        ClinicReportHandler(submission).handle_submission()

        # check that 17 clinic_submission record was created
        self.assertEqual(ClinicSubmission.count(), count + 17)
Beispiel #12
0
    def test_create_clinic_from_submission(self):
        # create user with matching id
        DBSession.add(User())
        user = User.newest()

        payload = self.clinic_registrations[0]
        raw_data = json.loads(payload)
        raw_data[constants.USER_ID] = user.id
        submission = Submission(raw_data=raw_data)
        count = Clinic.count()
        ClinicRegistrationHandler(submission).handle_submission()

        # check that a clinic_submission record was created
        self.assertEqual(Clinic.count(), count + 1)

        clinic = Clinic.newest()
        self.assertEqual(clinic.code, '1A2B')
Beispiel #13
0
 def test_raise_zero_submission_handlers_error_if_no_handlers(self):
     payload = self.submissions[0]
     handler_mapping = ()
     submission = Submission(raw_data=json.loads(payload))
     self.assertRaises(ZeroSubmissionHandlersError, determine_handler_class,
                       submission, handler_mapping)
Beispiel #14
0
 def test_return_clinic_registration_handler_for_registration_xforms(self):
     payload = self.clinic_registrations[0]
     submission = Submission(raw_data=json.loads(payload))
     handler_class = determine_handler_class(
         submission, Submission.HANDLER_TO_XFORMS_MAPPING)
     self.assertEqual(handler_class, ClinicRegistrationHandler)
Beispiel #15
0
def import_submissions_for(form_id):
    submissions = fetch_tool_submissions(form_id)

    with transaction.manager:
        for submission in submissions:
            Submission.create_from_json(submission)