71 lines
1.7 KiB
Python
71 lines
1.7 KiB
Python
from django.db import models
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
from django.core.validators import (
|
|
MaxLengthValidator,
|
|
MinLengthValidator,
|
|
)
|
|
|
|
from core.utils.base_model import UUIDPrimaryKeyBaseModel
|
|
from core.apps.companies.models.companies import CompanyModel
|
|
from core.apps.companies.validators.folders import (
|
|
CompanyFolderValidator
|
|
)
|
|
|
|
from core.apps.contracts.models import ContractModel
|
|
|
|
|
|
class CompanyFolderModel(UUIDPrimaryKeyBaseModel):
|
|
name = models.CharField(
|
|
_("name"),
|
|
max_length=150,
|
|
validators=[
|
|
MaxLengthValidator(150),
|
|
MinLengthValidator(3),
|
|
]
|
|
)
|
|
|
|
company = models.ForeignKey(
|
|
CompanyModel,
|
|
on_delete=models.PROTECT,
|
|
related_name="folders",
|
|
null=False,
|
|
blank=False
|
|
)
|
|
|
|
contracts = models.ManyToManyField( # type: ignore
|
|
ContractModel,
|
|
verbose_name=_("Contracts"),
|
|
related_name="folders",
|
|
null=True,
|
|
blank=True
|
|
)
|
|
|
|
def __str__(self):
|
|
return f"{self.name} {self.company!s}"
|
|
|
|
@classmethod
|
|
def _create_fake(cls):
|
|
return cls.objects.create(
|
|
name="mock",
|
|
company=CompanyModel._create_fake() # type: ignore
|
|
)
|
|
|
|
def clean(self) -> None:
|
|
super().clean()
|
|
validator = CompanyFolderValidator(self)
|
|
validator()
|
|
|
|
class Meta: # type: ignore
|
|
db_table = "company_folders"
|
|
|
|
verbose_name = _("Company Folder")
|
|
verbose_name_plural = _("Company Folders")
|
|
|
|
constraints = [
|
|
models.UniqueConstraint(
|
|
fields=["name", "company"],
|
|
name="company_folders_name_company_unique_constraint"
|
|
)
|
|
]
|