def update_cases(self): sector = get_sector(self._person_case) case_updates = [] for prop, value in six.iteritems(self.request_json): try: param = self.api_spec.get_param(prop, sector) except KeyError: raise NinetyNineDotsException( "{} is not a valid parameter to update".format(prop)) if not param.direction & DIRECTION_INBOUND: raise NinetyNineDotsException( "{} is not a valid parameter to update".format(prop)) case_type = param.get_by_sector('case_type', sector) case_id = self.case_types_to_cases[case_type].case_id if param.setter: update = to_function(param.setter)(param, value, sector) else: update = {param.get_by_sector('case_property', sector): value} case_updates.append((case_id, update, False)) return bulk_update_cases( self.domain, case_updates, "{}.{}".format(self.__module__, self.__class__.__name__), )
def validate_dates(start_date, end_date): if start_date is None: raise NinetyNineDotsException("start_date is null") if end_date is None: raise NinetyNineDotsException("end_date is null") try: parse_datetime(start_date).astimezone(pytz.UTC) parse_datetime(end_date).astimezone(pytz.UTC) except: raise NinetyNineDotsException("Malformed Date")
def _validate_choices(self): sector = get_sector(self._person_case) for param_name, value in six.iteritems(self.request_json): try: choices = self.api_spec.get_param(param_name, sector).get_by_sector('choices', sector) except KeyError: raise NinetyNineDotsException("{} is not a valid parameter to update".format(param_name)) if choices and value not in choices: raise NinetyNineDotsException( "{} is not a valid value for {}.".format(value, param_name) )
def _episode_case(self): try: episode_cases = get_all_episode_cases_from_person(self.domain, self._person_case.case_id) except ENikshayCaseNotFound as e: raise NinetyNineDotsException(e) if not episode_cases: raise NinetyNineDotsException("No episode cases found for {}".format(self._person_case.case_id)) open_cases = [c for c in episode_cases if not c.closed] if open_cases: return sorted(open_cases, key=lambda c: c.opened_on)[0] return episode_cases[0]
def _validate_required_props(self): missing_required_properties = set(self.api_spec.required_params) - set( self.request_json.keys()) if missing_required_properties: raise NinetyNineDotsException( "Missing {} which are required parameters.".format( ", ".join(missing_required_properties)))
def _occurrence_case(self): try: return get_occurrence_case_from_episode(self.domain, self._episode_case) except ENikshayCaseNotFound as e: raise NinetyNineDotsException(e)
def update_adherence_cases(self, start_date, end_date, confidence_level): try: adherence_cases = get_adherence_cases_between_dates( self.domain, self.person_id, start_date, end_date) except ENikshayCaseNotFound as e: raise NinetyNineDotsException(six.text_type(e)) adherence_case_ids = [case.case_id for case in adherence_cases]
def _validate_beneficiary(self): try: self.person_id = self.request_json.pop('beneficiary_id') except KeyError: raise NinetyNineDotsException( "Missing beneficiary_id which is a required parameter.") self._person_case # ensure person case before doing more work
def _parse_adherence_value(self, adherence_point): value = adherence_point.get('adherenceValue', self.DEFAULT_ADHERENCE_VALUE) if value not in VALID_ADHERENCE_VALUES: raise NinetyNineDotsException( "adherenceValue must be one of {}".format(", ".join(VALID_ADHERENCE_VALUES)) ) return value
def validate_confidence_level(confidence_level): valid_confidence_levels = ['low', 'medium', 'high'] if confidence_level not in valid_confidence_levels: raise NinetyNineDotsException( message="New confidence level invalid. Should be one of {}".format( valid_confidence_levels ) )
def _parse_adherence_date(self, iso_datestring): tz = timezone(ENIKSHAY_TIMEZONE) try: datetime_from_adherence = parser.parse(iso_datestring) datetime_in_india = datetime_from_adherence.astimezone(tz) except ValueError: raise NinetyNineDotsException( "Adherence date should be an ISO8601 formated string with timezone information." ) return datetime_in_india.date()
def set_merm_params(param, val, sector): try: params = MermParams(val) except AttributeError as e: raise NinetyNineDotsException( "Invalid MERM params sent. Full error was: {}".format(e)) return { MERM_ID: params.IMEI, MERM_DAILY_REMINDER_STATUS: params.daily_reminder_status, MERM_DAILY_REMINDER_TIME: params.daily_reminder_time, MERM_REFILL_REMINDER_STATUS: params.refill_reminder_datetime, MERM_RT_HOURS: params.RT_hours, }
def validate_adherence_values(adherence_values): if adherence_values is None or not isinstance(adherence_values, list): raise NinetyNineDotsException("Adherences invalid")
def validate_beneficiary_id(beneficiary_id): if beneficiary_id is None: raise NinetyNineDotsException("Beneficiary ID is null") if not isinstance(beneficiary_id, six.string_types): raise NinetyNineDotsException("Beneficiary ID should be a string")
def _person_case(self): try: return self.case_accessor.get_case(self.person_id) except CaseNotFound: raise NinetyNineDotsException( "No patient exists with this beneficiary ID")