| Home | Trees | Indices | Help |
|---|
|
|
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 2465 6627 """ 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()3436 """ 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, choices4648 """ 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)5759 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 False68 """A comment by a registered user.""" 69 user = models.ForeignKey(User, raw_id_admin=True) 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() 93171 17295 verbose_name = _('comment') 96 verbose_name_plural = _('comments') 97 ordering = ('-submit_date',)98100 fields = ( 101 (None, {'fields': ('content_type', 'object_id', 'site')}), 102 ('Content', {'fields': ('user', 'headline', 'comment')}), 103 ('Ratings', {'fields': ('rating1', 'rating2', 'rating3', 'rating4', 'rating5', 'rating6', 'rating7', 'rating8', 'valid_rating')}), 104 ('Meta', {'fields': ('is_public', 'is_removed', 'ip_address')}), 105 ) 106 list_display = ('user', 'submit_date', 'content_type', 'get_content_object') 107 list_filter = ('submit_date',) 108 date_hierarchy = 'submit_date' 109 search_fields = ('comment', 'user__username')110 113115 try: 116 return self.get_content_object().get_absolute_url() + "#c" + str(self.id) 117 except AttributeError: 118 return ""119121 return "/r/%d/%d/" % (self.content_type_id, self.object_id)122124 return "/comments/flag/%s/" % self.id125127 return "/comments/delete/%s/" % self.id128130 """ 131 Returns the object that this comment is a comment on. Returns None if 132 the object no longer exists. 133 """ 134 from django.core.exceptions import ObjectDoesNotExist 135 try: 136 return self.content_type.get_object_for_this_type(pk=self.object_id) 137 except ObjectDoesNotExist: 138 return None139 140 get_content_object.short_description = _('Content object') 141143 """Helper function that populates good/bad karma caches.""" 144 good, bad = 0, 0 145 for k in self.karmascore_set: 146 if k.score == -1: 147 bad +=1 148 elif k.score == 1: 149 good +=1 150 self._karma_total_good, self._karma_total_bad = good, bad151153 if not hasattr(self, "_karma_total_good"): 154 self._fill_karma_cache() 155 return self._karma_total_good156158 if not hasattr(self, "_karma_total_bad"): 159 self._fill_karma_cache() 160 return self._karma_total_bad161163 if not hasattr(self, "_karma_total_good") or not hasattr(self, "_karma_total_bad"): 164 self._fill_karma_cache() 165 return self._karma_total_good + self._karma_total_bad166168 return _('Posted by %(user)s at %(date)s\n\n%(comment)s\n\nhttp://%(domain)s%(url)s') % \ 169 {'user': self.user.username, 'date': self.submit_date, 170 'comment': self.comment, 'domain': self.site.domain, 'url': self.get_absolute_url()}