def parse_place(command, value): lvalue = value.lower().strip() for w in ('clinic', 'facility', 'hc', 'hospital'): if dl_distance(lvalue, w) <= 1: return 'FACILITY' if dl_distance(lvalue, 'home') <= 1: return 'HOME' else: raise ValidationError("Did not understand the location: %s." % value)
def parse_gender(command, value): # return m or f lvalue = value.lower().strip() if (lvalue == 'm') or (dl_distance(lvalue, 'male') <= 1): return 'M' elif (lvalue == 'f') or (dl_distance(lvalue, 'female') <= 1): return 'F' else: raise ValidationError("Expected the patient's gender " "(\"male\", \"female\", or simply \"m\" or \"f\"), " "but received instead: %s." % value)
def parse_muacreading(command, value): lvalue = value.lower().strip() rx = re.compile('[0-9]*') m = rx.match(lvalue) reading = lvalue[m.start():m.end()] remaining = lvalue[m.end():].strip() try: reading = int(reading) if remaining == 'mm': if reading <= 30: reading *= 10 elif remaining == 'cm': reading *= 10 if reading > 125: return'G' elif reading < 114: return 'R' else: return 'Y' except ValueError: for category, word in (('R', 'red'), ('Y', 'yellow'), ('G', 'green')): if (lvalue == category.lower()) or (dl_distance(lvalue, word) <= 1): return category raise ValidationError("Expected a muac reading " "(\"green\", \"red\", \"yellow\" or a number), " "but received instead: %s." % value)
def parse_timedelta(command, value): lvalue = value.lower().strip() now = datetime.datetime.now() try: return (now - datetime.datetime.strptime(lvalue, '%m-%d-%Y')).days except ValueError: try: return (now - datetime.datetime.strptime(lvalue, '%m/%d/%Y')).days except ValueError: rx = re.compile('[0-9]*') m = rx.match(lvalue) number = lvalue[m.start():m.end()].strip() unit = lvalue[m.end():].strip() if number: number = int(number) unit_amounts = { 'd':1, 'w':7, 'm':30, 'y':365, } unit_dict = { 'd':('day', 'days', 'dys', 'ds'), 'w':('wk', 'wks', 'weeks', 'week'), 'm':('mo', 'months', 'month', 'mnths', 'mos', 'ms', 'mns', 'mnth'), 'y':('year', 'years', 'yr', 'yrs'), } for key, words in unit_dict.iteritems(): if unit == key: return number * unit_amounts[key] for word in words: if dl_distance(word, unit) <= 1: return number * unit_amounts[key] raise ValidationError("Expected an age got: %s." % value)
def get_or_create_patient(health_provider, patient_name, birthdate=None, deathdate=None, gender=None): for p in Patient.objects.filter(health_worker=health_provider): if dl_distance(p.full_name(), patient_name) <= 1: first_name, middle_name, last_name = split_name(patient_name) p.first_name = first_name p.middle_name = middle_name p.last_name = last_name if birthdate: p.birthdate = birthdate if deathdate: p.deathdate = deathdate if gender: p.gender = gender p.save() return p return create_patient(health_provider, patient_name, birthdate, deathdate, gender)
def parse_yesno(command, value): lvalue = value.lower().strip() if dl_distance(lvalue, "yes") <= 1 or lvalue == "y": return 1 else: return 0
def parse_oedema(command, value): lvalue = value.lower().strip() if dl_distance(lvalue, 'oedema') <= 1: return 'T' else: return 'F'
def parse_yesno(command, value): lvalue = value.lower().strip() if dl_distance(lvalue, 'yes') <= 1 or lvalue == 'y': return 1 else: return 0