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 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(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 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_states_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, State.all()) self.assertNotEquals(len(locations), 0)
def test_state_show_with_authorised_user(self): state = State.get(State.name == "Sao Paolo") url = self.request.route_path('states', traverse=(state.id)) headers = self._login_user("state-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_state_show_for_user_without_perms(self): state = State.get(State.name == "Sao Paolo") self._create_user('renegade', 'state_official') url = self.request.route_path('states', traverse=(state.id)) headers = self._login_user("renegade") 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, status=403) self.assertEqual(response.status_code, 403)
def test_update_user_to_state_official(self): self.setup_test_data() self._create_state('Acre') state = State.get(Municipality.name == 'Acre') values = {'group': groups.STATE_OFFICIAL, 'state': state.id} manager = OnaUser.get(OnaUser.username == 'manager_a') manager.update(values) self.assertEqual(manager.user.location, state)
def test_national_official_can_access_state(self): self._create_dash_user("national", "national", "*****@*****.**", groups.NATIONAL_OFFICIAL) user = User.newest() state = State.get(State.name == "Sao Paolo") url = self.request.route_path('states', traverse=(state.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 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 index(self): user = self.request.user if user.group.name == groups.STATE_OFFICIAL: return HTTPFound(self.request.route_url( 'states', traverse=(user.location.id))) return { 'locations': State.all(), 'period': self.period, 'periods': self.periods, 'national_report': self.national_report(self.period), 'key_indicators_key_labels': self.key_indicators_key_labels }
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_state_users(self): self._create_state() ona_user = OnaUser.get(OnaUser.username == 'manager_a') user = ona_user.user old_user_count = User.count() state = State.newest() self.request.context = user self.request.method = 'POST' params = MultiDict({'email': "*****@*****.**", 'username': "******", 'password': {'password': '******', 'password-confirm': 'password'}, 'group': groups.STATE_OFFICIAL, 'state': "{}".format(state.id)}) self.request.POST = params response = self.view.register() self.assertEqual(User.count(), old_user_count + 1) self.assertEqual(response.status_code, 302)
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 state_selection_widget(node, kw): values = [('', '---')] [values.append((m.id, key_to_label(m.name))) for m in State.all()] return SelectWidget(values=values)