-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Data migrations - Question option indexing (including migrations) - Bump django-jsonattrs version for choice label handling
- Loading branch information
Ian Ross
committed
Sep 29, 2016
1 parent
952a0b0
commit 66f3de7
Showing
8 changed files
with
173 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
cadasta/questionnaires/migrations/0006_add_choice_labels.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# -*- coding: utf-8 -*- | ||
# Generated by Django 1.9.6 on 2016-09-26 13:07 | ||
from __future__ import unicode_literals | ||
|
||
from django.conf import settings | ||
from django.db import migrations | ||
from django.contrib.contenttypes.models import ContentType | ||
|
||
from organization.models import Project | ||
from jsonattrs.models import Schema, AttributeType | ||
|
||
|
||
def add_schemas(ss, ct, sels): | ||
for take in range(len(sels) + 1): | ||
for s in Schema.objects.filter(content_type=ct, | ||
selectors=sels[0:take]): | ||
ss.add(s) | ||
|
||
|
||
def add_attribute_choice_labels(apps, schema_editor): | ||
# Empty database? Probably during testing... | ||
if not AttributeType.objects.exists(): | ||
return | ||
|
||
Questionnaire = apps.get_model('questionnaires', 'Questionnaire') | ||
|
||
select_types = [AttributeType.objects.get(name=n) | ||
for n in ['select_one', 'select_multiple']] | ||
processed_schemas = set() | ||
for prj in Project.objects.all(): | ||
org = prj.organization | ||
|
||
if (prj.current_questionnaire is not None and | ||
prj.current_questionnaire != ''): | ||
quest = Questionnaire.objects.get(pk=prj.current_questionnaire) | ||
selectors = (org.pk, prj.pk, prj.current_questionnaire) | ||
for ct, sels in settings.JSONATTRS_SCHEMA_SELECTORS.items(): | ||
app_label, model = ct.split('.') | ||
content_type = ContentType.objects.get( | ||
app_label=app_label, model=model | ||
) | ||
schemas_to_process = set() | ||
if len(sels) > 3: | ||
cls = content_type.model_class() | ||
selfield = cls._meta.get_field(sels[3]) | ||
for choice in selfield.choices: | ||
add_schemas(schemas_to_process, content_type, | ||
selectors + (choice[0],)) | ||
else: | ||
add_schemas(schemas_to_process, content_type, selectors) | ||
for schema in schemas_to_process: | ||
if schema not in processed_schemas: | ||
processed_schemas.add(schema) | ||
for a in schema.attributes.filter( | ||
attr_type__in=select_types | ||
): | ||
q = quest.questions.get(name=a.name) | ||
opts = {o.name: o.label for o in q.options.all()} | ||
a.choice_labels = [opts[c] for c in a.choices] | ||
a.save() | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('questionnaires', '0005_auto_20160912_0720'), | ||
('jsonattrs', '0002_attribute_choice_labels') | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython( | ||
add_attribute_choice_labels, | ||
reverse_code=migrations.RunPython.noop | ||
) | ||
] |
25 changes: 25 additions & 0 deletions
25
cadasta/questionnaires/migrations/0007_add_question_option_index_field.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# -*- coding: utf-8 -*- | ||
# Generated by Django 1.9.6 on 2016-09-27 13:39 | ||
from __future__ import unicode_literals | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('questionnaires', '0006_add_choice_labels'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='historicalquestionoption', | ||
name='index', | ||
field=models.IntegerField(default=1, null=True), | ||
), | ||
migrations.AddField( | ||
model_name='questionoption', | ||
name='index', | ||
field=models.IntegerField(default=1, null=True), | ||
), | ||
] |
30 changes: 30 additions & 0 deletions
30
cadasta/questionnaires/migrations/0008_populate_question_option_index_field.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# -*- coding: utf-8 -*- | ||
# Generated by Django 1.9.6 on 2016-09-27 13:42 | ||
from __future__ import unicode_literals | ||
import itertools | ||
|
||
from django.db import migrations | ||
|
||
from questionnaires.models import Question | ||
|
||
|
||
def populate_index_fields(apps, schema_editor): | ||
for question in Question.objects.filter(type__in=['S1', 'SM']): | ||
for opt, idx in zip(question.options.order_by('index'), | ||
itertools.count()): | ||
opt.index = idx + 1 | ||
opt.save() | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('questionnaires', '0007_add_question_option_index_field'), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython( | ||
populate_index_fields, | ||
reverse_code=migrations.RunPython.noop | ||
) | ||
] |
29 changes: 29 additions & 0 deletions
29
cadasta/questionnaires/migrations/0009_set_question_option_index_field_properties.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# -*- coding: utf-8 -*- | ||
# Generated by Django 1.9.6 on 2016-09-27 14:31 | ||
from __future__ import unicode_literals | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('questionnaires', '0008_populate_question_option_index_field'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterModelOptions( | ||
name='questionoption', | ||
options={'ordering': ('index',)}, | ||
), | ||
migrations.AlterField( | ||
model_name='historicalquestionoption', | ||
name='index', | ||
field=models.IntegerField(), | ||
), | ||
migrations.AlterField( | ||
model_name='questionoption', | ||
name='index', | ||
field=models.IntegerField(), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters