def _save_contact(self, data): """Save contact to database""" # Create connection, or if this is a re-register, just use (one # of) the old one(s). old_connections = self.connections.filter(contact__isnull=True) if len(old_connections) > 0: conn = old_connections[0] else: gsm_backend = Backend.objects.get(name='gsm') #gsm_backend = Backend.objects.get(name='message_tester') conn = Connection(backend=gsm_backend, identity=data['phone_no']) # Create contact contact = Contact(name=data['name'], language=data['lang'], is_active=False, only_important=data['only_important']) contact.save() contact.categories = data['categories'] contact.save() conn.contact = contact conn.save() return contact
def registration(req, pk=None, template="registration/dashboard.html"): contact = None connection = None bulk_form = None if pk is not None: contact = get_object_or_404(Contact, pk=pk) connection = get_object_or_404(Connection, contact__name=contact.name) if req.method == "POST": if req.POST["submit"] == "Delete Contact": contact.delete() return HttpResponseRedirect(reverse(registration)) elif "bulk" in req.FILES: # TODO use csv module #reader = csv.reader(open(req.FILES["bulk"].read(), "rb")) #for row in reader: for line in req.FILES["bulk"]: line_list = line.split(',') name = line_list[0].strip() backend_name = line_list[1].strip() identity = line_list[2].strip() contact = Contact(name=name) contact.save() # TODO deal with errors! backend = Backend.objects.get(name=backend_name) connection = Connection(backend=backend, identity=identity,\ contact=contact) connection.save() return HttpResponseRedirect(reverse(registration)) else: contact_form = ContactForm(instance=contact, data=req.POST) connection_form = ConnectionForm(req.POST, instance=connection) if contact_form.is_valid() and connection_form.is_valid(): contact = contact_form.save() connection = connection_form.save(commit=False) connection.contact = contact connection.save() return HttpResponseRedirect(reverse(registration)) else: contact_form = ContactForm(instance=contact) bulk_form = BulkRegistrationForm() connection_form = ConnectionForm(instance=connection) return render_to_response( template, { "contacts_table": ContactTable(Contact.objects.all(), request=req), "contact_form": contact_form, "connection_form": connection_form, "bulk_form": bulk_form, "contact": contact }, context_instance=RequestContext(req))
def default_connection(self, identity): # when you re-assign default connection, should you delete the unused connection? probably. from rapidsms.models import Connection backend = self.default_backend default = self.default_connection if default is not None: if default.identity == identity and default.backend == backend: # our job is done return default.delete() try: conn = Connection.objects.get(backend=backend, identity=identity) except Connection.DoesNotExist: # good, it doesn't exist already conn = Connection(backend=backend, identity=identity) conn.contact = self conn.save()
def _save_contact (self, data): """Save contact to database""" # Create connection, or if this is a re-register, just use (one # of) the old one(s). old_connections = self.connections.filter(contact__isnull=True) if len(old_connections) > 0: conn = old_connections[0] else: gsm_backend = Backend.objects.get(name='gsm') #gsm_backend = Backend.objects.get(name='message_tester') conn = Connection(backend=gsm_backend, identity=data['phone_no']) # Create contact contact = Contact(name=data['name'], language=data['lang'], is_active=False, only_important=data['only_important']) contact.save() contact.categories = data['categories'] contact.save() conn.contact = contact conn.save() return contact
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 registration(req, pk=None): contact = None connection = None bulk_form = None if pk is not None: contact = get_object_or_404( Contact, pk=pk) connection = get_object_or_404(Connection,contact__name=contact.name) if req.method == "POST": if req.POST["submit"] == "Delete Contact": contact.delete() return HttpResponseRedirect( reverse(registration)) elif "bulk" in req.FILES: # TODO use csv module #reader = csv.reader(open(req.FILES["bulk"].read(), "rb")) #for row in reader: for line in req.FILES["bulk"]: line_list = line.split(',') name = line_list[0].strip() backend_name = line_list[1].strip() identity = line_list[2].strip() contact = Contact(name=name) contact.save() # TODO deal with errors! backend = Backend.objects.get(name=backend_name) connection = Connection(backend=backend, identity=identity,\ contact=contact) connection.save() return HttpResponseRedirect( reverse(registration)) else: contact_form = ContactForm( instance=contact, data=req.POST) connection_form = ConnectionForm(req.POST, instance=connection) if contact_form.is_valid() and connection_form.is_valid(): contact = contact_form.save() connection = connection_form.save(commit=False) connection.contact = contact connection.save() return HttpResponseRedirect( reverse(registration)) else: contact_form = ContactForm( instance=contact) bulk_form = BulkRegistrationForm() connection_form = ConnectionForm(instance=connection) return render_to_response( "registration/dashboard.html", { "contacts_table": ContactTable(Contact.objects.all(), request=req), "contact_form": contact_form, "connection_form": connection_form, "bulk_form": bulk_form, "contact": contact }, context_instance=RequestContext(req) )
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))