Skip to content

Commit

Permalink
Support non-US plates, prepare to drop localflavor
Browse files Browse the repository at this point in the history
We also narrow the set of license plate "states" down to just the 50
states and DC. While one should support American Samoa, and US military
plates in the *general* case, they should realistically never come up
when we're recording the license plates of cars that are being used to
drive to New Hampshire from Boston.

Indeed, of the many thousands of cars we have in the system already,
*all* fall under the 50 states or Canada.

We have 42 states represented, including DC. States that are missing:

- Arkansas
- Iowa
- Mississippi
- Nebraska
- North Dakota
- South Dakota
- West Virginia
- Wyoming

To truly drop `localflavor`, we'll need to squash migrations again.
  • Loading branch information
DavidCain committed Oct 2, 2023
1 parent 25144b5 commit e35b264
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 4 deletions.
70 changes: 70 additions & 0 deletions ws/car_states.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
"""
Provide a *simple* enumeration of US states without third-party deps.
We used to rely on `localflavor` for this, but given that plates are
the *only* use for this enum, it's easy/simple to just enumerate.
"""
from typing import Final

US_STATES: Final[tuple[tuple[str, str], ...]] = (
('AL', 'Alabama'),
('AK', 'Alaska'),
('AZ', 'Arizona'),
('AR', 'Arkansas'),
('CA', 'California'),
('CO', 'Colorado'),
('CT', 'Connecticut'),
('DE', 'Delaware'),
('DC', 'District of Columbia'),
('FL', 'Florida'),
('GA', 'Georgia'),
('HI', 'Hawaii'),
('ID', 'Idaho'),
('IL', 'Illinois'),
('IN', 'Indiana'),
('IA', 'Iowa'),
('KS', 'Kansas'),
('KY', 'Kentucky'),
('LA', 'Louisiana'),
('ME', 'Maine'),
('MD', 'Maryland'),
('MA', 'Massachusetts'),
('MI', 'Michigan'),
('MN', 'Minnesota'),
('MS', 'Mississippi'),
('MO', 'Missouri'),
('MT', 'Montana'),
('NE', 'Nebraska'),
('NV', 'Nevada'),
('NH', 'New Hampshire'),
('NJ', 'New Jersey'),
('NM', 'New Mexico'),
('NY', 'New York'),
('NC', 'North Carolina'),
('ND', 'North Dakota'),
('OH', 'Ohio'),
('OK', 'Oklahoma'),
('OR', 'Oregon'),
('PA', 'Pennsylvania'),
('RI', 'Rhode Island'),
('SC', 'South Carolina'),
('SD', 'South Dakota'),
('TN', 'Tennessee'),
('TX', 'Texas'),
('UT', 'Utah'),
('VT', 'Vermont'),
('VA', 'Virginia'),
('WA', 'Washington'),
('WV', 'West Virginia'),
('WI', 'Wisconsin'),
('WY', 'Wyoming'),
)


CAR_STATE_CHOICES: Final[tuple[tuple[str, str], ...]] = (
*US_STATES,
# License plates are *only* used for search & rescue
# So long as the plate number is correct, we needn't be more specific.
# In nine years, we've only seen one Canadian license plate requested.
('XX', 'Other (Canada, Mexico, etc.)'),
)
2 changes: 0 additions & 2 deletions ws/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from django import forms
from django.core.exceptions import ValidationError
from django.db.models.fields import TextField
from localflavor.us.us_states import US_STATES
from mitoc_const import affiliations

from ws import enums, models, widgets
Expand Down Expand Up @@ -165,7 +164,6 @@ class Meta:
model = models.Car
fields = ['license_plate', 'state', 'make', 'model', 'year', 'color']
widgets = {
'state': forms.Select(choices=US_STATES),
'year': forms.NumberInput(
attrs={'min': model.year_min, 'max': model.year_max}
),
Expand Down
85 changes: 85 additions & 0 deletions ws/migrations/0056_car.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Generated by Django 3.2.21 on 2023-10-02 13:42

import django.core.validators
import django.db.models.manager
from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
('ws', '0055_discount_url_non_null'),
]

operations = [
migrations.AlterField(
model_name='car',
name='state',
field=models.CharField(
choices=[
('AL', 'Alabama'),
('AK', 'Alaska'),
('AZ', 'Arizona'),
('AR', 'Arkansas'),
('CA', 'California'),
('CO', 'Colorado'),
('CT', 'Connecticut'),
('DE', 'Delaware'),
('DC', 'District of Columbia'),
('FL', 'Florida'),
('GA', 'Georgia'),
('HI', 'Hawaii'),
('ID', 'Idaho'),
('IL', 'Illinois'),
('IN', 'Indiana'),
('IA', 'Iowa'),
('KS', 'Kansas'),
('KY', 'Kentucky'),
('LA', 'Louisiana'),
('ME', 'Maine'),
('MD', 'Maryland'),
('MA', 'Massachusetts'),
('MI', 'Michigan'),
('MN', 'Minnesota'),
('MS', 'Mississippi'),
('MO', 'Missouri'),
('MT', 'Montana'),
('NE', 'Nebraska'),
('NV', 'Nevada'),
('NH', 'New Hampshire'),
('NJ', 'New Jersey'),
('NM', 'New Mexico'),
('NY', 'New York'),
('NC', 'North Carolina'),
('ND', 'North Dakota'),
('OH', 'Ohio'),
('OK', 'Oklahoma'),
('OR', 'Oregon'),
('PA', 'Pennsylvania'),
('RI', 'Rhode Island'),
('SC', 'South Carolina'),
('SD', 'South Dakota'),
('TN', 'Tennessee'),
('TX', 'Texas'),
('UT', 'Utah'),
('VT', 'Vermont'),
('VA', 'Virginia'),
('WA', 'Washington'),
('WV', 'West Virginia'),
('WI', 'Wisconsin'),
('WY', 'Wyoming'),
('XX', 'Other (Canada, Mexico, etc.)'),
],
max_length=2,
),
),
migrations.AlterField(
model_name='car',
name='year',
field=models.PositiveIntegerField(
validators=[
django.core.validators.MaxValueValidator(2025),
django.core.validators.MinValueValidator(1903),
]
),
),
]
4 changes: 2 additions & 2 deletions ws/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@
from django.utils import timezone
from django.utils.safestring import mark_safe
from django.utils.text import format_lazy
from localflavor.us.models import USStateField
from mitoc_const import affiliations
from mitoc_const.membership import RENEWAL_ALLOWED_WITH_DAYS_LEFT
from phonenumber_field.modelfields import PhoneNumberField
from typing_extensions import Self

import ws.utils.dates as date_utils
from ws import enums
from ws.car_states import CAR_STATE_CHOICES
from ws.utils.avatar import avatar_url

alphanum = RegexValidator(
Expand Down Expand Up @@ -63,7 +63,7 @@ class Car(models.Model):
year_min, year_max = 1903, date_utils.local_now().year + 2
# Loosely validate - may wish to use international plates in the future
license_plate = models.CharField(max_length=31, validators=[alphanum])
state = USStateField()
state = models.CharField(max_length=2, choices=CAR_STATE_CHOICES)
make = models.CharField(max_length=63)
model = models.CharField(max_length=63)
year = models.PositiveIntegerField(
Expand Down

0 comments on commit e35b264

Please sign in to comment.