Beispiel #1
0
    def test_can_access_own_municipality(self):
        municipality = Municipality.get(Municipality.name == "Brasillia")
        url = self.request.route_path(
            'municipalities', traverse=(municipality.id))
        headers = self._login_user("m-official")

        with patch('whoahqa.models.reporting_period.get_current_date') as mock:
            mock.return_value = datetime.date(2015, 6, 1)
            response = self.testapp.get(url, headers=headers)
            self.assertEqual(response.status_code, 200)
Beispiel #2
0
    def test_municipality_show(self):
        municipality = Municipality.get(Municipality.name == "Brasillia")
        self.request.context = municipality

        with patch('whoahqa.models.reporting_period.get_current_date') as mock:
            mock.return_value = datetime.date(2015, 6, 1)
            response = self.view.show()

            self.assertEqual(response['municipality'], municipality)
            self.assertEqual(response['locations'], municipality.clinics)
            self.assertNotEqual(len(response['locations']), 0)
Beispiel #3
0
    def test_update_ona_user_location(self):
        self.setup_test_data()
        self._create_municipality('Brazlandia')
        municipality = Municipality.get(Municipality.name == 'Brazlandia')

        values = {'group': groups.MUNICIPALITY_MANAGER,
                  'municipality': municipality.id}

        manager = OnaUser.get(OnaUser.username == 'manager_a')

        manager.update(values)

        self.assertEqual(manager.user.location, municipality)
Beispiel #4
0
    def test_update_new_user_group(self):
        user = User()
        ona_user = OnaUser(
            user=user, username='******', refresh_token="c563e9")

        self._create_municipality('Brazlandia')
        municipality = Municipality.get(Municipality.name == 'Brazlandia')

        values = {'group': groups.MUNICIPALITY_MANAGER,
                  'municipality': municipality.id}

        ona_user.update(values)

        self.assertEqual(ona_user.user.group.name, groups.MUNICIPALITY_MANAGER)
        self.assertEqual(ona_user.user.location, municipality)
Beispiel #5
0
    def test_national_official_can_access_municipality(self):
        self._create_dash_user(
            "national", "national", "*****@*****.**",
            groups.NATIONAL_OFFICIAL)
        user = User.newest()

        municipality = Municipality.get(Municipality.name == "Brasillia")
        url = self.request.route_path(
            'municipalities', traverse=(municipality.id))
        headers = self._login_dashboard_user(user)

        with patch('whoahqa.models.reporting_period.get_current_date') as mock:
            mock.return_value = datetime.date(2015, 6, 1)
            response = self.testapp.get(url, headers=headers)
            self.assertEqual(response.status_code, 200)
Beispiel #6
0
def import_health_data():
    file_name = os.path.relpath('whoahqa/data/clinics.csv')
    with open(file_name, 'rU') as source:
        rdr = UnicodeDictReader(source)
        existing_states = [
            normalizeString(state.name) for state in State.all()
        ]
        existing_municipalities = [
            normalizeString(municipality.name)
            for municipality in Municipality.all()
        ]
        existing_clinics = [clinic.code for clinic in Clinic.all()]

        with transaction.manager:
            for row in rdr:
                state = None
                municipality = None
                normalized_state = normalizeString(row['state'])
                normalized_municipality = normalizeString(row['municipality'])

                if normalized_state not in existing_states:
                    existing_states.append(normalized_state)
                    state = State(name=normalized_state)
                    DBSession.add(state)

                if normalized_municipality not in existing_municipalities:
                    existing_municipalities.append(normalized_municipality)

                    if state is None:
                        state = State.get(State.name == normalized_state)

                    municipality = Municipality(name=normalized_municipality,
                                                parent=state)
                    DBSession.add(municipality)

                if row['CNES'] not in existing_clinics:
                    # import ipdb; ipdb.set_trace()
                    existing_clinics.append(row['CNES'])

                    if municipality is None:
                        municipality = Municipality.get(
                            Municipality.name == normalized_municipality)

                    clinic = Clinic(name=row['facility_name'],
                                    code=row['CNES'],
                                    municipality=municipality)
                    DBSession.add(clinic)
Beispiel #7
0
    def test_municipality_manager_can_edit_clinics(self):
        municipality = Municipality.get(Municipality.name == "Brazilia")
        clinic = Clinic.get(Clinic.code == '1A2B')
        url = self.request.route_path('clinics',
                                      traverse=(clinic.id, 'edit_clinic'))

        headers = self._login_user('manager_a')
        params = MultiDict({
            'municipality': municipality.id,
            'name': "New Clinic Name",
            'code': clinic.code
        })
        response = self.testapp.post(url, params, headers=headers)
        self.assertEqual(response.status_code, 200)
        clinic = Clinic.get(Clinic.code == '1A2B')

        self.assertEqual(clinic.name, "New Clinic Name")
Beispiel #8
0
    def test_register_clinic_doesnt_save_clinics_with_same_codes(self):
        ona_user = OnaUser.get(OnaUser.username == 'manager_a')
        municipality = Municipality.get(Municipality.name == "Brazilia")
        clinic = Clinic.get(Clinic.code == '1A2B')
        params = MultiDict({
            'municipality': "{}".format(municipality.id),
            'name': "New Clinic Name",
            'code': clinic.code
        })
        self.request.method = 'POST'
        self.request.ona_user = ona_user
        self.request.POST = params

        self.clinic_views.register_clinic()

        flash_error = self.request.session.pop_flash('error')[0]
        self.assertTrue(flash_error.find("exists") != -1)
Beispiel #9
0
    def register_clinic(self):
        clinic = Clinic()
        period = get_period_from_request(self.request)

        form = Form(ClinicForm().bind(request=self.request, clinic=clinic),
                    button=('Save', Button(name='cancel', type='button')),
                    appstruct=clinic.appstruct)

        if self.request.method == 'POST':
            data = self.request.POST.items()
            try:
                values = form.validate(data)
            except ValidationFailure:
                pass
            else:
                try:
                    municipality = Municipality.get(
                        Municipality.id == values.get('municipality'))
                    clinic.update(values.get('name'), values.get('code'),
                                  municipality)
                    self.request.session.flash(
                        _("{} saved.".format(clinic.name)), "success")

                    return HTTPFound(
                        self.request.route_url('clinics',
                                               traverse=(clinic.id,
                                                         'edit_clinic')))

                except NoResultFound:
                    self.request.session.flash(
                        _("Cannot find selected municipality."), "error")

                except IntegrityError:
                    DBSession.rollback()

                    self.request.session.flash(
                        _("A clinic already exists with the \
                          provided CNES."), "error")

        return {'form': form, 'clinic': clinic, 'period': period}