def register_from_message(modelBase, message): potential_registrant = Respondent.objects.filter(phone_number=message.connection.identity) if potential_registrant: return potential_registrant[0] else: # have to create a Reporter and PersistentConnection # TODO: spl = message.text.partition(" ") reporter = Reporter(alias=message.connection.identity, first_name=spl[0], last_name=spl[2]) reporter.save() be = PersistantBackend.from_message(message) be.save() conn = PersistantConnection.from_message(message) conn.reporter = reporter conn.save() conn.seen() resp = Respondent( phone_number=message.connection.identity, registered_at=datetime.now(), reporter=reporter, connection=conn) resp.save() return resp
def reporter_http_identity(self, message, username): '''Gives a http identity to a httptester number of a registered reporter''' reporter = self.find_provider(message, username) # attach the reporter to the current connection if PersistantBackend.from_message(message).title == "http": mobile = reporter.connection().identity if mobile == message.persistant_connection.identity: message.respond(_("You already have a http identity!")) elif mobile[0] == "+" \ and mobile[1:] == message.persistant_connection.identity: message.persistant_connection.reporter = reporter message.persistant_connection.save() message.respond(_("You now have a http identity!")) else: message.respond(_("Illegal Operation!")) else: message.respond(_("Operation not allowed under this mode!")) return True
def report_case(self, message, ref_id, muac=None, weight=None, height=None, complications=''): '''Record muac, weight, height, complications if any Format: muac +[patient_ID\] muac[measurement] edema[e/n] symptoms separated by spaces[CG D A F V NR UF] reply with diagnostic message ''' # TODO use gettext instead of this dodgy dictionary _i = { 'units': {'MUAC': 'mm', 'weight': 'kg', 'height': 'cm'}, 'en': {'error': "Can't understand %s (%s): %s"}, 'fr': {'error': "Ne peux pas comprendre %s (%s): %s"}} def guess_language(msg): if msg.upper().startswith('MUAC'): return 'en' if msg.upper().startswith('PB'): return 'fr' # use reporter's preferred language, if possible if message.reporter: if message.reporter.language is not None: lang = message.reporter.language else: # otherwise make a crude guess lang = guess_language(message.text) message.reporter.language = lang else: lang = 'fr' # if there is no height, ASSUME that muac is the (newly) optional # field that has been omitted. swap weight and height to their # ASSUMED values. TODO change the order of the fields in the form if height is None: wt = weight mu = muac weight = mu height = wt case = self.find_case(ref_id) try: muac = float(muac) if muac < 30: # muac is in cm? muac *= 10 muac = int(muac) except ValueError: raise HandlerFailed((_i[lang] % ('MUAC', _i['units']['MUAC'], \ muac))) #_("Can't understand MUAC (mm): %s") % muac) if weight is not None: try: weight = float(weight) if weight > 100: # weight is in g? weight /= 1000.0 except ValueError: #raise HandlerFailed("Can't understand weight (kg): #%s" % weight) raise HandlerFailed((_i[lang] % ('weight', \ _i['units']['weight'], weight))) if height is not None: try: height = float(height) if height < 3: # weight height in m? height *= 100 height = int(height) except ValueError: #raise HandlerFailed("Can't understand height (cm): # %s" % height) raise HandlerFailed((_i[lang] % ('height', \ _i['units']['height'], height))) observed, choices = self.get_observations(complications) self.delete_similar(case.reportmalnutrition_set) reporter = message.persistant_connection.reporter report = ReportMalnutrition(case=case, reporter=reporter, muac=muac, weight=weight, height=height) report.save() for obs in observed: report.observed.add(obs) report.diagnose() report.save() #choice_term = dict(choices) info = case.get_dictionary() info.update(report.get_dictionary()) msg = _("%(diagnosis_msg)s. +%(ref_id)s %(last_name)s, "\ "%(first_name_short)s, %(gender)s/%(months)s (%(guardian)s). "\ "MUAC %(muac)s") % info if weight: msg += ", %.1f kg" % weight if height: msg += ", %.1d cm" % height if observed: msg += ', ' + info['observed'] #get the last reported muac b4 this one last_muac = report.get_last_muac() if last_muac is not None: psign = "%" #take care for cases when testing using httptester, % #sign prevents feedback. if PersistantBackend.from_message(message).title == "http": psign = "%" last_muac.update({'psign': psign}) msg += _(". Last MUAC (%(reported_date)s): %(muac)s "\ "(%(percentage)s%(psign)s)") % last_muac msg = "MUAC> " + msg if len(msg) > self.MAX_MSG_LEN: message.respond(msg[:msg.rfind('. ') + 1]) message.respond(msg[msg.rfind('. ') + 1:]) else: message.respond(msg) if report.status in (report.MODERATE_STATUS, report.SEVERE_STATUS, report.SEVERE_COMP_STATUS): alert = _("@%(username)s reports %(msg)s [%(mobile)s]")\ % {'username': report.reporter.alias, 'msg': msg, \ 'mobile': reporter.connection().identity} recipients = report.get_alert_recipients() for recipient in recipients: if len(alert) > self.MAX_MSG_LEN: message.forward(recipient.connection().identity, \ alert[:alert.rfind('. ') + 1]) message.forward(recipient.connection().identity, \ alert[alert.rfind('. ') + 1:]) else: message.forward(recipient.connection().identity, alert) log(case, 'muac_taken') return True