From a9b478512b10030d727216bbd704729423b179ed Mon Sep 17 00:00:00 2001 From: CJ Green <44074998+okaycj@users.noreply.github.com> Date: Wed, 25 Jan 2023 13:39:49 -0500 Subject: [PATCH] Update model to use bitfield --- accounts/forms.py | 3 ++ .../0055_replace_multiselectfield.py | 47 +++++++++++++++++++ accounts/models.py | 7 +-- 3 files changed, 52 insertions(+), 5 deletions(-) create mode 100644 accounts/migrations/0055_replace_multiselectfield.py diff --git a/accounts/forms.py b/accounts/forms.py index 461d3ae0b..6982951e3 100644 --- a/accounts/forms.py +++ b/accounts/forms.py @@ -365,6 +365,9 @@ class Meta: "us_race_ethnicity_identification_describe": forms.Textarea( attrs={"rows": 2} ), + "us_race_ethnicity_identification": BitFieldCheckboxSelectMultiple( + attrs={"class": "column-checkbox"} + ), } diff --git a/accounts/migrations/0055_replace_multiselectfield.py b/accounts/migrations/0055_replace_multiselectfield.py new file mode 100644 index 000000000..a691e92f1 --- /dev/null +++ b/accounts/migrations/0055_replace_multiselectfield.py @@ -0,0 +1,47 @@ +# Generated by Django 3.2.11 on 2023-01-25 16:20 + +import operator +from functools import reduce + +import bitfield.models +from django.db import migrations + +FIELDS = ( + ("white", "White"), + ("hisp", "Hispanic, Latino, or Spanish origin"), + ("black", "Black or African American"), + ("asian", "Asian"), + ("native", "American Indian or Alaska Native"), + ("mideast-naf", "Middle Eastern or North African"), + ("hawaiian-pac-isl", "Native Hawaiian or Other Pacific Islander"), + ("other", "Another race, ethnicity, or origin"), +) + + +def encode_ethnicity(apps, schema_editor): + fields = [v[0] for v in FIELDS] + for demo_data in apps.get_model("accounts", "DemographicData").objects.all(): + masks = ( + 2 ** fields.index(v) + for v in demo_data.us_race_ethnicity_identification + if v in fields + ) + encoded_value = reduce(operator.__or__, masks, 0) + demo_data.us_race_ethnicity_identification = [encoded_value] + demo_data.save() + + +class Migration(migrations.Migration): + + dependencies = [ + ("accounts", "0054_update_demo_fields"), + ] + + operations = [ + migrations.RunPython(encode_ethnicity), + migrations.AlterField( + model_name="demographicdata", + name="us_race_ethnicity_identification", + field=bitfield.models.BitField(FIELDS, default=0), + ), + ] diff --git a/accounts/models.py b/accounts/models.py index 448e75cb3..e34717797 100644 --- a/accounts/models.py +++ b/accounts/models.py @@ -25,7 +25,6 @@ from localflavor.us.models import USStateField from localflavor.us.us_states import USPS_CHOICES from model_utils import Choices -from multiselectfield import MultiSelectField from qrcode import make as make_qrcode from qrcode.image.svg import SvgPathImage @@ -395,7 +394,7 @@ class JSONAPIMeta: class DemographicData(models.Model): - RACE_CHOICES = Choices( + RACE_CHOICES = ( ("white", _("White")), ("hisp", _("Hispanic, Latino, or Spanish origin")), ("black", _("Black or African American")), @@ -519,9 +518,7 @@ class DemographicData(models.Model): choices=GUARDIAN_CHOICES, max_length=6, blank=True ) guardians_explanation = models.TextField(blank=True) - us_race_ethnicity_identification = MultiSelectField( - choices=RACE_CHOICES, blank=True - ) + us_race_ethnicity_identification = BitField(flags=RACE_CHOICES, default=0) us_race_ethnicity_identification_describe = models.TextField(blank=True) age = models.CharField(max_length=5, choices=AGE_CHOICES, blank=True) gender = models.CharField(max_length=2, choices=GENDER_CHOICES, blank=True)