コード例 #1
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)
        }
    })
コード例 #2
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)
        }
    })