Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace multiselectfield #1072

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
110 changes: 89 additions & 21 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ celery = "4.4.7"
ciso8601 = "2.1.3"
Django = "3.2.11"
django-ace-overlay = { git = "https://github.com/lookit/django-ace-overlay.git", branch = "master" }
django-bitfield = "2.1.0"
django-bootstrap5 = "22.2"
django-allauth = "0.42.0"
django-bitfield = "2.2.0"
django-celery-beat = "2.0.0"
django-cors-headers = "3.13.0"
django-countries = "7.2.1"
Expand All @@ -21,7 +22,6 @@ django-filter = "2.4.0"
django-guardian = "2.3.0"
django-localflavor = "3.1"
django-model-utils = "4.0.0"
django-multiselectfield = "0.1.12"
django-pandas = "0.6.2"
django-prettyjson = "0.4.1"
django-revproxy = { git = "https://github.com/Innovativity/django-revproxy.git", branch = "b9fa8375d03fd68747dcb7273a97c19d788aa51b" }
Expand Down
5 changes: 5 additions & 0 deletions web/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,15 @@ def test_demographic_data_update_authenticated(self):

# Update data and save
data["country"] = "BR"

# Setting ethnicity to None fixes issue with submitting data.
data["us_race_ethnicity_identification"] = None

cleaned_data = {key: val for (key, val) in data.items() if val is not None}
response = self.client.post(
reverse("web:demographic-data-update"), cleaned_data, follow=True
)
self.assertEqual(response.context["form"].errors, {})
self.assertEqual(
response.redirect_chain, [(reverse("web:demographic-data-update"), 302)]
)
Expand Down