Skip to content

Commit

Permalink
add time validation so we support time fields because time is time
Browse files Browse the repository at this point in the history
  • Loading branch information
regiscamimura committed Apr 26, 2023
1 parent 22d5618 commit 3a37bb4
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
1 change: 1 addition & 0 deletions tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Profile(models.Model):

last_active = models.DateField(blank=True, null=True)
created_at = models.DateTimeField(blank=True, null=True)
start_time = models.TimeField(blank=True, null=True)

is_subscribed = models.BooleanField(blank=True, null=True)
subscribed_at = models.DateTimeField(blank=True, null=True)
Expand Down
2 changes: 2 additions & 0 deletions tests/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class Meta:
"user",
"last_active",
"created_at",
"start_time",
]
writable = [
"id",
Expand All @@ -71,6 +72,7 @@ class Meta:
"user",
"last_active",
"created_at",
"start_time",
]


Expand Down
2 changes: 2 additions & 0 deletions tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def profile_view_fixture(db, now, profile_factory, rf):
recovery_phone=phone,
last_active=now().date().isoformat(),
created_at=now().isoformat(),
start_time=now().time().isoformat(),
)
)
view.request = rf.patch(f"/{uuid}/")
Expand Down Expand Up @@ -61,6 +62,7 @@ def test_validate_bundle(profile_view):
profile_view.validate_bundle("small_integer")
profile_view.validate_bundle("recovery_email")
profile_view.validate_bundle("last_active")
profile_view.validate_bundle("start_time")
profile_view.validate_bundle("created_at")


Expand Down
22 changes: 20 additions & 2 deletions worf/validators.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from datetime import datetime
from datetime import datetime, time
from uuid import UUID

from django.core.exceptions import ValidationError
from django.core.validators import validate_email
from django.db import models
from django.utils.dateparse import parse_datetime
from django.utils.dateparse import parse_datetime, parse_time

from worf.conf import settings

Expand Down Expand Up @@ -60,6 +60,20 @@ def _validate_datetime(self, key):
)

return coerced

def _validate_time(self, key):
value = self.bundle[key]
coerced = None

if isinstance(value, str):
coerced = parse_time(value)

if not isinstance(coerced, time):
raise ValidationError(
f"Field {self.keymap[key]} accepts a iso time string, got {value}, coerced to {coerced}"
)

return coerced

def _validate_many_to_many(self, key):
value = self.bundle[key]
Expand Down Expand Up @@ -225,6 +239,10 @@ def validate_bundle(self, key): # noqa: C901
elif isinstance(field, models.DateField):
self.bundle[key] = self._validate_date(key)
return

elif isinstance(field, models.TimeField):
self.bundle[key] = self._validate_time(key)
return

elif isinstance(field, models.ManyToManyField):
self._validate_many_to_many(key)
Expand Down

0 comments on commit 3a37bb4

Please sign in to comment.