1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
|
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.contrib.sites.models import Site
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 media.models import LuxImage, LuxImageSize
from locations.models import Location
from books.models import Book
from lttr.models import NewsletterMailing
#from fieldnotes.models import FieldNote
from taxonomy.models import TaggedItems, Category
from utils.util import render_images, render_products, parse_video, markdown_to_html, extract_main_image
def get_upload_path(self, filename):
return "images/post-images/%s/%s" % (datetime.datetime.today().strftime("%Y"), filename)
class PostType(models.IntegerChoices):
RANGE = 0, ('range')
REVIEW = 1, ('review')
ESSAY = 2, ('essay')
SRC = 3, ('src')
JRNL = 4, ('jrnl')
FIELD_NOTE = 5, ('field note')
GUIDE = 6, ('guide')
FRIENDS = 7, ('friends')
class Post(models.Model):
site = models.ForeignKey(Site, on_delete=models.SET_NULL, default=1, null=True)
title = models.CharField(max_length=200)
short_title = models.CharField(max_length=200, blank=True, null=True)
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.SET_NULL, null=True, blank=True)
TEMPLATES = (
(0, 'single'),
(1, 'double'),
(2, 'single-dark'),
(3, 'double-dark'),
(4, 'single-black'),
(5, 'double-black'),
)
post_type = models.IntegerField(choices=PostType.choices, default=PostType.JRNL)
template_name = models.IntegerField(choices=TEMPLATES, default=0)
has_video = models.BooleanField(blank=True, default=False)
has_code = models.BooleanField(blank=True, default=False)
disclaimer = models.BooleanField(blank=True, default=False)
books = models.ManyToManyField(Book, blank=True)
old_image = models.FileField(upload_to=get_upload_path, blank=True, null=True, help_text="should be 520 by 290")
related = models.ManyToManyField(RelatedPost, blank=True)
point = models.PointField(null=True, blank=True)
location = models.ForeignKey(Location, on_delete=models.CASCADE, null=True, blank=True)
topics = models.ManyToManyField(Category, 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('self', blank=True, symmetrical=False, limit_choices_to = {'post_type': PostType.FIELD_NOTE})
books = models.ManyToManyField(Book, 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 == PostType.ESSAY:
return reverse('essays:detail', kwargs={"slug": self.slug})
if self.post_type == PostType.RANGE:
m = NewsletterMailing.objects.get(post__id=self.pk)
return reverse('range:range-detail', kwargs={"issue": m.get_issue_str(), "slug": self.slug})
if self.post_type == PostType.SRC:
return reverse('src:detail', kwargs={"slug": self.slug})
if self.post_type == PostType.FIELD_NOTE:
return reverse('fieldnote:detail', kwargs={"year": self.pub_date.year, "month": self.pub_date.strftime("%m"), "slug": self.slug})
if self.post_type == PostType.JRNL:
return reverse('jrnl:detail', kwargs={"year": self.pub_date.year, "month": self.pub_date.strftime("%m"), "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_url_by_size("thumbnail")
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,post_type=self.post_type)
@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,post_type=self.post_type)
@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.'''
if self.point:
return self.point.x
@property
def latitude(self):
'''Get the site's latitude.'''
if self.point:
return self.point.y
@property
def sitemap_priority(self):
if self.post_type in [2,4,5]:
return 1.0
else:
return 0.7
def get_image_url(self):
'''
for legacy jrnl posts without a featured_image
'''
try:
image_dir, img = self.old_image.url.split('post-images/')[1].split('/')
return '%spost-images/%s/%s' % (settings.IMAGES_URL, image_dir, img)
except ValueError:
pass
def save(self, *args, **kwargs):
created = self.pk is None
if not created:
md = render_images(self.body_markdown)
prods = render_products(md)
print(self.title)
self.body_html = markdown_to_html(prods)
if self.epilogue_markdown:
self.epilogue_html = markdown_to_html(self.epilogue_markdown)
if self.prologue_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:
if self.post_type == PostType.FIELD_NOTE:
self.featured_image = extract_main_image(self.body_markdown)
else:
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
if self.featured_image:
s = LuxImageSize.objects.get(name="featured_jrnl")
ss = LuxImageSize.objects.get(name="picwide-sm")
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(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, pub_date=instance.pub_date, title=instance.title, slug=instance.slug, post_type=instance.get_post_type_display())
post_save.disconnect(post_save_events, sender=Post)
instance.save()
post_save.connect(post_save_events, sender=Post)
class PostSitemap(Sitemap):
changefreq = "never"
protocol = "https"
def items(self):
return Post.objects.filter(status=1)
def lastmod(self, obj):
return obj.pub_date
def priority(self, obj):
return obj.sitemap_priority
|