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
|
import re
from django import forms
from django.contrib.gis.db import models
from django.utils import timezone
from django.urls import reverse
from django.conf import settings
from django.contrib.sitemaps import Sitemap
from locations.models import Location, CheckIn
from photos.models import LuxImage
from utils.util import render_images, parse_image, markdown_to_html, extract_main_image
def render_images(s):
s = re.sub('<img(.*)/>', parse_image, s)
return s
class FieldNote(models.Model):
title = models.CharField(max_length=250, blank=True)
subtitle = models.CharField(max_length=250, blank=True)
slug = models.SlugField(unique_for_date='pub_date', blank=True)
pub_date = models.DateTimeField(default=timezone.now)
body_html = models.TextField(blank=True)
body_markdown = models.TextField('Note')
point = models.PointField(blank=True, null=True)
location = models.ForeignKey(Location, on_delete=models.CASCADE, blank=True, null=True)
PUB_STATUS = (
(0, 'Draft'),
(1, 'Published'),
)
status = models.IntegerField(choices=PUB_STATUS, default=1)
NOTE_TYPE = (
(0, 'Note'),
(1, 'Photo'),
)
note_type = models.IntegerField(choices=NOTE_TYPE, default=0)
featured_image = models.ForeignKey(LuxImage, on_delete=models.SET_NULL, blank=True, null=True)
class Meta:
ordering = ('-pub_date',)
get_latest_by = 'pub_date'
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse("fieldnotes:detail", kwargs={"year": self.pub_date.year, "month": self.pub_date.strftime("%m"), "slug": self.slug})
@property
def region(self):
return self.location.lux_region
@property
def longitude(self):
'''Get the site's longitude.'''
return round(self.point.x, 2)
@property
def latitude(self):
'''Get the site's latitude.'''
return round(self.point.y, 2)
@property
def get_previous_published(self):
return self.get_previous_by_pub_date()
@property
def get_next_published(self):
return self.get_next_by_pub_date()
@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 ''
def save(self, *args, **kwargs):
md = render_images(self.body_markdown)
self.body_html = markdown_to_html(md)
if not self.point:
self.point = CheckIn.objects.latest().point
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))
if not self.id:
self.pub_date = timezone.now()
self.date_last_updated = timezone.now()
if self.note_type == 1:
self.featured_image = extract_main_image(self.body_markdown)
super(FieldNote, self).save()
class FieldNoteSitemap(Sitemap):
changefreq = "never"
priority = 0.7
protocol = "https"
def items(self):
return FieldNote.objects.filter(status=1)
def lastmod(self, obj):
return obj.pub_date
|