This repository has been archived by the owner on Dec 22, 2021. It is now read-only.
/
models.py
121 lines (99 loc) · 4.33 KB
/
models.py
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
from datetime import datetime
from django.db import models
from django.template.defaultfilters import date as dj_date, linebreaks
from caching.base import CachingManager, CachingMixin
from sorl.thumbnail import ImageField
from source.people.models import Person, Organization
from source.code.models import Code
from taggit.managers import TaggableManager
ARTICLE_TYPE_CHOICES = (
('project', 'Project'),
('tool', 'Tool'),
('how-to', 'How-To'),
('interview', 'Interview'),
('roundtable', 'Roundtable'),
('roundup', 'Roundup'),
('event', 'Event'),
('update', 'Community Update'),
)
class LiveArticleManager(CachingManager):
def get_query_set(self):
return super(LiveArticleManager, self).get_query_set().filter(is_live=True, pubdate__lte=datetime.now())
class Article(CachingMixin, models.Model):
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
is_live = models.BooleanField('Display on site', default=True)
title = models.CharField(max_length=128)
slug = models.SlugField(unique=True)
pubdate = models.DateTimeField(default=datetime.now)
subhead = models.CharField(max_length=128)
authors = models.ManyToManyField(Person, blank=True, null=True, related_name='article_authors')
image = ImageField(upload_to='img/uploads/article_images', help_text='Resized to fit 100% column width in template', blank=True, null=True)
image_caption = models.TextField(blank=True)
image_credit = models.CharField(max_length=128, blank=True, help_text='Optional. Will be appended to end of caption in parens.')
body = models.TextField()
summary = models.TextField()
article_type = models.CharField(max_length=32, choices=ARTICLE_TYPE_CHOICES, blank=True)
people = models.ManyToManyField(Person, blank=True, null=True)
organizations = models.ManyToManyField(Organization, blank=True, null=True)
code = models.ManyToManyField(Code, blank=True, null=True)
tags = TaggableManager(blank=True)
objects = models.Manager()
live_objects = LiveArticleManager()
class Meta:
ordering = ('-pubdate','title',)
def __unicode__(self):
return u'%s' % self.title
@models.permalink
def get_absolute_url(self):
return ('article_detail', (), {
'slug': self.slug })
@property
def pretty_pubdate(self):
return dj_date(self.pubdate,"F j, Y")
@property
def pretty_caption(self):
_caption = self.image_caption or ''
_credit = self.image_credit
if _credit:
_caption = '%s (%s)' % (_caption, _credit)
return _caption
def get_live_organization_set(self):
return self.organizations.filter(is_live=True)
def get_live_people_set(self):
return self.people.filter(is_live=True)
def get_live_author_set(self):
return self.authors.filter(is_live=True)
def get_live_code_set(self):
return self.code.filter(is_live=True)
IMAGE_PRESENTATION_CHOICES = (
('full-width', 'Full-Width Above Text'),
('full-width-below', 'Full-Width Below Text'),
('inset-left', 'Inset Left'),
('inset-right', 'Inset Right'),
)
class ArticleBlock(CachingMixin, models.Model):
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
article = models.ForeignKey(Article)
title = models.CharField(max_length=128)
slug = models.SlugField()
order = models.PositiveIntegerField(default=1)
image = ImageField(upload_to='img/uploads/article_images', blank=True, null=True)
image_presentation = models.CharField(max_length=24, choices=IMAGE_PRESENTATION_CHOICES, blank=True)
image_caption = models.TextField(blank=True)
image_credit = models.CharField(max_length=128, blank=True, help_text='Optional. Will be appended to end of caption in parens.')
body = models.TextField()
objects = models.Manager()
class Meta:
ordering = ('article', 'order', 'title',)
verbose_name = 'Article Block'
def __unicode__(self):
return u'%s: %s' % (self.article.title, self.title)
@property
def pretty_caption(self):
_caption = self.image_caption or ''
_credit = self.image_credit
if _credit:
_caption = '%s (%s)' % (_caption, _credit)
return _caption