summaryrefslogtreecommitdiff
path: root/app/links/models.py
diff options
context:
space:
mode:
Diffstat (limited to 'app/links/models.py')
-rw-r--r--app/links/models.py39
1 files changed, 38 insertions, 1 deletions
diff --git a/app/links/models.py b/app/links/models.py
index 0a82366..6cbe497 100644
--- a/app/links/models.py
+++ b/app/links/models.py
@@ -1,12 +1,35 @@
+import requests
+import json
from django.core.urlresolvers import reverse
from django.utils.encoding import force_text
from django.db import models
from django.contrib.sitemaps import Sitemap
-from taggit.managers import TaggableManager
+from django.db.models.signals import post_save
+from django.dispatch import receiver
+from taggit.managers import TaggableManager
from utils.widgets import markdown_to_html
+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):
+ """
+ Sends an imported link to Gmail (never hurts to have backups)
+ """
+ subject = link.title
+ body = "%s\n\n%s\n\n\nvisit site:%s\n" % (link.title, link.description, link.url)
+ msg = EmailMessage(subject, striptags(body), 'sng@luxagraf.net', ['luxagraf+links@gmail.com'])
+ msg.send()
+ msg = EmailMessage(subject, striptags(body), 'sng@luxagraf.net', ['sng+links@luxagraf.net'])
+ msg.send()
+
+
class Link(models.Model):
title = models.CharField(max_length=400)
url = models.CharField(max_length=400)
@@ -43,6 +66,20 @@ class Link(models.Model):
admin_link.allow_tags = True
admin_link.short_description = 'Link'
+@receiver(post_save, sender=Link)
+def post_save_events(sender, update_fields, created, instance, **kwargs):
+ if instance.body_markdown:
+ pass
+ else:
+ try:
+ md = get_source(instance.url)
+ instance.body_markdown = md['markdown']
+ except:
+ pass
+ 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"