Skip to content

Commit 7cbc6d2

Browse files
committed
feat(utilities): add paths and validators
1 parent c374002 commit 7cbc6d2

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

apps/utilities/paths.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""Paths for Utilities App."""
2+
3+
from django.utils.text import slugify
4+
5+
6+
def image_path(instance, filename):
7+
"""Generates storage path for associated model images."""
8+
# appname = apps.contents.models
9+
appname = instance.__class__.__module__.split(".")[1]
10+
modelname = instance.__class__.__name__.lower()
11+
extension = filename.split(".")[-1]
12+
name_slug = slugify(instance.name)[:50]
13+
filename = f"{name_slug}.{extension}"
14+
15+
return f"{appname}/{modelname}/{filename}"

apps/utilities/validators.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
""""Validators for Drivers App."""
2+
3+
from datetime import date
4+
from django.core.exceptions import ValidationError
5+
from django.core.validators import RegexValidator
6+
7+
8+
def validate_phone(value):
9+
"""Validate a phone number according to the Chilean format."""
10+
validator = RegexValidator(
11+
regex=r'^\+56\d{9}$',
12+
message="Invalid phone number.",
13+
code="invalid_name"
14+
)
15+
validator(value)
16+
17+
18+
def validate_birth_date(value):
19+
"""Validate that the person is at least 18 years old."""
20+
if (date.today() - value).days < 6570: # 6570 days = 18 years
21+
raise ValidationError(
22+
"You must be at least 18 years old to register as a driver."
23+
)

0 commit comments

Comments
 (0)