def test_append_country_to_location(self): location_field = TextField(LOCATION_TYPE_FIELD_NAME, "location_code", "some label") form_model = EntityFormModel(self.dbm, 'some form', 'some', 'form_code_1', fields=[location_field], entity_type=['Clinic']) form = SubjectRegistrationForm(form_model, data={'location_code': "Bangalore", 'form_code': 'form_code_1', 't': ['Clinic']}, country='India') form.is_valid() self.assertEquals(form.cleaned_data['location_code'], 'Bangalore,India')
def test_regex_field_created_for_entity_question(self): entity_field = ShortCodeField("reporting on", "rep_on", "rep") form_model = EntityFormModel(self.dbm, 'some form', 'some', 'form_code_1', fields=[entity_field], entity_type=['Clinic']) form = SubjectRegistrationForm(form_model) self.assertEquals(type(form.fields['rep_on']), RegexField) self.assertEquals(form.short_code_question_code, 'rep_on')
def test_regex_field_created_for_entity_question(self): entity_field = TextField("reporting on", "rep_on", "rep", self.ddtype, entity_question_flag=True) form_model = FormModel(self.dbm, 'some form', 'some', 'form_code_1', fields=[entity_field], entity_type=['Clinic'], type="business") form = SubjectRegistrationForm(form_model) self.assertEquals(type(form.fields['rep_on']), RegexField) self.assertEquals(form.short_code_question_code, 'rep_on')
def create_subject(request, entity_type=None): manager = get_database_manager(request.user) form_model = get_form_model_by_entity_type(manager, [entity_type.lower()]) web_questionnaire_template = get_template(request.user) disable_link_class, hide_link_class = get_visibility_settings_for( request.user) if request.method == 'GET': questionnaire_form = SubjectRegistrationForm(form_model) form_context = _make_form_context( questionnaire_form, entity_type, disable_link_class, hide_link_class, form_model.form_code, get_organization_telephone_number(request), form_model.fields, web_view=request.GET.get("web_view", False)) form_context.update( {'is_pro_sms': get_organization(request).is_pro_sms}) return render_to_response(web_questionnaire_template, form_context, context_instance=RequestContext(request)) if request.method == 'POST': questionnaire_form = SubjectRegistrationForm( form_model, data=request.POST, country=get_organization_country(request)) if not questionnaire_form.is_valid(): form_context = _make_form_context( questionnaire_form, entity_type, disable_link_class, hide_link_class, form_model.form_code, get_organization_telephone_number(request), form_model.fields, web_view=True) return render_to_response(web_questionnaire_template, form_context, context_instance=RequestContext(request)) success_message = None error_message = None try: from datawinners.project.helper import create_request response = WebPlayer( manager, LocationBridge( location_tree=get_location_tree(), get_loc_hierarchy=get_location_hierarchy)).accept( create_request(questionnaire_form, request.user.username), logger=websubmission_logger) if response.success: ReportRouter().route( get_organization(request).org_id, response) success_message = _( "%s with Identification Number %s successfully registered." ) % (entity_type.capitalize(), response.short_code) detail_dict = dict({ "Subject Type": entity_type.capitalize(), "Unique ID": response.short_code }) UserActivityLog().log(request, action=REGISTERED_IDENTIFICATION_NUMBER, detail=json.dumps(detail_dict)) questionnaire_form = SubjectRegistrationForm(form_model) else: from datawinners.project.helper import errors_to_list questionnaire_form._errors = errors_to_list( response.errors, form_model.fields) form_context = _make_form_context( questionnaire_form, entity_type, disable_link_class, hide_link_class, form_model.form_code, get_organization_telephone_number(request), form_model.fields, web_view=True) return render_to_response( web_questionnaire_template, form_context, context_instance=RequestContext(request)) except DataObjectNotFound: message = exception_messages.get(DataObjectNotFound).get(WEB) error_message = _(message) % (form_model.entity_type[0], form_model.entity_type[0]) except DataObjectAlreadyExists as exception: error_message = _( "%s with ID Number '%s' already exists or has previously collected data." ) % (exception.data[2], exception.data[1]) except Exception as exception: error_message = _( get_exception_message_for(exception=exception, channel=Channel.WEB)) subject_context = _make_form_context( questionnaire_form, entity_type, disable_link_class, hide_link_class, form_model.form_code, get_organization_telephone_number(request), form_model.fields, web_view=True) subject_context.update({ 'success_message': success_message, 'error_message': error_message }) return render_to_response(web_questionnaire_template, subject_context, context_instance=RequestContext(request))
def edit_subject(request, entity_type, entity_id, project_id=None): manager = get_database_manager(request.user) form_model = get_form_model_by_entity_type(manager, [entity_type.lower()]) subject = get_by_short_code(manager, entity_id, [entity_type.lower()]) back_link = reverse(all_subjects, args=[entity_type]) web_questionnaire_template = get_template(request.user) disable_link_class, hide_link_class = get_visibility_settings_for( request.user) if request.method == 'GET': initialize_values(form_model, subject) questionnaire_form = SubjectRegistrationForm(form_model) form_context = _make_form_context( questionnaire_form, entity_type, disable_link_class, hide_link_class, form_model.form_code, get_organization_telephone_number(request), form_model.fields, is_update=True, back_link=back_link) form_context.update( {'is_pro_sms': get_organization(request).is_pro_sms}) return render_to_response(web_questionnaire_template, form_context, context_instance=RequestContext(request)) if request.method == 'POST': questionnaire_form = SubjectRegistrationForm( form_model, data=request.POST, country=get_organization_country(request)) if not questionnaire_form.is_valid(): form_context = _make_form_context( questionnaire_form, entity_type, disable_link_class, hide_link_class, form_model.form_code, get_organization_telephone_number(request), form_model.fields, is_update=True, back_link=back_link) return render_to_response(web_questionnaire_template, form_context, context_instance=RequestContext(request)) success_message = None error_message = None try: response = WebPlayer( manager, LocationBridge( location_tree=get_location_tree(), get_loc_hierarchy=get_location_hierarchy)).accept( create_request(questionnaire_form, request.user.username, is_update=True)) if response.success: #assumption q6 - unique_id code and q2 - lastname codes cannot be changed update_submission_search_for_subject_edition( manager, [entity_type], response.processed_data) success_message = _("Your changes have been saved.") questionnaire_form = SubjectRegistrationForm( form_model, data=request.POST, country=get_organization_country(request)) else: from datawinners.project.helper import errors_to_list questionnaire_form._errors = errors_to_list( response.errors, form_model.fields) form_context = _make_form_context( questionnaire_form, entity_type, disable_link_class, hide_link_class, form_model.form_code, get_organization_telephone_number(request), form_model.fields, is_update=True, back_link=back_link) return render_to_response( web_questionnaire_template, form_context, context_instance=RequestContext(request)) except DataObjectNotFound: message = exception_messages.get(DataObjectNotFound).get(WEB) error_message = _(message) % (form_model.entity_type[0], form_model.entity_type[0]) except Exception as exception: error_message = _( get_exception_message_for(exception=exception, channel=Channel.WEB)) subject_context = _make_form_context( questionnaire_form, entity_type, disable_link_class, hide_link_class, form_model.form_code, get_organization_telephone_number(request), form_model.fields, is_update=True, back_link=back_link) subject_context.update({ 'success_message': success_message, 'error_message': error_message }) return render_to_response(web_questionnaire_template, subject_context, context_instance=RequestContext(request))
def form(self, initial_data=None, country=None): return SubjectRegistrationForm(self.form_model, data=initial_data, country=country)