~widelands-dev/widelands-website/django_staticfiles

« back to all changes in this revision

Viewing changes to threadedcomments/models.py

merged trunk

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
4
3
from django.contrib.contenttypes.fields import GenericForeignKey
5
4
from django.contrib.auth.models import User
6
5
from datetime import datetime
16
15
MARKDOWN = 1
17
16
TEXTILE = 2
18
17
REST = 3
19
 
#HTML = 4
20
18
PLAINTEXT = 5
21
19
MARKUP_CHOICES = (
22
20
    (MARKDOWN, _('markdown')),
23
21
    (TEXTILE, _('textile')),
24
22
    (REST, _('restructuredtext')),
25
 
    #    (HTML, _("html")),
26
23
    (PLAINTEXT, _('plaintext')),
27
24
)
28
25
 
178
175
    is_public = models.BooleanField(_('is public'), default=True)
179
176
    is_approved = models.BooleanField(_('is approved'), default=False)
180
177
 
181
 
    # Extra Field
182
 
    ip_address = models.GenericIPAddressField(
183
 
        _('IP address'), null=True, blank=True)
184
 
 
185
178
    objects = ThreadedCommentManager()
186
179
    public = PublicThreadedCommentManager()
187
180
 
203
196
        and due to ``list_display`` limitations."""
204
197
        return self.content_object
205
198
 
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
 
 
237
199
    class Meta:
238
200
        ordering = ('-date_submitted',)
239
201
        verbose_name = _('Threaded Comment')
240
202
        verbose_name_plural = _('Threaded Comments')
241
203
        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)