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@128
|
11 from core.markup import site_markup
|
bgneal@124
|
12
|
gremmie@1
|
13
|
gremmie@1
|
14 COMMENT_MAX_LENGTH = getattr(settings, 'COMMENT_MAX_LENGTH', 3000)
|
gremmie@1
|
15
|
gremmie@1
|
16 class CommentManager(models.Manager):
|
gremmie@1
|
17 """Manager for the Comment model class."""
|
gremmie@1
|
18
|
gremmie@1
|
19 def for_object(self, obj, filter_public=True):
|
gremmie@1
|
20 """QuerySet for all comments for a particular model instance."""
|
gremmie@1
|
21 ct = ContentType.objects.get_for_model(obj)
|
gremmie@1
|
22 qs = self.get_query_set().filter(content_type__pk=ct.id,
|
gremmie@1
|
23 object_id=obj.id)
|
gremmie@1
|
24 if filter_public:
|
gremmie@1
|
25 qs = qs.filter(is_public=True)
|
gremmie@1
|
26 return qs
|
gremmie@1
|
27
|
gremmie@1
|
28
|
gremmie@1
|
29 class Comment(models.Model):
|
gremmie@1
|
30 """My own version of a Comment class that can attach comments to any other model."""
|
gremmie@1
|
31 content_type = models.ForeignKey(ContentType)
|
gremmie@1
|
32 object_id = models.PositiveIntegerField()
|
gremmie@1
|
33 content_object = generic.GenericForeignKey('content_type', 'object_id')
|
gremmie@1
|
34 user = models.ForeignKey(User)
|
gremmie@1
|
35 comment = models.TextField(max_length=COMMENT_MAX_LENGTH)
|
gremmie@1
|
36 html = models.TextField(blank=True)
|
gremmie@1
|
37 creation_date = models.DateTimeField(auto_now_add=True)
|
gremmie@1
|
38 ip_address = models.IPAddressField('IP Address')
|
gremmie@1
|
39 is_public = models.BooleanField(default=True,
|
gremmie@1
|
40 help_text='Uncheck this field to make the comment invisible.')
|
gremmie@1
|
41 is_removed = models.BooleanField(default=False,
|
gremmie@1
|
42 help_text='Check this field to replace the comment with a ' \
|
gremmie@1
|
43 '"This comment has been removed" message')
|
gremmie@1
|
44
|
gremmie@1
|
45 # Attach manager
|
gremmie@1
|
46 objects = CommentManager()
|
gremmie@1
|
47
|
bgneal@14
|
48 class Meta:
|
bgneal@14
|
49 ordering = ('creation_date', )
|
bgneal@14
|
50
|
gremmie@1
|
51 def __unicode__(self):
|
gremmie@1
|
52 return u'%s: %s...' % (self.user.username, self.comment[:50])
|
gremmie@1
|
53
|
gremmie@1
|
54 def save(self, force_insert=False, force_update=False):
|
bgneal@128
|
55 self.html = site_markup(self.comment)
|
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
|
bgneal@140
|
70 def not_removed(self):
|
bgneal@140
|
71 """
|
bgneal@140
|
72 Returns not self.is_removed. Used on the admin display for
|
bgneal@140
|
73 "green board" display purposes.
|
bgneal@140
|
74 """
|
bgneal@140
|
75 return not self.is_removed
|
bgneal@140
|
76 not_removed.boolean = True
|
bgneal@140
|
77
|
gremmie@1
|
78
|
gremmie@1
|
79 class CommentFlag(models.Model):
|
gremmie@1
|
80 """This model represents a user flagging a comment as inappropriate."""
|
gremmie@1
|
81 user = models.ForeignKey(User)
|
gremmie@1
|
82 comment = models.ForeignKey(Comment)
|
gremmie@1
|
83 flag_date = models.DateTimeField(auto_now_add=True)
|
gremmie@1
|
84
|
gremmie@1
|
85 def __unicode__(self):
|
bgneal@98
|
86 return u'Comment ID %s flagged by %s' % (self.comment.id, self.user.username)
|
gremmie@1
|
87
|
gremmie@1
|
88 class Meta:
|
gremmie@1
|
89 ordering = ('flag_date', )
|
gremmie@1
|
90
|
gremmie@1
|
91 def get_comment_url(self):
|
gremmie@1
|
92 return '<a href="/admin/comments/comment/%s">Comment</a>' % self.comment.id
|
gremmie@1
|
93 get_comment_url.allow_tags = True
|
gremmie@1
|
94
|