some fields validation and model cleaning
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,29 @@
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.db import models
|
||||
|
||||
|
||||
class UpperCaseCharField(models.CharField):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
def get_prep_value(self, value):
|
||||
value = super().get_prep_value(value)
|
||||
if value is not None:
|
||||
return value.upper()
|
||||
return value
|
||||
|
||||
|
||||
class ContainerNumberField(UpperCaseCharField):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
def get_prep_value(self, value):
|
||||
value = super().get_prep_value(value)
|
||||
if value is not None:
|
||||
return value.upper()
|
||||
return value
|
||||
|
||||
def validate(self, value, model_instance):
|
||||
super().validate(value, model_instance)
|
||||
if value and len(value) != 11:
|
||||
raise ValidationError('Container number must be exactly 11 characters long.')
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
Binary file not shown.
Binary file not shown.
+30
-8
@@ -1,22 +1,39 @@
|
||||
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 = models.CharField(max_length=100, unique=True)
|
||||
short_name = models.CharField(max_length=5, null=True, blank=True)
|
||||
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']
|
||||
app_label = 'common'
|
||||
|
||||
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):
|
||||
class Meta:
|
||||
app_label = 'common'
|
||||
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):
|
||||
@@ -26,8 +43,13 @@ class LinesModel(NomenclatureBaseModel):
|
||||
related_name='line_company'
|
||||
)
|
||||
|
||||
class Meta:
|
||||
app_label = 'common'
|
||||
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):
|
||||
...
|
||||
@@ -50,4 +72,4 @@ class ContainerTypeModel(models.Model):
|
||||
app_label = 'common'
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
return self.name
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user