コード例 #1
0
def field_update(request, pk):
    field = get_object_or_404(Field, pk=pk)

    if not request.user.has_perm("formly.edit_survey", obj=field.survey):
        raise PermissionDenied()

    if request.method == "POST":
        if request.POST.get("action") == "field_update":
            form = FieldForm(data=request.POST, instance=field)
            field_choice_form = FieldChoiceForm(prefix="choices")
            if form.is_valid():
                form.save()
                return redirect(field)
    else:
        form = FieldForm(instance=field)
        field_choice_form = FieldChoiceForm(prefix="choices")
        likert_scale_form = OrdinalScaleForm()
        rating_scale_form = OrdinalScaleForm()

    return render(request, "formly/design/survey_list.html", {
        "surveys": Survey.objects.all().order_by("-created"),
        "survey_form": SurveyCreateForm(user=request.user),
        "pages": field.page.survey.pages.all(),
        "selected_page": field.page,
        "selected_survey": field.survey,
        "fields": field.page.fields.all().order_by("ordinal"),
        "selected_field": field,
        "field_form": form,
        "field_choice_form": field_choice_form,
        "likert_scales": OrdinalScale.objects.filter(kind=OrdinalScale.ORDINAL_KIND_LIKERT),
        "likert_scale_form": likert_scale_form,
        "rating_scales": OrdinalScale.objects.filter(kind=OrdinalScale.ORDINAL_KIND_RATING),
        "rating_scale_form": rating_scale_form
    })
コード例 #2
0
def rating_scale_create(request, field_pk):
    field = get_object_or_404(Field, pk=field_pk, field_type=Field.RATING_FIELD)
    rating_scale_form = OrdinalScaleForm(request.POST)
    if rating_scale_form.is_valid():
        scale = rating_scale_form.save(commit=False)
        scale.kind = OrdinalScale.ORDINAL_KIND_RATING
        scale.save()
        choices = rating_scale_form.cleaned_data["scale"]
        for index, choice in enumerate(choices):
            scale.choices.create(
                label=choice,
                score=index
            )
        field.scale = scale
        field.save()
        rating_scale_form = OrdinalScaleForm()
    return JsonResponse({
        "html": render_to_string("formly/design/_rating_scale_form.html", {"selected_field": field, "rating_scale_form": rating_scale_form}, request),
        "fragments": {
            ".rating-scales": render_to_string("formly/design/_rating_scales.html", {"selected_field": field, "rating_scales": OrdinalScale.objects.filter(kind=OrdinalScale.ORDINAL_KIND_RATING)}, request)
        }
    })
コード例 #3
0
def likert_scale_create(request, field_pk):
    field = get_object_or_404(Field,
                              pk=field_pk,
                              field_type=Field.LIKERT_FIELD)
    likert_scale_form = OrdinalScaleForm(request.POST, balanced=True)
    if likert_scale_form.is_valid():
        scale = likert_scale_form.save(commit=False)
        scale.kind = OrdinalScale.ORDINAL_KIND_LIKERT
        scale.save()
        choices = likert_scale_form.cleaned_data["scale"]
        max_score = int((len(choices) - 1) / 2)
        min_score = -max_score
        for index, score in enumerate(range(min_score, max_score + 1)):
            scale.choices.create(label=choices[index], score=score)
        field.scale = scale
        field.save()
        likert_scale_form = OrdinalScaleForm()
    return JsonResponse({
        "html":
        render_to_string("formly/design/_likert_scale_form.html",
                         context={
                             "selected_field": field,
                             "likert_scale_form": likert_scale_form,
                         },
                         request=request),
        "fragments": {
            ".likert-scales":
            render_to_string("formly/design/_likert_scales.html",
                             context={
                                 "selected_field":
                                 field,
                                 "likert_scales":
                                 OrdinalScale.objects.filter(
                                     kind=OrdinalScale.ORDINAL_KIND_LIKERT),
                             },
                             request=request)
        }
    })