summaryrefslogtreecommitdiff
path: root/app/links/models.py
blob: cdbe68075c1a8936f964503e05b4e41f61ecd724 (plain)
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
import requests
import json
from django.template.defaultfilters import striptags
from django.urls import reverse
from django.utils.encoding import force_text
from django.utils.html import format_html
from django.apps import apps
from django.db import models
from django.utils import timezone
from django.core.mail import EmailMessage
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.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)
    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)

    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 get_previous_published(self):
        return self.get_previous_by_pub_date(status__exact=1)

    def get_next_published(self):
        return self.get_next_by_pub_date(status__exact=1)

    def admin_link(self):
        return format_html('<a href="%s">Visit Site</a>' % (self.url))
        admin_link.short_description = 'Link'

    @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:
        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"
    priority = 0.4

    def items(self):
        return Link.objects.filter(status=1)

    def lastmod(self, obj):
        return obj.pub_date