from django.db import models from django.core.exceptions import ValidationError from common.fields import UpperCaseCharField # Create your models here. class NomenclatureBaseModel(models.Model): name = UpperCaseCharField(max_length=100) short_name = UpperCaseCharField(max_length=5, null=True, blank=True) description = models.TextField(blank=True, null=True) active = models.BooleanField(default=True) class Meta: abstract = True ordering = ['name'] def __str__(self): return self.name def clean(self): if not self.name: raise ValidationError('Name cannot be empty.') if self.short_name and len(self.short_name) > 5: raise ValidationError('Short name cannot exceed 5 characters.') if self.description and len(self.description) > 255: raise ValidationError('Description cannot exceed 255 characters.') class CompanyModel(NomenclatureBaseModel): def clean(self): super().clean() if CompanyModel.objects.filter(name=self.name, active=True).exclude(id=self.id).exists(): raise ValidationError(f'Company with name {self.name} already exists and is active.') if CompanyModel.objects.filter(short_name=self.short_name, active=True).exclude(id=self.id).exists(): raise ValidationError(f'Company with short name {self.short_name} already exists and is active.') class LinesModel(NomenclatureBaseModel): company = models.ForeignKey( 'common.CompanyModel', on_delete=models.CASCADE, related_name='line_company' ) def clean(self): super().clean() if LinesModel.objects.filter(name=self.name, active=True).exclude(id=self.id).exists(): raise ValidationError(f'Line with name {self.name} already exists and is active.') if LinesModel.objects.filter(short_name=self.short_name, active=True).exclude(id=self.id).exists(): raise ValidationError(f'Line with short name {self.short_name} already exists and is active.') class OperationModel(NomenclatureBaseModel): ... class ContainerKindModel(NomenclatureBaseModel): ... class ContainerTypeModel(models.Model): name = models.CharField(max_length=6, unique=True) length = models.IntegerField() height = models.BooleanField() container_type = models.ForeignKey( 'common.ContainerKindModel', on_delete=models.CASCADE, related_name='container_type_container_kinds' ) deleted = models.BooleanField(default=False) class Meta: app_label = 'common' def __str__(self): return self.name