def add_sample(request, domain, sample_id=None): sample = None if sample_id is not None: sample = SurveySample.get(sample_id) if request.method == "POST": form = SurveySampleForm(request.POST) if form.is_valid(): name = form.cleaned_data.get("name") sample_contacts = form.cleaned_data.get("sample_contacts") if sample is None: sample = SurveySample ( domain = domain, name = name, contacts = sample_contacts ) else: sample.name = name sample.contacts = sample_contacts sample.save() return HttpResponseRedirect(reverse("sample_list", args=[domain])) else: initial = {} if sample is not None: initial["name"] = sample.name initial["sample_contacts"] = sample.contacts form = SurveySampleForm(initial=initial) context = { "domain" : domain, "form" : form, "sample_id" : sample_id } return render_to_response(request, "reminders/partial/add_sample.html", context)
def add_sample(request, domain, sample_id=None): sample = None if sample_id is not None: sample = SurveySample.get(sample_id) if request.method == "POST": form = SurveySampleForm(request.POST, request.FILES) if form.is_valid(): name = form.cleaned_data.get("name") sample_contacts = form.cleaned_data.get("sample_contacts") time_zone = form.cleaned_data.get("time_zone") use_contact_upload_file = form.cleaned_data.get("use_contact_upload_file") contact_upload_file = form.cleaned_data.get("contact_upload_file") if sample is None: sample = SurveySample ( domain = domain, name = name, time_zone = time_zone.zone ) else: sample.name = name sample.time_zone = time_zone.zone errors = [] phone_numbers = [] if use_contact_upload_file == "Y": for contact in contact_upload_file: phone_numbers.append(contact["phone_number"]) else: for contact in sample_contacts: phone_numbers.append(contact["phone_number"]) existing_number_entries = VerifiedNumber.view('sms/verified_number_by_number', keys=phone_numbers, include_docs=True ).all() for entry in existing_number_entries: if entry.domain != domain or entry.owner_doc_type != "CommCareCase": errors.append("Cannot use phone number %s" % entry.phone_number) if len(errors) > 0: if use_contact_upload_file == "Y": form._errors["contact_upload_file"] = form.error_class(errors) else: form._errors["sample_contacts"] = form.error_class(errors) else: existing_numbers = [v.phone_number for v in existing_number_entries] nonexisting_numbers = list(set(phone_numbers).difference(existing_numbers)) id_range = DomainCounter.increment(domain, "survey_contact_id", len(nonexisting_numbers)) ids = iter(range(id_range[0], id_range[1] + 1)) for phone_number in nonexisting_numbers: register_sms_contact(domain, "participant", str(ids.next()), request.couch_user.get_id, phone_number, contact_phone_number_is_verified="1", contact_backend_id="MOBILE_BACKEND_TROPO_US", language_code="en", time_zone=time_zone.zone) newly_registered_entries = VerifiedNumber.view('sms/verified_number_by_number', keys=nonexisting_numbers, include_docs=True ).all() sample.contacts = [v.owner_id for v in existing_number_entries] + [v.owner_id for v in newly_registered_entries] sample.save() # Update delegation tasks for surveys using this sample surveys = Survey.view("reminders/sample_to_survey", key=[domain, sample._id, "CATI"], include_docs=True).all() for survey in surveys: survey.update_delegation_tasks(request.couch_user.get_id) survey.save() return HttpResponseRedirect(reverse("sample_list", args=[domain])) else: initial = {} if sample is not None: initial["name"] = sample.name initial["time_zone"] = sample.time_zone contact_info = [] for case_id in sample.contacts: case = CommCareCase.get(case_id) contact_info.append({"id":case.name, "phone_number":case.contact_phone_number, "case_id" : case_id}) initial["sample_contacts"] = contact_info form = SurveySampleForm(initial=initial) context = { "domain" : domain, "form" : form, "sample_id" : sample_id } return render(request, "reminders/partial/add_sample.html", context)
def add_sample(request, domain, sample_id=None): sample = None if sample_id is not None: sample = SurveySample.get(sample_id) if request.method == "POST": form = SurveySampleForm(request.POST, request.FILES) if form.is_valid(): name = form.cleaned_data.get("name") sample_contacts = form.cleaned_data.get("sample_contacts") time_zone = form.cleaned_data.get("time_zone") use_contact_upload_file = form.cleaned_data.get( "use_contact_upload_file") contact_upload_file = form.cleaned_data.get("contact_upload_file") if sample is None: sample = SurveySample(domain=domain, name=name, time_zone=time_zone.zone) else: sample.name = name sample.time_zone = time_zone.zone errors = [] phone_numbers = [] if use_contact_upload_file == "Y": for contact in contact_upload_file: phone_numbers.append(contact["phone_number"]) else: for contact in sample_contacts: phone_numbers.append(contact["phone_number"]) existing_number_entries = VerifiedNumber.view( 'sms/verified_number_by_number', keys=phone_numbers, include_docs=True).all() for entry in existing_number_entries: if entry.domain != domain or entry.owner_doc_type != "CommCareCase": errors.append("Cannot use phone number %s" % entry.phone_number) if len(errors) > 0: if use_contact_upload_file == "Y": form._errors["contact_upload_file"] = form.error_class( errors) else: form._errors["sample_contacts"] = form.error_class(errors) else: existing_numbers = [ v.phone_number for v in existing_number_entries ] nonexisting_numbers = list( set(phone_numbers).difference(existing_numbers)) id_range = DomainCounter.increment(domain, "survey_contact_id", len(nonexisting_numbers)) ids = iter(range(id_range[0], id_range[1] + 1)) for phone_number in nonexisting_numbers: register_sms_contact( domain, "participant", str(ids.next()), request.couch_user.get_id, phone_number, contact_phone_number_is_verified="1", contact_backend_id="MOBILE_BACKEND_TROPO_US", language_code="en", time_zone=time_zone.zone) newly_registered_entries = VerifiedNumber.view( 'sms/verified_number_by_number', keys=nonexisting_numbers, include_docs=True).all() sample.contacts = [ v.owner_id for v in existing_number_entries ] + [v.owner_id for v in newly_registered_entries] sample.save() # Update delegation tasks for surveys using this sample surveys = Survey.view("reminders/sample_to_survey", key=[domain, sample._id, "CATI"], include_docs=True).all() for survey in surveys: survey.update_delegation_tasks(request.couch_user.get_id) survey.save() return HttpResponseRedirect( reverse("sample_list", args=[domain])) else: initial = {} if sample is not None: initial["name"] = sample.name initial["time_zone"] = sample.time_zone contact_info = [] for case_id in sample.contacts: case = CommCareCase.get(case_id) contact_info.append({ "id": case.name, "phone_number": case.contact_phone_number, "case_id": case_id }) initial["sample_contacts"] = contact_info form = SurveySampleForm(initial=initial)