~widelands-dev/widelands-website/django_staticfiles

« back to all changes in this revision

Viewing changes to pybb/models.py

merged trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
from django.conf import settings
18
18
from notification.models import send
19
19
from django.contrib.auth.models import User
 
20
from check_input.models import SuspiciousInput
 
21
 
20
22
 
21
23
try:
22
24
    from notification import models as notification
91
93
 
92
94
    @property
93
95
    def last_post(self):
94
 
        posts = self.posts.exclude(hidden=True).order_by(
 
96
        # This has better performance than using the posts manager hidden_topics
 
97
        # We search only for the last 10 topics
 
98
        topics = self.topics.order_by('-updated')[:10]
 
99
        for topic in topics:
 
100
            if topic.is_hidden:
 
101
                continue
 
102
            posts = topic.posts.exclude(hidden=True).order_by(
95
103
            '-created').select_related()
 
104
            break
 
105
 
96
106
        try:
97
107
            return posts[0]
98
108
        except IndexError:
122
132
 
123
133
    @property
124
134
    def head(self):
125
 
        return self.posts.all().order_by('created').select_related()[0]
 
135
        try:
 
136
            return self.posts.all().order_by('created').select_related()[0]
 
137
        except:
 
138
            return None
126
139
 
127
140
    @property
128
141
    def last_post(self):
129
142
        return self.posts.exclude(hidden=True).order_by('-created').select_related()[0]
130
143
 
131
 
    # If the first post of this topic is hidden, the topic is hidden
132
144
    @property
133
145
    def is_hidden(self):
 
146
        # If the first post of this topic is hidden, the topic is hidden
134
147
        try:
135
 
            p = self.posts.order_by('created').filter(
136
 
                hidden=False).select_related()[0]
137
 
        except IndexError:
138
 
            return True
139
 
        return False
 
148
            return self.posts.first().hidden
 
149
        except:
 
150
            return False
140
151
 
141
152
    @property
142
153
    def post_count(self):
191
202
        self.body_html = urlize(self.body_html)
192
203
 
193
204
 
 
205
class HiddenTopicsManager(models.Manager):
 
206
    """Find all hidden topics by posts.
 
207
 
 
208
    A whole topic is hidden, if the first post is hidden.
 
209
    This manager returns the hidden topics and can be used to filter them out
 
210
    like so:
 
211
 
 
212
    Post.objects.exclude(topic__in=Post.hidden_topics.all()).filter(...)
 
213
 
 
214
    Use this with caution, because it affects performance, see:
 
215
    https://docs.djangoproject.com/en/dev/ref/models/querysets/#in
 
216
    """
 
217
 
 
218
    def get_queryset(self, *args, **kwargs):
 
219
        qs = super(HiddenTopicsManager,
 
220
                   self).get_queryset().filter(hidden=True)
 
221
 
 
222
        hidden_topics = []
 
223
        for post in qs:
 
224
            if post.topic.is_hidden:
 
225
                hidden_topics.append(post.topic)
 
226
        return hidden_topics
 
227
 
 
228
 
194
229
class Post(RenderableItem):
195
230
    topic = models.ForeignKey(
196
231
        Topic, related_name='posts', verbose_name=_('Topic'))
205
240
    body_text = models.TextField(_('Text version'))
206
241
    hidden = models.BooleanField(_('Hidden'), blank=True, default=False)
207
242
 
 
243
    objects = models.Manager() # Normal manager 
 
244
    hidden_topics = HiddenTopicsManager() # Custom manager
 
245
 
208
246
    class Meta:
209
247
        ordering = ['created']
210
248
        verbose_name = _('Post')
260
298
        if self_id == head_post_id:
261
299
            self.topic.delete()
262
300
 
 
301
    def is_spam(self):
 
302
        try:
 
303
            SuspiciousInput.objects.get(object_id = self.pk)
 
304
            return True
 
305
        except:
 
306
            pass
 
307
        return False
 
308
 
263
309
 
264
310
class Read(models.Model):
265
311
    """For each topic that user has entered the time is logged to this