def setUp(self): super(TestStateViewsFunctional, self).setUp() with transaction.manager: state = State(name="Sao Paolo") municipality1 = Municipality(name="Brasillia", parent=state) municipality2 = Municipality(name="Brasil", parent=state) user_group = Group(name="state_official") user = User() user.group = user_group user.location = state ona_user = OnaUser(username="******", user=user, refresh_token="1239khyackas") ona_user.save() reporting_period = ReportingPeriod( title='Period 1', start_date=datetime.datetime(2015, 5, 1), end_date=datetime.datetime(2015, 7, 31)) reporting_period.save() DBSession.add_all([state, municipality1, municipality2])
def setUp(self): super(TestStateViews, self).setUp() self.request = testing.DummyRequest() self._create_user('state-official') with transaction.manager: reporting_period = ReportingPeriod( title='Period 1', start_date=datetime.datetime(2015, 5, 1), end_date=datetime.datetime(2015, 7, 31)) reporting_period.save() state = State(name="Sao Paolo") municipality1 = Municipality(name="Brasillia", parent=state) municipality2 = Municipality(name="Brasil", parent=state) DBSession.add_all([state, municipality1, municipality2]) for i in range(5): clinic = Clinic(name="Clinic {}".format(i), code="{}BCDE".format(i), municipality=municipality1) DBSession.add(clinic) self.request.user = OnaUser.get( OnaUser.username == 'state-official').user self.view = StateViews(self.request)
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
def setUp(self): super(TestMunicipalityViews, self).setUp() self.request = testing.DummyRequest() self._create_user('municipality-manager') with transaction.manager: reporting_period = ReportingPeriod( title='Period 1', start_date=datetime.datetime(2015, 5, 1), end_date=datetime.datetime(2015, 7, 31)) reporting_period.save() municipality = Municipality(name="Brasillia") DBSession.add(municipality) for i in range(5): clinic = Clinic(name="Clinic {}".format(i), code="{}BCDE".format(i), municipality=municipality) DBSession.add(clinic) self.request.user = OnaUser.get( OnaUser.username == 'municipality-manager').user self.view = MunicipalityViews(self.request)
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)
def test_municipality_index(self): with patch('whoahqa.models.reporting_period.get_current_date') as mock: mock.return_value = datetime.date(2015, 6, 1) response = self.view.index() locations = response['locations'] self.assertEquals(locations, Municipality.all()) self.assertNotEquals(len(locations), 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)
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)
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)
def test_state_show(self): state = State.get(State.name == "Sao Paolo") self.request.context = state 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['state'], state) self.assertEqual( response['locations'], Municipality.all(Municipality.name.in_(['Brasil', 'Brasillia']))) self.assertNotEqual(len(response['locations']), 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)
def index(self): user = self.request.user if user.group.name == groups.MUNICIPALITY_MANAGER: return HTTPFound( self.request.route_url('municipalities', traverse=(user.location.id))) return { 'locations': Municipality.all(), 'national_report': self.national_report(self.period), 'period': self.period, 'periods': self.periods, 'key_indicators_key_labels': self.key_indicators_key_labels, }
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)
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")
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)
def test_push_facilities_with_locations(self): # create dummy data self._create_state() self._create_municipality() municipality = Municipality.newest() state = State.newest() municipality.parent = state municipality.save() clinic1 = Clinic(id=1, name=u"Clinic A", code="1A2B", municipality=municipality) clinic1.save() # test push_facilities service response = push_facilities(self.request) self.assertEqual(1, len(response['rows']))
def test_can_register_new_municipality_users(self): self._create_municipality() ona_user = OnaUser.get(OnaUser.username == 'manager_a') user = ona_user.user old_user_count = User.count() municipality = Municipality.newest() self.request.context = user self.request.method = 'POST' params = MultiDict({'email': "*****@*****.**", 'username': "******", 'password': {'password': '******', 'password-confirm': 'password'}, 'group': groups.MUNICIPALITY_MANAGER, 'municipality': "{}".format(municipality.id)}) self.request.POST = params response = self.view.register() self.assertEqual(User.count(), old_user_count + 1) self.assertEqual(response.status_code, 302)
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}
def setup_clinics(): # add a couple of clinics state_params = {'name': "Acre"} state = State.get_or_create( State.name == state_params['name'], **state_params) municipality_params = {'name': 'Brasilia', 'parent': state} municipality = Municipality.get_or_create( Municipality.name == municipality_params['name'], **municipality_params) clinic_criteria = Clinic.name == "Clinic A" clinic_params = { "name": "Clinic A", "code": "1A2B", "municipality": municipality} clinic_a = Clinic.get_or_create( clinic_criteria, **clinic_params) clinic_a.save() clinic_params = { "name": "Clinic b", "code": "1B2C", "municipality": municipality} clinic_b = Clinic.get_or_create( clinic_criteria, **clinic_params) clinic_b.save() user = OnaUser.get(OnaUser.username == 'admin').user clinic_a.assign_to(user) clinic_b.assign_to(user)
def municipality_widget(node, kw): values = [('', '---')] [values.append((m.id, fmt(m.name))) for m in Municipality.all()] return SelectWidget(values=values)
def municipality_selection_widget(node, kw): values = [('', '---')] [values.append((m.id, key_to_label(m.name))) for m in Municipality.all()] return SelectWidget(values=values)