annotate gpp/comments/models.py @ 59:2022851c78cf

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