2015-10-03 01:07:05 +00:00
|
|
|
import uuid
|
2016-12-28 23:15:13 +00:00
|
|
|
from django.core.exceptions import ValidationError
|
2015-10-03 01:07:05 +00:00
|
|
|
from django.db import models
|
|
|
|
|
|
|
|
|
2016-12-25 14:52:55 +00:00
|
|
|
class CleanedModel(models.Model):
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
|
|
|
def save(self, **kwargs):
|
|
|
|
try:
|
|
|
|
# call this models full_clean() method before saving,
|
|
|
|
# which in turn calls .clean_fields(), .clean() and .validate_unique()
|
|
|
|
self.full_clean()
|
|
|
|
except ValidationError as e:
|
|
|
|
message = "Got ValidationError while saving: %s" % e
|
|
|
|
if hasattr(self, 'request'):
|
|
|
|
messages.error(self.request, message)
|
|
|
|
print(message)
|
|
|
|
# dont save, just return
|
|
|
|
return
|
2016-12-28 23:15:13 +00:00
|
|
|
super(CleanedModel, self).save(**kwargs)
|
2016-12-25 14:52:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UUIDModel(CleanedModel):
|
2015-10-03 01:07:05 +00:00
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
|
|
|
uuid = models.UUIDField(
|
|
|
|
primary_key=True,
|
|
|
|
default=uuid.uuid4,
|
|
|
|
editable=False,
|
|
|
|
)
|
2016-05-30 19:51:17 +00:00
|
|
|
|
|
|
|
|
2016-12-25 14:52:55 +00:00
|
|
|
class CreatedUpdatedModel(CleanedModel):
|
2016-05-30 19:51:17 +00:00
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
|
|
|
created = models.DateTimeField(auto_now_add=True)
|
2016-12-25 14:52:55 +00:00
|
|
|
updated = models.DateTimeField(auto_now=True)
|
|
|
|
|
|
|
|
|