Reorganize and sort models and managers according to Mayan's best practices.

Signed-off-by: Michael Price <loneviking72@gmail.com>
This commit is contained in:
Michael Price
2018-04-01 20:21:37 -04:00
committed by Roberto Rosario
parent a172538dfc
commit 28aa0b913c
21 changed files with 418 additions and 418 deletions
+22 -22
View File
@@ -80,17 +80,14 @@ class MetadataType(models.Model):
objects = MetadataTypeManager()
def __str__(self):
return self.label
def natural_key(self):
return (self.name,)
class Meta:
ordering = ('label',)
verbose_name = _('Metadata type')
verbose_name_plural = _('Metadata types')
def __str__(self):
return self.label
@staticmethod
def comma_splitter(string):
splitter = shlex.shlex(string.encode('utf-8'), posix=True)
@@ -114,6 +111,9 @@ class MetadataType(models.Model):
required=True, metadata_type=self
).exists()
def natural_key(self):
return (self.name,)
def validate_value(self, document_type, value):
# Check default
if not value and self.default:
@@ -171,6 +171,13 @@ class DocumentMetadata(models.Model):
def __str__(self):
return force_text(self.metadata_type)
def clean_fields(self, *args, **kwargs):
super(DocumentMetadata, self).clean_fields(*args, **kwargs)
self.value = self.metadata_type.validate_value(
document_type=self.document.document_type, value=self.value
)
def delete(self, enforce_required=True, *args, **kwargs):
"""
enforce_required prevents deletion of required metadata at the
@@ -184,6 +191,12 @@ class DocumentMetadata(models.Model):
return super(DocumentMetadata, self).delete(*args, **kwargs)
@property
def is_required(self):
return self.metadata_type.get_required_for(
document_type=self.document.document_type
)
def save(self, *args, **kwargs):
if self.metadata_type.pk not in self.document.document_type.metadata.values_list('metadata_type', flat=True):
raise ValidationError(
@@ -192,19 +205,6 @@ class DocumentMetadata(models.Model):
return super(DocumentMetadata, self).save(*args, **kwargs)
def clean_fields(self, *args, **kwargs):
super(DocumentMetadata, self).clean_fields(*args, **kwargs)
self.value = self.metadata_type.validate_value(
document_type=self.document.document_type, value=self.value
)
@property
def is_required(self):
return self.metadata_type.get_required_for(
document_type=self.document.document_type
)
@python_2_unicode_compatible
class DocumentTypeMetadataType(models.Model):
@@ -220,11 +220,11 @@ class DocumentTypeMetadataType(models.Model):
objects = DocumentTypeMetadataTypeManager()
def __str__(self):
return force_text(self.metadata_type)
class Meta:
ordering = ('metadata_type',)
unique_together = ('document_type', 'metadata_type')
verbose_name = _('Document type metadata type options')
verbose_name_plural = _('Document type metadata types options')
def __str__(self):
return force_text(self.metadata_type)