~widelands-dev/widelands-website/trunk

« back to all changes in this revision

Viewing changes to news/models.py

  • Committer: franku
  • Author(s): GunChleoc
  • Date: 2016-12-13 18:30:38 UTC
  • mfrom: (438.1.6 pyformat_util)
  • Revision ID: somal@arcor.de-20161213183038-5cgmvfh2fkgmoc1s
adding a script to run pyformat over the code base

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
import tagging
14
14
 
15
15
 
16
 
def get_upload_name( inst, fn ):
 
16
def get_upload_name(inst, fn):
17
17
    try:
18
18
        extension = fn.split('.')[-1].lower()
19
19
    except:
20
20
        extension = 'png'
21
 
    return 'news/img/%s.%s' % (inst.title,extension)
 
21
    return 'news/img/%s.%s' % (inst.title, extension)
 
22
 
22
23
 
23
24
class Category(models.Model):
24
25
    """Category model."""
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 )
 
26
    title = models.CharField(_('title'), max_length=100)
 
27
    slug = models.SlugField(_('slug'), unique=True)
 
28
    image = models.ImageField(upload_to=get_upload_name, max_length=100)
28
29
 
29
30
    class Meta:
30
31
        verbose_name = _('category')
45
46
        (1, _('Draft')),
46
47
        (2, _('Public')),
47
48
    )
48
 
    title           = models.CharField(_('title'), max_length=200)
49
 
    slug            = models.SlugField(_('slug'), unique_for_date='publish')
50
 
    author          = models.ForeignKey(User, null=True)
51
 
    body            = models.TextField(_('body'), help_text="Text entered here will be rendered using Markdown")
52
 
    tease           = models.TextField(_('tease'), blank=True)
53
 
    status          = models.IntegerField(_('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)
59
 
    tags            = TagField()
60
 
    objects         = PublicManager()
61
 
   
 
49
    title = models.CharField(_('title'), max_length=200)
 
50
    slug = models.SlugField(_('slug'), unique_for_date='publish')
 
51
    author = models.ForeignKey(User, null=True)
 
52
    body = models.TextField(
 
53
        _('body'), help_text='Text entered here will be rendered using Markdown')
 
54
    tease = models.TextField(_('tease'), blank=True)
 
55
    status = models.IntegerField(
 
56
        _('status'), choices=STATUS_CHOICES, default=2)
 
57
    allow_comments = models.BooleanField(_('allow comments'), default=True)
 
58
    publish = models.DateTimeField(_('publish'))
 
59
    created = models.DateTimeField(_('created'), auto_now_add=True)
 
60
    modified = models.DateTimeField(_('modified'), auto_now=True)
 
61
    categories = models.ManyToManyField(Category, blank=True)
 
62
    tags = TagField()
 
63
    objects = PublicManager()
 
64
 
62
65
    if settings.USE_SPHINX:
63
 
        search          = SphinxSearch(
64
 
            weights = {
 
66
        search = SphinxSearch(
 
67
            weights={
65
68
                'title': 100,
66
69
                'body': 80,
67
70
                'tease': 80,
68
 
                }
 
71
            }
69
72
        )
70
73
 
71
74
    class Meta:
72
75
        verbose_name = _('post')
73
76
        verbose_name_plural = _('posts')
74
 
        db_table  = 'news_posts'
75
 
        ordering  = ('-publish',)
 
77
        db_table = 'news_posts'
 
78
        ordering = ('-publish',)
76
79
        get_latest_by = 'publish'
77
80
 
78
81
    def __unicode__(self):
79
82
        return u'%s' % self.title
80
 
   
 
83
 
81
84
    #########
82
85
    # IMAGE #
83
86
    #########
88
91
        if self.categories.count() == 0:
89
92
            return False
90
93
        return self.categories.all()[0].image != ''
 
94
 
91
95
    @property
92
96
    def image(self):
93
97
        if self.categories.count() == 0:
94
 
            return None 
 
98
            return None
95
99
        return self.categories.all()[0].image
 
100
 
96
101
    @property
97
102
    def image_alt(self):
98
103
        "alt='' tag for <img>"
99
104
        if self.categories.count() == 0:
100
 
            return '' 
 
105
            return ''
101
106
        return self.categories.all()[0].title
102
107
 
103
108
    def get_absolute_url(self):
113
118
    def get_previous_post(self):
114
119
        # get_previous_by_FOO(**kwargs) is a django model function
115
120
        return self.get_previous_by_publish(status__gte=2)
116
 
    
 
121
 
117
122
    def get_next_post(self):
118
123
        # get_next_by_FOO(**kwargs) is a django model function
119
124
        return self.get_next_by_publish(status__gte=2, publish__lte=datetime.datetime.now())
120