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