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

(Feature) Add time validation so we support time fields because time is time #167

Merged
merged 1 commit into from
Apr 26, 2023
Merged
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
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 @@ -61,6 +61,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 @@ -226,6 +240,10 @@ def validate_bundle(self, key): # noqa: C901
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)
return
Expand Down