annotate gpp/comments/models.py @ 124:9c18250972d5

Refactored the markdown/smiley logic. Created classes for Markdown and Smilify. No longer call render_to_string() in models.py for various models.
author Brian Neal <bgneal@gmail.com>
date Sat, 14 Nov 2009 04:32:32 +0000
parents d0d779dd0832
children 48621ba5c385
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
bgneal@14 9 from django.core import urlresolvers
gremmie@1 10
bgneal@124 11 from core.markup import markdown
bgneal@124 12 from smiley import smilify
bgneal@124 13
gremmie@1 14
gremmie@1 15 COMMENT_MAX_LENGTH = getattr(settings, 'COMMENT_MAX_LENGTH', 3000)
gremmie@1 16
gremmie@1 17 class CommentManager(models.Manager):
gremmie@1 18 """Manager for the Comment model class."""
gremmie@1 19
gremmie@1 20 def for_object(self, obj, filter_public=True):
gremmie@1 21 """QuerySet for all comments for a particular model instance."""
gremmie@1 22 ct = ContentType.objects.get_for_model(obj)
gremmie@1 23 qs = self.get_query_set().filter(content_type__pk=ct.id,
gremmie@1 24 object_id=obj.id)
gremmie@1 25 if filter_public:
gremmie@1 26 qs = qs.filter(is_public=True)
gremmie@1 27 return qs
gremmie@1 28
gremmie@1 29
gremmie@1 30 class Comment(models.Model):
gremmie@1 31 """My own version of a Comment class that can attach comments to any other model."""
gremmie@1 32 content_type = models.ForeignKey(ContentType)
gremmie@1 33 object_id = models.PositiveIntegerField()
gremmie@1 34 content_object = generic.GenericForeignKey('content_type', 'object_id')
gremmie@1 35 user = models.ForeignKey(User)
gremmie@1 36 comment = models.TextField(max_length=COMMENT_MAX_LENGTH)
gremmie@1 37 html = models.TextField(blank=True)
gremmie@1 38 creation_date = models.DateTimeField(auto_now_add=True)
gremmie@1 39 ip_address = models.IPAddressField('IP Address')
gremmie@1 40 is_public = models.BooleanField(default=True,
gremmie@1 41 help_text='Uncheck this field to make the comment invisible.')
gremmie@1 42 is_removed = models.BooleanField(default=False,
gremmie@1 43 help_text='Check this field to replace the comment with a ' \
gremmie@1 44 '"This comment has been removed" message')
gremmie@1 45
gremmie@1 46 # Attach manager
gremmie@1 47 objects = CommentManager()
gremmie@1 48
bgneal@14 49 class Meta:
bgneal@14 50 ordering = ('creation_date', )
bgneal@14 51
gremmie@1 52 def __unicode__(self):
gremmie@1 53 return u'%s: %s...' % (self.user.username, self.comment[:50])
gremmie@1 54
gremmie@1 55 def save(self, force_insert=False, force_update=False):
bgneal@124 56 self.html = smilify(markdown(self.comment))
gremmie@1 57 super(Comment, self).save(force_insert, force_update)
gremmie@1 58
bgneal@14 59 def get_absolute_url(self):
bgneal@14 60 return self.get_content_object_url() + ('#c%s' % self.id)
bgneal@14 61
bgneal@14 62 def get_content_object_url(self):
bgneal@14 63 """
bgneal@14 64 Get a URL suitable for redirecting to the content object.
bgneal@14 65 """
bgneal@14 66 return urlresolvers.reverse(
bgneal@14 67 "comments-url-redirect",
bgneal@14 68 args=(self.content_type_id, self.object_id)
bgneal@14 69 )
bgneal@14 70
gremmie@1 71
gremmie@1 72 class CommentFlag(models.Model):
gremmie@1 73 """This model represents a user flagging a comment as inappropriate."""
gremmie@1 74 user = models.ForeignKey(User)
gremmie@1 75 comment = models.ForeignKey(Comment)
gremmie@1 76 flag_date = models.DateTimeField(auto_now_add=True)
gremmie@1 77
gremmie@1 78 def __unicode__(self):
bgneal@98 79 return u'Comment ID %s flagged by %s' % (self.comment.id, self.user.username)
gremmie@1 80
gremmie@1 81 class Meta:
gremmie@1 82 ordering = ('flag_date', )
gremmie@1 83
gremmie@1 84 def get_comment_url(self):
gremmie@1 85 return '<a href="/admin/comments/comment/%s">Comment</a>' % self.comment.id
gremmie@1 86 get_comment_url.allow_tags = True
gremmie@1 87