comparison comments/models.py @ 581:ee87ea74d46b

For Django 1.4, rearranged project structure for new manage.py.
author Brian Neal <bgneal@gmail.com>
date Sat, 05 May 2012 17:10:48 -0500
parents gpp/comments/models.py@24f1230f3ee3
children 66d46d31d543
comparison
equal deleted inserted replaced
580:c525f3e0b5d0 581:ee87ea74d46b
1 """
2 Models for the comments application.
3 """
4 import datetime
5
6 from django.db import models
7 from django.conf import settings
8 from django.contrib.contenttypes.models import ContentType
9 from django.contrib.contenttypes import generic
10 from django.contrib.auth.models import User
11 from django.core import urlresolvers
12
13 from core.markup import site_markup
14
15
16 COMMENT_MAX_LENGTH = getattr(settings, 'COMMENT_MAX_LENGTH', 3000)
17
18 class CommentManager(models.Manager):
19 """Manager for the Comment model class."""
20
21 def for_object(self, obj, filter_public=True):
22 """QuerySet for all comments for a particular model instance."""
23 ct = ContentType.objects.get_for_model(obj)
24 qs = self.get_query_set().filter(content_type__pk=ct.id,
25 object_id=obj.id)
26 if filter_public:
27 qs = qs.filter(is_public=True)
28 return qs
29
30
31 class Comment(models.Model):
32 """My own version of a Comment class that can attach comments to any other model."""
33 content_type = models.ForeignKey(ContentType)
34 object_id = models.PositiveIntegerField(db_index=True)
35 content_object = generic.GenericForeignKey('content_type', 'object_id')
36 user = models.ForeignKey(User)
37 comment = models.TextField(max_length=COMMENT_MAX_LENGTH)
38 html = models.TextField(blank=True)
39 creation_date = models.DateTimeField()
40 ip_address = models.IPAddressField('IP Address')
41 is_public = models.BooleanField(default=True,
42 help_text='Uncheck this field to make the comment invisible.')
43 is_removed = models.BooleanField(default=False,
44 help_text='Check this field to replace the comment with a ' \
45 '"This comment has been removed" message')
46
47 # Attach manager
48 objects = CommentManager()
49
50 class Meta:
51 ordering = ('creation_date', )
52
53 def __unicode__(self):
54 return u'%s: %s...' % (self.user.username, self.comment[:50])
55
56 def save(self, *args, **kwargs):
57 if not self.id:
58 self.creation_date = datetime.datetime.now()
59
60 self.html = site_markup(self.comment)
61 super(Comment, self).save(*args, **kwargs)
62
63 def get_absolute_url(self):
64 return self.get_content_object_url() + ('#c%s' % self.id)
65
66 def get_content_object_url(self):
67 """
68 Get a URL suitable for redirecting to the content object.
69 """
70 return urlresolvers.reverse(
71 "comments-url-redirect",
72 args=(self.content_type_id, self.object_id)
73 )
74
75 def not_removed(self):
76 """
77 Returns not self.is_removed. Used on the admin display for
78 "green board" display purposes.
79 """
80 return not self.is_removed
81 not_removed.boolean = True
82
83
84 class CommentFlag(models.Model):
85 """This model represents a user flagging a comment as inappropriate."""
86 user = models.ForeignKey(User)
87 comment = models.ForeignKey(Comment)
88 flag_date = models.DateTimeField(auto_now_add=True)
89
90 def __unicode__(self):
91 return u'Comment ID %s flagged by %s' % (self.comment.id, self.user.username)
92
93 class Meta:
94 ordering = ('flag_date', )
95
96 def get_comment_url(self):
97 return '<a href="/admin/comments/comment/%s">Comment</a>' % self.comment.id
98 get_comment_url.allow_tags = True
99