import datetime
import os
import re
from django.contrib.gis.db import models
from django.utils.html import format_html
from django.core.urlresolvers import reverse
from django.template.loader import render_to_string
from django.conf import settings
from django.template import Context
from django.contrib.syndication.views import Feed
from django.contrib.sitemaps import Sitemap
from django import forms
from django.template.defaultfilters import slugify
# http://freewisdom.org/projects/python-markdown/
import markdown
from bs4 import BeautifulSoup
from photos.models import PhotoGallery, LuxImage
from locations.models import Location
def get_upload_path(self, filename):
return "images/post-images/%s/%s" % (datetime.datetime.today().strftime("%Y"), filename)
def get_tn_path(self, filename):
return "images/post-thumbnail/%s/%s" % (datetime.datetime.today().strftime("%Y"), filename)
def image_url_replace(s):
s = s.replace('[[base_url]]', settings.IMAGES_URL)
return s
def parse_image(s):
soup = BeautifulSoup(s.group(), "lxml")
for img in soup.find_all('img'):
print(img)
src = img['src'].split("images/")[1]
print(src)
i = LuxImage.objects.get(image__icontains=src)
cl = img['class']
caption = False
if len(cl) > 1:
css_class = cl[0]
if cl[1] == 'caption':
caption = True
else:
css_class = cl[0]
c = Context({'image': i, 'caption': caption})
return render_to_string("lib/img_%s.html" % css_class, c)
def render_images(s):
s = re.sub('', parse_image, s)
return s
def extract_images(s):
soup = BeautifulSoup(s, "lxml")
imgs = []
for img in soup.find_all('img'):
imgs.append(img['src'])
return imgs
class PostImage(models.Model):
title = models.CharField(max_length=100)
image = models.ImageField(upload_to="%s/%s" % (settings.IMAGES_ROOT, datetime.datetime.today().strftime("%Y")))
def __unicode__(self):
return self.title
def post_image(self):
return format_html('' % (
settings.IMAGES_URL,
self.image.url.split('images')[1].split('/', 1)[1],
self.title)
)
post_image.allow_tags = True
class Entry(models.Model):
title = models.CharField(max_length=200)
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, null=True, blank=True)
PUB_STATUS = (
(0, 'Draft'),
(1, 'Published'),
)
status = models.IntegerField(choices=PUB_STATUS, default=0)
photo_gallery = models.ForeignKey(PhotoGallery, blank=True, null=True, verbose_name='photo set')
image = models.FileField(upload_to=get_upload_path, null=True, blank=True, help_text="should be 205px high")
thumbnail = models.FileField(upload_to=get_tn_path, null=True, blank=True, help_text="should be 160 wide")
meta_description = models.CharField(max_length=256, null=True, blank=True)
TEMPLATES = (
(0, 'single'),
(1, 'double'),
(2, 'single-dark'),
(3, 'double-dark'),
(4, 'bigimg'),
(5, 'bigimg-dark'),
)
template_name = models.IntegerField(choices=TEMPLATES, default=0)
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.thumbnail.url.split('post-thumbnail/')[1].split('/')
return '%spost-thumbnail/%s/%s' % (settings.IMAGES_URL, image_dir, img)
def get_image_url(self):
image_dir, img = self.image.url.split('post-images/')[1].split('/')
return '%spost-images/%s/%s' % (settings.IMAGES_URL, image_dir, img)
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 '/media/images/home-images/%s' % (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_images(self):
return extract_images(self.body_html)
@property
def state(self):
return self.location.state
@property
def country(self):
return self.location.state.country
@property
def region(self):
return self.location.state.country.lux_region
@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_next_published(self):
return self.get_next_by_pub_date(status__exact=1)
def save(self):
if self.pk:
if self.pk >= 167:
md = render_images(self.body_markdown)
else:
md = image_url_replace(self.body_markdown)
self.body_html = markdown.markdown(md, extensions=['extra'], safe_mode=False)
self.dek == markdown.markdown(self.dek, safe_mode=False)
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))
super(Entry, self).save()
class EntryTitleSong(models.Model):
title = models.CharField(max_length=200, blank=True)
band = models.CharField(max_length=200)
album = models.CharField(max_length=200, blank=True, null=True)
song = models.CharField(max_length=200, blank=True, null=True)
listen_link = models.CharField(max_length=200, blank=True, null=True)
entry = models.ForeignKey(Entry)
slug = models.SlugField(unique_for_date='pub_date', blank=True)
body_markdown = models.TextField(blank=True)
body_html = models.TextField(blank=True)
pub_date = models.DateField('Date published')
class Meta:
ordering = ('-pub_date',)
get_latest_by = 'pub_date'
verbose_name_plural = 'Entry Title Songs'
def __str__(self):
return self.title
def save(self):
if not self.id and not self.pub_date:
self.pub_date = datetime.datetime.now()
self.slug = slugify(self.title)
self.title = self.entry.title
super(EntryTitleSong, self).save()
class EntryAside(models.Model):
title = models.CharField(max_length=200)
body = models.TextField(null=True, blank=True)
entry = models.ForeignKey(Entry)
class HomepageCurrator(models.Model):
alt_text = models.CharField(max_length=200)
image_base_url = models.CharField(max_length=200)
tag_line = models.CharField(max_length=200)
banner = models.ForeignKey(Entry, related_name="banner")
entry_list = models.ManyToManyField(Entry)
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
import urllib.request
import urllib.parse
import urllib.error
from django_gravatar.helpers import get_gravatar_url, has_gravatar, calculate_gravatar_hash
from django.dispatch import receiver
from django_comments.signals import comment_was_posted
from django_comments.models import Comment
from django_comments.moderation import CommentModerator, moderator
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)
# 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
@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)