Commits

Guilherme Gondim committed 9d3a4a6

require a content or raise a validation error and add default text to Newsletter.text_content when the newsletter has only html content

  • Participants
  • Parent commits 35c5d52

Comments (0)

Files changed (1)

informativo/models.py

 from datetime import datetime
 
 from django.conf import settings
+from django.core.exceptions import ValidationError
 from django.db import models
 from django.utils.translation import ugettext_lazy as _
 
     def __unicode__(self):
         return unicode(self.title)
 
+    def clean(self):
+        if not self.html_content and not self.text_content:
+            raise ValidationError(_('You must provide a HTML or text content!'))
+        # Set the pub_date for published items if it hasn't been set already.
+        if self.status == 'published' and self.pub_date is None:
+            self.pub_date = datetime.datetime.now()
+
+    def save(self, *args, **kwargs):
+        if not self.text_content and self.html_content:
+            self.text_content = _(
+                'This message contains an HTML formatted message but your '
+                'e-mail client does not support the display of HTML.'
+            )
+        return super(Newsletter, self).save(*args, **kwargs)
+
     def send(self):
         subject = self.title
         text_content, html_content = self.text_content, self.html_content