Skip to content

Commit

Permalink
Update model to use bitfield
Browse files Browse the repository at this point in the history
  • Loading branch information
okaycj committed Apr 24, 2023
1 parent 1813604 commit a9b4785
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
3 changes: 3 additions & 0 deletions accounts/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
),
}


Expand Down
47 changes: 47 additions & 0 deletions accounts/migrations/0055_replace_multiselectfield.py
Original file line number Diff line number Diff line change
@@ -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),
),
]
7 changes: 2 additions & 5 deletions accounts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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")),
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit a9b4785

Please sign in to comment.