Package django :: Package contrib :: Package comments :: Module models
[hide private]
[frames] | no frames]

Source Code for Module django.contrib.comments.models

  1  from django.db import models 
  2  from django.contrib.contenttypes.models import ContentType 
  3  from django.contrib.sites.models import Site 
  4  from django.contrib.auth.models import User 
  5  from django.utils.translation import ugettext_lazy as _ 
  6  from django.conf import settings 
  7  import datetime 
  8   
  9  MIN_PHOTO_DIMENSION = 5 
 10  MAX_PHOTO_DIMENSION = 1000 
 11   
 12  # option codes for comment-form hidden fields 
 13  PHOTOS_REQUIRED = 'pr' 
 14  PHOTOS_OPTIONAL = 'pa' 
 15  RATINGS_REQUIRED = 'rr' 
 16  RATINGS_OPTIONAL = 'ra' 
 17  IS_PUBLIC = 'ip' 
 18   
 19  # what users get if they don't have any karma 
 20  DEFAULT_KARMA = 5 
 21  KARMA_NEEDED_BEFORE_DISPLAYED = 3 
 22   
23 -class CommentManager(models.Manager):
24 - def get_security_hash(self, options, photo_options, rating_options, target):
25 """ 26 Returns the MD5 hash of the given options (a comma-separated string such as 27 'pa,ra') and target (something like 'lcom.eventtimes:5157'). Used to 28 validate that submitted form options have not been tampered-with. 29 """ 30 import md5 31 return md5.new(options + photo_options + rating_options + target + settings.SECRET_KEY).hexdigest()
32
33 - def get_rating_options(self, rating_string):
34 """ 35 Given a rating_string, this returns a tuple of (rating_range, options). 36 >>> s = "scale:1-10|First_category|Second_category" 37 >>> Comment.objects.get_rating_options(s) 38 ([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], ['First category', 'Second category']) 39 """ 40 rating_range, options = rating_string.split('|', 1) 41 rating_range = range(int(rating_range[6:].split('-')[0]), int(rating_range[6:].split('-')[1])+1) 42 choices = [c.replace('_', ' ') for c in options.split('|')] 43 return rating_range, choices
44
45 - def get_list_with_karma(self, **kwargs):
46 """ 47 Returns a list of Comment objects matching the given lookup terms, with 48 _karma_total_good and _karma_total_bad filled. 49 """ 50 extra_kwargs = {} 51 extra_kwargs.setdefault('select', {}) 52 extra_kwargs['select']['_karma_total_good'] = 'SELECT COUNT(*) FROM comments_karmascore, comments_comment WHERE comments_karmascore.comment_id=comments_comment.id AND score=1' 53 extra_kwargs['select']['_karma_total_bad'] = 'SELECT COUNT(*) FROM comments_karmascore, comments_comment WHERE comments_karmascore.comment_id=comments_comment.id AND score=-1' 54 return self.filter(**kwargs).extra(**extra_kwargs)
55
56 - def user_is_moderator(self, user):
57 if user.is_superuser: 58 return True 59 for g in user.groups.all(): 60 if g.id == settings.COMMENTS_MODERATORS_GROUP: 61 return True 62 return False
63
64 -class Comment(models.Model):
65 user = models.ForeignKey(User, raw_id_admin=True) 66 content_type = models.ForeignKey(ContentType) 67 object_id = models.IntegerField(_('object ID')) 68 headline = models.CharField(_('headline'), max_length=255, blank=True) 69 comment = models.TextField(_('comment'), max_length=3000) 70 rating1 = models.PositiveSmallIntegerField(_('rating #1'), blank=True, null=True) 71 rating2 = models.PositiveSmallIntegerField(_('rating #2'), blank=True, null=True) 72 rating3 = models.PositiveSmallIntegerField(_('rating #3'), blank=True, null=True) 73 rating4 = models.PositiveSmallIntegerField(_('rating #4'), blank=True, null=True) 74 rating5 = models.PositiveSmallIntegerField(_('rating #5'), blank=True, null=True) 75 rating6 = models.PositiveSmallIntegerField(_('rating #6'), blank=True, null=True) 76 rating7 = models.PositiveSmallIntegerField(_('rating #7'), blank=True, null=True) 77 rating8 = models.PositiveSmallIntegerField(_('rating #8'), blank=True, null=True) 78 # This field designates whether to use this row's ratings in aggregate 79 # functions (summaries). We need this because people are allowed to post 80 # multiple reviews on the same thing, but the system will only use the 81 # latest one (with valid_rating=True) in tallying the reviews. 82 valid_rating = models.BooleanField(_('is valid rating')) 83 submit_date = models.DateTimeField(_('date/time submitted'), auto_now_add=True) 84 is_public = models.BooleanField(_('is public')) 85 ip_address = models.IPAddressField(_('IP address'), blank=True, null=True) 86 is_removed = models.BooleanField(_('is removed'), help_text=_('Check this box if the comment is inappropriate. A "This comment has been removed" message will be displayed instead.')) 87 site = models.ForeignKey(Site) 88 objects = CommentManager()
89 - class Meta:
90 verbose_name = _('comment') 91 verbose_name_plural = _('comments') 92 ordering = ('-submit_date',)
93 - class Admin:
94 fields = ( 95 (None, {'fields': ('content_type', 'object_id', 'site')}), 96 ('Content', {'fields': ('user', 'headline', 'comment')}), 97 ('Ratings', {'fields': ('rating1', 'rating2', 'rating3', 'rating4', 'rating5', 'rating6', 'rating7', 'rating8', 'valid_rating')}), 98 ('Meta', {'fields': ('is_public', 'is_removed', 'ip_address')}), 99 ) 100 list_display = ('user', 'submit_date', 'content_type', 'get_content_object') 101 list_filter = ('submit_date',) 102 date_hierarchy = 'submit_date' 103 search_fields = ('comment', 'user__username')
104
105 - def __unicode__(self):
106 return "%s: %s..." % (self.user.username, self.comment[:100])
107
108 - def get_absolute_url(self):
109 try: 110 return self.get_content_object().get_absolute_url() + "#c" + str(self.id) 111 except AttributeError: 112 return ""
113
114 - def get_crossdomain_url(self):
115 return "/r/%d/%d/" % (self.content_type_id, self.object_id)
116
117 - def get_flag_url(self):
118 return "/comments/flag/%s/" % self.id
119
120 - def get_deletion_url(self):
121 return "/comments/delete/%s/" % self.id
122
123 - def get_content_object(self):
124 """ 125 Returns the object that this comment is a comment on. Returns None if 126 the object no longer exists. 127 """ 128 from django.core.exceptions import ObjectDoesNotExist 129 try: 130 return self.content_type.get_object_for_this_type(pk=self.object_id) 131 except ObjectDoesNotExist: 132 return None
133 134 get_content_object.short_description = _('Content object') 135
136 - def _fill_karma_cache(self):
137 "Helper function that populates good/bad karma caches" 138 good, bad = 0, 0 139 for k in self.karmascore_set: 140 if k.score == -1: 141 bad +=1 142 elif k.score == 1: 143 good +=1 144 self._karma_total_good, self._karma_total_bad = good, bad
145
146 - def get_good_karma_total(self):
147 if not hasattr(self, "_karma_total_good"): 148 self._fill_karma_cache() 149 return self._karma_total_good
150
151 - def get_bad_karma_total(self):
152 if not hasattr(self, "_karma_total_bad"): 153 self._fill_karma_cache() 154 return self._karma_total_bad
155
156 - def get_karma_total(self):
157 if not hasattr(self, "_karma_total_good") or not hasattr(self, "_karma_total_bad"): 158 self._fill_karma_cache() 159 return self._karma_total_good + self._karma_total_bad
160
161 - def get_as_text(self):
162 return _('Posted by %(user)s at %(date)s\n\n%(comment)s\n\nhttp://%(domain)s%(url)s') % \ 163 {'user': self.user.username, 'date': self.submit_date, 164 'comment': self.comment, 'domain': self.site.domain, 'url': self.get_absolute_url()}
165
166 -class FreeComment(models.Model):
167 # A FreeComment is a comment by a non-registered user. 168 content_type = models.ForeignKey(ContentType) 169 object_id = models.IntegerField(_('object ID')) 170 comment = models.TextField(_('comment'), max_length=3000) 171 person_name = models.CharField(_("person's name"), max_length=50) 172 submit_date = models.DateTimeField(_('date/time submitted'), auto_now_add=True) 173 is_public = models.BooleanField(_('is public')) 174 ip_address = models.IPAddressField(_('ip address')) 175 # TODO: Change this to is_removed, like Comment 176 approved = models.BooleanField(_('approved by staff')) 177 site = models.ForeignKey(Site)
178 - class Meta:
179 verbose_name = _('free comment') 180 verbose_name_plural = _('free comments') 181 ordering = ('-submit_date',)
182 - class Admin:
183 fields = ( 184 (None, {'fields': ('content_type', 'object_id', 'site')}), 185 ('Content', {'fields': ('person_name', 'comment')}), 186 ('Meta', {'fields': ('submit_date', 'is_public', 'ip_address', 'approved')}), 187 ) 188 list_display = ('person_name', 'submit_date', 'content_type', 'get_content_object') 189 list_filter = ('submit_date',) 190 date_hierarchy = 'submit_date' 191 search_fields = ('comment', 'person_name')
192
193 - def __unicode__(self):
194 return "%s: %s..." % (self.person_name, self.comment[:100])
195
196 - def get_absolute_url(self):
197 try: 198 return self.get_content_object().get_absolute_url() + "#c" + str(self.id) 199 except AttributeError: 200 return ""
201
202 - def get_content_object(self):
203 """ 204 Returns the object that this comment is a comment on. Returns None if 205 the object no longer exists. 206 """ 207 from django.core.exceptions import ObjectDoesNotExist 208 try: 209 return self.content_type.get_object_for_this_type(pk=self.object_id) 210 except ObjectDoesNotExist: 211 return None
212 213 get_content_object.short_description = _('Content object')
214
215 -class KarmaScoreManager(models.Manager):
216 - def vote(self, user_id, comment_id, score):
217 try: 218 karma = self.get(comment__pk=comment_id, user__pk=user_id) 219 except self.model.DoesNotExist: 220 karma = self.model(None, user_id=user_id, comment_id=comment_id, score=score, scored_date=datetime.datetime.now()) 221 karma.save() 222 else: 223 karma.score = score 224 karma.scored_date = datetime.datetime.now() 225 karma.save()
226
227 - def get_pretty_score(self, score):
228 """ 229 Given a score between -1 and 1 (inclusive), returns the same score on a 230 scale between 1 and 10 (inclusive), as an integer. 231 """ 232 if score is None: 233 return DEFAULT_KARMA 234 return int(round((4.5 * score) + 5.5))
235
236 -class KarmaScore(models.Model):
237 user = models.ForeignKey(User) 238 comment = models.ForeignKey(Comment) 239 score = models.SmallIntegerField(_('score'), db_index=True) 240 scored_date = models.DateTimeField(_('score date'), auto_now=True) 241 objects = KarmaScoreManager()
242 - class Meta:
243 verbose_name = _('karma score') 244 verbose_name_plural = _('karma scores') 245 unique_together = (('user', 'comment'),)
246
247 - def __unicode__(self):
248 return _("%(score)d rating by %(user)s") % {'score': self.score, 'user': self.user}
249
250 -class UserFlagManager(models.Manager):
251 - def flag(self, comment, user):
252 """ 253 Flags the given comment by the given user. If the comment has already 254 been flagged by the user, or it was a comment posted by the user, 255 nothing happens. 256 """ 257 if int(comment.user_id) == int(user.id): 258 return # A user can't flag his own comment. Fail silently. 259 try: 260 f = self.objects.get(user__pk=user.id, comment__pk=comment.id) 261 except self.model.DoesNotExist: 262 from django.core.mail import mail_managers 263 f = self.model(None, user.id, comment.id, None) 264 message = _('This comment was flagged by %(user)s:\n\n%(text)s') % {'user': user.username, 'text': comment.get_as_text()} 265 mail_managers('Comment flagged', message, fail_silently=True) 266 f.save()
267
268 -class UserFlag(models.Model):
269 user = models.ForeignKey(User) 270 comment = models.ForeignKey(Comment) 271 flag_date = models.DateTimeField(_('flag date'), auto_now_add=True) 272 objects = UserFlagManager()
273 - class Meta:
274 verbose_name = _('user flag') 275 verbose_name_plural = _('user flags') 276 unique_together = (('user', 'comment'),)
277
278 - def __unicode__(self):
279 return _("Flag by %r") % self.user
280
281 -class ModeratorDeletion(models.Model):
282 user = models.ForeignKey(User, verbose_name='moderator') 283 comment = models.ForeignKey(Comment) 284 deletion_date = models.DateTimeField(_('deletion date'), auto_now_add=True)
285 - class Meta:
286 verbose_name = _('moderator deletion') 287 verbose_name_plural = _('moderator deletions') 288 unique_together = (('user', 'comment'),)
289
290 - def __unicode__(self):
291 return _("Moderator deletion by %r") % self.user
292