Skip to content

Commit f06e647

Browse files
committed
model.Service: move validation logic in validators
1 parent bce6349 commit f06e647

File tree

2 files changed

+16
-31
lines changed

2 files changed

+16
-31
lines changed

appointment/models.py

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,14 @@ class Service(models.Model):
6464
Version: 1.1.0
6565
Since: 1.0.0
6666
"""
67-
name = models.CharField(max_length=100)
67+
name = models.CharField(max_length=100, blank=False)
6868
description = models.TextField(blank=True, null=True)
69-
duration = models.DurationField()
69+
duration = models.DurationField(validators=[MinValueValidator(datetime.timedelta(seconds=1))])
7070
price = models.DecimalField(max_digits=6, decimal_places=2, validators=[MinValueValidator(0)])
7171
down_payment = models.DecimalField(max_digits=6, decimal_places=2, default=0, validators=[MinValueValidator(0)])
7272
image = models.ImageField(upload_to='services/', blank=True, null=True)
7373
currency = models.CharField(max_length=3, default='USD', validators=[MaxLengthValidator(3), MinLengthValidator(3)])
74-
background_color = models.CharField(max_length=50, null=True, blank=True, default="")
74+
background_color = models.CharField(max_length=50, null=True, blank=True, default=generate_rgb_color)
7575
reschedule_limit = models.PositiveIntegerField(
7676
default=0,
7777
help_text=_("Maximum number of times an appointment can be rescheduled.")
@@ -88,22 +88,6 @@ class Service(models.Model):
8888
def __str__(self):
8989
return self.name
9090

91-
def save(self, *args, **kwargs):
92-
# duration shouldn't be negative or equal to 0
93-
if self.duration <= datetime.timedelta(seconds=0):
94-
raise ValidationError(_("Duration must be greater than 0"))
95-
# name shouldn't be empty
96-
if self.name == "":
97-
raise ValidationError(_("Name cannot be empty"))
98-
# price shouldn't be negative
99-
if self.price < 0:
100-
raise ValidationError(_("Price cannot be negative"))
101-
if self.down_payment < 0:
102-
raise ValidationError(_("Down payment cannot be negative"))
103-
if self.background_color == "":
104-
self.background_color = generate_rgb_color()
105-
return super().save(*args, **kwargs)
106-
10791
def to_dict(self):
10892
return {
10993
"id": self.id,

appointment/tests/models/test_model_service.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ def test_invalid_service_currency_length(self):
137137

138138
def test_service_duration_zero(self):
139139
"""A service cannot be created with a duration of zero."""
140-
with self.assertRaises(ValidationError):
141-
Service.objects.create(name="Test Service", duration=timedelta(), price=100)
140+
service = Service(name="Test Service", duration=timedelta(0), price=100)
141+
self.assertRaises(ValidationError, service.full_clean)
142142

143143
def test_price_and_down_payment_same(self):
144144
"""A service can be created with a price and down payment of the same value."""
@@ -148,30 +148,31 @@ def test_price_and_down_payment_same(self):
148148
def test_service_with_no_name(self):
149149
"""A service cannot be created with no name."""
150150
with self.assertRaises(ValidationError):
151-
Service.objects.create(name="", duration=timedelta(hours=1), price=100)
151+
Service.objects.create(name="", duration=timedelta(hours=1), price=100).full_clean()
152152

153153
def test_service_with_invalid_duration(self):
154154
"""Service should not be created with a negative or zero duration."""
155-
with self.assertRaises(ValidationError):
156-
Service.objects.create(name="Invalid Duration Service", duration=timedelta(seconds=-1), price=50)
157-
with self.assertRaises(ValidationError):
158-
Service.objects.create(name="Zero Duration Service", duration=timedelta(seconds=0), price=50)
155+
service = Service(name="Invalid Duration Service", duration=timedelta(seconds=-1), price=50)
156+
self.assertRaises(ValidationError, service.full_clean)
157+
service = Service(name="Zero Duration Service", duration=timedelta(seconds=0), price=50)
158+
self.assertRaises(ValidationError, service.full_clean)
159159

160160
def test_service_with_empty_name(self):
161161
"""Service should not be created with an empty name."""
162-
with self.assertRaises(ValidationError):
163-
Service.objects.create(name="", duration=timedelta(hours=1), price=50)
162+
service = Service.objects.create(name="", duration=timedelta(hours=1), price=50)
163+
self.assertRaises(ValidationError, service.full_clean)
164164

165165
def test_service_with_negative_price(self):
166166
"""Service should not be created with a negative price."""
167-
with self.assertRaises(ValidationError):
168-
Service.objects.create(name="Negative Price Service", duration=timedelta(hours=1), price=-1)
167+
service = Service(name="Negative Price Service", duration=timedelta(hours=1), price=-1)
168+
self.assertRaises(ValidationError, service.full_clean)
169169

170170
def test_service_with_negative_down_payment(self):
171171
"""Service should not have a negative down payment."""
172172
with self.assertRaises(ValidationError):
173-
Service.objects.create(name="Service with Negative Down Payment", duration=timedelta(hours=1), price=50,
173+
service = Service(name="Service with Negative Down Payment", duration=timedelta(hours=1), price=50,
174174
down_payment=-1)
175+
service.full_clean()
175176

176177
def test_service_auto_generate_background_color(self):
177178
"""Service should auto-generate a background color if none is provided."""

0 commit comments

Comments
 (0)