Added django badge
[e_cidadania.git] / src / apps / ecidadania / news / models.py
blob459d68724cbb8d7f1cee45c4daaf2f9a3908b9b3
1 # -*- coding: utf-8 -*-
3 # Copyright (c) 2013 Clione Software
4 # Copyright (c) 2010-2013 Cidadania S. Coop. Galega
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
18 from django.db import models
19 from django.utils.translation import ugettext_lazy as _
20 from django.contrib.auth.models import User
22 from apps.thirdparty.tagging.fields import TagField
23 from apps.thirdparty.tagging.models import Tag
24 from core.spaces.models import Space
27 class Post(models.Model):
29 """
30 Model of a news post.
31 """
32 title = models.CharField(_('Title'), max_length=200,
33 help_text=_('Max: 200 characters'))
34 description = models.TextField(_('Description'))
35 pub_date = models.DateTimeField(_('Date'), auto_now_add=True)
36 post_lastup = models.DateTimeField(_('Last update'), auto_now=True)
37 author = models.ForeignKey(User, verbose_name=_('Author'), blank=True,
38 null=True, help_text=_('Change the user that will figure as the \
39 author'))
40 pub_index = models.BooleanField(_('Publish in index'),
41 help_text=_('This will publish the post in the main site page'))
42 space = models.ForeignKey(Space, verbose_name=_('Publish in'),
43 blank=True, null=True,
44 help_text=_('If you want to post to the index leave this blank'))
45 post_tags = TagField(help_text=_('Insert here relevant words related with the post'))
46 views = models.IntegerField(_('Views'), blank=True, null=True)
48 class Meta:
49 ordering = ['title']
50 verbose_name = _('Post')
51 verbose_name_plural = _('Posts')
52 get_latest_by = 'pub_date'
54 def __unicode__(self):
55 return self.title
57 def comment_count(self):
58 ct = ContentType.objects.get_for_model(Post)
59 obj_pk = self.id
60 return Comment.objects.filter(content_type=ct, object_pk=obj_pk).count()
62 def set_tags(self, tags):
63 Tag.objects.update_tags(self, tags)
65 def get_tags(self, tags):
66 return Tag.objects.get_for_object(self)
68 @models.permalink
69 def get_absolute_url(self):
70 if self.space is not None:
71 return ('view-post', (), {
72 'space_url': self.space.url,
73 'post_id': str(self.id)})
74 else:
75 return ('view-site-post', (), {
76 'post_id': str(self.id)})