annotate gpp/comments/models.py @ 7:ca66189c7c44

Added tiny_mce to the GPP_THIRD_PARTY_JS setting. Updated code to use it.
author Brian Neal <bgneal@gmail.com>
date Sun, 12 Apr 2009 00:31:36 +0000
parents dbd703f7d63a
children 7b6540b185d9
rev   line source
gremmie@1 1 """
gremmie@1 2 Models for the comments application.
gremmie@1 3 """
gremmie@1 4 from django.db import models
gremmie@1 5 from django.conf import settings
gremmie@1 6 from django.contrib.contenttypes.models import ContentType
gremmie@1 7 from django.contrib.contenttypes import generic
gremmie@1 8 from django.contrib.auth.models import User
gremmie@1 9 from django.template.loader import render_to_string
gremmie@1 10
gremmie@1 11
gremmie@1 12 COMMENT_MAX_LENGTH = getattr(settings, 'COMMENT_MAX_LENGTH', 3000)
gremmie@1 13
gremmie@1 14 class CommentManager(models.Manager):
gremmie@1 15 """Manager for the Comment model class."""
gremmie@1 16
gremmie@1 17 def for_object(self, obj, filter_public=True):
gremmie@1 18 """QuerySet for all comments for a particular model instance."""
gremmie@1 19 ct = ContentType.objects.get_for_model(obj)
gremmie@1 20 qs = self.get_query_set().filter(content_type__pk=ct.id,
gremmie@1 21 object_id=obj.id)
gremmie@1 22 if filter_public:
gremmie@1 23 qs = qs.filter(is_public=True)
gremmie@1 24 return qs
gremmie@1 25
gremmie@1 26
gremmie@1 27 class Comment(models.Model):
gremmie@1 28 """My own version of a Comment class that can attach comments to any other model."""
gremmie@1 29 content_type = models.ForeignKey(ContentType)
gremmie@1 30 object_id = models.PositiveIntegerField()
gremmie@1 31 content_object = generic.GenericForeignKey('content_type', 'object_id')
gremmie@1 32 user = models.ForeignKey(User)
gremmie@1 33 comment = models.TextField(max_length=COMMENT_MAX_LENGTH)
gremmie@1 34 html = models.TextField(blank=True)
gremmie@1 35 creation_date = models.DateTimeField(auto_now_add=True)
gremmie@1 36 ip_address = models.IPAddressField('IP Address')
gremmie@1 37 is_public = models.BooleanField(default=True,
gremmie@1 38 help_text='Uncheck this field to make the comment invisible.')
gremmie@1 39 is_removed = models.BooleanField(default=False,
gremmie@1 40 help_text='Check this field to replace the comment with a ' \
gremmie@1 41 '"This comment has been removed" message')
gremmie@1 42
gremmie@1 43 # Attach manager
gremmie@1 44 objects = CommentManager()
gremmie@1 45
gremmie@1 46 def __unicode__(self):
gremmie@1 47 return u'%s: %s...' % (self.user.username, self.comment[:50])
gremmie@1 48
gremmie@1 49 class Meta:
gremmie@1 50 ordering = ('creation_date', )
gremmie@1 51
gremmie@1 52 def save(self, force_insert=False, force_update=False):
gremmie@1 53 html = render_to_string('comments/markdown.html', {'data': self.comment})
gremmie@1 54 self.html = html.strip()
gremmie@1 55 super(Comment, self).save(force_insert, force_update)
gremmie@1 56
gremmie@1 57
gremmie@1 58 class CommentFlag(models.Model):
gremmie@1 59 """This model represents a user flagging a comment as inappropriate."""
gremmie@1 60 user = models.ForeignKey(User)
gremmie@1 61 comment = models.ForeignKey(Comment)
gremmie@1 62 flag_date = models.DateTimeField(auto_now_add=True)
gremmie@1 63
gremmie@1 64 def __unicode__(self):
gremmie@1 65 return u'Comment ID %s flagged by %s' % (self.comment_id, self.user.username)
gremmie@1 66
gremmie@1 67 class Meta:
gremmie@1 68 ordering = ('flag_date', )
gremmie@1 69
gremmie@1 70 def get_comment_url(self):
gremmie@1 71 return '<a href="/admin/comments/comment/%s">Comment</a>' % self.comment.id
gremmie@1 72 get_comment_url.allow_tags = True
gremmie@1 73