Ticket #1637: comments.2.diff
File comments.2.diff, 11.2 KB (added by , 19 years ago) |
---|
-
django/contrib/comments/templatetags/comments.py
2 2 from django.contrib.comments.models import PHOTOS_REQUIRED, PHOTOS_OPTIONAL, RATINGS_REQUIRED, RATINGS_OPTIONAL, IS_PUBLIC 3 3 from django.contrib.comments.models import MIN_PHOTO_DIMENSION, MAX_PHOTO_DIMENSION 4 4 from django import template 5 from django. core.template import loader5 from django.template import loader 6 6 from django.core.exceptions import ObjectDoesNotExist 7 7 from django.contrib.contenttypes.models import ContentType 8 8 import re … … 77 77 78 78 def render(self, context): 79 79 from django.conf import settings 80 get_count_function = self.free and FreeComment.objects.get_count or Comment.objects.get_count80 manager = self.free and FreeComment.objects or Comment.objects 81 81 if self.context_var_name is not None: 82 82 self.obj_id = template.resolve_variable(self.context_var_name, context) 83 comment_count = get_count_function(object_id__exact=self.obj_id,84 content_type__ package__label__exact=self.package,85 content_type__ python_module_name__exact=self.module, site__id__exact=settings.SITE_ID)83 comment_count = manager.filter(object_id__exact=self.obj_id, 84 content_type__app_label__exact=self.package, 85 content_type__model__exact=self.module, site__id__exact=settings.SITE_ID).count() 86 86 context[self.var_name] = comment_count 87 87 return '' 88 88 … … 96 96 97 97 def render(self, context): 98 98 from django.conf import settings 99 get_list_function = self.free and FreeComment.objects. get_listor Comment.objects.get_list_with_karma99 get_list_function = self.free and FreeComment.objects.filter or Comment.objects.get_list_with_karma 100 100 if self.context_var_name is not None: 101 101 try: 102 102 self.obj_id = template.resolve_variable(self.context_var_name, context) … … 104 104 return '' 105 105 kwargs = { 106 106 'object_id__exact': self.obj_id, 107 'content_type__ package__label__exact': self.package,108 'content_type__ python_module_name__exact': self.module,107 'content_type__app_label__exact': self.package, 108 'content_type__model__exact': self.module, 109 109 'site__id__exact': settings.SITE_ID, 110 'select_related': True,111 'order_by': (self.ordering + 'submit_date',),112 110 } 113 111 kwargs.update(self.extra_kwargs) 114 112 if not self.free and settings.COMMENTS_BANNED_USERS_GROUP: 115 113 kwargs['select'] = {'is_hidden': 'user_id IN (SELECT user_id FROM auth_users_groups WHERE group_id = %s)' % settings.COMMENTS_BANNED_USERS_GROUP} 116 comment_list = get_list_function(**kwargs) 114 comment_list = get_list_function(**kwargs).order_by(self.ordering + 'submit_date').select_related() 117 115 118 116 if not self.free: 119 117 if context.has_key('user') and not context['user'].is_anonymous(): … … 157 155 except ValueError: # unpack list of wrong size 158 156 raise template.TemplateSyntaxError, "Third argument in %r tag must be in the format 'package.module'" % tokens[0] 159 157 try: 160 content_type = ContentType.objects.get _object(package__label__exact=package, python_module_name__exact=module)158 content_type = ContentType.objects.get(app_label__exact=package, model__exact=module) 161 159 except ContentType.DoesNotExist: 162 160 raise template.TemplateSyntaxError, "%r tag has invalid content-type '%s.%s'" % (tokens[0], package, module) 163 161 obj_id_lookup_var, obj_id = None, None … … 237 235 except ValueError: # unpack list of wrong size 238 236 raise template.TemplateSyntaxError, "Third argument in %r tag must be in the format 'package.module'" % tokens[0] 239 237 try: 240 content_type = ContentType.objects.get _object(package__label__exact=package, python_module_name__exact=module)238 content_type = ContentType.objects.get(app_label__exact=package, model__exact=module) 241 239 except ContentType.DoesNotExist: 242 240 raise template.TemplateSyntaxError, "%r tag has invalid content-type '%s.%s'" % (tokens[0], package, module) 243 241 var_name, obj_id = None, None … … 292 290 except ValueError: # unpack list of wrong size 293 291 raise template.TemplateSyntaxError, "Third argument in %r tag must be in the format 'package.module'" % tokens[0] 294 292 try: 295 content_type = ContentType.objects.get _object(package__label__exact=package, python_module_name__exact=module)293 content_type = ContentType.objects.get(app_label__exact=package,model__exact=module) 296 294 except ContentType.DoesNotExist: 297 295 raise template.TemplateSyntaxError, "%r tag has invalid content-type '%s.%s'" % (tokens[0], package, module) 298 296 var_name, obj_id = None, None -
django/contrib/comments/models.py
50 50 kwargs.setdefault('select', {}) 51 51 kwargs['select']['_karma_total_good'] = 'SELECT COUNT(*) FROM comments_karma WHERE comments_karma.comment_id=comments.id AND score=1' 52 52 kwargs['select']['_karma_total_bad'] = 'SELECT COUNT(*) FROM comments_karma WHERE comments_karma.comment_id=comments.id AND score=-1' 53 return self. get_list(**kwargs)53 return self.filter(**kwargs) 54 54 55 55 def user_is_moderator(self, user): 56 56 if user.is_superuser: … … 102 102 search_fields = ('comment', 'user__username') 103 103 104 104 def __repr__(self): 105 return "%s: %s..." % (self. get_user().username, self.comment[:100])105 return "%s: %s..." % (self.user.username, self.comment[:100]) 106 106 107 107 def get_absolute_url(self): 108 108 return self.get_content_object().get_absolute_url() + "#c" + str(self.id) … … 156 156 157 157 def get_as_text(self): 158 158 return _('Posted by %(user)s at %(date)s\n\n%(comment)s\n\nhttp://%(domain)s%(url)s') % \ 159 {'user': self. get_user().username, 'date': self.submit_date,159 {'user': self.user.username, 'date': self.submit_date, 160 160 'comment': self.comment, 'domain': self.get_site().domain, 'url': self.get_absolute_url()} 161 161 162 162 class FreeComment(models.Model): … … 208 208 class KarmaScoreManager(models.Manager): 209 209 def vote(self, user_id, comment_id, score): 210 210 try: 211 karma = self. get_object(comment__id__exact=comment_id, user__id__exact=user_id)211 karma = self.objects.get(comment__id__exact=comment_id, user__id__exact=user_id) 212 212 except self.model.DoesNotExist: 213 213 karma = self.model(None, user_id, comment_id, score, datetime.datetime.now()) 214 214 karma.save() … … 238 238 unique_together = (('user', 'comment'),) 239 239 240 240 def __repr__(self): 241 return _("%(score)d rating by %(user)s") % {'score': self.score, 'user': self. get_user()}241 return _("%(score)d rating by %(user)s") % {'score': self.score, 'user': self.user} 242 242 243 243 class UserFlagManager(models.Manager): 244 244 def flag(self, comment, user): … … 250 250 if int(comment.user_id) == int(user.id): 251 251 return # A user can't flag his own comment. Fail silently. 252 252 try: 253 f = self. get_object(user__id__exact=user.id, comment__id__exact=comment.id)253 f = self.objects.get(user__id__exact=user.id, comment__id__exact=comment.id) 254 254 except self.model.DoesNotExist: 255 255 from django.core.mail import mail_managers 256 256 f = self.model(None, user.id, comment.id, None) … … 269 269 unique_together = (('user', 'comment'),) 270 270 271 271 def __repr__(self): 272 return _("Flag by %r") % self. get_user()272 return _("Flag by %r") % self.user 273 273 274 274 class ModeratorDeletion(models.Model): 275 275 user = models.ForeignKey(User, verbose_name='moderator') … … 281 281 unique_together = (('user', 'comment'),) 282 282 283 283 def __repr__(self): 284 return _("Moderator deletion by %r") % self. get_user()284 return _("Moderator deletion by %r") % self.user -
django/contrib/comments/views/comments.py
90 90 def save(self, new_data): 91 91 today = datetime.date.today() 92 92 c = self.get_comment(new_data) 93 for old in Comment.objects. get_list(content_type__id__exact=new_data["content_type_id"],93 for old in Comment.objects.filter(content_type__id__exact=new_data["content_type_id"], 94 94 object_id__exact=new_data["object_id"], user__id__exact=self.get_user_id()): 95 95 # Check that this comment isn't duplicate. (Sometimes people post 96 96 # comments twice by mistake.) If it is, fail silently by pretending … … 146 146 # Check that this comment isn't duplicate. (Sometimes people post 147 147 # comments twice by mistake.) If it is, fail silently by pretending 148 148 # the comment was posted successfully. 149 for old_comment in FreeComment.objects. get_list(content_type__id__exact=new_data["content_type_id"],149 for old_comment in FreeComment.objects.filter(content_type__id__exact=new_data["content_type_id"], 150 150 object_id__exact=new_data["object_id"], person_name__exact=new_data["person_name"], 151 151 submit_date__year=today.year, submit_date__month=today.month, 152 152 submit_date__day=today.day): … … 202 202 rating_range, rating_choices = [], [] 203 203 content_type_id, object_id = target.split(':') # target is something like '52:5157' 204 204 try: 205 obj = ContentType.objects.get _object(pk=content_type_id).get_object_for_this_type(pk=object_id)205 obj = ContentType.objects.get(pk=content_type_id).get_object_for_this_type(pk=object_id) 206 206 except ObjectDoesNotExist: 207 207 raise Http404, _("The comment form had an invalid 'target' parameter -- the object ID was invalid") 208 208 option_list = options.split(',') # options is something like 'pa,ra' … … 285 285 if Comment.objects.get_security_hash(options, '', '', target) != security_hash: 286 286 raise Http404, _("Somebody tampered with the comment form (security violation)") 287 287 content_type_id, object_id = target.split(':') # target is something like '52:5157' 288 content_type = ContentType.objects.get _object(pk=content_type_id)288 content_type = ContentType.objects.get(pk=content_type_id) 289 289 try: 290 290 obj = content_type.get_object_for_this_type(pk=object_id) 291 291 except ObjectDoesNotExist: … … 333 333 if request.GET.has_key('c'): 334 334 content_type_id, object_id = request.GET['c'].split(':') 335 335 try: 336 content_type = ContentType.objects.get _object(pk=content_type_id)336 content_type = ContentType.objects.get(pk=content_type_id) 337 337 obj = content_type.get_object_for_this_type(pk=object_id) 338 338 except ObjectDoesNotExist: 339 339 pass