~widelands-dev/widelands-website/django_staticfiles

« back to all changes in this revision

Viewing changes to threadedcomments/models.py

  • Committer: franku
  • Date: 2018-10-03 09:01:09 UTC
  • mto: This revision was merged to the branch mainline in revision 502.
  • Revision ID: somal@arcor.de-20181003090109-so4zn8x9vujarq6n
removed IPAddressField from pybb

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
from django.db import models
2
2
from django.contrib.contenttypes.models import ContentType
 
3
#from django.contrib.contenttypes import generic
3
4
from django.contrib.contenttypes.fields import GenericForeignKey
4
5
from django.contrib.auth.models import User
5
6
from datetime import datetime
15
16
MARKDOWN = 1
16
17
TEXTILE = 2
17
18
REST = 3
 
19
#HTML = 4
18
20
PLAINTEXT = 5
19
21
MARKUP_CHOICES = (
20
22
    (MARKDOWN, _('markdown')),
21
23
    (TEXTILE, _('textile')),
22
24
    (REST, _('restructuredtext')),
 
25
    #    (HTML, _("html")),
23
26
    (PLAINTEXT, _('plaintext')),
24
27
)
25
28
 
175
178
    is_public = models.BooleanField(_('is public'), default=True)
176
179
    is_approved = models.BooleanField(_('is approved'), default=False)
177
180
 
 
181
    # Extra Field
 
182
    ip_address = models.GenericIPAddressField(
 
183
        _('IP address'), null=True, blank=True)
 
184
 
178
185
    objects = ThreadedCommentManager()
179
186
    public = PublicThreadedCommentManager()
180
187
 
196
203
        and due to ``list_display`` limitations."""
197
204
        return self.content_object
198
205
 
 
206
    def get_base_data(self, show_dates=True):
 
207
        """Outputs a Python dictionary representing the most useful bits of
 
208
        information about this particular object instance.
 
209
 
 
210
        This is mostly useful for testing purposes, as the output from
 
211
        the serializer changes from run to run.  However, this may end
 
212
        up being useful for JSON and/or XML data exchange going forward
 
213
        and as the serializer system is changed.
 
214
 
 
215
        """
 
216
        markup = 'plaintext'
 
217
        for markup_choice in MARKUP_CHOICES:
 
218
            if self.markup == markup_choice[0]:
 
219
                markup = markup_choice[1]
 
220
                break
 
221
        to_return = {
 
222
            'content_object': self.content_object,
 
223
            'parent': self.parent,
 
224
            'user': self.user,
 
225
            'comment': self.comment,
 
226
            'is_public': self.is_public,
 
227
            'is_approved': self.is_approved,
 
228
            'ip_address': self.ip_address,
 
229
            'markup': force_unicode(markup),
 
230
        }
 
231
        if show_dates:
 
232
            to_return['date_submitted'] = self.date_submitted
 
233
            to_return['date_modified'] = self.date_modified
 
234
            to_return['date_approved'] = self.date_approved
 
235
        return to_return
 
236
 
199
237
    class Meta:
200
238
        ordering = ('-date_submitted',)
201
239
        verbose_name = _('Threaded Comment')
202
240
        verbose_name_plural = _('Threaded Comments')
203
241
        get_latest_by = 'date_submitted'
 
242
 
 
243
 
 
244
class FreeThreadedComment(models.Model):
 
245
    """
 
246
    A threaded comment which need not be associated with an instance of 
 
247
    ``django.contrib.auth.models.User``.  Instead, it requires minimally a name,
 
248
    and maximally a name, website, and e-mail address.  It is given its hierarchy
 
249
    by a nullable relationship back on itself named ``parent``.
 
250
 
 
251
    This ``FreeThreadedComment`` supports several kinds of markup languages,
 
252
    including Textile, Markdown, and ReST.
 
253
 
 
254
    It also includes two Managers: ``objects``, which is the same as the normal
 
255
    ``objects`` Manager with a few added utility functions (see above), and
 
256
    ``public``, which has those same utility functions but limits the QuerySet to
 
257
    only those values which are designated as public (``is_public=True``).
 
258
    """
 
259
    # Generic Foreign Key Fields
 
260
    content_type = models.ForeignKey(ContentType)
 
261
    object_id = models.PositiveIntegerField(_('object ID'))
 
262
    content_object = GenericForeignKey()
 
263
 
 
264
    # Hierarchy Field
 
265
    parent = models.ForeignKey(
 
266
        'self', null=True, blank=True, default=None, related_name='children')
 
267
 
 
268
    # User-Replacement Fields
 
269
    name = models.CharField(_('name'), max_length=128)
 
270
    website = models.URLField(_('site'), blank=True)
 
271
    email = models.EmailField(_('e-mail address'), blank=True)
 
272
 
 
273
    # Date Fields
 
274
    date_submitted = models.DateTimeField(
 
275
        _('date/time submitted'), default=datetime.now)
 
276
    date_modified = models.DateTimeField(
 
277
        _('date/time modified'), default=datetime.now)
 
278
    date_approved = models.DateTimeField(
 
279
        _('date/time approved'), default=None, null=True, blank=True)
 
280
 
 
281
    # Meat n' Potatoes
 
282
    comment = models.TextField(_('comment'))
 
283
    markup = models.IntegerField(
 
284
        choices=MARKUP_CHOICES, default=DEFAULT_MARKUP, null=True, blank=True)
 
285
 
 
286
    # Status Fields
 
287
    is_public = models.BooleanField(_('is public'), default=True)
 
288
    is_approved = models.BooleanField(_('is approved'), default=False)
 
289
 
 
290
    # Extra Field
 
291
    ip_address = models.GenericIPAddressField(
 
292
        _('IP address'), null=True, blank=True)
 
293
 
 
294
    objects = ThreadedCommentManager()
 
295
    public = PublicThreadedCommentManager()
 
296
 
 
297
    def __unicode__(self):
 
298
        if len(self.comment) > 50:
 
299
            return self.comment[:50] + '...'
 
300
        return self.comment[:50]
 
301
 
 
302
    def save(self, **kwargs):
 
303
        if not self.markup:
 
304
            self.markup = DEFAULT_MARKUP
 
305
        self.date_modified = datetime.now()
 
306
        if not self.date_approved and self.is_approved:
 
307
            self.date_approved = datetime.now()
 
308
        super(FreeThreadedComment, self).save()
 
309
 
 
310
    def get_content_object(self, **kwargs):
 
311
        """Wrapper around the GenericForeignKey due to compatibility reasons
 
312
        and due to ``list_display`` limitations."""
 
313
        return self.content_object
 
314
 
 
315
    def get_base_data(self, show_dates=True):
 
316
        """Outputs a Python dictionary representing the most useful bits of
 
317
        information about this particular object instance.
 
318
 
 
319
        This is mostly useful for testing purposes, as the output from
 
320
        the serializer changes from run to run.  However, this may end
 
321
        up being useful for JSON and/or XML data exchange going forward
 
322
        and as the serializer system is changed.
 
323
 
 
324
        """
 
325
        markup = 'plaintext'
 
326
        for markup_choice in MARKUP_CHOICES:
 
327
            if self.markup == markup_choice[0]:
 
328
                markup = markup_choice[1]
 
329
                break
 
330
        to_return = {
 
331
            'content_object': self.content_object,
 
332
            'parent': self.parent,
 
333
            'name': self.name,
 
334
            'website': self.website,
 
335
            'email': self.email,
 
336
            'comment': self.comment,
 
337
            'is_public': self.is_public,
 
338
            'is_approved': self.is_approved,
 
339
            'ip_address': self.ip_address,
 
340
            'markup': force_unicode(markup),
 
341
        }
 
342
        if show_dates:
 
343
            to_return['date_submitted'] = self.date_submitted
 
344
            to_return['date_modified'] = self.date_modified
 
345
            to_return['date_approved'] = self.date_approved
 
346
        return to_return
 
347
 
 
348
    class Meta:
 
349
        ordering = ('-date_submitted',)
 
350
        verbose_name = _('Free Threaded Comment')
 
351
        verbose_name_plural = _('Free Threaded Comments')
 
352
        get_latest_by = 'date_submitted'
 
353
 
 
354
 
 
355
class TestModel(models.Model):
 
356
    """This model is simply used by this application's test suite as a model to
 
357
    which to attach comments."""
 
358
    name = models.CharField(max_length=5)
 
359
    is_public = models.BooleanField(default=True)
 
360
    date = models.DateTimeField(default=datetime.now)