~widelands-dev/widelands-website/django_staticfiles

« back to all changes in this revision

Viewing changes to pybb/models.py

  • Committer: franku
  • Date: 2018-05-06 08:20:39 UTC
  • mto: This revision was merged to the branch mainline in revision 494.
  • Revision ID: somal@arcor.de-20180506082039-v8n40alffhi2ulct
This is an Attribute error

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
 
 
22
20
 
23
21
try:
24
22
    from notification import models as notification
93
91
 
94
92
    @property
95
93
    def last_post(self):
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(
 
94
        posts = self.posts.exclude(hidden=True).order_by(
103
95
            '-created').select_related()
104
 
            break
105
 
 
106
96
        try:
107
97
            return posts[0]
108
98
        except IndexError:
132
122
 
133
123
    @property
134
124
    def head(self):
135
 
        try:
136
 
            return self.posts.all().order_by('created').select_related()[0]
137
 
        except:
138
 
            return None
 
125
        return self.posts.all().order_by('created').select_related()[0]
139
126
 
140
127
    @property
141
128
    def last_post(self):
142
129
        return self.posts.exclude(hidden=True).order_by('-created').select_related()[0]
143
130
 
 
131
    # If the first post of this topic is hidden, the topic is hidden
144
132
    @property
145
133
    def is_hidden(self):
146
 
        # If the first post of this topic is hidden, the topic is hidden
147
134
        try:
148
 
            return self.posts.first().hidden
149
 
        except:
150
 
            return False
 
135
            p = self.posts.order_by('created').filter(
 
136
                hidden=False).select_related()[0]
 
137
        except IndexError:
 
138
            return True
 
139
        return False
151
140
 
152
141
    @property
153
142
    def post_count(self):
202
191
        self.body_html = urlize(self.body_html)
203
192
 
204
193
 
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
 
        try:
224
 
            for post in qs:
225
 
                if post.topic.is_hidden:
226
 
                    hidden_topics.append(post.topic)
227
 
            return hidden_topics
228
 
        except:
229
 
            return []
230
 
 
231
 
 
232
194
class Post(RenderableItem):
233
195
    topic = models.ForeignKey(
234
196
        Topic, related_name='posts', verbose_name=_('Topic'))
241
203
    body = models.TextField(_('Message'))
242
204
    body_html = models.TextField(_('HTML version'))
243
205
    body_text = models.TextField(_('Text version'))
 
206
    user_ip = models.GenericIPAddressField(_('User IP'), default='')
244
207
    hidden = models.BooleanField(_('Hidden'), blank=True, default=False)
245
208
 
246
 
    objects = models.Manager() # Normal manager 
247
 
    hidden_topics = HiddenTopicsManager() # Custom manager
248
 
 
249
209
    class Meta:
250
210
        ordering = ['created']
251
211
        verbose_name = _('Post')
301
261
        if self_id == head_post_id:
302
262
            self.topic.delete()
303
263
 
304
 
    def is_spam(self):
305
 
        try:
306
 
            SuspiciousInput.objects.get(object_id = self.pk)
307
 
            return True
308
 
        except:
309
 
            pass
310
 
        return False
311
 
 
312
264
 
313
265
class Read(models.Model):
314
266
    """For each topic that user has entered the time is logged to this