Skip to content

Commit

Permalink
Add ability to attach likert scale and use as a template
Browse files Browse the repository at this point in the history
  • Loading branch information
paltman committed Dec 6, 2016
1 parent acf449e commit 74c62d5
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 4 deletions.
20 changes: 19 additions & 1 deletion formly/forms/design.py
@@ -1,6 +1,6 @@
from django import forms

from formly.models import Survey, Page, Field, FieldChoice
from formly.models import Survey, Page, Field, FieldChoice, LikertScale


class SurveyCreateForm(forms.ModelForm):
Expand Down Expand Up @@ -32,6 +32,24 @@ class Meta:
]


class LikertScaleForm(forms.ModelForm):

scale = forms.CharField()

def clean_scale(self):
scale = self.cleaned_data["scale"]
if scale:
return [s.strip() for s in scale.split(",")]
scale

class Meta:
model = LikertScale
fields = [
"name",
"scale"
]


class FieldForm(forms.ModelForm):

class Meta:
Expand Down
10 changes: 10 additions & 0 deletions formly/templates/formly/design/_field_edit.html
Expand Up @@ -30,4 +30,14 @@ <h3>Choices</h3>
</div>
{% endif %}

{% if selected_field.field_type == selected_field.LIKERT_FIELD %}
<h3>Likert Scales</h3>
<p class="lead">Select a Likert Scale</p>

{% include "formly/design/_likert_scales.html" %}

<h4>Add a new Likert scale</h4>
{% include "formly/design/_likert_scale_form.html" %}

{% endif %}
{% endif %}
11 changes: 11 additions & 0 deletions formly/templates/formly/design/_likert_scale.html
@@ -0,0 +1,11 @@
<div class="list-group-item">
<a class="btn {% if scale == selected_field.scale %}btn-primary{% else %}btn-default{% endif %} btn-xs pull-right ajax" data-method="post" data-replace-closest=".likert-scales" href="{% url "formly_dt_ajax_likert_scale_set" selected_field.pk scale.pk %}">
<i class="fa fa-fw fa-check"></i>
</a>
<div>
<div>{{ scale.name }}</div>
<small class="text-muted">
{{ scale.choices.all|join:", " }}
</small>
</div>
</div>
6 changes: 6 additions & 0 deletions formly/templates/formly/design/_likert_scale_form.html
@@ -0,0 +1,6 @@
{% load bootstrap %}
<form method="post" action="{% url "formly_dt_ajax_likert_scale_create" selected_field.pk %}" class="ajax likert-scale-form" data-replace=".likert-scale-form">
{% csrf_token %}
{{ likert_scale_form|bootstrap }}
<button class="btn btn-sm btn-primary">Save</button>
</form>
5 changes: 5 additions & 0 deletions formly/templates/formly/design/_likert_scales.html
@@ -0,0 +1,5 @@
<div class="list-group likert-scales">
{% for scale in likert_scales %}
{% include "formly/design/_likert_scale.html" %}
{% endfor %}
</div>
2 changes: 2 additions & 0 deletions formly/urls.py
Expand Up @@ -27,6 +27,8 @@
url(r"^design/surveys/(?P<pk>\d+)/delete/$", design.SurveyDeleteView.as_view(), name="formly_dt_survey_delete"),

url(r"^ajax/design/choices/(?P<pk>\d+)/delete/$", design.choice_delete, name="formly_dt_ajax_choice_delete"),
url(r"^ajax/design/fields/(?P<field_pk>\d+)/likert-scales/(?P<scale_pk>\d+)/set/$", design.likert_scale_set, name="formly_dt_ajax_likert_scale_set"),
url(r"^ajax/design/fields/(?P<field_pk>\d+)/likert-scales/create/$", design.likert_scale_create, name="formly_dt_ajax_likert_scale_create"),

url(r"^run/survey/(?P<pk>\d+)/$", run.take_survey, name="formly_rt_take_survey"),
url(r"^run/ajax/choice-question/(?P<pk>\d+)/$", run.choice_question, name="formly_rt_choice_question"),
Expand Down
44 changes: 41 additions & 3 deletions formly/views/design.py
Expand Up @@ -12,8 +12,8 @@
from django.contrib.auth.decorators import login_required

from formly.utils.views import BaseDeleteView
from formly.forms.design import SurveyCreateForm, PageUpdateForm, FieldForm, FieldChoiceForm
from formly.models import Survey, Page, Field, FieldChoice
from formly.forms.design import SurveyCreateForm, PageUpdateForm, FieldForm, FieldChoiceForm, LikertScaleForm
from formly.models import Survey, Page, Field, FieldChoice, LikertScale


@login_required
Expand Down Expand Up @@ -264,6 +264,41 @@ def field_add_choice(request, pk):
})


@require_POST
@login_required
def likert_scale_set(request, field_pk, scale_pk):
field = get_object_or_404(Field, pk=field_pk, field_type=Field.LIKERT_FIELD)
scale = get_object_or_404(LikertScale, pk=scale_pk)
field.scale = scale
field.save()
return JsonResponse({
"html": render_to_string("formly/design/_likert_scales.html", {"selected_field": field, "likert_scales": LikertScale.objects.all()}, request)
})


@require_POST
@login_required
def likert_scale_create(request, field_pk):
field = get_object_or_404(Field, pk=field_pk, field_type=Field.LIKERT_FIELD)
likert_scale_form = LikertScaleForm(request.POST)
if likert_scale_form.is_valid():
scale = likert_scale_form.save()
choices = likert_scale_form.cleaned_data["scale"]
for index, scale_choice in enumerate(scale.choices.all().order_by("score")):
if index < len(choices):
scale_choice.label = choices[index]
scale_choice.save()
field.scale = scale
field.save()
likert_scale_form = LikertScaleForm()
return JsonResponse({
"html": render_to_string("formly/design/_likert_scale_form.html", {"selected_field": field, "likert_scale_form": likert_scale_form}, request),
"fragments": {
".likert-scales": render_to_string("formly/design/_likert_scales.html", {"selected_field": field, "likert_scales": LikertScale.objects.all()}, request)
}
})


@login_required
def field_update(request, pk):
field = get_object_or_404(Field, pk=pk)
Expand All @@ -281,6 +316,7 @@ def field_update(request, pk):
else:
form = FieldForm(instance=field)
field_choice_form = FieldChoiceForm(prefix="choices")
likert_scale_form = LikertScaleForm()

return render(request, "formly/design/survey_list.html", {
"surveys": Survey.objects.all().order_by("-created"),
Expand All @@ -291,7 +327,9 @@ def field_update(request, pk):
"fields": field.page.fields.all().order_by("ordinal"),
"selected_field": field,
"field_form": form,
"field_choice_form": field_choice_form
"field_choice_form": field_choice_form,
"likert_scales": LikertScale.objects.all(),
"likert_scale_form": likert_scale_form
})


Expand Down

0 comments on commit 74c62d5

Please sign in to comment.