Code

Ticket #1637: comments.3.diff

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

includes other patches to comments

Line 
1Index: django/contrib/comments/templatetags/comments.py
2===================================================================
3--- django/contrib/comments/templatetags/comments.py    (revision 2721)
4+++ django/contrib/comments/templatetags/comments.py    (working copy)
5@@ -2,15 +2,15 @@
6 from django.contrib.comments.models import PHOTOS_REQUIRED, PHOTOS_OPTIONAL, RATINGS_REQUIRED, RATINGS_OPTIONAL, IS_PUBLIC
7 from django.contrib.comments.models import MIN_PHOTO_DIMENSION, MAX_PHOTO_DIMENSION
8 from django import template
9-from django.core.template import loader
10+from django.template import loader
11 from django.core.exceptions import ObjectDoesNotExist
12 from django.contrib.contenttypes.models import ContentType
13 import re
14 
15 register = template.Library()
16 
17-COMMENT_FORM = 'comments/form'
18-FREE_COMMENT_FORM = 'comments/freeform'
19+COMMENT_FORM = 'comments/form.html'
20+FREE_COMMENT_FORM = 'comments/freeform.html'
21 
22 class CommentFormNode(template.Node):
23     def __init__(self, content_type, obj_id_lookup_var, obj_id, free,
24@@ -77,12 +77,12 @@
25 
26     def render(self, context):
27         from django.conf import settings
28-        get_count_function = self.free and FreeComment.objects.get_count or Comment.objects.get_count
29+        manager = self.free and FreeComment.objects or Comment.objects
30         if self.context_var_name is not None:
31             self.obj_id = template.resolve_variable(self.context_var_name, context)
32-        comment_count = get_count_function(object_id__exact=self.obj_id,
33-            content_type__package__label__exact=self.package,
34-            content_type__python_module_name__exact=self.module, site__id__exact=settings.SITE_ID)
35+        comment_count = manager.filter(object_id__exact=self.obj_id,
36+            content_type__app_label__exact=self.package,
37+            content_type__model__exact=self.module, site__id__exact=settings.SITE_ID).count()
38         context[self.var_name] = comment_count
39         return ''
40 
41@@ -96,7 +96,7 @@
42 
43     def render(self, context):
44         from django.conf import settings
45-        get_list_function = self.free and FreeComment.objects.get_list or Comment.objects.get_list_with_karma
46+        get_list_function = self.free and FreeComment.objects.filter or Comment.objects.get_list_with_karma
47         if self.context_var_name is not None:
48             try:
49                 self.obj_id = template.resolve_variable(self.context_var_name, context)
50@@ -104,16 +104,14 @@
51                 return ''
52         kwargs = {
53             'object_id__exact': self.obj_id,
54-            'content_type__package__label__exact': self.package,
55-            'content_type__python_module_name__exact': self.module,
56+            'content_type__app_label__exact': self.package,
57+            'content_type__model__exact': self.module,
58             'site__id__exact': settings.SITE_ID,
59-            'select_related': True,
60-            'order_by': (self.ordering + 'submit_date',),
61         }
62         kwargs.update(self.extra_kwargs)
63         if not self.free and settings.COMMENTS_BANNED_USERS_GROUP:
64-            kwargs['select'] = {'is_hidden': 'user_id IN (SELECT user_id FROM auth_users_groups WHERE group_id = %s)' % settings.COMMENTS_BANNED_USERS_GROUP}
65-        comment_list = get_list_function(**kwargs)
66+            kwargs['select'] = {'is_hidden': 'user_id IN (SELECT user_id FROM auth_user_groups WHERE group_id = %s)' % settings.COMMENTS_BANNED_USERS_GROUP}
67+        comment_list = get_list_function(**kwargs).order_by(self.ordering + 'submit_date').select_related()
68 
69         if not self.free:
70             if context.has_key('user') and not context['user'].is_anonymous():
71@@ -157,7 +155,7 @@
72         except ValueError: # unpack list of wrong size
73             raise template.TemplateSyntaxError, "Third argument in %r tag must be in the format 'package.module'" % tokens[0]
74         try:
75-            content_type = ContentType.objects.get_object(package__label__exact=package, python_module_name__exact=module)
76+            content_type = ContentType.objects.get(app_label__exact=package, model__exact=module)
77         except ContentType.DoesNotExist:
78             raise template.TemplateSyntaxError, "%r tag has invalid content-type '%s.%s'" % (tokens[0], package, module)
79         obj_id_lookup_var, obj_id = None, None
80@@ -237,7 +235,7 @@
81         except ValueError: # unpack list of wrong size
82             raise template.TemplateSyntaxError, "Third argument in %r tag must be in the format 'package.module'" % tokens[0]
83         try:
84-            content_type = ContentType.objects.get_object(package__label__exact=package, python_module_name__exact=module)
85+            content_type = ContentType.objects.get(app_label__exact=package, model__exact=module)
86         except ContentType.DoesNotExist:
87             raise template.TemplateSyntaxError, "%r tag has invalid content-type '%s.%s'" % (tokens[0], package, module)
88         var_name, obj_id = None, None
89@@ -292,7 +290,7 @@
90         except ValueError: # unpack list of wrong size
91             raise template.TemplateSyntaxError, "Third argument in %r tag must be in the format 'package.module'" % tokens[0]
92         try:
93-            content_type = ContentType.objects.get_object(package__label__exact=package, python_module_name__exact=module)
94+            content_type = ContentType.objects.get(app_label__exact=package,model__exact=module)
95         except ContentType.DoesNotExist:
96             raise template.TemplateSyntaxError, "%r tag has invalid content-type '%s.%s'" % (tokens[0], package, module)
97         var_name, obj_id = None, None
98Index: django/contrib/comments/models.py
99===================================================================
100--- django/contrib/comments/models.py   (revision 2721)
101+++ django/contrib/comments/models.py   (working copy)
102@@ -48,9 +48,9 @@
103         _karma_total_good and _karma_total_bad filled.
104         """
105         kwargs.setdefault('select', {})
106-        kwargs['select']['_karma_total_good'] = 'SELECT COUNT(*) FROM comments_karma WHERE comments_karma.comment_id=comments.id AND score=1'
107-        kwargs['select']['_karma_total_bad'] = 'SELECT COUNT(*) FROM comments_karma WHERE comments_karma.comment_id=comments.id AND score=-1'
108-        return self.get_list(**kwargs)
109+        kwargs['select']['_karma_total_good'] = 'SELECT COUNT(*) FROM comments_karmascore WHERE comments_karmascore.comment_id=comments.id AND score=1'
110+        kwargs['select']['_karma_total_bad'] = 'SELECT COUNT(*) FROM comments_karmascore WHERE comments_karmascore.comment_id=comments.id AND score=-1'
111+        return self.filter(**kwargs)
112 
113     def user_is_moderator(self, user):
114         if user.is_superuser:
115@@ -102,7 +102,7 @@
116         search_fields = ('comment', 'user__username')
117 
118     def __repr__(self):
119-        return "%s: %s..." % (self.get_user().username, self.comment[:100])
120+        return "%s: %s..." % (self.user.username, self.comment[:100])
121 
122     def get_absolute_url(self):
123         return self.get_content_object().get_absolute_url() + "#c" + str(self.id)
124@@ -156,7 +156,7 @@
125 
126     def get_as_text(self):
127         return _('Posted by %(user)s at %(date)s\n\n%(comment)s\n\nhttp://%(domain)s%(url)s') % \
128-            {'user': self.get_user().username, 'date': self.submit_date,
129+            {'user': self.user.username, 'date': self.submit_date,
130             'comment': self.comment, 'domain': self.get_site().domain, 'url': self.get_absolute_url()}
131 
132 class FreeComment(models.Model):
133@@ -208,7 +208,7 @@
134 class KarmaScoreManager(models.Manager):
135     def vote(self, user_id, comment_id, score):
136         try:
137-            karma = self.get_object(comment__id__exact=comment_id, user__id__exact=user_id)
138+            karma = self.objects.get(comment__id__exact=comment_id, user__id__exact=user_id)
139         except self.model.DoesNotExist:
140             karma = self.model(None, user_id, comment_id, score, datetime.datetime.now())
141             karma.save()
142@@ -238,7 +238,7 @@
143         unique_together = (('user', 'comment'),)
144 
145     def __repr__(self):
146-        return _("%(score)d rating by %(user)s") % {'score': self.score, 'user': self.get_user()}
147+        return _("%(score)d rating by %(user)s") % {'score': self.score, 'user': self.user}
148 
149 class UserFlagManager(models.Manager):
150     def flag(self, comment, user):
151@@ -250,7 +250,7 @@
152         if int(comment.user_id) == int(user.id):
153             return # A user can't flag his own comment. Fail silently.
154         try:
155-            f = self.get_object(user__id__exact=user.id, comment__id__exact=comment.id)
156+            f = self.objects.get(user__id__exact=user.id, comment__id__exact=comment.id)
157         except self.model.DoesNotExist:
158             from django.core.mail import mail_managers
159             f = self.model(None, user.id, comment.id, None)
160@@ -269,7 +269,7 @@
161         unique_together = (('user', 'comment'),)
162 
163     def __repr__(self):
164-        return _("Flag by %r") % self.get_user()
165+        return _("Flag by %r") % self.user
166 
167 class ModeratorDeletion(models.Model):
168     user = models.ForeignKey(User, verbose_name='moderator')
169@@ -281,4 +281,4 @@
170         unique_together = (('user', 'comment'),)
171 
172     def __repr__(self):
173-        return _("Moderator deletion by %r") % self.get_user()
174+        return _("Moderator deletion by %r") % self.user
175Index: django/contrib/comments/views/karma.py
176===================================================================
177--- django/contrib/comments/views/karma.py      (revision 2721)
178+++ django/contrib/comments/views/karma.py      (working copy)
179@@ -1,7 +1,7 @@
180 from django.http import Http404
181 from django.shortcuts import render_to_response
182 from django.template import RequestContext
183-from django.models.comments import comments, karma
184+from django.contrib.comments.models import Comment, KarmaScore
185 
186 def vote(request, comment_id, vote):
187     """
188@@ -18,12 +18,12 @@
189     if request.user.is_anonymous():
190         raise Http404, _("Anonymous users cannot vote")
191     try:
192-        comment = comments.get_object(pk=comment_id)
193-    except comments.CommentDoesNotExist:
194+        comment = Comment.objects.get(pk=comment_id)
195+    except Comment.DoesNotExist:
196         raise Http404, _("Invalid comment ID")
197     if comment.user_id == request.user.id:
198         raise Http404, _("No voting for yourself")
199-    karma.vote(request.user.id, comment_id, rating)
200+    KarmaScore.objects.vote(request.user.id, comment_id, rating)
201     # Reload comment to ensure we have up to date karma count
202-    comment = comments.get_object(pk=comment_id)
203+    comment = Comment.objects.get(pk=comment_id)
204     return render_to_response('comments/karma_vote_accepted.html', {'comment': comment}, context_instance=RequestContext(request))
205Index: django/contrib/comments/views/userflags.py
206===================================================================
207--- django/contrib/comments/views/userflags.py  (revision 2721)
208+++ django/contrib/comments/views/userflags.py  (working copy)
209@@ -1,7 +1,7 @@
210-from django.shortcuts import render_to_response
211+from django.shortcuts import render_to_response, get_object_or_404
212 from django.template import RequestContext
213 from django.http import Http404
214-from django.models.comments import comments, moderatordeletions, userflags
215+from django.contrib.comments.models import Comment, ModeratorDeletion, UserFlag
216 from django.contrib.auth.decorators import login_required
217 from django.http import HttpResponseRedirect
218 from django.conf import settings
219@@ -15,21 +15,15 @@
220         comment
221             the flagged `comments.comments` object
222     """
223-    try:
224-        comment = comments.get_object(pk=comment_id, site__id__exact=settings.SITE_ID)
225-    except comments.CommentDoesNotExist:
226-        raise Http404
227+    comment = get_object_or_404(Comment,pk=comment_id, site__id__exact=settings.SITE_ID)
228     if request.POST:
229-        userflags.flag(comment, request.user)
230+        UserFlag.objects.flag(comment, request.user)
231         return HttpResponseRedirect('%sdone/' % request.path)
232     return render_to_response('comments/flag_verify.html', {'comment': comment}, context_instance=RequestContext(request))
233 flag = login_required(flag)
234 
235 def flag_done(request, comment_id):
236-    try:
237-        comment = comments.get_object(pk=comment_id, site__id__exact=settings.SITE_ID)
238-    except comments.CommentDoesNotExist:
239-        raise Http404
240+    comment = get_object_or_404(Comment,pk=comment_id, site__id__exact=settings.SITE_ID)
241     return render_to_response('comments/flag_done.html', {'comment': comment}, context_instance=RequestContext(request))
242 
243 def delete(request, comment_id):
244@@ -41,26 +35,20 @@
245         comment
246             the flagged `comments.comments` object
247     """
248-    try:
249-        comment = comments.get_object(pk=comment_id, site__id__exact=settings.SITE_ID)
250-    except comments.CommentDoesNotExist:
251+    comment = get_object_or_404(Comment,pk=comment_id, site__id__exact=settings.SITE_ID)
252+    if not Comment.objects.user_is_moderator(request.user):
253         raise Http404
254-    if not comments.user_is_moderator(request.user):
255-        raise Http404
256     if request.POST:
257         # If the comment has already been removed, silently fail.
258         if not comment.is_removed:
259             comment.is_removed = True
260             comment.save()
261-            m = moderatordeletions.ModeratorDeletion(None, request.user.id, comment.id, None)
262+            m = ModeratorDeletion(None, request.user.id, comment.id, None)
263             m.save()
264         return HttpResponseRedirect('%sdone/' % request.path)
265     return render_to_response('comments/delete_verify.html', {'comment': comment}, context_instance=RequestContext(request))
266 delete = login_required(delete)
267 
268 def delete_done(request, comment_id):
269-    try:
270-        comment = comments.get_object(pk=comment_id, site__id__exact=settings.SITE_ID)
271-    except comments.CommentDoesNotExist:
272-        raise Http404
273+    comment = get_object_or_404(Comment,pk=comment_id, site__id__exact=settings.SITE_ID)
274     return render_to_response('comments/delete_done.html', {'comment': comment}, context_instance=RequestContext(request))
275Index: django/contrib/comments/views/comments.py
276===================================================================
277--- django/contrib/comments/views/comments.py   (revision 2721)
278+++ django/contrib/comments/views/comments.py   (working copy)
279@@ -90,7 +90,7 @@
280     def save(self, new_data):
281         today = datetime.date.today()
282         c = self.get_comment(new_data)
283-        for old in Comment.objects.get_list(content_type__id__exact=new_data["content_type_id"],
284+        for old in Comment.objects.filter(content_type__id__exact=new_data["content_type_id"],
285             object_id__exact=new_data["object_id"], user__id__exact=self.get_user_id()):
286             # Check that this comment isn't duplicate. (Sometimes people post
287             # comments twice by mistake.) If it is, fail silently by pretending
288@@ -146,7 +146,7 @@
289         # Check that this comment isn't duplicate. (Sometimes people post
290         # comments twice by mistake.) If it is, fail silently by pretending
291         # the comment was posted successfully.
292-        for old_comment in FreeComment.objects.get_list(content_type__id__exact=new_data["content_type_id"],
293+        for old_comment in FreeComment.objects.filter(content_type__id__exact=new_data["content_type_id"],
294             object_id__exact=new_data["object_id"], person_name__exact=new_data["person_name"],
295             submit_date__year=today.year, submit_date__month=today.month,
296             submit_date__day=today.day):
297@@ -202,7 +202,7 @@
298         rating_range, rating_choices = [], []
299     content_type_id, object_id = target.split(':') # target is something like '52:5157'
300     try:
301-        obj = ContentType.objects.get_object(pk=content_type_id).get_object_for_this_type(pk=object_id)
302+        obj = ContentType.objects.get(pk=content_type_id).get_object_for_this_type(pk=object_id)
303     except ObjectDoesNotExist:
304         raise Http404, _("The comment form had an invalid 'target' parameter -- the object ID was invalid")
305     option_list = options.split(',') # options is something like 'pa,ra'
306@@ -285,7 +285,7 @@
307     if Comment.objects.get_security_hash(options, '', '', target) != security_hash:
308         raise Http404, _("Somebody tampered with the comment form (security violation)")
309     content_type_id, object_id = target.split(':') # target is something like '52:5157'
310-    content_type = ContentType.objects.get_object(pk=content_type_id)
311+    content_type = ContentType.objects.get(pk=content_type_id)
312     try:
313         obj = content_type.get_object_for_this_type(pk=object_id)
314     except ObjectDoesNotExist:
315@@ -333,7 +333,7 @@
316     if request.GET.has_key('c'):
317         content_type_id, object_id = request.GET['c'].split(':')
318         try:
319-            content_type = ContentType.objects.get_object(pk=content_type_id)
320+            content_type = ContentType.objects.get(pk=content_type_id)
321             obj = content_type.get_object_for_this_type(pk=object_id)
322         except ObjectDoesNotExist:
323             pass
324Index: django/contrib/comments/feeds.py
325===================================================================
326--- django/contrib/comments/feeds.py    (revision 2721)
327+++ django/contrib/comments/feeds.py    (working copy)
328@@ -25,7 +25,7 @@
329         return "Latest comments on %s" % self._site.name
330 
331     def items(self):
332-        return self.comments_class.objects.get_list(**self._get_lookup_kwargs())
333+        return self.comments_class.objects.filter(**self._get_lookup_kwargs())
334 
335     def _get_lookup_kwargs(self):
336         return {