From 3a37bb4be5ebe387cb174b448bc73185e7c7bb8b Mon Sep 17 00:00:00 2001 From: Regis Camimura Date: Mon, 24 Apr 2023 16:43:42 -0300 Subject: [PATCH] add time validation so we support time fields because time is time --- tests/models.py | 1 + tests/serializers.py | 2 ++ tests/test_validators.py | 2 ++ worf/validators.py | 22 ++++++++++++++++++++-- 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/tests/models.py b/tests/models.py index 12c28e3..974d097 100644 --- a/tests/models.py +++ b/tests/models.py @@ -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) diff --git a/tests/serializers.py b/tests/serializers.py index 91122ab..99b6c52 100644 --- a/tests/serializers.py +++ b/tests/serializers.py @@ -49,6 +49,7 @@ class Meta: "user", "last_active", "created_at", + "start_time", ] writable = [ "id", @@ -71,6 +72,7 @@ class Meta: "user", "last_active", "created_at", + "start_time", ] diff --git a/tests/test_validators.py b/tests/test_validators.py index 0013e4a..76d36a6 100644 --- a/tests/test_validators.py +++ b/tests/test_validators.py @@ -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}/") @@ -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") diff --git a/worf/validators.py b/worf/validators.py index f4e8602..09be2bb 100644 --- a/worf/validators.py +++ b/worf/validators.py @@ -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 @@ -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] @@ -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)