Ticket #1637: comments.3.diff

File comments.3.diff, 17.2 KB (added by Dave St.Germain <dcs@…>, 19 years ago)

includes other patches to comments

  • django/contrib/comments/templatetags/comments.py

     
    22from django.contrib.comments.models import PHOTOS_REQUIRED, PHOTOS_OPTIONAL, RATINGS_REQUIRED, RATINGS_OPTIONAL, IS_PUBLIC
    33from django.contrib.comments.models import MIN_PHOTO_DIMENSION, MAX_PHOTO_DIMENSION
    44from django import template
    5 from django.core.template import loader
     5from django.template import loader
    66from django.core.exceptions import ObjectDoesNotExist
    77from django.contrib.contenttypes.models import ContentType
    88import re
    99
    1010register = template.Library()
    1111
    12 COMMENT_FORM = 'comments/form'
    13 FREE_COMMENT_FORM = 'comments/freeform'
     12COMMENT_FORM = 'comments/form.html'
     13FREE_COMMENT_FORM = 'comments/freeform.html'
    1414
    1515class CommentFormNode(template.Node):
    1616    def __init__(self, content_type, obj_id_lookup_var, obj_id, free,
     
    7777
    7878    def render(self, context):
    7979        from django.conf import settings
    80         get_count_function = self.free and FreeComment.objects.get_count or Comment.objects.get_count
     80        manager = self.free and FreeComment.objects or Comment.objects
    8181        if self.context_var_name is not None:
    8282            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()
    8686        context[self.var_name] = comment_count
    8787        return ''
    8888
     
    9696
    9797    def render(self, context):
    9898        from django.conf import settings
    99         get_list_function = self.free and FreeComment.objects.get_list or Comment.objects.get_list_with_karma
     99        get_list_function = self.free and FreeComment.objects.filter or Comment.objects.get_list_with_karma
    100100        if self.context_var_name is not None:
    101101            try:
    102102                self.obj_id = template.resolve_variable(self.context_var_name, context)
     
    104104                return ''
    105105        kwargs = {
    106106            '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,
    109109            'site__id__exact': settings.SITE_ID,
    110             'select_related': True,
    111             'order_by': (self.ordering + 'submit_date',),
    112110        }
    113111        kwargs.update(self.extra_kwargs)
    114112        if not self.free and settings.COMMENTS_BANNED_USERS_GROUP:
    115             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)
     113            kwargs['select'] = {'is_hidden': 'user_id IN (SELECT user_id FROM auth_user_groups WHERE group_id = %s)' % settings.COMMENTS_BANNED_USERS_GROUP}
     114        comment_list = get_list_function(**kwargs).order_by(self.ordering + 'submit_date').select_related()
    117115
    118116        if not self.free:
    119117            if context.has_key('user') and not context['user'].is_anonymous():
     
    157155        except ValueError: # unpack list of wrong size
    158156            raise template.TemplateSyntaxError, "Third argument in %r tag must be in the format 'package.module'" % tokens[0]
    159157        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)
    161159        except ContentType.DoesNotExist:
    162160            raise template.TemplateSyntaxError, "%r tag has invalid content-type '%s.%s'" % (tokens[0], package, module)
    163161        obj_id_lookup_var, obj_id = None, None
     
    237235        except ValueError: # unpack list of wrong size
    238236            raise template.TemplateSyntaxError, "Third argument in %r tag must be in the format 'package.module'" % tokens[0]
    239237        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)
    241239        except ContentType.DoesNotExist:
    242240            raise template.TemplateSyntaxError, "%r tag has invalid content-type '%s.%s'" % (tokens[0], package, module)
    243241        var_name, obj_id = None, None
     
    292290        except ValueError: # unpack list of wrong size
    293291            raise template.TemplateSyntaxError, "Third argument in %r tag must be in the format 'package.module'" % tokens[0]
    294292        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)
    296294        except ContentType.DoesNotExist:
    297295            raise template.TemplateSyntaxError, "%r tag has invalid content-type '%s.%s'" % (tokens[0], package, module)
    298296        var_name, obj_id = None, None
  • django/contrib/comments/models.py

     
    4848        _karma_total_good and _karma_total_bad filled.
    4949        """
    5050        kwargs.setdefault('select', {})
    51         kwargs['select']['_karma_total_good'] = 'SELECT COUNT(*) FROM comments_karma WHERE comments_karma.comment_id=comments.id AND score=1'
    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)
     51        kwargs['select']['_karma_total_good'] = 'SELECT COUNT(*) FROM comments_karmascore WHERE comments_karmascore.comment_id=comments.id AND score=1'
     52        kwargs['select']['_karma_total_bad'] = 'SELECT COUNT(*) FROM comments_karmascore WHERE comments_karmascore.comment_id=comments.id AND score=-1'
     53        return self.filter(**kwargs)
    5454
    5555    def user_is_moderator(self, user):
    5656        if user.is_superuser:
     
    102102        search_fields = ('comment', 'user__username')
    103103
    104104    def __repr__(self):
    105         return "%s: %s..." % (self.get_user().username, self.comment[:100])
     105        return "%s: %s..." % (self.user.username, self.comment[:100])
    106106
    107107    def get_absolute_url(self):
    108108        return self.get_content_object().get_absolute_url() + "#c" + str(self.id)
     
    156156
    157157    def get_as_text(self):
    158158        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,
    160160            'comment': self.comment, 'domain': self.get_site().domain, 'url': self.get_absolute_url()}
    161161
    162162class FreeComment(models.Model):
     
    208208class KarmaScoreManager(models.Manager):
    209209    def vote(self, user_id, comment_id, score):
    210210        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)
    212212        except self.model.DoesNotExist:
    213213            karma = self.model(None, user_id, comment_id, score, datetime.datetime.now())
    214214            karma.save()
     
    238238        unique_together = (('user', 'comment'),)
    239239
    240240    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}
    242242
    243243class UserFlagManager(models.Manager):
    244244    def flag(self, comment, user):
     
    250250        if int(comment.user_id) == int(user.id):
    251251            return # A user can't flag his own comment. Fail silently.
    252252        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)
    254254        except self.model.DoesNotExist:
    255255            from django.core.mail import mail_managers
    256256            f = self.model(None, user.id, comment.id, None)
     
    269269        unique_together = (('user', 'comment'),)
    270270
    271271    def __repr__(self):
    272         return _("Flag by %r") % self.get_user()
     272        return _("Flag by %r") % self.user
    273273
    274274class ModeratorDeletion(models.Model):
    275275    user = models.ForeignKey(User, verbose_name='moderator')
     
    281281        unique_together = (('user', 'comment'),)
    282282
    283283    def __repr__(self):
    284         return _("Moderator deletion by %r") % self.get_user()
     284        return _("Moderator deletion by %r") % self.user
  • django/contrib/comments/views/karma.py

     
    11from django.http import Http404
    22from django.shortcuts import render_to_response
    33from django.template import RequestContext
    4 from django.models.comments import comments, karma
     4from django.contrib.comments.models import Comment, KarmaScore
    55
    66def vote(request, comment_id, vote):
    77    """
     
    1818    if request.user.is_anonymous():
    1919        raise Http404, _("Anonymous users cannot vote")
    2020    try:
    21         comment = comments.get_object(pk=comment_id)
    22     except comments.CommentDoesNotExist:
     21        comment = Comment.objects.get(pk=comment_id)
     22    except Comment.DoesNotExist:
    2323        raise Http404, _("Invalid comment ID")
    2424    if comment.user_id == request.user.id:
    2525        raise Http404, _("No voting for yourself")
    26     karma.vote(request.user.id, comment_id, rating)
     26    KarmaScore.objects.vote(request.user.id, comment_id, rating)
    2727    # Reload comment to ensure we have up to date karma count
    28     comment = comments.get_object(pk=comment_id)
     28    comment = Comment.objects.get(pk=comment_id)
    2929    return render_to_response('comments/karma_vote_accepted.html', {'comment': comment}, context_instance=RequestContext(request))
  • django/contrib/comments/views/userflags.py

     
    1 from django.shortcuts import render_to_response
     1from django.shortcuts import render_to_response, get_object_or_404
    22from django.template import RequestContext
    33from django.http import Http404
    4 from django.models.comments import comments, moderatordeletions, userflags
     4from django.contrib.comments.models import Comment, ModeratorDeletion, UserFlag
    55from django.contrib.auth.decorators import login_required
    66from django.http import HttpResponseRedirect
    77from django.conf import settings
     
    1515        comment
    1616            the flagged `comments.comments` object
    1717    """
    18     try:
    19         comment = comments.get_object(pk=comment_id, site__id__exact=settings.SITE_ID)
    20     except comments.CommentDoesNotExist:
    21         raise Http404
     18    comment = get_object_or_404(Comment,pk=comment_id, site__id__exact=settings.SITE_ID)
    2219    if request.POST:
    23         userflags.flag(comment, request.user)
     20        UserFlag.objects.flag(comment, request.user)
    2421        return HttpResponseRedirect('%sdone/' % request.path)
    2522    return render_to_response('comments/flag_verify.html', {'comment': comment}, context_instance=RequestContext(request))
    2623flag = login_required(flag)
    2724
    2825def flag_done(request, comment_id):
    29     try:
    30         comment = comments.get_object(pk=comment_id, site__id__exact=settings.SITE_ID)
    31     except comments.CommentDoesNotExist:
    32         raise Http404
     26    comment = get_object_or_404(Comment,pk=comment_id, site__id__exact=settings.SITE_ID)
    3327    return render_to_response('comments/flag_done.html', {'comment': comment}, context_instance=RequestContext(request))
    3428
    3529def delete(request, comment_id):
     
    4135        comment
    4236            the flagged `comments.comments` object
    4337    """
    44     try:
    45         comment = comments.get_object(pk=comment_id, site__id__exact=settings.SITE_ID)
    46     except comments.CommentDoesNotExist:
     38    comment = get_object_or_404(Comment,pk=comment_id, site__id__exact=settings.SITE_ID)
     39    if not Comment.objects.user_is_moderator(request.user):
    4740        raise Http404
    48     if not comments.user_is_moderator(request.user):
    49         raise Http404
    5041    if request.POST:
    5142        # If the comment has already been removed, silently fail.
    5243        if not comment.is_removed:
    5344            comment.is_removed = True
    5445            comment.save()
    55             m = moderatordeletions.ModeratorDeletion(None, request.user.id, comment.id, None)
     46            m = ModeratorDeletion(None, request.user.id, comment.id, None)
    5647            m.save()
    5748        return HttpResponseRedirect('%sdone/' % request.path)
    5849    return render_to_response('comments/delete_verify.html', {'comment': comment}, context_instance=RequestContext(request))
    5950delete = login_required(delete)
    6051
    6152def delete_done(request, comment_id):
    62     try:
    63         comment = comments.get_object(pk=comment_id, site__id__exact=settings.SITE_ID)
    64     except comments.CommentDoesNotExist:
    65         raise Http404
     53    comment = get_object_or_404(Comment,pk=comment_id, site__id__exact=settings.SITE_ID)
    6654    return render_to_response('comments/delete_done.html', {'comment': comment}, context_instance=RequestContext(request))
  • django/contrib/comments/views/comments.py

     
    9090    def save(self, new_data):
    9191        today = datetime.date.today()
    9292        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"],
    9494            object_id__exact=new_data["object_id"], user__id__exact=self.get_user_id()):
    9595            # Check that this comment isn't duplicate. (Sometimes people post
    9696            # comments twice by mistake.) If it is, fail silently by pretending
     
    146146        # Check that this comment isn't duplicate. (Sometimes people post
    147147        # comments twice by mistake.) If it is, fail silently by pretending
    148148        # 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"],
    150150            object_id__exact=new_data["object_id"], person_name__exact=new_data["person_name"],
    151151            submit_date__year=today.year, submit_date__month=today.month,
    152152            submit_date__day=today.day):
     
    202202        rating_range, rating_choices = [], []
    203203    content_type_id, object_id = target.split(':') # target is something like '52:5157'
    204204    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)
    206206    except ObjectDoesNotExist:
    207207        raise Http404, _("The comment form had an invalid 'target' parameter -- the object ID was invalid")
    208208    option_list = options.split(',') # options is something like 'pa,ra'
     
    285285    if Comment.objects.get_security_hash(options, '', '', target) != security_hash:
    286286        raise Http404, _("Somebody tampered with the comment form (security violation)")
    287287    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)
    289289    try:
    290290        obj = content_type.get_object_for_this_type(pk=object_id)
    291291    except ObjectDoesNotExist:
     
    333333    if request.GET.has_key('c'):
    334334        content_type_id, object_id = request.GET['c'].split(':')
    335335        try:
    336             content_type = ContentType.objects.get_object(pk=content_type_id)
     336            content_type = ContentType.objects.get(pk=content_type_id)
    337337            obj = content_type.get_object_for_this_type(pk=object_id)
    338338        except ObjectDoesNotExist:
    339339            pass
  • django/contrib/comments/feeds.py

     
    2525        return "Latest comments on %s" % self._site.name
    2626
    2727    def items(self):
    28         return self.comments_class.objects.get_list(**self._get_lookup_kwargs())
     28        return self.comments_class.objects.filter(**self._get_lookup_kwargs())
    2929
    3030    def _get_lookup_kwargs(self):
    3131        return {
Back to Top