17
by Holger Rapp
Main Page contains now the same informations as before |
1 |
from django.db import models |
2 |
from django.utils.translation import ugettext_lazy as _ |
|
3 |
from django.db.models import permalink |
|
4 |
from django.contrib.auth.models import User |
|
5 |
from tagging.fields import TagField |
|
404.2.1
by franku
first try on Django1.8 |
6 |
from news.managers import PublicManager |
489.1.13
by franku
django.conf.urlresolvers -> django.urls |
7 |
from django.urls import reverse |
429.4.2
by franku
cleanups and fixes |
8 |
import datetime |
17
by Holger Rapp
Main Page contains now the same informations as before |
9 |
import tagging |
10 |
||
11 |
||
438.1.6
by franku
run the script |
12 |
def get_upload_name(inst, fn): |
36
by Holger Rapp
Added support for graphics in news items |
13 |
try: |
14 |
extension = fn.split('.')[-1].lower() |
|
15 |
except: |
|
16 |
extension = 'png' |
|
438.1.6
by franku
run the script |
17 |
return 'news/img/%s.%s' % (inst.title, extension) |
18 |
||
36
by Holger Rapp
Added support for graphics in news items |
19 |
|
17
by Holger Rapp
Main Page contains now the same informations as before |
20 |
class Category(models.Model): |
21 |
"""Category model."""
|
|
438.1.6
by franku
run the script |
22 |
title = models.CharField(_('title'), max_length=100) |
23 |
slug = models.SlugField(_('slug'), unique=True) |
|
24 |
image = models.ImageField(upload_to=get_upload_name, max_length=100) |
|
17
by Holger Rapp
Main Page contains now the same informations as before |
25 |
|
26 |
class Meta: |
|
27 |
verbose_name = _('category') |
|
28 |
verbose_name_plural = _('categories') |
|
29 |
db_table = 'news_categories' |
|
30 |
ordering = ('title',) |
|
31 |
||
32 |
def __unicode__(self): |
|
33 |
return u'%s' % self.title |
|
34 |
||
35 |
def get_absolute_url(self): |
|
429.4.4
by franku
category view as list; cleanups |
36 |
return reverse('category_posts', args=(self.slug,)) |
17
by Holger Rapp
Main Page contains now the same informations as before |
37 |
|
38 |
||
39 |
class Post(models.Model): |
|
40 |
"""Post model."""
|
|
41 |
STATUS_CHOICES = ( |
|
42 |
(1, _('Draft')), |
|
43 |
(2, _('Public')), |
|
44 |
)
|
|
438.1.6
by franku
run the script |
45 |
title = models.CharField(_('title'), max_length=200) |
46 |
slug = models.SlugField(_('slug'), unique_for_date='publish') |
|
47 |
author = models.ForeignKey(User, null=True) |
|
48 |
body = models.TextField( |
|
49 |
_('body'), help_text='Text entered here will be rendered using Markdown') |
|
50 |
tease = models.TextField(_('tease'), blank=True) |
|
51 |
status = models.IntegerField( |
|
52 |
_('status'), choices=STATUS_CHOICES, default=2) |
|
53 |
allow_comments = models.BooleanField(_('allow comments'), default=True) |
|
54 |
publish = models.DateTimeField(_('publish')) |
|
55 |
created = models.DateTimeField(_('created'), auto_now_add=True) |
|
56 |
modified = models.DateTimeField(_('modified'), auto_now=True) |
|
57 |
categories = models.ManyToManyField(Category, blank=True) |
|
58 |
tags = TagField() |
|
59 |
objects = PublicManager() |
|
60 |
||
17
by Holger Rapp
Main Page contains now the same informations as before |
61 |
class Meta: |
62 |
verbose_name = _('post') |
|
63 |
verbose_name_plural = _('posts') |
|
438.1.6
by franku
run the script |
64 |
db_table = 'news_posts' |
65 |
ordering = ('-publish',) |
|
17
by Holger Rapp
Main Page contains now the same informations as before |
66 |
get_latest_by = 'publish' |
67 |
||
68 |
def __unicode__(self): |
|
69 |
return u'%s' % self.title |
|
438.1.6
by franku
run the script |
70 |
|
36
by Holger Rapp
Added support for graphics in news items |
71 |
#########
|
72 |
# IMAGE #
|
|
73 |
#########
|
|
74 |
# Currently this is only inherited from the category, but one
|
|
75 |
# day we might want to override the post image here
|
|
76 |
@property
|
|
77 |
def has_image(self): |
|
78 |
if self.categories.count() == 0: |
|
79 |
return False |
|
80 |
return self.categories.all()[0].image != '' |
|
438.1.6
by franku
run the script |
81 |
|
36
by Holger Rapp
Added support for graphics in news items |
82 |
@property
|
83 |
def image(self): |
|
84 |
if self.categories.count() == 0: |
|
438.1.6
by franku
run the script |
85 |
return None |
36
by Holger Rapp
Added support for graphics in news items |
86 |
return self.categories.all()[0].image |
438.1.6
by franku
run the script |
87 |
|
36
by Holger Rapp
Added support for graphics in news items |
88 |
@property
|
89 |
def image_alt(self): |
|
90 |
"alt='' tag for <img>"
|
|
91 |
if self.categories.count() == 0: |
|
438.1.6
by franku
run the script |
92 |
return '' |
36
by Holger Rapp
Added support for graphics in news items |
93 |
return self.categories.all()[0].title |
17
by Holger Rapp
Main Page contains now the same informations as before |
94 |
|
95 |
def get_absolute_url(self): |
|
429.4.4
by franku
category view as list; cleanups |
96 |
return reverse('news_detail', args=(self.publish.year, self.publish.strftime('%b'), self.publish.day, self.slug, )) |
429.4.3
by franku
added first attempt to show post by category |
97 |
|
98 |
def get_category_slug(self): |
|
99 |
try: |
|
100 |
s = self.categories.all()[0].slug |
|
101 |
except IndexError: |
|
429.4.4
by franku
category view as list; cleanups |
102 |
return 'none' |
429.4.3
by franku
added first attempt to show post by category |
103 |
return s |
104 |
||
17
by Holger Rapp
Main Page contains now the same informations as before |
105 |
def get_previous_post(self): |
429.4.2
by franku
cleanups and fixes |
106 |
# get_previous_by_FOO(**kwargs) is a django model function
|
17
by Holger Rapp
Main Page contains now the same informations as before |
107 |
return self.get_previous_by_publish(status__gte=2) |
438.1.6
by franku
run the script |
108 |
|
17
by Holger Rapp
Main Page contains now the same informations as before |
109 |
def get_next_post(self): |
429.4.2
by franku
cleanups and fixes |
110 |
# get_next_by_FOO(**kwargs) is a django model function
|
111 |
return self.get_next_by_publish(status__gte=2, publish__lte=datetime.datetime.now()) |