comparison gpp/comments/models.py @ 1:dbd703f7d63a

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