3
3
from django.db.models import permalink
4
4
from django.contrib.auth.models import User
5
5
from tagging.fields import TagField
6
from news.managers import PublicManager
7
from django.urls import reverse
6
from widelands.news.managers import PublicManager
7
from django.core.urlresolvers import reverse
10
if settings.USE_SPHINX:
11
from djangosphinx import SphinxSearch
13
def get_upload_name(inst, fn):
16
def get_upload_name( inst, fn ):
15
18
extension = fn.split('.')[-1].lower()
18
return 'news/img/%s.%s' % (inst.title, extension)
21
return 'news/img/%s.%s' % (inst.title,extension)
21
23
class Category(models.Model):
22
24
"""Category model."""
23
title = models.CharField(_('title'), max_length=100)
24
slug = models.SlugField(_('slug'), unique=True)
25
image = models.ImageField(upload_to=get_upload_name, max_length=100)
25
title = models.CharField(_('title'), max_length=100)
26
slug = models.SlugField(_('slug'), unique=True)
27
image = models.ImageField( upload_to=get_upload_name, max_length=100 )
28
30
verbose_name = _('category')
30
32
db_table = 'news_categories'
31
33
ordering = ('title',)
33
38
def __unicode__(self):
34
39
return u'%s' % self.title
36
42
def get_absolute_url(self):
37
return reverse('category_posts', args=(self.slug,))
43
return ('news_category_detail', None, {'slug': self.slug})
40
46
class Post(models.Model):
46
title = models.CharField(_('title'), max_length=200)
47
slug = models.SlugField(_('slug'), unique_for_date='publish')
48
author = models.ForeignKey(User, null=True)
49
body = models.TextField(
50
_('body'), help_text='Text entered here will be rendered using Markdown')
51
tease = models.TextField(_('tease'), blank=True)
52
status = models.IntegerField(
53
_('status'), choices=STATUS_CHOICES, default=2)
54
allow_comments = models.BooleanField(_('allow comments'), default=True)
55
publish = models.DateTimeField(_('publish'))
56
created = models.DateTimeField(_('created'), auto_now_add=True)
57
modified = models.DateTimeField(_('modified'), auto_now=True)
58
categories = models.ManyToManyField(Category, blank=True)
60
objects = PublicManager()
52
title = models.CharField(_('title'), max_length=200)
53
slug = models.SlugField(_('slug'), unique_for_date='publish')
54
author = models.ForeignKey(User, blank=True, null=True)
55
body = models.TextField(_('body'))
56
tease = models.TextField(_('tease'), blank=True)
57
status = models.IntegerField(_('status'), choices=STATUS_CHOICES, default=2)
58
allow_comments = models.BooleanField(_('allow comments'), default=True)
59
publish = models.DateTimeField(_('publish'))
60
created = models.DateTimeField(_('created'), auto_now_add=True)
61
modified = models.DateTimeField(_('modified'), auto_now=True)
62
categories = models.ManyToManyField(Category, blank=True)
64
objects = PublicManager()
66
if settings.USE_SPHINX:
67
search = SphinxSearch(
63
76
verbose_name = _('post')
64
77
verbose_name_plural = _('posts')
65
db_table = 'news_posts'
66
ordering = ('-publish',)
78
db_table = 'news_posts'
79
ordering = ('-publish',)
67
80
get_latest_by = 'publish'
83
list_display = ('title', 'publish', 'status')
84
list_filter = ('publish', 'categories', 'status')
85
search_fields = ('title', 'body')
69
87
def __unicode__(self):
70
88
return u'%s' % self.title
79
97
if self.categories.count() == 0:
81
99
return self.categories.all()[0].image != ''
85
102
if self.categories.count() == 0:
87
104
return self.categories.all()[0].image
90
106
def image_alt(self):
91
107
"alt='' tag for <img>"
92
108
if self.categories.count() == 0:
94
110
return self.categories.all()[0].title
96
113
def get_absolute_url(self):
97
return reverse('news_detail', args=(self.publish.year, self.publish.strftime('%b'), self.publish.day, self.slug, ))
99
def get_category_slug(self):
101
s = self.categories.all()[0].slug
114
return ('news_detail', None, {
116
'year': self.publish.year,
117
'month': self.publish.strftime('%m'),
118
'day': self.publish.day,
106
121
def get_previous_post(self):
107
# get_previous_by_FOO(**kwargs) is a django model function
108
122
return self.get_previous_by_publish(status__gte=2)
110
124
def get_next_post(self):
111
# get_next_by_FOO(**kwargs) is a django model function
112
return self.get_next_by_publish(status__gte=2, publish__lte=datetime.datetime.now())
125
return self.get_next_by_publish(status__gte=2)