initial commit
This commit is contained in:
4
core/apps/contracts/validators/__init__.py
Normal file
4
core/apps/contracts/validators/__init__.py
Normal file
@@ -0,0 +1,4 @@
|
||||
from .attached_files import * # noqa
|
||||
from .contracts import * # noqa
|
||||
from .file_contents import * # noqa
|
||||
from .owners import * # noqa
|
||||
34
core/apps/contracts/validators/attached_files.py
Normal file
34
core/apps/contracts/validators/attached_files.py
Normal file
@@ -0,0 +1,34 @@
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.core.validators import RegexValidator
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from django_core.models.base import AbstractBaseModel # type: ignore
|
||||
|
||||
|
||||
def starts_with_letter_validator(value: str) -> None:
|
||||
"""
|
||||
Checks if the value starts with a letter.
|
||||
"""
|
||||
if value[0].isalpha():
|
||||
return
|
||||
|
||||
raise ValidationError(
|
||||
_("File name must start with a letter."),
|
||||
code="invalid_start",
|
||||
)
|
||||
|
||||
|
||||
allowed_chars_validator = RegexValidator(
|
||||
regex=r"^[A-Za-z0-9\s._-]+$",
|
||||
message=_(
|
||||
"File name can only contain letters, " \
|
||||
"digits, spaces, dots, underscores, or hyphens."
|
||||
),
|
||||
code="invalid_characters",
|
||||
)
|
||||
|
||||
|
||||
class ContractAttachedFileValidator:
|
||||
def __init__(self): ...
|
||||
|
||||
def __call__(self):
|
||||
return True
|
||||
23
core/apps/contracts/validators/contracts.py
Normal file
23
core/apps/contracts/validators/contracts.py
Normal file
@@ -0,0 +1,23 @@
|
||||
# from django.core.exceptions import ValidationError
|
||||
from django.core.validators import RegexValidator
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from django_core.models.base import AbstractBaseModel # type: ignore
|
||||
|
||||
|
||||
name_validator = RegexValidator(
|
||||
regex=r"^[A-Za-z0-9À-ÿ&()\-.,'\" ]{3,100}$",
|
||||
message=_(
|
||||
"Enter a valid contract name. "
|
||||
"Use letters, numbers, spaces, and "
|
||||
"limited punctuation (e.g., &, -, (), ., ', \"). "
|
||||
"Length must be 3 to 100 characters."
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
class ContractValidator:
|
||||
def __init__(self, instance: AbstractBaseModel):
|
||||
self.instance = instance
|
||||
|
||||
def __call__(self):
|
||||
return True
|
||||
8
core/apps/contracts/validators/file_contents.py
Normal file
8
core/apps/contracts/validators/file_contents.py
Normal file
@@ -0,0 +1,8 @@
|
||||
# from django.core.exceptions import ValidationError
|
||||
|
||||
|
||||
class ContractfilecontentValidator:
|
||||
def __init__(self): ...
|
||||
|
||||
def __call__(self):
|
||||
return True
|
||||
146
core/apps/contracts/validators/owners.py
Normal file
146
core/apps/contracts/validators/owners.py
Normal file
@@ -0,0 +1,146 @@
|
||||
import re
|
||||
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from django_core.models.base import AbstractBaseModel # type: ignore
|
||||
from django.core.validators import RegexValidator
|
||||
|
||||
|
||||
full_name_validator = RegexValidator(
|
||||
regex=r"^[A-Z][a-z]+ [A-Z][a-z]+ [A-Z][a-z]+$",
|
||||
message=_(
|
||||
"Invalid Full Name, Please enter Full Name in the "
|
||||
"format: <first name> <last name> <father name>"
|
||||
)
|
||||
)
|
||||
|
||||
iin_code_validator = RegexValidator(
|
||||
regex=r'^\d{14}$',
|
||||
message=_("IIN code must consist of exactly 14 digits."),
|
||||
code='invalid_iin'
|
||||
)
|
||||
|
||||
|
||||
person_code_validator = RegexValidator(
|
||||
regex=r'^[A-Za-z0-9_-]{3,64}$',
|
||||
message=_(
|
||||
"Person Code must be 3 to 64 characters long and contain"
|
||||
" only letters, digits, dashes, or underscores."
|
||||
),
|
||||
code='invalid_person_code'
|
||||
)
|
||||
|
||||
|
||||
phone_validator = RegexValidator(
|
||||
regex=r'^\+?[1-9]\d{1,14}$',
|
||||
message=_(
|
||||
"Enter a valid international phone number "
|
||||
"(E.164 format, e.g., +14155552671)."
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def bin_code_validator(value: str):
|
||||
if not value.isdigit():
|
||||
raise ValidationError(_("BIN code must contain only digits."))
|
||||
|
||||
if len(value) != 14:
|
||||
raise ValidationError(_("BIN code must be exactly 14 digits long."))
|
||||
|
||||
if not re.match(r"^\d{14}$", value):
|
||||
raise ValidationError(_("Invalid BIN code format."))
|
||||
|
||||
# Optional: check if first 6 digits represent a valid date (YYMMDD)
|
||||
try:
|
||||
from datetime import datetime
|
||||
datetime.strptime(value[:6], "%y%m%d")
|
||||
except ValueError:
|
||||
raise ValidationError(_("BIN code starts with an invalid date."))
|
||||
|
||||
|
||||
def name_validator(value: str):
|
||||
if len(value.strip()) < 3:
|
||||
raise ValidationError(_("Name is too short."))
|
||||
|
||||
if re.fullmatch(r"(.)\1{2,}", value): # e.g., "aaa", "!!!"
|
||||
raise ValidationError(_("Name cannot contain excessive repeated characters."))
|
||||
|
||||
if not re.match(r"^[A-Za-z0-9&\-\.\,\(\)\s]+$", value):
|
||||
raise ValidationError(_("Name contains invalid characters. Only letters, numbers, spaces, and symbols like . , & - ( ) are allowed."))
|
||||
|
||||
if value.lower() in {"test", "company", "name", "example", "sample"}:
|
||||
raise ValidationError(_("This name is too generic. Please choose a more specific name."))
|
||||
|
||||
if value.isupper() or value.islower():
|
||||
raise ValidationError(_("Name must have both uppercase and lowercase letters."))
|
||||
|
||||
if not value[0].isupper():
|
||||
raise ValidationError(_("Name should start with a capital letter."))
|
||||
|
||||
if len(set(value.lower())) < 4:
|
||||
raise ValidationError(_("Name is too repetitive or lacks meaningful structure."))
|
||||
|
||||
|
||||
class IndividualValidator:
|
||||
def __init__(self, instance: AbstractBaseModel):
|
||||
self.instance = instance
|
||||
|
||||
def __call__(self):
|
||||
if (
|
||||
self.instance.iin_code is None # type: ignore
|
||||
and self.instance.person_code is None # type: ignore
|
||||
):
|
||||
raise ValidationError(_(
|
||||
"Either IIN code or Person Code must be provided."
|
||||
))
|
||||
|
||||
if (
|
||||
self.instance.iin_code is not None # type: ignore
|
||||
and self.instance.person_code is not None # type: ignore
|
||||
):
|
||||
raise ValidationError(_(
|
||||
"One of IIN code or Person code must be empty"
|
||||
))
|
||||
|
||||
|
||||
class LegalEntityValidator:
|
||||
def __init__(self, instance: AbstractBaseModel):
|
||||
self.instance = instance
|
||||
|
||||
def __call__(self):
|
||||
|
||||
if (
|
||||
self.instance.identifier is None # type: ignore
|
||||
and self.instance.bin_code is None # type: ignore
|
||||
):
|
||||
raise ValidationError(_(
|
||||
"Either Identifier or BIN code "
|
||||
"must contain a value"
|
||||
))
|
||||
|
||||
if (
|
||||
self.instance.identifier is not None # type: ignore
|
||||
and self.instance.bin_code is not None # type: ignore
|
||||
):
|
||||
raise ValidationError(_(
|
||||
"One of Indentifier or BIN code "
|
||||
"must be empty"
|
||||
))
|
||||
|
||||
|
||||
class ContractOwnerValidator:
|
||||
def __init__(self, instance: AbstractBaseModel):
|
||||
self.instance = instance
|
||||
|
||||
def __call__(self):
|
||||
if (
|
||||
self.instance.legal_entity is None # type: ignore
|
||||
and self.instance.individual is None # type: ignore
|
||||
):
|
||||
raise ValidationError("Either entity or individual should be not None")
|
||||
|
||||
if (
|
||||
self.instance.legal_entity is not None # type: ignore
|
||||
and self.instance.individual is not None # type: ignore
|
||||
):
|
||||
raise ValidationError("One of Individual or Legal entity should be given")
|
||||
Reference in New Issue
Block a user