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