import requests
import datetime
from random import randint
from django.template.defaultfilters import striptags
from django.urls import reverse
from django.utils.html import format_html, format_html_join
from django.apps import apps
from django.db import models
from django.utils import timezone
from django.core.mail import EmailMessage
from django.core.mail import send_mail
from django.contrib.sitemaps import Sitemap
from django.db.models.signals import post_save
from django.dispatch import receiver
from taggit.managers import TaggableManager
from utils.util import markdown_to_html
def random_link():
"""
Emails me a random link everyday (via cron)
"""
total = Link.objects.all().count()
pk = randint(1, total)
try:
link = Link.objects.get(pk=pk)
except Link.DoesNotExist:
try:
link = Link.objects.get(pk=pk+1)
except Link.DoesNotExist:
link = Link.objects.get(pk=pk+91)
subject = "today's link: %s" % link.title
body = "%s\n\n%s\n\n\nvisit site:%s\n\n\ndelete link: https://live.luxagraf.net/admin/links/link/%s/" % (link.title, link.description, link.url, link.pk)
msg = EmailMessage(subject, striptags(body), 'sng@luxagraf.net', ['sng@luxagraf.net'])
msg.send()
def get_source(source):
url = "http://heckyesmarkdown.com/go/?read=1&preview=0&showframe=0&output=json&u=%s" % (source)
r = requests.get(url, timeout=15.001)
data = r.json()
return data
def email_link(link):
"""
Emails me a copy of link
"""
subject = link.title
body = "%s\n\n%s\n\n\nvisit site:%s\n" % (link.title, link.description, link.url)
send_mail(
subject,
body,
'sng@luxagraf.net',
['sng@luxagraf.net']
)
def get_upload_path(self, filename):
return "images/links/%s/%s" % (datetime.datetime.today().strftime("%Y"), filename)
class Link(models.Model):
title = models.CharField(max_length=400)
url = models.CharField(max_length=400)
description = models.TextField(blank=True, null=True)
body_markdown = models.TextField(blank=True, null=True)
pub_date = models.DateTimeField(default=timezone.now)
PUB_STATUS = (
(0, 'Private'),
(1, 'Public'),
)
status = models.IntegerField(choices=PUB_STATUS, default=0)
tags = TaggableManager(blank=True)
image = models.FileField(blank=True, null=True, upload_to=get_upload_path)
class Meta:
ordering = ['-pub_date']
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse("links:detail", kwargs={"slug": self.pk})
def render_description(self):
return markdown_to_html(self.description)
def admin_link(self):
return format_html('Visit Site' % (self.url))
admin_link.short_description = 'Link'
def admin_tags(self):
return format_html_join(
'\n', "{},",
((tag.slug, tag.name) for tag in self.tags.all())
)
admin_tags.short_description = 'Tags'
@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)
@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_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 ''
@receiver(post_save, sender=Link)
def post_save_events(sender, update_fields, created, instance, **kwargs):
if instance.body_markdown:
pass
else:
md = get_source(instance.url)
instance.body_markdown = md['markdown']
post_save.disconnect(post_save_events, sender=Link)
instance.save()
post_save.connect(post_save_events, sender=Link)
email_link(instance)
class LinkSitemap(Sitemap):
changefreq = "never"
priority = 0.4
def items(self):
return Link.objects.filter(status=1)
def lastmod(self, obj):
return obj.pub_date