Skip to content

Commit

Permalink
Eregcsc 2037 update resource use fields (#899)
Browse files Browse the repository at this point in the history
* EREGCSC-2037 update eregs to use fields.py

* add migration file
  • Loading branch information
peggles2 authored Jul 21, 2023
1 parent b4de477 commit 8a866d2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 31 deletions.
25 changes: 25 additions & 0 deletions solution/backend/resources/migrations/0030_auto_20230720_1427.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 3.2.19 on 2023-07-20 14:27

import common.fields
import django.core.validators
from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('resources', '0029_alter_federalregisterdocument_doc_type'),
]

operations = [
migrations.AlterField(
model_name='federalregisterdocument',
name='date',
field=common.fields.VariableDateField(blank=True, help_text='Leave blank or enter one of: "YYYY", "YYYY-MM", or "YYYY-MM-DD".', max_length=10, null=True, validators=[common.fields.validate_date, django.core.validators.RegexValidator(message='Date field must be blank or of the format "YYYY", "YYYY-MM", or "YYYY-MM-DD". For example: 2021, 2021-01, or 2021-01-31.', regex='^\\d{4}((-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01]))|(-(0[1-9]|1[0-2])))?$')]),
),
migrations.AlterField(
model_name='supplementalcontent',
name='date',
field=common.fields.VariableDateField(blank=True, help_text='Leave blank or enter one of: "YYYY", "YYYY-MM", or "YYYY-MM-DD".', max_length=10, null=True, validators=[common.fields.validate_date, django.core.validators.RegexValidator(message='Date field must be blank or of the format "YYYY", "YYYY-MM", or "YYYY-MM-DD". For example: 2021, 2021-01, or 2021-01-31.', regex='^\\d{4}((-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01]))|(-(0[1-9]|1[0-2])))?$')]),
),
]
34 changes: 3 additions & 31 deletions solution/backend/resources/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import datetime
from model_utils.managers import InheritanceManager, InheritanceQuerySet
from django.core.validators import RegexValidator
from django.core.exceptions import ValidationError, NON_FIELD_ERRORS
from common.fields import VariableDateField
from django.db import models
from django_jsonform.models.fields import ArrayField
from django.db.models.signals import post_save
Expand All @@ -19,40 +18,13 @@ class Meta:
abstract = True


class DateFieldMixin(models.Model):
date = models.CharField(
max_length=10,
null=True,
blank=True,
help_text="Leave blank or enter one of: \"YYYY\", \"YYYY-MM\", or \"YYYY-MM-DD\".",
validators=[RegexValidator(
regex="^\\d{4}((-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01]))|(-(0[1-9]|1[0-2])))?$",
message="Date field must be blank or of format \"YYYY\", \"YYYY-MM\", or \"YYYY-MM-DD\"! "
"For example: 2021, 2021-01, or 2021-01-31.",
)],
)

def clean(self):
# If a day is entered into the date field, validate for months with less than 31 days.
if self.date is not None:
date_fields = self.date.split("-")
if len(date_fields) == 3:
(year, month, day) = date_fields
try:
_ = datetime.date(int(year), int(month), int(day))
except ValueError:
raise ValidationError(f'{day} is not a valid day for the month of {month}!')

class Meta:
abstract = True


class TypicalResourceFieldsMixin(DateFieldMixin, InternalNotesFieldMixin):
class TypicalResourceFieldsMixin(InternalNotesFieldMixin):
name = models.CharField(max_length=512, null=True, blank=True)
description = models.TextField(null=True, blank=True)
url = models.URLField(max_length=512, null=True, blank=True)
name_sort = NaturalSortField('name', null=True)
description_sort = NaturalSortField('description', null=True)
date = VariableDateField(null=True, blank=True)

class Meta:
abstract = True
Expand Down

0 comments on commit 8a866d2

Please sign in to comment.