コード例 #1
0
ファイル: views.py プロジェクト: manish211/datawinners
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))
コード例 #2
0
def edit(request, project_id, survey_response_id, tab=0):
    manager = get_database_manager(request.user)
    project = Project.load(manager.database, project_id)
    questionnaire_form_model = FormModel.get(manager, project.qid)

    disable_link_class, hide_link_class = get_visibility_settings_for(
        request.user)
    survey_response = get_survey_response_by_id(manager, survey_response_id)
    back_link = reverse(index,
                        kwargs={
                            "project_id": project_id,
                            "questionnaire_code":
                            questionnaire_form_model.form_code,
                            "tab": tab
                        })
    form_ui_model = build_static_info_context(manager, survey_response)
    form_ui_model.update({"back_link": back_link})
    if request.method == 'GET':
        form_initial_values = construct_request_dict(survey_response,
                                                     questionnaire_form_model)
        survey_response_form = EditSubmissionForm(manager, project,
                                                  questionnaire_form_model,
                                                  form_initial_values)

        form_ui_model.update(
            get_form_context(questionnaire_form_model.form_code, project,
                             survey_response_form, manager, hide_link_class,
                             disable_link_class))
        form_ui_model.update({"redirect_url": ""})

        if not survey_response_form.is_valid():
            error_message = _("Please check your answers below for errors.")
            form_ui_model.update({'error_message': error_message})
        return render_to_response("project/web_questionnaire.html",
                                  form_ui_model,
                                  context_instance=RequestContext(request))

    if request.method == 'POST':
        original_survey_response = survey_response.copy()
        is_errored_before_edit = True if survey_response.errors != '' else False
        form_ui_model.update(
            {"redirect_url": request.POST.get("redirect_url")})
        form_ui_model.update(
            {"click_after_reload": request.POST.get("click_after_reload")})
        if request.POST.get("discard"):
            survey_response_form = EditSubmissionForm(
                manager, project, questionnaire_form_model,
                survey_response.values)

            form_ui_model.update(
                get_form_context(questionnaire_form_model.form_code, project,
                                 survey_response_form, manager,
                                 hide_link_class, disable_link_class))
            return render_to_response("project/web_questionnaire.html",
                                      form_ui_model,
                                      context_instance=RequestContext(request))
        else:
            survey_response_form = EditSubmissionForm(
                manager, project, questionnaire_form_model, request.POST)

        form_ui_model.update(
            get_form_context(questionnaire_form_model.form_code, project,
                             survey_response_form, manager, hide_link_class,
                             disable_link_class))
        if not survey_response_form.is_valid():
            error_message = _("Please check your answers below for errors.")
            form_ui_model.update({'error_message': error_message})
            return render_to_response("project/web_questionnaire.html",
                                      form_ui_model,
                                      context_instance=RequestContext(request))

        success_message = _("Your changes have been saved.")
        form_ui_model.update({'success_message': success_message})
        if len(survey_response_form.changed_data) or is_errored_before_edit:
            created_request = helper.create_request(survey_response_form,
                                                    request.user.username)

            additional_feed_dictionary = get_project_details_dict_for_feed(
                project)
            user_profile = NGOUserProfile.objects.get(user=request.user)
            feeds_dbm = get_feeds_database(request.user)
            if questionnaire_form_model.entity_type == ["reporter"]:
                owner_id = request.POST["eid"]
            else:
                owner_id = None
            response = WebPlayerV2(manager, feeds_dbm, user_profile.reporter_id) \
                .edit_survey_response(created_request, survey_response, owner_id,
                                      additional_feed_dictionary, websubmission_logger)
            mail_feed_errors(response, manager.database_name)
            if response.success:
                build_static_info_context(manager, survey_response,
                                          form_ui_model)
                ReportRouter().route(
                    get_organization(request).org_id, response)
                _update_static_info_block_status(form_ui_model,
                                                 is_errored_before_edit)
                log_edit_action(original_survey_response, survey_response,
                                request, project.name,
                                questionnaire_form_model)
                if request.POST.get("redirect_url"):
                    return HttpResponseRedirect(
                        request.POST.get("redirect_url"))
            else:
                del form_ui_model["success_message"]
                survey_response_form._errors = helper.errors_to_list(
                    response.errors, questionnaire_form_model.fields)
        return render_to_response("project/web_questionnaire.html",
                                  form_ui_model,
                                  context_instance=RequestContext(request))
コード例 #3
0
def send_message(incoming_request, response):
    ReportRouter().route(incoming_request['organization'].org_id, response)
コード例 #4
0
def edit(request, project_id, survey_response_id, tab=0):
    manager = get_database_manager(request.user)
    questionnaire_form_model = Project.get(manager, project_id)
    dashboard_page = settings.HOME_PAGE + "?deleted=true"
    reporter_id = NGOUserProfile.objects.get(user=request.user).reporter_id
    is_linked = reporter_id in questionnaire_form_model.data_senders
    reporter_name = NGOUserProfile.objects.get(
        user=request.user).user.first_name
    if questionnaire_form_model.is_void():
        return HttpResponseRedirect(dashboard_page)

    disable_link_class, hide_link_class = get_visibility_settings_for(
        request.user)
    survey_response = get_survey_response_by_id(manager, survey_response_id)
    back_link = reverse(index,
                        kwargs={
                            "project_id": project_id,
                            "questionnaire_code":
                            questionnaire_form_model.form_code,
                            "tab": tab
                        })
    form_ui_model = build_static_info_context(
        manager,
        survey_response,
        questionnaire_form_model=questionnaire_form_model,
        reporter_id=reporter_id)
    form_ui_model.update({
        "back_link":
        back_link,
        'is_datasender':
        is_data_sender(request),
        'hide_change':
        questionnaire_form_model.is_poll
        and questionnaire_form_model.is_open_survey
    })
    data_sender = get_data_sender(manager, survey_response)
    short_code = data_sender[1]
    enable_datasender_edit = True if survey_response.owner_uid else False
    if request.method == 'GET':
        form_initial_values = construct_request_dict(survey_response,
                                                     questionnaire_form_model,
                                                     short_code)
        survey_response_form = SurveyResponseForm(
            questionnaire_form_model,
            form_initial_values,
            datasender_name=data_sender[0],
            reporter_id=reporter_id,
            reporter_name=reporter_name,
            enable_datasender_edit=enable_datasender_edit)

        form_ui_model.update(
            get_form_context(questionnaire_form_model, survey_response_form,
                             manager, hide_link_class, disable_link_class))
        form_ui_model.update({
            "redirect_url": "",
            "reporter_id": reporter_id,
            "is_linked": is_linked,
            "is_pro_sms": get_organization(request).is_pro_sms,
            "reporter_name": reporter_name
        })

        if not survey_response_form.is_valid(
        ) or form_ui_model['datasender_error_message']:
            error_message = _("Please check your answers below for errors.")
            form_ui_model.update({
                'error_message': error_message,
                "reporter_id": reporter_id,
                "is_linked": is_linked,
                "reporter_name": reporter_name
            })
        return render_to_response("project/web_questionnaire.html",
                                  form_ui_model,
                                  context_instance=RequestContext(request))

    if request.method == 'POST':
        send_to_carbon(create_path('submissions.web.simple'), 1)
        original_survey_response = survey_response.copy()
        is_errored_before_edit = True if survey_response.errors != '' else False
        submitted_values = request.POST
        owner_id = submitted_values.get("dsid")
        form_ui_model.update({
            "redirect_url":
            submitted_values.get("redirect_url"),
            'is_datasender':
            is_data_sender(request),
            "is_pro_sms":
            get_organization(request).is_pro_sms
        })
        form_ui_model.update(
            {"click_after_reload": submitted_values.get("click_after_reload")})
        if submitted_values.get("discard"):
            survey_response_form = SurveyResponseForm(questionnaire_form_model,
                                                      survey_response.values)

            form_ui_model.update(
                get_form_context(questionnaire_form_model,
                                 survey_response_form, manager,
                                 hide_link_class, disable_link_class))
            form_ui_model.update({
                "reporter_id": reporter_id,
                "is_linked": is_linked,
                "reporter_name": reporter_name
            })
            return render_to_response("project/web_questionnaire.html",
                                      form_ui_model,
                                      context_instance=RequestContext(request))
        else:
            form_initial_values = construct_request_dict(
                survey_response, questionnaire_form_model, short_code)
            if not owner_id:
                submitted_values = submitted_values.copy()
                submitted_values['dsid'] = form_initial_values['dsid']

            survey_response_form = SurveyResponseForm(
                questionnaire_form_model,
                submitted_values,
                initial=form_initial_values,
                enable_datasender_edit=enable_datasender_edit)

        form_ui_model.update(
            get_form_context(questionnaire_form_model, survey_response_form,
                             manager, hide_link_class, disable_link_class))
        form_ui_model.update({
            "reporter_id": reporter_id,
            "is_linked": is_linked
        })
        if not survey_response_form.is_valid():
            error_message = _("Please check your answers below for errors.")
            form_ui_model.update({
                'error_message': error_message,
                "reporter_id": reporter_id,
                "is_linked": is_linked
            })
            return render_to_response("project/web_questionnaire.html",
                                      form_ui_model,
                                      context_instance=RequestContext(request))

        success_message = _("Your changes have been saved.")
        form_ui_model.update({
            'success_message': success_message,
            "reporter_id": reporter_id,
            "is_linked": is_linked,
            "reporter_name": reporter_name
        })
        # if len(survey_response_form.changed_data) or is_errored_before_edit:
        created_request = helper.create_request(survey_response_form,
                                                request.user.username)

        additional_feed_dictionary = get_project_details_dict_for_feed(
            questionnaire_form_model)
        user_profile = NGOUserProfile.objects.get(user=request.user)
        feeds_dbm = get_feeds_database(request.user)
        response = WebPlayerV2(manager, feeds_dbm, user_profile.reporter_id) \
            .edit_survey_response(created_request, survey_response, owner_id,
                                  additional_feed_dictionary, websubmission_logger)
        mail_feed_errors(response, manager.database_name)
        if response.success:
            build_static_info_context(manager, survey_response, form_ui_model,
                                      questionnaire_form_model, reporter_id)
            ReportRouter().route(get_organization(request).org_id, response)
            _update_static_info_block_status(form_ui_model,
                                             is_errored_before_edit)
            log_edit_action(original_survey_response, survey_response, request,
                            questionnaire_form_model.name,
                            questionnaire_form_model)
            if submitted_values.get("redirect_url"):
                return HttpResponseRedirect(
                    submitted_values.get("redirect_url"))
        else:
            del form_ui_model["success_message"]
            survey_response_form._errors = helper.errors_to_list(
                response.errors, questionnaire_form_model.fields)
            form_ui_model.update({
                "reporter_id": reporter_id,
                "is_linked": is_linked,
                "reporter_name": reporter_name
            })
        return render_to_response("project/web_questionnaire.html",
                                  form_ui_model,
                                  context_instance=RequestContext(request))
コード例 #5
0
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()])

    QuestionnaireForm = WebQuestionnaireFormCreator(
        None, form_model=form_model).create()

    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 = QuestionnaireForm()
        form_context = _make_form_context(questionnaire_form, entity_type,
                                          disable_link_class, hide_link_class)
        return render_to_response(web_questionnaire_template,
                                  form_context,
                                  context_instance=RequestContext(request))

    if request.method == 'POST':
        questionnaire_form = QuestionnaireForm(
            country=get_organization_country(request), data=request.POST)
        if not questionnaire_form.is_valid():
            form_context = _make_form_context(questionnaire_form, entity_type,
                                              disable_link_class,
                                              hide_link_class)
            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 = (_(
                    "Successfully submitted. Unique identification number(ID) is:"
                ) + " %s") % (response.short_code, )
                detail_dict = dict({
                    "Subject Type": entity_type.capitalize(),
                    "Unique ID": response.short_code
                })
                UserActivityLog().log(request,
                                      action=REGISTERED_SUBJECT,
                                      detail=json.dumps(detail_dict))
                questionnaire_form = QuestionnaireForm()
            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)
                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 = _(
                "Entity with Unique Identification Number (ID) = %s already exists."
            ) % 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)
        subject_context.update({
            'success_message': success_message,
            'error_message': error_message
        })

        return render_to_response(web_questionnaire_template,
                                  subject_context,
                                  context_instance=RequestContext(request))