2015-10-03 01:07:05 +00:00
|
|
|
import uuid
|
2017-05-21 15:23:48 +00:00
|
|
|
from django.contrib.postgres.fields import ArrayField
|
2016-12-28 23:15:13 +00:00
|
|
|
from django.core.exceptions import ValidationError
|
2017-03-07 20:44:30 +00:00
|
|
|
from django.contrib import messages
|
2015-10-03 01:07:05 +00:00
|
|
|
from django.db import models
|
2017-03-23 17:32:13 +00:00
|
|
|
import logging
|
|
|
|
logger = logging.getLogger("bornhack.%s" % __name__)
|
2015-10-03 01:07:05 +00:00
|
|
|
|
|
|
|
|
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()
|
2017-03-07 20:44:30 +00:00
|
|
|
# self.full_clean()
|
2017-02-01 00:09:21 +00:00
|
|
|
# for some reason self.full_clean() appears to call self.clean() before self.clean_fields()
|
|
|
|
# which is not supposed to happen. Call them manually one by one instead.
|
|
|
|
self.clean_fields()
|
|
|
|
self.clean()
|
|
|
|
self.validate_unique()
|
2016-12-25 14:52:55 +00:00
|
|
|
except ValidationError as e:
|
|
|
|
message = "Got ValidationError while saving: %s" % e
|
|
|
|
if hasattr(self, 'request'):
|
|
|
|
messages.error(self.request, message)
|
2017-03-23 17:32:13 +00:00
|
|
|
logger.error(message)
|
2017-01-25 22:19:53 +00:00
|
|
|
# dont save, re-raise the exception
|
|
|
|
raise
|
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)
|
|
|
|
|
|
|
|
|
2017-03-07 23:24:14 +00:00
|
|
|
class CampRelatedModel(CreatedUpdatedModel):
|
2017-03-07 20:44:30 +00:00
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
|
|
|
def save(self, **kwargs):
|
|
|
|
if self.camp.read_only:
|
|
|
|
if hasattr(self, 'request'):
|
|
|
|
messages.error(self.request, 'Camp is in read only mode.')
|
|
|
|
raise ValidationError('This camp is in read only mode.')
|
|
|
|
|
2017-03-18 15:03:10 +00:00
|
|
|
super().save(**kwargs)
|
2017-03-07 20:44:30 +00:00
|
|
|
|
|
|
|
def delete(self, **kwargs):
|
|
|
|
if self.camp.read_only:
|
|
|
|
if hasattr(self, 'request'):
|
|
|
|
messages.error(self.request, 'Camp is in read only mode.')
|
|
|
|
raise ValidationError('This camp is in read only mode.')
|
|
|
|
|
2017-03-18 15:03:10 +00:00
|
|
|
super().delete(**kwargs)
|
2017-04-23 20:04:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
class OutgoingEmail(CreatedUpdatedModel):
|
|
|
|
subject = models.CharField(max_length=500)
|
|
|
|
text_template = models.TextField()
|
|
|
|
html_template = models.TextField(blank=True)
|
|
|
|
sender = models.CharField(max_length=500)
|
2017-05-21 18:13:49 +00:00
|
|
|
to_recipients = ArrayField(
|
|
|
|
models.CharField(max_length=500, blank=True),
|
|
|
|
null=True,
|
|
|
|
blank=True
|
|
|
|
)
|
|
|
|
cc_recipients = ArrayField(
|
|
|
|
models.CharField(max_length=500, blank=True),
|
|
|
|
null=True,
|
|
|
|
blank=True
|
|
|
|
)
|
|
|
|
bcc_recipients = ArrayField(
|
|
|
|
models.CharField(max_length=500, blank=True),
|
|
|
|
null=True,
|
|
|
|
blank=True
|
|
|
|
)
|
2017-04-30 09:32:49 +00:00
|
|
|
attachment = models.FileField(blank=True)
|
2017-04-23 20:04:58 +00:00
|
|
|
processed = models.BooleanField(default=False)
|
2017-04-30 09:32:49 +00:00
|
|
|
|
2017-05-21 18:57:16 +00:00
|
|
|
def __str__(self):
|
|
|
|
return 'OutgoingEmail Object id: {} '.format(self.id)
|
|
|
|
|
2017-05-21 18:13:49 +00:00
|
|
|
def clean(self):
|
|
|
|
if not self.to_recipients \
|
|
|
|
and not self.bcc_recipients \
|
|
|
|
and not self.cc_recipients:
|
|
|
|
raise ValidationError(
|
|
|
|
{'recipient': 'either to_recipient, bcc_recipient or cc_recipient required.'}
|
|
|
|
)
|