Skip to content

Commit 1bc9174

Browse files
Response stores score not FK on available_response
I'm changing the available responses for the questionnaires because I'm finding them difficult to answer consistently. I'm therefore updating their labels. In order to maintain the previous data, I'm modifying the way responses get stored. Instead of recording the ID of the response (as a FK), I'm going to simply store the score of the response. This is done as a simple `IntegerField()` instead of a FK.
1 parent d3f1719 commit 1bc9174

File tree

8 files changed

+114
-13
lines changed

8 files changed

+114
-13
lines changed

oolong_app/forms.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ class QuestionnaireForm(forms.Form):
4141
questionnaire have the same questions; so we need a dynamic way
4242
of generatic a Form.
4343
44+
Each question in the questionnaire will get a select generate
45+
for it, the id of that select will be in the form qid_XX, where
46+
XX is the ID (in the DB) of the question.
47+
4448
https://jacobian.org/writing/dynamic-form-generation/
4549
'''
4650

@@ -84,14 +88,20 @@ def clean(self):
8488
hr_diff = diff.total_seconds() / 3600.0
8589
min_diff = 12 # minumum 12 hrs between submits
8690

87-
if hr_diff < min_diff:
91+
if False and hr_diff < min_diff:
8892
raise forms.ValidationError('You cannot submit this questionnaire more than once per day; you last submitted it %.3f hours ago.' %hr_diff)
8993

9094
return form_data
9195

9296

9397
def get_answers(self):
94-
# returns the tuple (question_id, response_id)
98+
'''
99+
Iterates over self.cleaned_data and parses the question
100+
answers into a tuple (qid, score) where score is
101+
the score given to a particular response, and qid is the
102+
ID (in the DB) of the question.
103+
'''
104+
# returns the tuple (question_id, score)
95105
for name, value in self.cleaned_data.items():
96106
if name.startswith('qid_'):
97107
qid = int(name.replace('qid_',''))
@@ -102,10 +112,10 @@ def save(self, commit=True, *args, **kwargs):
102112
super(QuestionnaireForm, self).__init__(*args, **kwargs)
103113
user = self.cleaned_data.get('user',None)
104114

105-
for (question_id, response_id) in self.get_answers():
115+
for (question_id, score) in self.get_answers():
106116
r = Response(
107117
question_id = question_id,
108-
response_id = response_id,
118+
score = score,
109119
user_id = user
110120
)
111121
r.save()
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# -*- coding: utf-8 -*-
2+
# Generated by Django 1.11.10 on 2018-03-31 13:43
3+
from __future__ import unicode_literals
4+
5+
from django.db import migrations, models
6+
7+
8+
class Migration(migrations.Migration):
9+
10+
dependencies = [
11+
('oolong_app', '0017_questionnaire_default_response'),
12+
]
13+
14+
operations = [
15+
migrations.AddField(
16+
model_name='response',
17+
name='score',
18+
field=models.IntegerField(db_index=True, default=0, help_text='Score for given response; e.g. 4.'),
19+
preserve_default=False,
20+
),
21+
]
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# -*- coding: utf-8 -*-
2+
# Generated by Django 1.11.10 on 2018-03-31 13:58
3+
from __future__ import unicode_literals
4+
5+
from django.db import migrations, models
6+
import django.db.models.deletion
7+
8+
9+
class Migration(migrations.Migration):
10+
11+
dependencies = [
12+
('oolong_app', '0018_response_score'),
13+
]
14+
15+
operations = [
16+
migrations.AlterField(
17+
model_name='response',
18+
name='response',
19+
field=models.ForeignKey(db_index=False, help_text='Response to given question.', on_delete=django.db.models.deletion.CASCADE, to='oolong_app.AvailableResponse'),
20+
),
21+
]
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# -*- coding: utf-8 -*-
2+
# Generated by Django 1.11.10 on 2018-03-31 13:59
3+
from __future__ import unicode_literals
4+
5+
from django.db import migrations, models
6+
7+
8+
class Migration(migrations.Migration):
9+
10+
dependencies = [
11+
('oolong_app', '0019_auto_20180331_1358'),
12+
]
13+
14+
operations = [
15+
migrations.AlterField(
16+
model_name='response',
17+
name='response',
18+
field=models.IntegerField(help_text='Response to given question.'),
19+
),
20+
]
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# -*- coding: utf-8 -*-
2+
# Generated by Django 1.11.10 on 2018-03-31 14:00
3+
from __future__ import unicode_literals
4+
5+
from django.db import migrations, models
6+
7+
8+
class Migration(migrations.Migration):
9+
10+
dependencies = [
11+
('oolong_app', '0020_auto_20180331_1359'),
12+
]
13+
14+
operations = [
15+
migrations.AlterField(
16+
model_name='response',
17+
name='response',
18+
field=models.IntegerField(help_text='Response to given question.', null=True),
19+
),
20+
]

oolong_app/models.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -727,11 +727,17 @@ class Response(models.Model):
727727
"Question being answered/scored."
728728
)
729729
)
730-
response = models.ForeignKey(
731-
AvailableResponse,
730+
score = models.IntegerField(
732731
blank=False,
733-
null=False,
734732
db_index=True,
733+
null=False,
734+
help_text=(
735+
"Score for given response; e.g. 4."
736+
)
737+
)
738+
response = models.IntegerField(
739+
db_index=False,
740+
null=True,
735741
help_text=(
736742
"Response to given question."
737743
)

oolong_app/templates/plot.html

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
{% if plotdat %}
1616
<div id="chart"></div>
1717
{% else %}
18-
<p class="lead">No data found for {{ user.get_username }}; please fill out the <a href='/submit_questionnaire/'>questionnaire</a> first.</p>
18+
<p class="lead" style="padding-left:15px;">No data found for {{ user.get_username }}; please fill out the <a href='/submit_questionnaire/'>questionnaire</a> first.</p>
1919
{% endif %}
2020
{% endcolumn %}
2121
{% endrow %}
@@ -25,7 +25,9 @@
2525

2626
<script>
2727
jQuery(function () {
28-
render_response_plot({{plotdat|safe}}, 'avg')
28+
if (typeof {{plotdat|safe}} !== 'undefined') {
29+
render_response_plot({{plotdat|safe}}, 'avg')
30+
}
2931
})
3032
</script>
3133

oolong_app/views.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def index(request):
3838
@login_required
3939
def plot(request):
4040

41-
dat = PlotResponse.objects.filter(user=request.user)
41+
dat = PlotResponse.objects.filter(user=1)
4242
plotdat = None if not dat.count() else json.dumps(list(dat.values()))
4343
context = {
4444
'plotdat': plotdat
@@ -164,9 +164,10 @@ def submit_questionnaire(request):
164164

165165
# get the available responses for the questions
166166
# [(response_id, response_label),()]
167-
responses = [(None,'----')]
168-
tmp = AvailableResponse.objects.filter(questionnaire_id=qid).values_list('id','label')
169-
responses.extend(list(tmp))
167+
responses = list(AvailableResponse.objects
168+
.filter(questionnaire_id=qid)
169+
.order_by('score')
170+
.values_list('score','label'))
170171
responses_dict = {k:v for k,v in responses if k}
171172

172173
form = QuestionnaireForm(

0 commit comments

Comments
 (0)