def handle(self, text): if not hasattr(self.msg, 'logistics_contact'): self.respond(config.Messages.LEAVE_NOT_REGISTERED) else: self.msg.logistics_contact.is_active = False self.msg.logistics_contact.commodities.clear() self.msg.logistics_contact.save() if self.msg.logistics_contact.supply_point and \ self.msg.logistics_contact.supply_point.type == config.hsa_supply_point_type(): self.msg.logistics_contact.supply_point.deprecate() self.respond(config.Messages.LEAVE_CONFIRM)
def handle(self, text): if not hasattr(self.msg,'logistics_contact'): self.respond(config.Messages.LEAVE_NOT_REGISTERED) else: self.msg.logistics_contact.is_active = False self.msg.logistics_contact.commodities.clear() self.msg.logistics_contact.save() if self.msg.logistics_contact.supply_point and \ self.msg.logistics_contact.supply_point.type == config.hsa_supply_point_type(): self.msg.logistics_contact.supply_point.deprecate() self.respond(config.Messages.LEAVE_CONFIRM)
def get_hsa(hsa_id): """ Attempt to get an HSA by code, return None if unable to find them. """ # in the future we should do some massaging of this code as well # to catch things like o's -> 0's and such. try: sp = SupplyPoint.objects.get(active=True, code=hsa_id, type=config.hsa_supply_point_type()) return Contact.objects.get(is_active=True, supply_point=sp) except (SupplyPoint.DoesNotExist, Contact.DoesNotExist): return None except Contact.MultipleObjectsReturned: # this is weird, shouldn't be possible, but who knows. raise MultipleHSAException("More than one HSA found with id %s" % hsa_id)
def reactivate_hsa(request, code, name): hsa = get_object_or_404(Contact, supply_point__code=code, name=name) hsa.is_active = True hsa.save() if hsa.supply_point and \ hsa.supply_point.type == config.hsa_supply_point_type(): hsa.supply_point.active = True hsa.supply_point.save() if hsa.supply_point.location: hsa.supply_point.location.is_active = True hsa.supply_point.location.save() messages.success(request, "HSA %(name)s reactivated." % { "name": hsa.name, }) return redirect('malawi_hsa', code=hsa.supply_point.code)
class HSARegistrationHandler(RegistrationBaseHandler): """ Registration for HSAs """ keyword = "reg|register" def help(self): self.respond(config.Messages.HSA_HELP) def handle(self, text): if self.handle_preconditions(text): return # default to HSA role = ContactRole.objects.get(code=config.Roles.HSA) try: hsa_id = format_id(self.supply_point.code, self.extra) except IdFormatException, e: self.respond(str(e)) return if Location.objects.filter(code=hsa_id, is_active=True).exists(): self.respond( "Sorry, a location with %(code)s already exists. Another HSA may have already registered this ID", code=hsa_id) return if SupplyPoint.objects.filter(code=hsa_id, contact__is_active=True).exists(): self.respond( "Sorry, a supply point with %(code)s already exists. Another HSA may have already registered this ID", code=hsa_id) return # create a location and supply point for the HSA if SupplyPoint.objects.filter(code=hsa_id, type=config.hsa_supply_point_type(), active=False).exists(): # We have a previously deactivated HSA. Reassociate. sp = SupplyPoint.objects.get(code=hsa_id, type=config.hsa_supply_point_type(), active=False) sp.name = self.contact_name sp.active = True sp.save() sp.location.is_active = True sp.location.name = self.contact_name sp.location.save() else: hsa_loc = Location.objects.create( name=self.contact_name, type=config.hsa_location_type(), code=hsa_id, parent=self.supply_point.location) sp = SupplyPoint.objects.create( name=self.contact_name, code=hsa_id, type=config.hsa_supply_point_type(), location=hsa_loc, supplied_by=self.supply_point) # overwrite the existing contact data if it was already there # we know at least they were not active since we checked above contact = self.msg.logistics_contact if hasattr( self.msg, 'logistics_contact') else Contact() contact.name = self.contact_name contact.supply_point = sp contact.role = role contact.is_active = True if not settings.LOGISTICS_APPROVAL_REQUIRED: contact.is_approved = True contact.save() self.msg.connection.contact = contact self.msg.connection.save() if settings.LOGISTICS_APPROVAL_REQUIRED: try: sh = Contact.objects.get( supply_point__location=self.supply_point.location, role=ContactRole.objects.get(code=Roles.HSA_SUPERVISOR)) sh.message(config.Messages.APPROVAL_REQUEST, hsa=contact.name, code=hsa_id) self.respond(_(config.Messages.APPROVAL_WAITING), hsa=contact.name) except Contact.DoesNotExist: # If there's no HSA supervisor registered, we silently approve them. Oh well. contact.is_approved = True self.respond(_(config.Messages.REGISTRATION_CONFIRM), sp_name=self.supply_point.name, contact_name=contact.name, role=contact.role.name) else: self.respond(_(config.Messages.REGISTRATION_CONFIRM), sp_name=self.supply_point.name, contact_name=contact.name, role=contact.role.name)
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))