root / cloudcmsblog / models.py @ c1468bcc
History | View | Annotate | Download (4.7 kB)
1 |
from datetime import datetime |
---|---|
2 |
|
3 |
from django.db import models |
4 |
from django.conf import settings |
5 |
from django.contrib.auth.models import User |
6 |
from django.utils.translation import ugettext_lazy as _, ugettext, ungettext |
7 |
|
8 |
from feincms import translations |
9 |
from feincms.models import Base |
10 |
from feincms.content.richtext.models import RichTextContent |
11 |
from feincms.content.section.models import SectionContent |
12 |
from feincms.module.medialibrary.fields import MediaFileForeignKey |
13 |
from feincms.module.medialibrary.models import MediaFile |
14 |
from feincms.module.page.extensions.navigation import NavigationExtension |
15 |
from feincms.module.page.extensions.navigation import PagePretender |
16 |
|
17 |
from cloudcms.models import Application |
18 |
|
19 |
|
20 |
class Category(models.Model, translations.TranslatedObjectMixin): |
21 |
"""
|
22 |
Blog entry category. Each blog post may belong to multiple categories.
|
23 |
"""
|
24 |
|
25 |
ordering = models.SmallIntegerField(_('ordering'), default=0) |
26 |
display_on_menu = models.BooleanField(default=False)
|
27 |
|
28 |
class Meta: |
29 |
verbose_name = _('category')
|
30 |
verbose_name_plural = _('categories')
|
31 |
ordering = ['-ordering',]
|
32 |
|
33 |
objects = translations.TranslatedObjectManager() |
34 |
|
35 |
def __unicode__(self): |
36 |
trans = translations.TranslatedObjectMixin.__unicode__(self)
|
37 |
return trans or _('Unnamed category') |
38 |
|
39 |
|
40 |
class CategoryTranslation(translations.Translation(Category)): |
41 |
"""
|
42 |
Category translation
|
43 |
"""
|
44 |
title = models.CharField(_('category title'), max_length=100) |
45 |
slug = models.SlugField(_('slug'), unique=True) |
46 |
description = models.CharField(_('description'), max_length=250, blank=True) |
47 |
|
48 |
class Meta: |
49 |
verbose_name = _('category translation')
|
50 |
verbose_name_plural = _('category translations')
|
51 |
ordering = ['title']
|
52 |
|
53 |
def __unicode__(self): |
54 |
return self.title |
55 |
|
56 |
@models.permalink
|
57 |
def get_absolute_url(self): |
58 |
return ('cloudcmsblog_entries_archive', (), { |
59 |
'category': self.slug, |
60 |
}) |
61 |
|
62 |
def save(self, *args, **kwargs): |
63 |
if not self.slug: |
64 |
self.slug = slugify(self.title) |
65 |
|
66 |
super(CategoryTranslation, self).save(*args, **kwargs) |
67 |
|
68 |
|
69 |
class EntryManager(models.Manager): |
70 |
|
71 |
def active(self): |
72 |
return self.filter(is_active=True) |
73 |
|
74 |
class Entry(Base): |
75 |
"""
|
76 |
Blog post entry
|
77 |
"""
|
78 |
is_active = models.BooleanField(_('is active'), default=True) |
79 |
is_featured = models.BooleanField(_('is featured'), default=False) |
80 |
|
81 |
title = models.CharField(_('title'), max_length=100) |
82 |
slug = models.SlugField(_('slug'), max_length=100, unique_for_date='published_on') |
83 |
author = models.ForeignKey(User, related_name='blogentries', verbose_name=_('author')) |
84 |
language = models.CharField(max_length=255, choices=settings.LANGUAGES)
|
85 |
|
86 |
intro_text = models.TextField(max_length=255,
|
87 |
help_text="Displayed in list views", blank=True) |
88 |
image = MediaFileForeignKey(MediaFile, null=True, blank=True) |
89 |
application = models.ManyToManyField(Application, |
90 |
related_name="blogentries",
|
91 |
verbose_name=_('application'))
|
92 |
|
93 |
published_on = models.DateTimeField(_('published on'), blank=True, null=True, default=datetime.now, |
94 |
help_text=_('Will be filled in automatically when entry gets published.'))
|
95 |
last_changed = models.DateTimeField(_('last change'), auto_now=True, editable=False) |
96 |
|
97 |
categories = models.ManyToManyField(Category, verbose_name=_('categories'),
|
98 |
related_name='blogentries', null=True, blank=True) |
99 |
|
100 |
objects = EntryManager() |
101 |
|
102 |
class Meta: |
103 |
get_latest_by = 'published_on'
|
104 |
ordering = ['-published_on']
|
105 |
verbose_name = _('entry')
|
106 |
verbose_name_plural = _('entries')
|
107 |
|
108 |
def __unicode__(self): |
109 |
return self.title |
110 |
|
111 |
@models.permalink
|
112 |
def get_absolute_url(self): |
113 |
return ('cloudcmsblog_entry_detail', (), { |
114 |
'year': self.published_on.strftime('%Y'), |
115 |
'month': self.published_on.strftime('%m'), |
116 |
'day': self.published_on.strftime('%d'), |
117 |
'slug': self.slug, |
118 |
}) |
119 |
|
120 |
|
121 |
# Feincms navigation extension
|
122 |
class BlogCategoriesNavigationExtension(NavigationExtension): |
123 |
"""
|
124 |
Navigation extension for FeinCMS which lists all categories that user
|
125 |
wants to include in global site navigation.
|
126 |
"""
|
127 |
|
128 |
name = _('blog categories')
|
129 |
|
130 |
def children(self, page, **kwargs): |
131 |
for category in Category.objects.filter(display_on_menu=True): |
132 |
url='%scategory/%s/' % (page.get_absolute_url(), category.translation.slug)
|
133 |
yield PagePretender(
|
134 |
title=category.translation.title, |
135 |
tree_id=page.tree_id, |
136 |
url=url, |
137 |
lft=0,
|
138 |
rght=0,
|
139 |
slug=category.translation.slug, |
140 |
) |
141 |
|