def run(): print "starting" from hq.models import ExtUser, ReporterProfile from reporters.models import Reporter, PersistantBackend, PersistantConnection all_users = ExtUser.objects.all() for user in all_users: print "processing user %s" % user rep = user.reporter if rep: print "%s already has attached reporter object! %s" % (user, reporter) else: rep = Reporter() # if they have a first and last name set, use those, # otherwise just use the login if user.first_name and user.last_name: alias, fn, ln = Reporter.parse_name( "%s %s" % (user.first_name, user.last_name)) else: alias, fn, ln = Reporter.parse_name(user.username) print "Chose alias: %s first last: %s %s" % (alias, fn, ln) rep.first_name = fn rep.last_name = ln rep.alias = alias rep.save() profile = ReporterProfile() profile.reporter = rep profile.chw_id = user.chw_id profile.chw_username = user.chw_username profile.domain = user.domain profile.save() print "Saved profile %s for %s" % (profile, user) if user.primary_phone: # create a backend / connection for them. This is # still a little hazy as it's not clear how the # backend is properly chosen # this will create an arbitrary backend if none is # found if len(PersistantBackend.objects.all()) == 0: PersistantBackend.objects.create( slug="data_migration", title="Data Migration Backend") backend = PersistantBackend.objects.all()[0] try: conn = PersistantConnection.objects.create( backend=backend, identity=user.primary_phone, reporter=rep) print "created connection %s for %s" % (conn, user) except Exception, e: print "Error creating connection for %s for number %s. Is it possible you have duplicate phone numbers?" % ( user, user.primary_phone)
def run(): print "starting" from hq.models import ExtUser, ReporterProfile from reporters.models import Reporter, PersistantBackend, PersistantConnection all_users = ExtUser.objects.all() for user in all_users: print "processing user %s" % user rep = user.reporter if rep: print "%s already has attached reporter object! %s" % (user, reporter) else: rep = Reporter() # if they have a first and last name set, use those, # otherwise just use the login if user.first_name and user.last_name: alias, fn, ln = Reporter.parse_name("%s %s" % (user.first_name, user.last_name)) else: alias, fn, ln = Reporter.parse_name(user.username) print "Chose alias: %s first last: %s %s" % (alias, fn, ln) rep.first_name = fn rep.last_name = ln rep.alias = alias rep.save() profile = ReporterProfile() profile.reporter = rep profile.chw_id = user.chw_id profile.chw_username = user.chw_username profile.domain = user.domain profile.save() print "Saved profile %s for %s" % (profile, user) if user.primary_phone: # create a backend / connection for them. This is # still a little hazy as it's not clear how the # backend is properly chosen # this will create an arbitrary backend if none is # found if len(PersistantBackend.objects.all()) == 0: PersistantBackend.objects.create(slug="data_migration", title="Data Migration Backend") backend = PersistantBackend.objects.all()[0] try: conn = PersistantConnection.objects.create(backend=backend, identity=user.primary_phone, reporter=rep) print "created connection %s for %s" % (conn, user) except Exception, e: print "Error creating connection for %s for number %s. Is it possible you have duplicate phone numbers?" % (user, user.primary_phone)
def handle_register(self, message, msg_text): connection, unused = getConnectionAndReporter(message, self.ALLOWED_ROLE_CODES) text = msg_text.strip() if text == u'': message.respond(self.HELP_MESSAGES[u'register']) return try: location_code, role_code, full_name = grammar(text).register() except parsley.ParseError: message.respond(self.ERROR_MESSAGES[u'invalid_message'] % {u'text': message.text}) return location = Location.get_by_code(location_code) if location is None: message.respond(self.ERROR_MESSAGES[u'invalid_location'] % { u'location_code': location_code, u'text': message.text }) return role = Role.get_by_code(role_code) if role is None or role.code.lower() not in self.ALLOWED_ROLE_CODES: message.respond(self.ERROR_MESSAGES[u'invalid_role'] % { u'role_code': role_code, u'text': message.text }) return kwargs = {u'location': location, u'role': role} kwargs[u'alias'], kwargs[u'first_name'], kwargs[ u'last_name'] = Reporter.parse_name(full_name) rep = Reporter(**kwargs) if Reporter.exists(rep, connection): message.respond( self.RESPONSE_MESSAGES[u'already_registered'] % { u'name': rep.first_name, u'role': rep.role.name, u'location': rep.location.name, u'location_type': rep.location.type.name }) return rep.save() connection.reporters.add(rep) message.respond( self.RESPONSE_MESSAGES[u'register'] % { u'name': rep.first_name, u'role': rep.role.code, u'location': rep.location.name, u'location_type': rep.location.type.name })
def autoregister_reporters(domain): """Scan the metadata to see all submissions and autoregister Reporter and ReporterProfile for pending approval for the domain""" # for a given domain, get all the formdefs fdefs = FormDefModel.objects.filter(domain=domain) # for all those formdefs, scan the metadata for parsed submissions allmetas_for_domain = Metadata.objects.filter(formdefmodel__in=fdefs) username_tuples = allmetas_for_domain.values_list("username").distinct() # the values_list returns a list of tuples, so we need to flip it into a list # from [(),()...] to [...] usernames = [] for uname in username_tuples: usernames.append(uname[0]) seen_profiles = ReporterProfile.objects.filter(domain=domain).filter(chw_username__in=usernames) new_profiles = [] for uname in usernames: if seen_profiles.filter(chw_username=uname).count() == 0: new_profiles.append(uname) for prof in new_profiles: # create a new ReporterProfile chw_id = allmetas_for_domain.filter(username=prof)[0].chw_id newProf = ReporterProfile( chw_id=chw_id, chw_username=prof, domain=domain, guid=str(uuid.uuid1()).replace("-", "") ) # create a new Reporter rep = Reporter() alias, fn, ln = Reporter.parse_name("%s %s" % ("chw", prof)) rep.first_name = fn rep.last_name = ln rep.alias = alias rep.save() newProf.reporter = rep newProf.save()
def register(self, message, location_code, role, name=''): conn, unused = getConnectionAndReporter(message, ALLOWED_ROLE_CODES) data = {} try: data['location'] = Location.objects.get(code=location_code, type__name=u'RC', active=True) data['role'] = Role.objects.get(code__iexact=role) data['alias'], data['first_name'], data[ 'last_name'] = Reporter.parse_name(name.strip()) rep = Reporter(**data) if Reporter.exists(rep, conn): message.respond(self.response_messages['already_registered'] % dict(name=rep.first_name, role=rep.role, location_name=rep.location)) return True rep.save() conn.reporters.add(rep) message.respond(self.response_messages['registered'] % dict(name=rep.first_name, role=rep.role, location_name=rep.location, location_type=rep.location.type)) except Role.DoesNotExist: message.respond(self.error_messages['invalid_role'] % dict(role_code=role, text=message.text)) except Location.DoesNotExist: message.respond( self.error_messages['invalid_location'] % dict(location_code=location_code, text=message.text)) return True
def handle(self, text): # extract the optional fields early, if they were # provided, to avoid them ending up in the name languages, text = extract_objects(text, [Language]) locations, text = extract_objects(text, [Location]) resp = "Thank you for registering" # REMOVE THE VILLAGE NAME! :O text = re.sub("\s+\S+$", "", text, re.I) # create the new reporter alias, fn, ln = Reporter.parse_name(text) rep = Reporter.objects.create( alias=alias, first_name=fn, last_name=ln, registered_self=True) # set the optional fields, if they were provided if languages: rep.language = languages[0] resp += " in %s" % (rep.language) if locations: rep.location = locations[0] resp += " at %s" % (rep.location) rep.save() # attach the reporter to the current connection self.msg.persistant_connection.reporter = rep self.msg.persistant_connection.save() self.respond("%s." % resp)
def join(self, message, location_code, last_name, first_name, role=None): ''' register as a user and join the system Format: join [location code] [last name] [first name] [role - leave blank for CHEW] ''' #default alias for everyone until further notice username = None # do not skip roles for now role_code = role try: name = "%(fname)s %(lname)s" % {'fname': first_name, \ 'lname': last_name} # parse the name, and create a reporter alias, fn, ln = Reporter.parse_name(name) if not message.persistant_connection.reporter: rep = Reporter(alias=alias, first_name=fn, last_name=ln) else: rep = message.persistant_connection.reporter rep.alias = alias rep.first_name = fn rep.last_name = ln rep.save() # attach the reporter to the current connection message.persistant_connection.reporter = rep message.persistant_connection.save() # something went wrong - at the # moment, we don't care what except: message.respond(_("Join Error. Unable to register your account.")) if role_code == None or role_code.__len__() < 1: role_code = Cfg.get('default_chw_role') reporter = message.persistant_connection.reporter # check location code try: location = Location.objects.get(code=location_code) except models.ObjectDoesNotExist: message.forward(reporter.connection().identity, \ _(u"Join Error. Provided location code (%(loc)s) is wrong.") \ % {'loc': location_code}) return True # check that location is a clinic (not sure about that) #if not clinic.type in LocationType.objects.filter(name='Clinic'): # message.forward(reporter.connection().identity, \ # _(u"Join Error. You must provide a Clinic code.")) # return True # set location reporter.location = location # check role code try: role = Role.objects.get(code=role_code) except models.ObjectDoesNotExist: message.forward(reporter.connection().identity, \ _(u"Join Error. Provided Role code (%(role)s) is wrong.") \ % {'role': role_code}) return True reporter.role = role # set account active # /!\ we use registered_self as active reporter.registered_self = True # save modifications reporter.save() # inform target message.forward(reporter.connection().identity, \ _("Success. You are now registered as %(role)s at %(loc)s with " \ "alias @%(alias)s.") \ % {'loc': location, 'role': reporter.role, 'alias': reporter.alias}) #inform admin if message.persistant_connection.reporter != reporter: message.respond(_("Success. %(reporter)s is now registered as " \ "%(role)s at %(loc)s with alias @%(alias)s.") \ % {'reporter': reporter, 'loc': location, \ 'role': reporter.role, 'alias': reporter.alias}) return True