def handle(self): words = self.args if len(words) < 2 or len(words) > 3: self.respond(REGISTER_HELP) return name = words[0] code = words[1] params = { "msd_code": code } if not self.user: domains = [config.domain for config in ILSGatewayConfig.get_all_configs()] for domain in domains: loc = self._get_facility_location(domain, code) if not loc: continue splited_name = name.split(' ', 1) first_name = splited_name[0] last_name = splited_name[1] if len(splited_name) > 1 else "" clean_name = name.replace(' ', '.') username = "******" % (clean_name, domain) password = User.objects.make_random_password() user = CommCareUser.create(domain=domain, username=username, password=password, commit=False) user.first_name = first_name user.last_name = last_name if len(words) == 3: user.user_data = { 'role': words[2] } try: user.set_default_phone_number(self.msg.phone_number.replace('', '')) user.save_verified_number(domain, self.msg.phone_number.replace('', ''), True, self.msg.backend_api) except PhoneNumberInUseException as e: v = VerifiedNumber.by_phone(self.msg.phone_number, include_pending=True) v.delete() user.save_verified_number(domain, self.msg.phone_number.replace('', ''), True, self.msg.backend_api) except CommCareUser.Inconsistent: continue user.language = Languages.DEFAULT params.update({ 'sdp_name': loc.name, 'contact_name': name }) dm = user.get_domain_membership(domain) dm.location_id = loc._id user.save() add_location(user, loc._id) self.respond(REGISTRATION_CONFIRM, **params)
def sms_user_sync(self, ilsgateway_smsuser, **kwargs): from custom.logistics.commtrack import add_location sms_user = super(ILSGatewayAPI, self).sms_user_sync(ilsgateway_smsuser, **kwargs) if not sms_user: return None sp = SupplyPointCase.view('hqcase/by_domain_external_id', key=[self.domain, str(ilsgateway_smsuser.supply_point)], reduce=False, include_docs=True, limit=1).first() location_id = sp.location_id if sp else None dm = sms_user.get_domain_membership(self.domain) dm.location_id = location_id sms_user.save() add_location(sms_user, location_id) return sms_user
def sms_user_sync(self, ews_smsuser, **kwargs): sms_user = super(EWSApi, self).sms_user_sync(ews_smsuser, **kwargs) if not sms_user: return None sms_user.user_data['to'] = ews_smsuser.to if ews_smsuser.supply_point: if ews_smsuser.supply_point.id: sp = SupplyPointCase.view('hqcase/by_domain_external_id', key=[self.domain, str(ews_smsuser.supply_point.id)], reduce=False, include_docs=True, limit=1).first() else: sp = None if sp: couch_location_id = sp.location_id elif ews_smsuser.supply_point.location_id: try: location = SQLLocation.objects.get(domain=self.domain, external_id=ews_smsuser.supply_point.location_id) couch_location_id = location.location_id except SQLLocation.DoesNotExist: couch_location_id = None else: couch_location_id = None if couch_location_id: dm = sms_user.get_domain_membership(self.domain) dm.location_id = couch_location_id sms_user.save() add_location(sms_user, couch_location_id) if ews_smsuser.role == 'facility_manager': role = UserRole.by_domain_and_name(self.domain, 'Facility manager') if role: dm = sms_user.get_domain_membership(self.domain) dm.role_id = role[0].get_id sms_user.save() return sms_user
def handle(self): text = ' '.join(self.msg.text.split()[1:]) is_district = False sp = "" msd_code = "" if text.find(self.DISTRICT_REG_DELIMITER) != -1: phrases = [x.strip() for x in text.split(":")] if len(phrases) != 2: self.respond(REGISTER_HELP) return name = phrases[0] sp = phrases[1] role = Roles.DISTRICT_PHARMACIST message = REGISTRATION_CONFIRM_DISTRICT params = {} is_district = True else: names = [] msd_codes = [] location_regex = '^({prefs})\d+'.format(prefs='|'.join(p.lower() for p in DISTRICT_PREFIXES)) for the_string in self.args: if re.match(location_regex, the_string.strip().lower()): msd_codes.append(the_string.strip().lower()) else: names.append(the_string) name = " ".join(names) if len(msd_codes) != 1: self.respond(REGISTER_HELP) return else: [msd_code] = msd_codes role = Roles.IN_CHARGE message = REGISTRATION_CONFIRM params = { "msd_code": msd_code } if not self.user: domains = [config.domain for config in ILSGatewayConfig.get_all_configs()] for domain in domains: if is_district: loc = self._get_district_location(domain, sp) else: loc = self._get_facility_location(domain, msd_code) if not loc: continue splited_name = name.split(' ', 1) first_name = splited_name[0] last_name = splited_name[1] if len(splited_name) > 1 else "" clean_name = name.replace(' ', '.') username = "******" % (clean_name, domain) password = User.objects.make_random_password() user = CommCareUser.create(domain=domain, username=username, password=password, commit=False) user.first_name = first_name user.last_name = last_name try: user.set_default_phone_number(self.msg.phone_number.replace('+', '')) user.save_verified_number(domain, self.msg.phone_number.replace('+', ''), True, self.msg.backend_api) except PhoneNumberInUseException as e: v = VerifiedNumber.by_phone(self.msg.phone_number, include_pending=True) v.delete() user.save_verified_number(domain, self.msg.phone_number.replace('+', ''), True, self.msg.backend_api) except CommCareUser.Inconsistent: continue user.language = Languages.DEFAULT params.update({ 'sdp_name': loc.name, 'contact_name': name }) user.user_data = { 'role': role } dm = user.get_domain_membership(domain) dm.location_id = loc._id user.save() add_location(user, loc._id) if params: self.respond(message, **params)
def handle(self): text = ' '.join(self.msg.text.split()[1:]) is_district = False sp = "" msd_code = "" if text.find(self.DISTRICT_REG_DELIMITER) != -1: phrases = [x.strip() for x in text.split(":")] if len(phrases) != 2: self.respond(REGISTER_HELP) return name = phrases[0] sp = phrases[1] role = Roles.DISTRICT_PHARMACIST message = REGISTRATION_CONFIRM_DISTRICT params = {} is_district = True else: names = [] msd_codes = [] location_regex = '^({prefs})\d+'.format(prefs='|'.join( p.lower() for p in DISTRICT_PREFIXES)) for the_string in self.args: if re.match(location_regex, the_string.strip().lower()): msd_codes.append(the_string.strip().lower()) else: names.append(the_string) name = " ".join(names) if len(msd_codes) != 1: self.respond(REGISTER_HELP) return else: [msd_code] = msd_codes role = Roles.IN_CHARGE message = REGISTRATION_CONFIRM params = {"msd_code": msd_code} if not self.user: domains = [ config.domain for config in ILSGatewayConfig.get_all_configs() ] for domain in domains: if is_district: loc = self._get_district_location(domain, sp) else: loc = self._get_facility_location(domain, msd_code) if not loc: continue splited_name = name.split(' ', 1) first_name = splited_name[0] last_name = splited_name[1] if len(splited_name) > 1 else "" clean_name = name.replace(' ', '.') username = "******" % (clean_name, domain) password = User.objects.make_random_password() user = CommCareUser.create(domain=domain, username=username, password=password, commit=False) user.first_name = first_name user.last_name = last_name try: user.set_default_phone_number( self.msg.phone_number.replace('+', '')) user.save_verified_number( domain, self.msg.phone_number.replace('+', ''), True, self.msg.backend_api) except PhoneNumberInUseException as e: v = VerifiedNumber.by_phone(self.msg.phone_number, include_pending=True) v.delete() user.save_verified_number( domain, self.msg.phone_number.replace('+', ''), True, self.msg.backend_api) except CommCareUser.Inconsistent: continue user.language = Languages.DEFAULT params.update({'sdp_name': loc.name, 'contact_name': name}) user.user_data = {'role': role} dm = user.get_domain_membership(domain) dm.location_id = loc._id user.save() add_location(user, loc._id) if params: self.respond(message, **params)