summaryrefslogtreecommitdiff
path: root/app/guide/models.py
blob: 7f0d501d7e9eb50577a99afd8585762406b1abe1 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import datetime
from django.contrib.gis.db import models
from django.conf import settings
from django.contrib.sitemaps import Sitemap
from django.contrib.syndication.views import Feed
from PIL import Image

from taggit.managers import TaggableManager
from locations.models import Location
from jrnl.models import Entry
from photos.models import PhotoGallery

from utils.widgets import markdown_to_html


def get_upload_path(self, filename):
    return "images/guide-images/%s/%s" % (datetime.datetime.today().strftime("%Y"), filename)


def get_tn_path(self, filename):
    return "images/guide-thumbnail/%s/%s" % (datetime.datetime.today().strftime("%Y"), filename)


def image_url_replace(str):
    str = str.replace('[[base_url]]', settings.IMAGES_URL)
    return str


def markdown_processor(md):
    return markdown_to_html(md)


class Guide(models.Model):
    title = models.CharField(max_length=200)
    slug = models.SlugField(unique_for_date='pub_date')
    entry = models.ForeignKey(Entry, null=True, blank=True)
    body_html = models.TextField(blank=True)
    body_markdown = models.TextField()
    dek = models.TextField(null=True, blank=True)
    pub_date = models.DateTimeField('Date published')
    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')
    meta_description = models.CharField(max_length=256, null=True, blank=True)
    TEMPLATES = (
        (0, 'single'),
        (1, 'double'),
        (2, 'single-dark'),
        (3, 'double-dark'),
    )
    template_name = models.IntegerField(choices=TEMPLATES, default=0)
    tags = TaggableManager(blank=True)
    image = models.FileField(upload_to=get_upload_path, null=True, blank=True)
    thumbnail = models.FileField(upload_to=get_tn_path, null=True, blank=True)

    @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

    class Meta:
        ordering = ('-pub_date',)
        get_latest_by = 'pub_date'
        verbose_name_plural = 'entries'

    def __unicode__(self):
        return self.title

    def get_absolute_url(self):
        if self.location:
            return "/travel-guide/location/%s/%s/" % (self.location.slug, self.slug)
        else:
            return "/travel-guide/%s/" % (self.slug)

    def get_thumbnail_url(self):
        image_dir, img = self.thumbnail.url.split('guide-thumbnail/')[1].split('/')
        return '%sguide-thumbnail/%s/%s' % (settings.IMAGES_URL, image_dir, img)

    def get_image_url(self):
        image_dir, img = self.image.url.split('guide-images/')[1].split('/')
        return '%sguide-images/%s/%s' % (settings.IMAGES_URL, image_dir, img)

    def save(self):
        if self.image:
            img = Image.open(self.image)
            self.image_width, self.image_height = img.size
        img = Image.open(self.thumbnail)
        self.thumb_width, self.thumb_height = img.size
        md = image_url_replace(self.body_markdown)
        self.body_html = markdown_to_html(md)
        self.dek = markdown_to_html(self.dek)
        super(Guide, self).save()


class GuideSitemap(Sitemap):
    changefreq = "never"
    priority = 1.0

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

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


class GuideFull(Feed):
    title = "Luxagraf: Topographical Writings"
    link = "/writing/"
    description = "Latest postings to luxagraf.net"
    description_template = 'feeds/blog_description.html'

    def items(self):
        return Entry.objects.filter(status__exact=1).order_by('-pub_date')[:10]


#from django.dispatch import dispatcher
#from django.db.models import signals

#signals.post_save.connect(update_recent, sender=Entry)