mirror of
https://github.com/Southampton-RSG/breccia-mapper.git
synced 2026-03-03 11:27:09 +00:00
refactor: move age and gender to question set
This commit is contained in:
@@ -32,8 +32,6 @@ class PersonForm(forms.ModelForm):
|
|||||||
model = models.Person
|
model = models.Person
|
||||||
fields = [
|
fields = [
|
||||||
'name',
|
'name',
|
||||||
'gender',
|
|
||||||
'age_group',
|
|
||||||
'nationality',
|
'nationality',
|
||||||
'country_of_residence',
|
'country_of_residence',
|
||||||
'organisation',
|
'organisation',
|
||||||
|
|||||||
120
people/migrations/0024_remove_age_gender.py
Normal file
120
people/migrations/0024_remove_age_gender.py
Normal file
@@ -0,0 +1,120 @@
|
|||||||
|
# Generated by Django 2.2.10 on 2020-11-26 13:03
|
||||||
|
|
||||||
|
from django.core.exceptions import ObjectDoesNotExist
|
||||||
|
from django.db import migrations
|
||||||
|
|
||||||
|
from backports.db.models.enums import TextChoices
|
||||||
|
from .utils.question_sets import port_question
|
||||||
|
|
||||||
|
|
||||||
|
class GenderChoices(TextChoices):
|
||||||
|
MALE = 'M', 'Male'
|
||||||
|
FEMALE = 'F', 'Female'
|
||||||
|
OTHER = 'O', 'Other'
|
||||||
|
PREFER_NOT_TO_SAY = 'N', 'Prefer not to say'
|
||||||
|
|
||||||
|
|
||||||
|
class AgeGroupChoices(TextChoices):
|
||||||
|
LTE_25 = '<=25', '25 or under'
|
||||||
|
BETWEEN_26_30 = '26-30', '26-30'
|
||||||
|
BETWEEN_31_35 = '31-35', '31-35'
|
||||||
|
BETWEEN_36_40 = '36-40', '36-40'
|
||||||
|
BETWEEN_41_45 = '41-45', '41-45'
|
||||||
|
BETWEEN_46_50 = '46-50', '46-50'
|
||||||
|
BETWEEN_51_55 = '51-55', '51-55'
|
||||||
|
BETWEEN_56_60 = '56-60', '56-60'
|
||||||
|
GTE_61 = '>=61', '61 or older'
|
||||||
|
PREFER_NOT_TO_SAY = 'N', 'Prefer not to say'
|
||||||
|
|
||||||
|
|
||||||
|
def migrate_forward(apps, schema_editor):
|
||||||
|
Person = apps.get_model('people', 'Person')
|
||||||
|
|
||||||
|
gender_question = port_question(apps, 'Gender', GenderChoices.labels)
|
||||||
|
age_question = port_question(apps, 'Age', AgeGroupChoices.labels)
|
||||||
|
|
||||||
|
for person in Person.objects.all():
|
||||||
|
try:
|
||||||
|
answer_set = person.answer_sets.latest('timestamp')
|
||||||
|
|
||||||
|
except ObjectDoesNotExist:
|
||||||
|
answer_set = person.answer_sets.create()
|
||||||
|
|
||||||
|
try:
|
||||||
|
gender = [
|
||||||
|
item for item in GenderChoices if item.value == person.gender
|
||||||
|
][0]
|
||||||
|
answer_set.question_answers.filter(
|
||||||
|
question__text=gender_question.text).delete()
|
||||||
|
answer_set.question_answers.add(
|
||||||
|
gender_question.answers.get(text__iexact=gender.label))
|
||||||
|
|
||||||
|
except (AttributeError, IndexError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
try:
|
||||||
|
age = [
|
||||||
|
item for item in AgeGroupChoices
|
||||||
|
if item.value == person.age_group
|
||||||
|
][0]
|
||||||
|
answer_set.question_answers.filter(
|
||||||
|
question__text=age_question.text).delete()
|
||||||
|
answer_set.question_answers.add(
|
||||||
|
age_question.answers.get(text__iexact=age.label))
|
||||||
|
|
||||||
|
except (AttributeError, IndexError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def migrate_backward(apps, schema_editor):
|
||||||
|
Person = apps.get_model('people', 'Person')
|
||||||
|
|
||||||
|
for person in Person.objects.all():
|
||||||
|
try:
|
||||||
|
current_answers = person.answer_sets.latest('timestamp')
|
||||||
|
age_answer = current_answers.question_answers.get(
|
||||||
|
question__text='Age')
|
||||||
|
|
||||||
|
person.age_group = [
|
||||||
|
item for item in AgeGroupChoices
|
||||||
|
if item.label == age_answer.text
|
||||||
|
][0].value
|
||||||
|
|
||||||
|
person.save()
|
||||||
|
|
||||||
|
except ObjectDoesNotExist:
|
||||||
|
pass
|
||||||
|
|
||||||
|
try:
|
||||||
|
current_answers = person.answer_sets.latest('timestamp')
|
||||||
|
gender_answer = current_answers.question_answers.get(
|
||||||
|
question__text='Gender')
|
||||||
|
person.gender = [
|
||||||
|
|
||||||
|
item for item in GenderChoices
|
||||||
|
if item.label == gender_answer.text
|
||||||
|
][0].value
|
||||||
|
|
||||||
|
person.save()
|
||||||
|
|
||||||
|
except ObjectDoesNotExist:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('people', '0023_remove_person_role'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.RunPython(migrate_forward, migrate_backward),
|
||||||
|
migrations.RemoveField(
|
||||||
|
model_name='person',
|
||||||
|
name='age_group',
|
||||||
|
),
|
||||||
|
migrations.RemoveField(
|
||||||
|
model_name='person',
|
||||||
|
name='gender',
|
||||||
|
),
|
||||||
|
]
|
||||||
@@ -186,34 +186,6 @@ class Person(models.Model):
|
|||||||
###############################################################
|
###############################################################
|
||||||
# Data collected for analysis of community makeup and structure
|
# Data collected for analysis of community makeup and structure
|
||||||
|
|
||||||
class GenderChoices(TextChoices):
|
|
||||||
MALE = 'M', _('Male')
|
|
||||||
FEMALE = 'F', _('Female')
|
|
||||||
OTHER = 'O', _('Other')
|
|
||||||
PREFER_NOT_TO_SAY = 'N', _('Prefer not to say')
|
|
||||||
|
|
||||||
gender = models.CharField(max_length=1,
|
|
||||||
choices=GenderChoices.choices,
|
|
||||||
blank=True,
|
|
||||||
null=False)
|
|
||||||
|
|
||||||
class AgeGroupChoices(TextChoices):
|
|
||||||
LTE_25 = '<=25', _('25 or under')
|
|
||||||
BETWEEN_26_30 = '26-30', _('26-30')
|
|
||||||
BETWEEN_31_35 = '31-35', _('31-35')
|
|
||||||
BETWEEN_36_40 = '36-40', _('36-40')
|
|
||||||
BETWEEN_41_45 = '41-45', _('41-45')
|
|
||||||
BETWEEN_46_50 = '46-50', _('46-50')
|
|
||||||
BETWEEN_51_55 = '51-55', _('51-55')
|
|
||||||
BETWEEN_56_60 = '56-60', _('56-60')
|
|
||||||
GTE_61 = '>=61', _('61 or older')
|
|
||||||
PREFER_NOT_TO_SAY = 'N', _('Prefer not to say')
|
|
||||||
|
|
||||||
age_group = models.CharField(max_length=5,
|
|
||||||
choices=AgeGroupChoices.choices,
|
|
||||||
blank=True,
|
|
||||||
null=False)
|
|
||||||
|
|
||||||
nationality = CountryField(blank=True, null=True)
|
nationality = CountryField(blank=True, null=True)
|
||||||
|
|
||||||
country_of_residence = CountryField(blank=True, null=True)
|
country_of_residence = CountryField(blank=True, null=True)
|
||||||
|
|||||||
@@ -23,16 +23,6 @@
|
|||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
<dl>
|
<dl>
|
||||||
{% if person.gender %}
|
|
||||||
<dt>Gender</dt>
|
|
||||||
<dd>{{ person.get_gender_display }}</dd>
|
|
||||||
{% endif %}
|
|
||||||
|
|
||||||
{% if person.age_group %}
|
|
||||||
<dt>Age Group</dt>
|
|
||||||
<dd>{{ person.get_age_group_display }}</dd>
|
|
||||||
{% endif %}
|
|
||||||
|
|
||||||
{% if person.nationality %}
|
{% if person.nationality %}
|
||||||
<dt>Nationality</dt>
|
<dt>Nationality</dt>
|
||||||
<dd>{{ person.nationality.name }}</dd>
|
<dd>{{ person.nationality.name }}</dd>
|
||||||
|
|||||||
Reference in New Issue
Block a user