def process(self): try: is_sm = False report = self.get_report() for s in self.symptoms: if s.upper() in KEYS.keys(): is_sm = True break if is_sm == False: return False #print "REPORT: %s" % report if self.report and not self.errors: filters = {} for k in self.report.keys(): filters.update({ k+' = %s' : self.report[k]}) old = filter_data(self.table, filters) if not ( old and old[0].created_at.date() == datetime.date.today()): notif = self.get_sm_notif() if notif and self.keyword == 'SMR': migrate(self.table, {'indexcol': notif.indexcol, 'result': True}) self.report.update({'notif': notif.indexcol}) migrate(self.table, self.report) notif = self.send_notifications() return True except Exception, e: print "ERROR: %s" % e pass
def get_or_create( data): try: simcard = Enduser.get_or_create_simcard(data.get('telephone')) if not simcard: return ( 'Bad Telephone', None) telephone = simcard.msisdn amb = Ambulance.get_ambulance(telephone) if not amb: data.update({"simcard_pk": simcard.indexcol}) data.update({"telephone": telephone}) #print "SAVE DATA: ", data migrate(Ambulance._table, data) amb = Ambulance.get_ambulance(telephone) sent = False sent = Enduser.send_message(telephone, "Twabamenyeshaga ko mumaze kwandikwa muri sisitemu ya RapidSMS Rwanda, nk'ukurikirana imbagukira gutabara, ikigo nderabuzima cya %(hc)s, ibitaro bya %(hd)s. Murakoze." % { 'hc': amb.facility_name, 'hd': amb.referral_facility_name }) sent = Enduser.send_message(telephone, "You have been registered in RapidSMS as ambulance coordinator, in %(hc)s health centre, %(hd)s district hospital. Thanks." % { 'hc': amb.facility_name, 'hd': amb.referral_facility_name }) if not sent: return ("Ambulance Created, but SMS not sent, please contact telephone: %(phone)s." % {'phone': amb.telephone}, amb) return ( 'Ambulance created', amb) return ( 'Ambulance exists', amb) except Exception, e: print e pass
def update_user(data): try: simcard = Enduser.get_or_create_simcard(data.get('telephone')) if not simcard: return ( 'Bad Telephone', None) telephone = simcard.msisdn old_amb = Ambulance.get_ambulance(telephone) ## Check ambulance registered for the telephone if not old_amb: return ( 'Ambulance does not exist with Telephone %s' % (telephone) , None) else: data.update({"indexcol": old_amb.indexcol}) data.update({"simcard_pk": simcard.indexcol}) data.update({"telephone": telephone}) #print "UPDATE DATA: ", data migrate(Ambulance._table, data) amb = Ambulance.get_ambulance(telephone) if amb: sent = Enduser.send_message(telephone, "Twabamenyeshaga ko mumaze gukosorwa imyirondoro muri sisitemu ya RapidSMS Rwanda, nk'ukurikirana imbagukira gutabara, ikigo nderabuzima cya %(hc)s, ibitaro bya %(hd)s. Murakoze." % { 'hc': amb.facility_name, 'hd': amb.referral_facility_name }) sent = Enduser.send_message(telephone, "You have been updated in RapidSMS as ambulance coordinator, in %(hc)s health centre, %(hd)s district hospital. Thanks." % { 'hc': amb.facility_name, 'hd': amb.referral_facility_name }) if not sent: return ("Ambulance Updated, but SMS not sent, please contact telephone: %(phone)s." % {'phone': amb.telephone}, amb) return ( 'Ambulance Updated', amb) else: return ( 'Ambulance with telephone %s cannot be updated, contact system administrator.' % ( old_amb.telephone) , None) except Exception, e: print e pass
def update_user(data): try: simcard = Enduser.get_or_create_simcard(data.get('telephone')) if not simcard: return ( 'Bad Telephone',None) telephone = simcard.msisdn user = Enduser.get_active_user(telephone) #if nid is invalid return if len(data.get('national_id')) != 16: return ( 'Bad National ID',None) national_id = data.get('national_id') old_user = fetch_enduser(national_id, telephone) ## Check national_id registered for the telephone if not old_user: return ( 'User does not exist with NID %s and Telephone %s' % (national_id, telephone) , None) else: if (user and old_user) and old_user.indexcol != user.indexcol: return ( 'User with NID %s and Telephone %s is active, please deactive him/her first.' % ( user.national_id, user.telephone) , None) elif (old_user.indexcol == user.indexcol) or (old_user.indexcol == data.get('indexcol')): # He is the same user, check if credentials have been updated new_passwd = data.get('passwd') if old_user.email != data.get('email') or new_passwd: if new_passwd: generated_password = MchSecurity.generatedPassword(new_passwd) data.update({"salt" : generated_password[0]}) data.update({"passwd" : generated_password[1]}) else: new_passwd = simcard.msin generated_password = MchSecurity.generatedPassword(new_passwd) data.update({"salt" : generated_password[0]}) data.update({"passwd" : generated_password[1]}) data.update({"simcard_pk": simcard.indexcol}) data.update({"telephone": telephone}) #print "SAVE DATA: ", data migrate(Enduser._table, data) user = Enduser.get_active_user( data.get('telephone') ) sent = False if user.role_code == 'BINOME' or user.role_code == 'ASM' or user.language_code == 'RW': sent = Enduser.send_message(telephone, "Twabamenyeshaga ko mumaze gukosorwa kuri list, muri sisitemu ya RapidSMS Rwanda, nka %(role)s, ku rwego rw %(level)s, %(nat)s, Intara %(prv)s, Akarere ka %(dst)s, ibitaro bya %(hp)s ikigo nderabuzima cya %(hc)s, umurenge wa %(sec)s, akagari ka %(cel)s, umudugudu wa %(vil)s. Murakoze."% { 'role': user.role_name, 'hc': user.facility_name, 'sec': user.sector_name, 'cel': user.cell_name , 'vil': user.village_name, 'level': Enduser._levels.get(user.location_level_code)[1], 'dst': user.district_name, 'hp': user.referral_name, 'nat': user.nation_name, 'prv': user.province_name}) else: sent = Enduser.send_message(telephone, "You have been updated in RapidSMS, your username is: %(email)s, and password is: %(pwd)s. Thanks." % {'email': user.email, 'pwd': new_passwd}) if not sent: return ("User updated, but SMS not sent, please contact username: %(email)s, and password: %(pwd)s" % {'email': user.email, 'pwd': data.get('passwd')}, old_user) return ( 'User updated', old_user) else: return ( 'User with NID %s and Telephone %s cannot be updated, contact system administrator.' % ( old_user.national_id, old_user.telephone) , None) except Exception, e: print e pass
def get_or_create(data): try: code = data.get('code') role = Role.get_role(code) if not role: #print "SAVE DATA: ", data migrate(Role._table, data) role = Role.get_role(code) if not role: return ("Role has failed to be created, try again.", None) return ('Role created', role) return ('Role exists', role) except Exception, e: print e pass
def update_user_info(data): """ Not all info is update here except few supplied data info""" try: old_user = fetch_enduser(data.get('national_id'), data.get('telephone')) ## Check national_id registered for the telephone if not old_user: return ( 'User does not exist with NID %s and Telephone %s' % (data.get('national_id'), data.get('telephone')) , None) else: #print "UPDATE DATA: ", data data.update({'indexcol': old_user.indexcol}) migrate(Enduser._table, data) return ( 'User updated', Enduser.get_users({'indexcol = %s ': old_user.indexcol})[0]) except Exception, e: print e pass
def update_ambulance_info(data): """ Not all info is update here except few supplied data info""" try: old_amb = Ambulance.get_ambulance(data.get('telephone')) ## Check ambulance registered for the telephone if not old_amb: return ( 'Ambulance does not exist withtelephone %s' % ( data.get('telephone')) , None) else: #print "UPDATE DATA: ", data data.update({'indexcol': old_amb.indexcol}) migrate(Ambulance._table, data) return ( 'Ambulance updated', old_amb) except Exception, e: print e pass
def update_download_status(user_pk, filename, status="COMPLETE"): """ Not all info is update here except few supplied data info""" try: download = fetch_download(user_pk, filename) if not download: return ( 'Download does not exist with user_pk %s and filename %s' % (user_pk, filename), None) else: #print "UPDATE DATA: ", data data = {'indexcol': download.indexcol, 'status': status} migrate(Download._table, data) return ('Download updated', download) except Exception, e: print e pass
def update_privilege_info(data): """ Not all info is update here except few supplied data info""" try: old_privilege = Privilege.get_privilege(data.get('code')) ## Check privilege registered for the code if not old_privilege: return ('Privilege does not exist with code %s' % (data.get('code')), None) else: #print "UPDATE DATA: ", data data.update({'indexcol': old_privilege.indexcol}) migrate(Privilege._table, data) return ('Privilege updated', old_privilege) except Exception, e: print e pass
def get_or_create(data): try: code = data.get('code') privilege = Privilege.get_privilege(code) if not privilege: #print "SAVE DATA: ", data migrate(Privilege._table, data) privilege = Privilege.get_privilege(code) if not privilege: return ("Privilege has failed to be created, try again.", None) return ('Privilege created', privilege) return ('Privilege exists', privilege) except Exception, e: print e pass
def update_facility_info(data): """ Not all info is update here except few supplied data info""" try: old_fac = Facility.get_facility(data.get('code')) ## Check facility registered for the code if not old_fac: return ('Facility does not exist with code %s' % (data.get('code')), None) else: #print "UPDATE DATA: ", data data.update({'indexcol': old_fac.indexcol}) migrate(Facility._table, data) return ('Facility updated', old_fac) except Exception, e: print e pass
def bulk_messaging(data): try: saved = migrate('bulk_sms', data) return saved except Exception, e: print e pass
def get_or_create(data): try: code = data.get('code') fac = Facility.get_facility(code) if not fac: #print "SAVE DATA: ", data migrate(Facility._table, data) fac = Facility.get_facility(code) facs = fetch_facilities() if not fac: return ("Facility has failed to be created, try again.", None, facs) return ('Facility created', fac, facs) return ('Facility exists', fac, facs) except Exception, e: print e pass
def get_or_assign(data): try: pk = data.get('privilege_pk') user_pk = data.get('user_pk') role_pk = data.get('role_pk') privilege = Privilege.get_assigned_privilege(pk, user_pk, role_pk) if not privilege: #print "SAVE DATA: ", data migrate(Privilege._usertable, data) privilege = Privilege.get_assigned_privilege( pk, user_pk, role_pk) if not privilege: return ("Privilege has failed to be assigned, try again.", None) return ('Privilege assigned', privilege) return ('Privilege assignment exists', privilege) except Exception, e: print e pass
def get_or_create( data): try: simcard = Enduser.get_or_create_simcard(data.get('telephone')) if not simcard: return ( 'Bad Telephone',None) telephone = simcard.msisdn user = Enduser.get_active_user(telephone) if not user: if len(data.get('national_id')) != 16: return ( 'Bad National ID',None) if not data.get('email'): data.update({"email": '*****@*****.**' % simcard.indexcol}) data.update({"simcard_pk": simcard.indexcol}) data.update({"telephone": telephone}) generated_password = MchSecurity.generatedPassword(simcard.msin) data.update({"salt" : generated_password[0]}) data.update({"passwd" : generated_password[1]}) national_id = data.get('national_id') #print "SAVE DATA: ", data migrate(Enduser._table, data) #user = fetch_enduser(national_id, telephone) user = Enduser.get_active_user( telephone ) sent = False if user.role_code == 'BINOME' or user.role_code == 'ASM' or user.language_code == 'RW': sent = Enduser.send_message(telephone, "Twabamenyeshaga ko mumaze kwandikwa kuri list, muri sisitemu ya RapidSMS Rwanda, nka %(role)s, ku rwego rw %(level)s, %(nat)s, Intara %(prv)s, Akarere ka %(dst)s, ibitaro bya %(hp)s ikigo nderabuzima cya %(hc)s, umurenge wa %(sec)s, akagari ka %(cel)s, umudugudu wa %(vil)s. Murakoze."% { 'role': user.role_name, 'hc': user.facility_name, 'sec': user.sector_name, 'cel': user.cell_name , 'vil': user.village_name, 'level': Enduser._levels.get(user.location_level_code)[1], 'dst': user.district_name, 'hp': user.referral_name, 'nat': user.nation_name, 'prv': user.province_name}) else: sent = Enduser.send_message(telephone, "You have been registered in RapidSMS as %(role)s, your username is: %(email)s, and password is: %(pwd)s. Thanks." % {'role': user.role_name, 'email': user.email, 'pwd': simcard.msin}) email = Enduser.send_email("RapidSMS Credentials", user.email, "You have been registered in RapidSMS as %(role)s, your username is: %(email)s, and password is: %(pwd)s. Thanks." % {'role': user.role_name, 'email': user.email, 'pwd': simcard.msin}) if not sent: return ("User Created, but SMS not sent, please contact username: %(email)s, and password: %(pwd)s" % {'email': user.email, 'pwd': simcard.msin}, user) return ( 'User created', user) return ( 'User exists', user) except Exception, e: print e pass
def update_privilege(data): try: code = data.get('code') old_privilege = Privilege.get_privilege(code) ## Check privilege registered for the code if not old_privilege: return ('Privilege does not exist with code %s' % (code), None) else: data.update({"indexcol": old_privilege.indexcol}) #print "UPDATE DATA: ", data migrate(Privilege._table, data) privilege = Privilege.get_privilege(code) if privilege: return ('Privilege Updated', privilege) else: return ( 'Privilege with code %s cannot be updated, contact system administrator.' % (old_privilege.code), None) except Exception, e: print e pass
def update_role(data): try: code = data.get('code') old_role = Role.get_role(code) ## Check role registered for the code if not old_role: return ('Role does not exist with code %s' % (code), None) else: data.update({"indexcol": old_role.indexcol}) #print "UPDATE DATA: ", data migrate(Role._table, data) role = Role.get_role(code) if role: return ('Role Updated', role) else: return ( 'Role with code %s cannot be updated, contact system administrator.' % (old_role.code), None) except Exception, e: print e pass
def update_facility(data): try: code = data.get('code') old_fac = Facility.get_facility(code) ## Check facility registered for the code if not old_fac: return ('Facility does not exist with code %s' % (code), None) else: data.update({"indexcol": old_fac.indexcol}) #print "UPDATE DATA: ", data migrate(Facility._table, data) fac = Facility.get_facility(code) if fac: facs = fetch_facilities() return ('Facility Updated', fac, facs) else: return ( 'Facility with code %s cannot be updated, contact system administrator.' % (old_fac.code), None, []) except Exception, e: print e pass
def process(self): try: is_st = False report = self.get_report() for s in self.drugs: if s.upper() in DRUGS.keys(): is_st = True break if is_st == False: return False #print "REPORT: %s" % report if self.report and not self.errors: filters = {} for k in self.report.keys(): filters.update({k + ' = %s': self.report[k]}) old = filter_data(self.table, filters) if not old: migrate(self.table, self.report) notif = self.send_notifications() return True except Exception, e: print "ERROR: %s" % e pass
def save(self, user=None): """ Save a enderror object and return indexcol """ try: FIELDS = self.__dict__ if not user: user = Enduser.get_active_user(self.user_phone) if user: FIELDS.update({ "user_pk": user.indexcol, "nation_pk": user.nation_pk, "province_pk": user.province_pk, "district_pk": user.district_pk, "referral_facility_pk": user.referral_facility_pk, "facility_pk": user.facility_pk, "sector_pk": user.facility_pk, "cell_pk": user.facility_pk, "village_pk": user.facility_pk, }) err = migrate(Enderror._table, FIELDS) return err except Exception, e: print e raise MchCriticalError(Exception('Enderror cannot be saved'))
def get_or_create_simcard( phone): try: msin = phone[len(phone)-7: ] if len(msin) != 7: return None mnc = phone[len(phone)-9:len(phone)-7] mcc = '250' spn = '' if mnc == '78': spn = 'MTN' if mnc == '73': spn = 'AIRTEL' if mnc == '72': spn = 'TIGO' msisdn = '+%(mcc)s%(mnc)s%(msin)s' % {'mcc': mcc, 'mnc': mnc, 'msin': msin} simcard = fetch_simcard(msisdn) indexcol = None if simcard: indexcol = simcard.indexcol #print {'indexcol' : indexcol, 'phone': phone, 'mcc': mcc, 'mnc': mnc, 'msin': msin, 'msisdn': msisdn, 'spn': spn} if spn: if not simcard: saved = migrate('simcard', {'indexcol' : indexcol, 'mcc': mcc, 'mnc': mnc, 'msin': msin, 'msisdn': msisdn, 'spn': spn}) if saved: simcard = fetch_simcard(msisdn) return simcard except Exception, e: print e pass
def save(self, user=None, FIELDS={}): """ Save a download object and return indexcol """ try: if user: FIELDS.update({ "user_pk": user.indexcol, "user_phone": user.telephone, "nation_pk": user.nation_pk, "province_pk": user.province_pk, "district_pk": user.district_pk, "referral_facility_pk": user.referral_facility_pk, "facility_pk": user.facility_pk, "sector_pk": user.facility_pk, "cell_pk": user.facility_pk, "village_pk": user.facility_pk, }) print "FIELDS: ", FIELDS dwn = migrate(Download._table, FIELDS) return dwn else: raise MchCriticalError(Exception('Download cannot be saved')) except Exception, e: print e raise MchCriticalError(Exception('Download cannot be saved'))
def update_report(data): try: p = migrate(Malaria._table, data) return True except Exception, e: pass