summaryrefslogtreecommitdiff
path: root/app/unused_apps/jrnl/models.py
diff options
context:
space:
mode:
authorluxagraf <sng@luxagraf.net>2020-11-15 10:13:08 -0500
committerluxagraf <sng@luxagraf.net>2020-11-15 10:13:08 -0500
commit7509da286bccd1dda358507cd455f9297db59247 (patch)
tree779aa3660e453d73268d22a72d9a5599cc94aa02 /app/unused_apps/jrnl/models.py
parentb2434dcba142961b4a4b67780cf64e01d0908bf5 (diff)
ported jrnl building to posts
Diffstat (limited to 'app/unused_apps/jrnl/models.py')
-rw-r--r--app/unused_apps/jrnl/models.py309
1 files changed, 309 insertions, 0 deletions
diff --git a/app/unused_apps/jrnl/models.py b/app/unused_apps/jrnl/models.py
new file mode 100644
index 0000000..f3c0626
--- /dev/null
+++ b/app/unused_apps/jrnl/models.py
@@ -0,0 +1,309 @@
+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 photos.models import PhotoGallery, LuxImage, LuxImageSize
+from locations.models import Location
+from books.models import Book
+from posts.models import Post
+from normalize.models import RelatedPost
+
+from utils.util import render_images, parse_video, markdown_to_html
+
+
+def get_upload_path(self, filename):
+ return "images/post-images/%s/%s" % (datetime.datetime.today().strftime("%Y"), filename)
+
+
+def image_url_replace(s):
+ s = s.replace('[[base_url]]', settings.IMAGES_URL)
+ return s
+
+
+class OldRelatedPost(models.Model):
+ post_model = models.ForeignKey(ContentType, null=True, on_delete=models.SET_NULL)
+ slug = models.SlugField()
+
+ def __str__(self):
+ return self.slug
+
+
+class Entry(models.Model):
+ title = models.CharField(max_length=200)
+ subtitle = models.CharField(max_length=200, blank=True)
+ slug = models.SlugField(unique_for_date='pub_date')
+ body_html = models.TextField(blank=True)
+ body_markdown = models.TextField()
+ dek = models.TextField(null=True, blank=True)
+ pub_date = models.DateTimeField('Date published')
+ enable_comments = models.BooleanField(default=False)
+ point = models.PointField(null=True, blank=True)
+ location = models.ForeignKey(Location, on_delete=models.CASCADE, null=True, blank=True)
+ PUB_STATUS = (
+ (0, 'Draft'),
+ (1, 'Published'),
+ )
+ status = models.IntegerField(choices=PUB_STATUS, default=0)
+ photo_gallery = models.ForeignKey(PhotoGallery, on_delete=models.CASCADE, blank=True, null=True, verbose_name='photo set')
+ image = models.FileField(upload_to=get_upload_path, blank=True, help_text="should be 520 by 290")
+ meta_description = models.CharField(max_length=256, null=True, blank=True)
+ TEMPLATES = (
+ (0, 'single'),
+ (1, 'double'),
+ (2, 'single-dark'),
+ (3, 'double-dark'),
+ (4, 'single-black'),
+ (5, 'double-black'),
+ )
+ template_name = models.IntegerField(choices=TEMPLATES, default=0)
+ featured_image = models.ForeignKey(LuxImage, on_delete=models.CASCADE, null=True, blank=True)
+ has_video = models.BooleanField(blank=True, default=False)
+ #old_field_notes = models.ManyToManyField(FieldNote, blank=True)
+ field_notes = models.ManyToManyField(Post, blank=True, limit_choices_to={'post_type': 5})
+ books = models.ManyToManyField(Book, related_name="luxbooks", blank=True)
+ oldrelated = models.ManyToManyField(OldRelatedPost, blank=True)
+ related = models.ManyToManyField(RelatedPost, blank=True)
+
+ class Meta:
+ ordering = ('-pub_date',)
+ get_latest_by = 'pub_date'
+ verbose_name_plural = 'entries'
+
+ def __str__(self):
+ return self.title
+
+ def get_absolute_url(self):
+ # return "/jrnl/%s/%s" % (self.pub_date.strftime("%Y/%m").lower(), self.slug)
+ return reverse("jrnl:detail", kwargs={"year": self.pub_date.year, "month": self.pub_date.strftime("%m"), "slug": self.slug})
+
+ def get_absolute_url_old(self):
+ return "/%s/%s/" % (self.pub_date.strftime("%Y/%b/%d").lower(), self.slug)
+
+ def comment_period_open(self):
+ return self.enable_comments and datetime.datetime.today() - datetime.timedelta(30) <= self.pub_date
+
+ def get_thumbnail_url(self):
+ image_dir, img = self.image.url.split('post-thumbnail/')[1].split('/')
+ return '%spost-thumbnail/%s/%s' % (settings.IMAGES_URL, image_dir, img)
+
+ def get_image_url(self):
+ try:
+ image_dir, img = self.image.url.split('post-images/')[1].split('/')
+ return '%spost-images/%s/%s' % (settings.IMAGES_URL, image_dir, img)
+ except ValueError:
+ pass
+
+ def get_image_wide_url(self):
+ img = self.image.url.split('post-images/')[1].split('/')[1]
+ # return '%shome-images/%s' % (settings.IMAGES_URL, img)
+ return '%shome-images/%s' % (settings.IMAGES_URL, img)
+
+ def get_image_hero_url(self):
+ img = self.image.url.split('post-images/')[1].split('/')[1]
+ return '/media/images/home-images/hero%s' % (img)
+
+ def get_image_hero_url_sm(self):
+ img = self.image.url.split('post-images/')[1].split('/')[1]
+ img = os.path.splitext(img)[0]
+ return '/media/images/home-images/hero%s_sm.jpg' % (img)
+
+ def get_featured_image(self):
+ if self.featured_image:
+ return "%s%s" % (settings.IMAGES_URL, self.featured_image.image.url[14:])
+ else:
+ return self.image.url
+
+ def get_featured_image_thumb(self):
+ if self.featured_image:
+ return self.featured_image.get_image_by_size("tn")
+ else:
+ print(self.image.url)
+ image_dir, img = self.image.url.split('post-images/')[1].split('/')
+ print(image_dir, img)
+ return '%spost-images/%s/%s' % (settings.IMAGES_URL, image_dir, img)
+
+ @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
+
+ @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 ''
+
+ def get_image_name(self):
+ return self.image.url.split("post-images/")[1][5:-4]
+
+ def get_image_ext(self):
+ return self.image.url[-3:]
+
+ def get_content_type(self):
+ return ContentType.objects.get(app_label="jrnl", model="entry")
+
+ 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.has_video = parse_video(self.body_html)
+ 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()
+ if old and old.title != self.title or old and old.slug != self.slug:
+ related, c = RelatedPost.objects.get_or_create(model_name=self.get_content_type(), entry_id = self.id, pub_date=self.pub_date)
+ related.title = self.title
+ related.slug = self.slug
+ related.save()
+
+ super(Entry, self).save(*args, **kwargs)
+
+
+class HomepageCurrator(models.Model):
+ """
+ simple model to control the featured article on the homepage
+ also allows me to fudge the "popular" section to be what I want
+ """
+ image_offset_vertical = models.CharField(max_length=20, help_text="add negative top margin to shift image (include css unit)")
+ images = models.ManyToManyField(LuxImage)
+ #featured_image = models.ForeignKey(LuxImage, on_delete=models.CASCADE, null=True, blank=True)
+ tag_line = models.CharField(max_length=200)
+ featured = models.ForeignKey(Entry, on_delete=models.CASCADE, related_name="oldbanner")
+ popular = models.ManyToManyField(Entry)
+ template_name = models.CharField(max_length=200, help_text="full path")
+
+
+class Home(models.Model):
+ """
+ simple model to control the featured article on the homepage
+ also allows me to fudge the "popular" section to be what I want
+ """
+ image_offset_vertical = models.CharField(max_length=20, help_text="add negative top margin to shift image (include css unit)")
+ featured_image = models.ForeignKey(LuxImage, on_delete=models.CASCADE, null=True, blank=True)
+ tag_line = models.CharField(max_length=200)
+ featured = models.ForeignKey(Entry, on_delete=models.CASCADE, related_name="banner")
+ popular = models.ManyToManyField(Entry, related_name="popular")
+ template_name = models.CharField(max_length=200, help_text="full path")
+
+
+class BlogSitemap(Sitemap):
+ changefreq = "never"
+ priority = 1.0
+ protocol = "https"
+
+ def items(self):
+ return Entry.objects.filter(status=1)
+
+ def lastmod(self, obj):
+ return obj.pub_date
+
+
+class EntryModerator(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(Entry, EntryModerator)
+
+
+@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=Entry)
+def post_save_events(sender, update_fields, created, instance, **kwargs):
+ """
+ Creates a generic related entry when a new post is added
+ """
+ if created:
+ related, c = RelatedPost.objects.get_or_create(model_name=instance.get_content_type(), entry_id = instance.id, pub_date=instance.pub_date, title=instance.title, slug=instance.slug)
+
+# from django_comments.signals import comment_will_be_posted
+# from django_comments import akismet
+
+
+# @receiver(comment_will_be_posted, sender=Comment)
+# def spam_check(sender, comment, request, **kwargs):
+# akismet.USERAGENT = "David Lynch's Python library/1.0"
+# try:
+# real_key = akismet.verify_key(settings.AKISMET_KEY, "https://luxagraf.net/")
+# if real_key:
+# is_spam = akismet.comment_check(settings.AKISMET_KEY, "https://luxagraf.net/", request.META['REMOTE_ADDR'], request.META['HTTP_USER_AGENT'], comment_content=comment.comment)
+# if is_spam:
+# return False
+# else:
+# return True
+# except akismet.AkismetError as e:
+# print('Something went wrong, allowing comment')
+# print(e.response, e.statuscode)
+# return True