def show(self): period = self.request.context clinic = period.__parent__ # if clinic is not assigned, throw a bad request # if not clinic.is_assigned: # raise HTTPBadRequest("The clinic is not yet assigned") scores = clinic.get_scores(period.form_xpath) return { 'period': period, 'periods': ReportingPeriod.get_active_periods(), 'clinic': clinic, 'characteristics': tuple_to_dict_list(("id", "description", "number"), constants.CHARACTERISTICS), 'client_tools': tuple_to_dict_list(("id", "name"), constants.CLIENT_TOOLS), 'recommended_sample_frame': constants.RECOMMENDED_SAMPLE_FRAMES, 'key_indicators': constants.KEY_INDICATORS, 'scores': scores }
def characteristics_list(self): # get the reporting period from the GET params period = self.request.context clinic = period.__parent__ user = clinic.user # if clinic is not assigned, throw a bad request if not clinic.is_assigned: raise HTTPBadRequest("The clinic is not yet assigned") scores = clinic.get_scores(period.form_xpath) characteristics = tuple_to_dict_list(("id", "description", "number"), constants.CHARACTERISTICS) clinic_characteristics = clinic.get_active_characteristics(period) # filter out active characteristics active_characteristic_ids = [ c.characteristic_id for c in clinic_characteristics ] # filter out char_type = self.request.GET.get('char_type') filtered_characteristic_ids = [] if char_type is not None and char_type != 'All': for key_char in tuple_to_dict_list(("id", "characteristics"), constants.KEY_INDICATORS): if key_char['id'] != char_type: for char in key_char['characteristics']: filtered_characteristic_ids.append(char) # merge active characteristics and characteristics to be removed # after filter merged_filter_list = \ active_characteristic_ids + filtered_characteristic_ids inactive_characteristics = filter_dict_list_by_attr(merged_filter_list, characteristics, 'id', invert=True) return { 'period': period, 'clinic': clinic, 'user': user, 'client_tools': tuple_to_dict_list(("id", "name"), constants.CLIENT_TOOLS), 'characteristics': inactive_characteristics, 'scores': scores, 'indicator_labels': dict(constants.INDICATOR_LABELS), 'characteristic_indicator_mapping': constants.CHARACTERISTIC_INDICATOR_MAPPING }
def assess_clinics(self): user = self.request.user clinics = [] if user.location: clinics = user.location.clinics period = get_period_from_request(self.request) sample_frames = {} client_tools = {} _dict = { '2017': (constants.RECOMMENDED_SAMPLE_FRAMES, constants.CLIENT_TOOLS) } sample_frames, client_tools_cont = _dict.get( period.form_xpath, (constants.RECOMMENDED_SAMPLE_FRAMES_V2, constants.CLIENT_TOOLS_V2)) client_tools = tuple_to_dict_list(("id", "name"), client_tools_cont) return { 'clinics': clinics, 'period': period, 'periods': ReportingPeriod.get_active_periods(), 'client_tools': client_tools, 'recommended_sample_frame': sample_frames, }
def test_tuple_to_dict_list_creates_dict_from_list_of_tuples(self): result = tuple_to_dict_list(("name", "age"), [("Billy", 12), ("Bob", 15)]) self.assertEqual(result, [{ "name": "Billy", "age": 12 }, { "name": "Bob", "age": 15 }])
def test_show(self): period = ReportingPeriod.get(ReportingPeriod.title == 'Period 1') clinic = Clinic.get(Clinic.id == 1) period.__parent__ = clinic self.request.context = period response = self.clinic_views.show() self.assertIsInstance(response['clinic'], Clinic) self.assertEqual(response['clinic'].id, clinic.id) self.assertIn('client_tools', response) self.assertEqual( response['characteristics'], tuple_to_dict_list(("id", "description", "number"), constants.CHARACTERISTICS))
def get_num_responses_per_characteristic_xform_id(self, period): clinic_submissions_table = Base.metadata.tables['clinic_submissions'] result = DBSession.execute( select([ 'COUNT(*)', clinic_submissions_table.c.characteristic, clinic_submissions_table.c.xform_id ]).select_from(clinic_submissions_table).where( and_(clinic_submissions_table.c.clinic_id == self.id, clinic_submissions_table.c.valid == true(), clinic_submissions_table.c.period == period)).group_by( clinic_submissions_table.c.characteristic, clinic_submissions_table.c.xform_id)).fetchall() return tuple_to_dict_list(('count', 'characteristic', 'xform_id'), result)
def test_characteristics_list(self): period = ReportingPeriod.get(ReportingPeriod.title == 'Period 1') clinic = Clinic.get(Clinic.id == 1) period.__parent__ = clinic self.request.context = period response = self.clinic_views.characteristics_list() self.assertIsInstance(response['period'], ReportingPeriod) self.assertIsInstance(response['clinic'], Clinic) self.assertEqual(response['clinic'].id, clinic.id) self.assertEqual( response['characteristics'], tuple_to_dict_list(("id", "description", "number"), constants.CHARACTERISTICS)), self.assertEqual(response['indicator_labels'], dict(constants.INDICATOR_LABELS)), self.assertEqual(response['characteristic_indicator_mapping'], constants.CHARACTERISTIC_INDICATOR_MAPPING)