def handle(self, text): tokens = self.check_message_valid_and_clean(text) if not tokens: return clinic_code = LocationCode(tokens[0]) name = tokens[2] pin = tokens[4] worker_type = clinic_code.get_worker_type() location_type = clinic_code.get_location_type() if is_already_valid_connection_type(self.msg.connection, worker_type): # refuse re-registration if they're still active and eligible self.respond(self.ALREADY_REGISTERED, name=self.msg.connection.contact.name, location=self.msg.connection.contact.location) return False try: location = Location.objects.get(slug__iexact=clinic_code.slug, type__slug__in=location_type) if self.msg.connection.contact is not None \ and self.msg.connection.contact.is_active: # this means they were already registered and active, but not yet # receiving results. clinic = get_clinic_or_default(self.msg.connection.contact) if clinic != location: self.respond(self.ALREADY_REGISTERED, name=self.msg.connection.contact.name, location=clinic) return True else: contact = self.msg.contact else: contact = Contact(location=location) clinic = get_clinic_or_default(contact) contact.name = name contact.pin = pin contact.save() contact.types.add(worker_type) self.msg.connection.contact = contact self.msg.connection.save() self.respond( "Hi %(name)s, thanks for registering for " "Results160 from %(location)s. " "Your PIN is %(pin)s. " "Reply with keyword 'HELP' if this is " "incorrect", name=contact.name, location=clinic.name, pin=pin) except Location.DoesNotExist: self.respond( "Sorry, I don't know about a location with code %(code)s. Please check your code and try again.", code=clinic_code)
def handle(self, text): tokens = self.check_message_valid_and_clean(text) if not tokens: return clinic_code = LocationCode(tokens[0]) name = tokens[2] pin = tokens[4] worker_type = clinic_code.get_worker_type() location_type = clinic_code.get_location_type() if is_already_valid_connection_type(self.msg.connection, worker_type): # refuse re-registration if they're still active and eligible self.respond(self.ALREADY_REGISTERED, name=self.msg.connection.contact.name, location=self.msg.connection.contact.location) return False try: location = Location.objects.get(slug__iexact=clinic_code.slug, type__slug__in=location_type) if self.msg.connection.contact is not None \ and self.msg.connection.contact.is_active: # this means they were already registered and active, but not yet # receiving results. clinic = get_clinic_or_default(self.msg.connection.contact) if clinic != location: self.respond(self.ALREADY_REGISTERED, name=self.msg.connection.contact.name, location=clinic) return True else: contact = self.msg.contact else: contact = Contact(location=location) clinic = get_clinic_or_default(contact) contact.name = name contact.pin = pin contact.save() contact.types.add(worker_type) self.msg.connection.contact = contact self.msg.connection.save() self.respond("Hi %(name)s, thanks for registering for " "Results160 from %(location)s. " "Your PIN is %(pin)s. " "Reply with keyword 'HELP' if this is " "incorrect", name=contact.name, location=clinic.name, pin=pin) except Location.DoesNotExist: self.respond("Sorry, I don't know about a location with code %(code)s. Please check your code and try again.", code=clinic_code)
def populate_user(row): for k, v in row.items(): if v == '': del row[k] NON_REQUIRED_FIELDS = ['email', 'supervisor', 'phone'] for k, v in row.iteritems(): if v is None and k not in NON_REQUIRED_FIELDS: print '%s is required' % k return try: User.objects.get(username=row['username']) print 'user %s already exists' % row['username'] return except User.DoesNotExist: pass ALLOWED_STATES = ('fct', 'nasawara', 'national') if not row['state'] in ALLOWED_STATES: print 'state must be one of: %s' % ', '.join(ALLOWED_STATES) return ALLOWED_PROGRAMS = ('pbf', 'fadama', 'both') if not row['program'] in ALLOWED_PROGRAMS: print 'program must be one of: %s' % ', '.join(ALLOWED_PROGRAMS) return PROGRAM_PERMS = { 'pbf': 'pbf_view', 'fadama': 'fadama_view', } perms = [] if row['program'] == 'both': perms.extend(PROGRAM_PERMS.values()) else: perms.append(PROGRAM_PERMS[row['program']]) is_supervisor = (row.get('supervisor', '').lower() in ('y', 'yes', 'x')) if is_supervisor: perms.append('supervisor') def add_perm(u, perm_name): u.user_permissions.add(Permission.objects.get(codename=perm_name)) u = User() u.username = row['username'] u.first_name = row['first name'] u.last_name = row['last name'] u.email = row.get('email', '*****@*****.**') u.set_password(row['password']) u.save() for p in perms: add_perm(u, p) u.save() try: contact = Contact.objects.get(user__username=row['username']) return except Contact.DoesNotExist: pass if row['state'] == 'national': loc = Location.objects.get(slug='nigeria') else: loc = Location.objects.get(type__slug='state', slug=row['state']) c = Contact() c.name = '%s %s' % (u.first_name, u.last_name) c.first_name = u.first_name c.last_name = u.last_name c.email = row.get('email', '') c.user = u c.location = loc c.save() backend = Backend.objects.get(name='httptester') if row.get('phone'): conn = Connection() conn.backend = backend conn.identity = row['phone'] conn.contact = c conn.save()
def handle(self, text): b = InputCleaner() if is_eligible_for_results(self.msg.connection): # refuse re-registration if they're still active and eligible self.respond(self.ALREADY_REGISTERED, name=self.msg.connection.contact.name, location=self.msg.connection.contact.location) return text = text.strip() text = b.remove_double_spaces(text) if len(text) < (self.PIN_LENGTH + self.MIN_CLINIC_CODE_LENGTH + self.MIN_NAME_LENGTH + 1): self.mulformed_msg_help() return #signed pin if text[-5:-4] == '-' or text[-5:-4] == '+': self.invalid_pin(text[-5:]) return #too long pin if ' ' in text and text[1 + text.rindex(' '):].isdigit() and len(text[1 + text.rindex(' '):]) > self.PIN_LENGTH: self.invalid_pin(text[1 + text.rindex(' '):]) return #non-white space before pin if text[-5:-4] != ' ' and text[-4:-3] != ' ': self.respond("Sorry, you should put a space before your pin. " "Please make sure your code is a %s-digit number like %s. " "Send JOIN <CLINIC CODE> <YOUR NAME> <SECURITY CODE>." % ( self.PIN_LENGTH, ''.join(str(i) for i in range(1, int(self.PIN_LENGTH) + 1)))) return #reject invalid pin user_pin = text[-4:] if not user_pin: self.help() return elif len(user_pin) < 4: self.invalid_pin(user_pin) return elif not user_pin.isdigit(): self.invalid_pin(user_pin) return group = self.PATTERN.search(text) if group is None: self.mulformed_msg_help() return tokens = group.groups() if not tokens: self.mulformed_msg_help() return clinic_code = tokens[0].strip() clinic_code = b.try_replace_oil_with_011(clinic_code) #we expect all codes have format PPDDFF or PPDDFFS clinic_code = clinic_code[0:6] name = tokens[2] name = name.title().strip() pin = tokens[4].strip() if len(pin) != self.PIN_LENGTH: self.respond(self.INVALID_PIN) return if not name: self.respond("Sorry, you must provide a name to register. %s" % self.HELP_TEXT) return elif len(name) < self.MIN_NAME_LENGTH: self.respond("Sorry, you must provide a valid name to register. %s" % self.HELP_TEXT) return try: location = Location.objects.get(slug__iexact=clinic_code, type__slug__in=const.CLINIC_SLUGS) if self.msg.connection.contact is not None \ and self.msg.connection.contact.is_active: # this means they were already registered and active, but not yet # receiving results. clinic = get_clinic_or_default(self.msg.connection.contact) if clinic != location: self.respond(self.ALREADY_REGISTERED, name=self.msg.connection.contact.name, location=clinic) return True else: contact = self.msg.contact else: contact = Contact(location=location) clinic = get_clinic_or_default(contact) contact.name = name contact.pin = pin contact.save() contact.types.add(const.get_clinic_worker_type()) self.msg.connection.contact = contact self.msg.connection.save() self.respond("Hi %(name)s, thanks for registering for " "Results160 from %(location)s. " "Your PIN is %(pin)s. " "Reply with keyword 'HELP' if this is " "incorrect", name=contact.name, location=clinic.name, pin=pin) except Location.DoesNotExist: self.respond("Sorry, I don't know about a location with code %(code)s. Please check your code and try again.", code=clinic_code)
def register_user(request, template="malawi/register-user.html"): context = dict() context['facilities'] = SupplyPoint.objects.filter(type__code="hf").order_by('code') context['backends'] = Backend.objects.all() context['dialing_code'] = settings.COUNTRY_DIALLING_CODE # [sic] if request.method != 'POST': return render_to_response(template, context, context_instance=RequestContext(request)) id = request.POST.get("id", None) facility = request.POST.get("facility", None) name = request.POST.get("name", None) number = request.POST.get("number", None) backend = request.POST.get("backend", None) if not (id and facility and name and number and backend): messages.error(request, "All fields must be filled in.") return render_to_response(template, context, context_instance=RequestContext(request)) hsa_id = None try: hsa_id = format_id(facility, id) except IdFormatException: messages.error(request, "HSA ID must be a number between 0 and 99.") return render_to_response(template, context, context_instance=RequestContext(request)) try: parent = SupplyPoint.objects.get(code=facility) except SupplyPoint.DoesNotExist: messages.error(request, "No facility with that ID.") return render_to_response(template, context, context_instance=RequestContext(request)) if Location.objects.filter(code=hsa_id).exists(): messages.error(request, "HSA with that code already exists.") return render_to_response(template, context, context_instance=RequestContext(request)) try: number = int(number) except ValueError: messages.error(request, "Phone number must contain only numbers.") return render_to_response(template, context, context_instance=RequestContext(request)) hsa_loc = Location.objects.create(name=name, type=config.hsa_location_type(), code=hsa_id, parent=parent.location) sp = SupplyPoint.objects.create(name=name, code=hsa_id, type=config.hsa_supply_point_type(), location=hsa_loc, supplied_by=parent, active=True) sp.save() contact = Contact() contact.name = name contact.supply_point = sp contact.role = ContactRole.objects.get(code=config.Roles.HSA) contact.is_active = True contact.save() connection = Connection() connection.backend = Backend.objects.get(pk=int(backend)) connection.identity = "+%s%s" % (settings.COUNTRY_DIALLING_CODE, number) #TODO: Check validity of numbers connection.contact = contact connection.save() messages.success(request, "HSA added!") return render_to_response(template, context, context_instance=RequestContext(request))
def populate_user(row): for k, v in row.items(): if v == "": del row[k] NON_REQUIRED_FIELDS = ["email", "perm"] for k, v in row.iteritems(): if v is None and k not in NON_REQUIRED_FIELDS: print "%s is required" % k return try: User.objects.get(username=row["username"]) print "user %s already exists" % row["username"] return except User.DoesNotExist: pass ALLOWED_STATES = ("fct", "nasawara", "national") if not row["state"] in ALLOWED_STATES: print "state must be one of: %s" % ", ".join(ALLOWED_STATES) return if "perm" in row and not row["perm"] in ALLOWED_PERMS: print "perm must be one of: %s" % ", ".join(ALLOWED_PERMS) return if "perm" in row: perm = Permission.objects.get(codename=row["perm"]) else: perm = None u = User() u.username = row["username"] u.first_name = row["first name"] u.last_name = row["last name"] u.email = row.get("email", "*****@*****.**") u.set_password(row["password"]) u.save() if perm: u.user_permissions.add(perm) try: contact = Contact.objects.get(user__username=row["username"]) return except Contact.DoesNotExist: pass if row["state"] == "national": loc = Location.objects.get(slug="nigeria") else: loc = Location.objects.get(type__slug="state", slug=row["state"]) c = Contact() c.name = "%s %s" % (u.first_name, u.last_name) c.first_name = u.first_name c.last_name = u.last_name c.email = row.get("email", "") c.user = u c.location = loc c.save()