summaryrefslogtreecommitdiff
path: root/app/posts/models.py
diff options
context:
space:
mode:
authorluxagraf <sng@luxagraf.net>2019-09-18 21:14:38 -0400
committerluxagraf <sng@luxagraf.net>2019-09-18 21:14:38 -0400
commit5bc22c064fbc51987007d85fbc8296a998cf6e84 (patch)
tree7d961ff86c478171ffee14168e9ae3f9d09f1ca5 /app/posts/models.py
parent9e7aadbcf9bab0ecfcd4afc96f6a09f9fe11a197 (diff)
added posts, consolidated essays into posts
Diffstat (limited to 'app/posts/models.py')
-rw-r--r--app/posts/models.py200
1 files changed, 200 insertions, 0 deletions
diff --git a/app/posts/models.py b/app/posts/models.py
new file mode 100644
index 0000000..1e1da1e
--- /dev/null
+++ b/app/posts/models.py
@@ -0,0 +1,200 @@
+import datetime
+import os
+
+from django.dispatch import receiver
+from django.contrib.gis.db import models
+from django.db.models.signals import post_save
+from django.contrib.contenttypes.fields import GenericRelation, GenericForeignKey
+from django.contrib.contenttypes.models import ContentType
+from django.urls import reverse
+from django.utils.functional import cached_property
+from django.apps import apps
+from django.conf import settings
+from django.contrib.sitemaps import Sitemap
+from django import forms
+
+import urllib.request
+import urllib.parse
+import urllib.error
+from django_gravatar.helpers import get_gravatar_url, has_gravatar, calculate_gravatar_hash
+from django_comments.signals import comment_was_posted
+from django_comments.models import Comment
+from django_comments.moderation import CommentModerator, moderator
+
+from taggit.managers import TaggableManager
+
+from normalize.models import RelatedPost
+from photos.models import LuxImage, LuxImageSize
+from locations.models import Location
+from books.models import Book
+from fieldnotes.models import FieldNote
+from taxonomy.models import TaggedItems, Category
+from jrnl.models import Entry
+from utils.util import render_images, parse_video, markdown_to_html
+
+
+class Post(models.Model):
+ old_id = models.IntegerField(blank=True, null=True)
+ title = models.CharField(max_length=200)
+ subtitle = models.CharField(max_length=200, blank=True)
+ slug = models.SlugField(unique_for_date='pub_date')
+ prologue_markdown = models.TextField(blank=True, null=True)
+ prologue_html = models.TextField(blank=True, null=True)
+ body_markdown = models.TextField()
+ body_html = models.TextField(blank=True)
+ epilogue_markdown = models.TextField(blank=True, null=True)
+ epilogue_html = models.TextField(blank=True, null=True)
+ dek = models.TextField(null=True, blank=True)
+ meta_description = models.CharField(max_length=256, blank=True)
+ pub_date = models.DateTimeField('Date published')
+ last_updated = models.DateTimeField(auto_now=True)
+ enable_comments = models.BooleanField(default=False)
+ PUB_STATUS = (
+ (0, 'Draft'),
+ (1, 'Published'),
+ )
+ status = models.IntegerField(choices=PUB_STATUS, default=0)
+ featured_image = models.ForeignKey(LuxImage, on_delete=models.CASCADE, null=True, blank=True)
+ TEMPLATES = (
+ (0, 'single'),
+ )
+ POST_TYPE = (
+ (0, 'guide'),
+ (1, 'review'),
+ (2, 'essay'),
+ )
+ post_type = models.IntegerField(choices=POST_TYPE, default=0)
+ template_name = models.IntegerField(choices=TEMPLATES, default=0)
+ has_video = models.BooleanField(blank=True, default=False)
+ disclaimer = models.BooleanField(blank=True, default=True)
+ books = models.ManyToManyField(Book, blank=True)
+ field_notes = models.ManyToManyField(FieldNote, blank=True)
+ related = models.ManyToManyField(RelatedPost, blank=True)
+ tags = TaggableManager(through=TaggedItems, blank=True, help_text='Topics Covered')
+ category = models.ForeignKey(Category, on_delete=models.CASCADE, null=True, blank=True)
+ point = models.PointField(null=True, blank=True)
+ location = models.ForeignKey(Location, on_delete=models.CASCADE, null=True, blank=True)
+ originally_published_by = models.CharField(max_length=400, null=True, blank=True)
+ originally_published_by_url = models.CharField(max_length=400, null=True, blank=True)
+ field_notes = models.ManyToManyField(FieldNote, blank=True)
+ books = models.ManyToManyField(Book, blank=True)
+ jrnl = models.ManyToManyField(Entry, blank=True)
+
+ class Meta:
+ ordering = ('-pub_date',)
+ get_latest_by = 'pub_date'
+
+ def __str__(self):
+ return self.title
+
+ def get_absolute_url(self):
+ if self.post_type == 0:
+ return reverse('guides:detail', kwargs={"slug": self.slug})
+ if self.post_type == 1:
+ return reverse('reviews:detail', kwargs={"slug": self.slug})
+ if self.post_type == 2:
+ return reverse('essays:detail', kwargs={"slug": self.slug})
+
+ def comment_period_open(self):
+ return self.enable_comments and datetime.datetime.today() - datetime.timedelta(30) <= self.pub_date
+
+ def get_featured_image_thumb(self):
+ return self.featured_image.get_image_by_size("tn")
+
+ def get_content_type(self):
+ return ContentType.objects.get(app_label="posts", model="post")
+
+ @property
+ def get_previous_published(self):
+ return self.get_previous_by_pub_date(status__exact=1)
+
+ @property
+ def get_previous_admin_url(self):
+ n = self.get_previous_by_pub_date()
+ return reverse('admin:%s_%s_change' %(self._meta.app_label, self._meta.model_name), args=[n.id] )
+
+ @property
+ def get_next_published(self):
+ return self.get_next_by_pub_date(status__exact=1)
+
+ @property
+ def get_next_admin_url(self):
+ model = apps.get_model(app_label=self._meta.app_label, model_name=self._meta.model_name)
+ try:
+ return reverse('admin:%s_%s_change' %(self._meta.app_label, self._meta.model_name), args=[self.get_next_by_pub_date().pk] )
+ except model.DoesNotExist:
+ return ''
+
+ @property
+ def longitude(self):
+ '''Get the site's longitude.'''
+ return self.point.x
+
+ @property
+ def latitude(self):
+ '''Get the site's latitude.'''
+ return self.point.y
+
+ def save(self, *args, **kwargs):
+ created = self.pk is None
+ if not created:
+ md = render_images(self.body_markdown)
+ self.body_html = markdown_to_html(md)
+ self.epilogue_html = markdown_to_html(self.epilogue_markdown)
+ self.prologue_html = markdown_to_html(self.prologue_markdown)
+ self.has_video = parse_video(self.body_html)
+ if self.point:
+ try:
+ self.location = Location.objects.filter(geometry__contains=self.point).get()
+ except Location.DoesNotExist:
+ raise forms.ValidationError("There is no location associated with that point, add it: %sadmin/locations/location/add/" % (settings.BASE_URL))
+ if created and not self.featured_image:
+ self.featured_image = LuxImage.objects.latest()
+ old = type(self).objects.get(pk=self.pk) if self.pk else None
+ if old and old.featured_image != self.featured_image: # Field has changed
+ s = LuxImageSize.objects.get(name="featured_jrnl")
+ ss = LuxImageSize.objects.get(name="picwide-med")
+ self.featured_image.sizes.add(s)
+ self.featured_image.sizes.add(ss)
+ self.featured_image.save()
+ super(Post, self).save(*args, **kwargs)
+
+
+class PostModerator(CommentModerator):
+ '''
+ Moderate everything except people with multiple approvals
+ '''
+ email_notification = True
+
+ def moderate(self, comment, content_object, request):
+ previous_approvals = Comment.objects.filter(user_email=comment.email, is_public=True)
+ for approval in previous_approvals:
+ if approval.submit_date <= datetime.datetime.today() - datetime.timedelta(21):
+ approve = True
+ if previous_approvals.count() > 2 and approve:
+ return False
+ # do entry build right here so it goes to live site
+ return True
+moderator.register(Post, PostModerator)
+
+
+@receiver(comment_was_posted, sender=Comment)
+def cache_gravatar(sender, comment, **kwargs):
+ gravatar_exists = has_gravatar(comment.email)
+ grav_dir = settings.IMAGES_ROOT + '/gravcache/'
+ if gravatar_exists:
+ url = get_gravatar_url(comment.email, size=60)
+ if not os.path.isdir(grav_dir):
+ os.makedirs(grav_dir)
+ local_grav = '%s/%s.jpg' % (grav_dir, calculate_gravatar_hash(comment.email))
+ urllib.request.urlretrieve(url, local_grav)
+
+
+@receiver(post_save, sender=Post)
+def post_save_events(sender, update_fields, created, instance, **kwargs):
+ related, created = RelatedPost.objects.get_or_create(model_name=instance.get_content_type(), entry_id = instance.id)
+ related.title = instance.title
+ related.slug = instance.slug
+ post_save.disconnect(post_save_events, sender=Post)
+ instance.save()
+ post_save.connect(post_save_events, sender=Post)