diff --git a/export/serializers/people.py b/export/serializers/people.py index 5b7dd43..463feaa 100644 --- a/export/serializers/people.py +++ b/export/serializers/people.py @@ -32,7 +32,6 @@ class PersonAnswerSetSerializer(base.FlattenedModelSerializer): 'organisation', 'organisation_started_date', 'job_title', - 'disciplines', 'themes', 'latitude', 'longitude', diff --git a/people/forms.py b/people/forms.py index afa2207..6ff2b37 100644 --- a/people/forms.py +++ b/people/forms.py @@ -80,7 +80,6 @@ class PersonAnswerSetForm(forms.ModelForm, DynamicAnswerSetBase): 'organisation', 'organisation_started_date', 'job_title', - 'disciplines', 'themes', 'latitude', 'longitude', diff --git a/people/migrations/0034_remove_personanswerset_disciplines.py b/people/migrations/0034_remove_personanswerset_disciplines.py new file mode 100644 index 0000000..a7224b2 --- /dev/null +++ b/people/migrations/0034_remove_personanswerset_disciplines.py @@ -0,0 +1,63 @@ +# Generated by Django 2.2.10 on 2021-02-15 13:54 + +import re + +from django.db import migrations + +from .utils.question_sets import port_question + + +def migrate_forward(apps, schema_editor): + """Replace discipline text field with admin-editable question.""" + PersonAnswerSet = apps.get_model('people', 'PersonAnswerSet') + + discipline_question = port_question(apps, + 'Disciplines', [], + is_multiple_choice=True, + allow_free_text=True) + + for answerset in PersonAnswerSet.objects.all(): + try: + disciplines = [ + d.strip() for d in re.split(r'[,;]+', answerset.disciplines) + ] + + except TypeError: + continue + + for discipline in disciplines: + answer, _ = discipline_question.answers.get_or_create( + text=discipline) + answerset.question_answers.add(answer) + + +def migrate_backward(apps, schema_editor): + """Replace discipline admin-editable question with text field.""" + PersonAnswerSet = apps.get_model('people', 'PersonAnswerSet') + PersonQuestion = apps.get_model('people', 'PersonQuestion') + + discipline_question = PersonQuestion.objects.filter( + text='Disciplines').latest('version') + + for answerset in PersonAnswerSet.objects.all(): + answerset.disciplines = ', '.join( + answerset.question_answers.filter( + question=discipline_question).values_list('text', flat=True)) + answerset.save() + + PersonQuestion.objects.filter(text='Disciplines').delete() + + +class Migration(migrations.Migration): + + dependencies = [ + ('people', '0033_person_sort_by_name'), + ] + + operations = [ + migrations.RunPython(migrate_forward, migrate_backward), + migrations.RemoveField( + model_name='personanswerset', + name='disciplines', + ), + ] diff --git a/people/migrations/utils/question_sets.py b/people/migrations/utils/question_sets.py index 6dfa082..cb66ff2 100644 --- a/people/migrations/utils/question_sets.py +++ b/people/migrations/utils/question_sets.py @@ -1,21 +1,20 @@ - import typing from django.core.exceptions import ObjectDoesNotExist -def port_question(apps, question_text: str, - answers_text: typing.Iterable[str]): +def port_question(apps, question_text: str, answers_text: typing.Iterable[str], + **kwargs): PersonQuestion = apps.get_model('people', 'PersonQuestion') try: prev_question = PersonQuestion.objects.filter( text=question_text).latest('version') question = PersonQuestion.objects.create( - text=question_text, version=prev_question.version + 1) + text=question_text, version=prev_question.version + 1, **kwargs) except ObjectDoesNotExist: - question = PersonQuestion.objects.create(text=question_text) + question = PersonQuestion.objects.create(text=question_text, **kwargs) for answer_text in answers_text: question.answers.get_or_create(text=answer_text) diff --git a/people/models/person.py b/people/models/person.py index 03ab88b..6543491 100644 --- a/people/models/person.py +++ b/people/models/person.py @@ -190,9 +190,6 @@ class PersonAnswerSet(AnswerSet): #: Job title this person holds within their organisation job_title = models.CharField(max_length=255, blank=True, null=False) - #: Discipline(s) within which this person works - disciplines = models.CharField(max_length=255, blank=True, null=True) - #: Project themes within this person works themes = models.ManyToManyField(Theme, related_name='people', blank=True) diff --git a/people/templates/people/person/includes/answer_set_full.html b/people/templates/people/person/includes/answer_set_full.html index 8bcb346..799464e 100644 --- a/people/templates/people/person/includes/answer_set_full.html +++ b/people/templates/people/person/includes/answer_set_full.html @@ -27,10 +27,6 @@