Exemplo n.º 1
0
    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
Exemplo n.º 2
0
 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
Exemplo n.º 3
0
 def reset_password(email):
     try:
         user, otp, token = MchSecurity.get_otp(email)
         #print "OTP: ",  otp
         sent = Enduser.send_message(
             user.telephone,
             "Your OTP to change password , now, is: %s" % otp)
         return (token, sent)
     except Exception, e:
         print e
Exemplo n.º 4
0
    def group_messaging(self):
        cnds = self.navb.conditions()
        message = ''
        if self.navb.kw.get("send") and self.navb.kw.get('text'):
            text = self.navb.kw.get('text')
            group = self.navb.kw.get('group')
            cnds.update({'is_active': ''})
            if group and group != 'all': cnds.update({'role_pk = %s': group})
            users = Enduser.get_users_cols(cnds, cols=['telephone'])
            unsents = []
            bulk_sms_data = {
                "message": text,
                "to_group": group,
                "user_phone": self.navb.user.telephone,
                "user_pk": self.navb.user.indexcol,
                "nation_pk": self.navb.user.nation_pk,
                "province_pk": self.navb.kw.get('province'),
                "district_pk": self.navb.kw.get('district'),
                "referral_facility_pk": self.navb.kw.get('hd'),
                "facility_pk": self.navb.kw.get('hc'),
                "sector_pk": self.navb.kw.get('sector'),
                "cell_pk": self.navb.kw.get('cell'),
                "village_pk": self.navb.kw.get('village')
            }
            saved = Enduser.bulk_messaging(bulk_sms_data)

            for user in users:
                try:
                    #print user.telephone, text
                    sent = Enduser.send_message(user.telephone, text)
                except Exception, e:
                    unsents.append(user.telephone)
                    continue

            if len(unsents) > 0:
                message = ','.join(u for u in unsents)
                message = 'The message has not been delivered to: %s' % message
            else:
                message = 'The message you sent has been delivered successfully. Thanks.'

            #print "\nRESPONSE: ", message, "\n", cnds, "\n"
            self.navb.kw = {}
Exemplo n.º 5
0
    def notify_level(self, level_code, role_code, location_pk):
        """ level codes :  'NATION', 'PRV', 'DST', 'NRH', 'MH', 'HD', 'HP', 'HC', 'CL', 'SEC', 'CEL', 'VIL' """
        try:
            unsent = []
            users = []
            if level_code and role_code and location_pk:
                #print "HERE", level_code, role_code, location_pk
                users = fetch_users_per_level_and_role(level_code=level_code,
                                                       role_code=role_code,
                                                       pk=location_pk)
            elif level_code and location_pk:
                #print "HERE", level_code, location_pk
                users = fetch_users_per_level(level_code=level_code,
                                              pk=location_pk)
            else:
                #print "HERE", level_code
                users = fetch_users_per_level(level_code=level_code)
            print[(u.telephone, u.role_name, u.facility_name) for u in users]
            for u in users:
                try:
                    #print u.__dict__
                    message = self.format_message(u)
                    sent = False
                    if level_code == 'HQ':
                        print "Destination %s, Email: %s" % (u.email, message)
                        sent = Enduser.send_email(self.ntype, email, message)
                    else:
                        print "Destination %s, SMS: %s" % (u.telephone,
                                                           message)
                        sent = Enduser.send_message(u.telephone, message)

                    if not sent:
                        unsent.append(u.telephone)
                except Exception, e:
                    print "SEND MESSAGE TO USER: "******"UNSENT : ", unsent
            return True
Exemplo n.º 6
0
    def notify_level_per_privilege(self,
                                   level_code=None,
                                   role_code=None,
                                   privilege_code=None,
                                   location_pk=None,
                                   chw_pk=None,
                                   sms=True):
        u = None
        try:
            users_ids = []
            filters = {}
            #print level_code, privilege_code
            if level_code:
                level = fetch_location_level(level_code)
                if level:
                    filters.update({'location_level_pk = %s': level.indexcol})

            if role_code:
                role = fetch_role(role_code)
                if role: filters.update({'role_pk = %s': role.indexcol})

            if location_pk:
                if level_code == 'NATION':
                    filters.update({'nation_pk = %s': location_pk})
                if level_code == 'PRV':
                    filters.update({'province_pk = %s': location_pk})
                if level_code == 'DST':
                    filters.update({'district_pk = %s': location_pk})
                if level_code == 'NRH':
                    filters.update({'facility_pk = %s': location_pk})
                if level_code == 'MH':
                    filters.update({'facility_pk = %s': location_pk})
                if level_code == 'HD':
                    filters.update({'referral_facility_pk = %s': location_pk})
                if level_code == 'HP':
                    filters.update({'facility_pk = %s': location_pk})
                if level_code == 'HC':
                    filters.update({'facility_pk = %s': location_pk})
                if level_code == 'CL':
                    filters.update({'facility_pk = %s': location_pk})
                if level_code == 'SEC':
                    filters.update({'sector_pk = %s': location_pk})
                if level_code == 'CEL':
                    filters.update({'cell_pk = %s': location_pk})
                if level_code == 'VIL':
                    filters.update({'village_pk = %s': location_pk})

            if chw_pk:
                filters.update({'indexcol = %s': chw_pk})

            if privilege_code:
                privilege = fetch_privilege(privilege_code)
                users_ids = fetch_users_ids_with_privilege(
                    filters={'privilege_pk = %s': privilege.indexcol})

            unsent = []
            users = fetch_users(filters=filters)

            for u in users:
                #print [ x.__dict__ for x in users_ids], [u.indexcol for u in users]
                if u.indexcol in [x.user_pk for x in users_ids]:
                    try:
                        #print self.message, u.indexcol, u.telephone
                        message = self.format_message(u)
                        sent = False
                        if not sms:
                            #print "Destination %s, Email: %s" % (u.email, message)
                            sent = Enduser.send_email(self.ntype, email,
                                                      message)
                        else:
                            #print "Destination %s, SMS: %s" % (u.telephone, message)
                            sent = Enduser.send_message(u.telephone, message)

                        if not sent:
                            unsent.append(u.telephone)
                    except Exception, ex:
                        print "SEND MESSAGE TO USER: %s" % ex, self.chw.sector_name
                        continue

                else:
                    continue
            return True