Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Refactored comment templatetags to remove redundant 'tag_name' parame…

…ter. Also added an 'extra_kwargs' hook to CommentListNode

git-svn-id: http://code.djangoproject.com/svn/django/trunk@578 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 5569c75eb632d104e9b86a785752edb44cada85a 1 parent e1f0acc
Adrian Holovaty authored
80  django/contrib/comments/templatetags/comments.py
@@ -128,11 +128,12 @@ def render(self, context):
128 128
         return ''
129 129
 
130 130
 class CommentListNode(template.Node):
131  
-    def __init__(self, package, module, context_var_name, obj_id, var_name, free, ordering):
  131
+    def __init__(self, package, module, context_var_name, obj_id, var_name, free, ordering, extra_kwargs=None):
132 132
         self.package, self.module = package, module
133 133
         self.context_var_name, self.obj_id = context_var_name, obj_id
134 134
         self.var_name, self.free = var_name, free
135 135
         self.ordering = ordering
  136
+        self.extra_kwargs = extra_kwargs or {}
136 137
 
137 138
     def render(self, context):
138 139
         from django.conf.settings import COMMENTS_BANNED_USERS_GROUP, SITE_ID
@@ -150,6 +151,7 @@ def render(self, context):
150 151
             'select_related': True,
151 152
             'order_by': (self.ordering + 'submit_date',),
152 153
         }
  154
+        kwargs.update(self.extra_kwargs)
153 155
         if not self.free and COMMENTS_BANNED_USERS_GROUP:
154 156
             kwargs['select'] = {'is_hidden': 'user_id IN (SELECT user_id FROM auth_users_groups WHERE group_id = %s)' % COMMENTS_BANNED_USERS_GROUP}
155 157
         comment_list = get_list_function(**kwargs)
@@ -182,64 +184,64 @@ class DoCommentForm:
182 184
 
183 185
     ``[context_var_containing_obj_id]`` can be a hard-coded integer or a variable containing the ID.
184 186
     """
185  
-    def __init__(self, free, tag_name):
186  
-        self.free, self.tag_name = free, tag_name
  187
+    def __init__(self, free):
  188
+        self.free = free
187 189
 
188 190
     def __call__(self, parser, token):
189 191
         tokens = token.contents.split()
190 192
         if len(tokens) < 4:
191  
-            raise template.TemplateSyntaxError, "'%s' tag requires at least 3 arguments" % self.tag_name
  193
+            raise template.TemplateSyntaxError, "%r tag requires at least 3 arguments" % tokens[0]
192 194
         if tokens[1] != 'for':
193  
-            raise template.TemplateSyntaxError, "Second argument in '%s' tag must be 'for'" % self.tag_name
  195
+            raise template.TemplateSyntaxError, "Second argument in %r tag must be 'for'" % tokens[0]
194 196
         try:
195 197
             package, module = tokens[2].split('.')
196 198
         except ValueError: # unpack list of wrong size
197  
-            raise template.TemplateSyntaxError, "Third argument in '%s' tag must be in the format 'package.module'" % self.tag_name
  199
+            raise template.TemplateSyntaxError, "Third argument in %r tag must be in the format 'package.module'" % tokens[0]
198 200
         try:
199 201
             content_type = contenttypes.get_object(package__label__exact=package, python_module_name__exact=module)
200 202
         except contenttypes.ContentTypeDoesNotExist:
201  
-            raise template.TemplateSyntaxError, "'%s' tag has invalid content-type '%s.%s'" % (self.tag_name, package, module)
  203
+            raise template.TemplateSyntaxError, "%r tag has invalid content-type '%s.%s'" % (tokens[0], package, module)
202 204
         obj_id_lookup_var, obj_id = None, None
203 205
         if tokens[3].isdigit():
204 206
             obj_id = tokens[3]
205 207
             try: # ensure the object ID is valid
206 208
                 content_type.get_object_for_this_type(pk=obj_id)
207 209
             except ObjectDoesNotExist:
208  
-                raise template.TemplateSyntaxError, "'%s' tag refers to %s object with ID %s, which doesn't exist" % (self.tag_name, content_type.name, obj_id)
  210
+                raise template.TemplateSyntaxError, "%r tag refers to %s object with ID %s, which doesn't exist" % (tokens[0], content_type.name, obj_id)
209 211
         else:
210 212
             obj_id_lookup_var = tokens[3]
211 213
         kwargs = {}
212 214
         if len(tokens) > 4:
213 215
             if tokens[4] != 'with':
214  
-                raise template.TemplateSyntaxError, "Fourth argument in '%s' tag must be 'with'" % self.tag_name
  216
+                raise template.TemplateSyntaxError, "Fourth argument in %r tag must be 'with'" % tokens[0]
215 217
             for option, args in zip(tokens[5::2], tokens[6::2]):
216 218
                 if option in ('photos_optional', 'photos_required') and not self.free:
217 219
                     # VALIDATION ##############################################
218 220
                     option_list = args.split(',')
219 221
                     if len(option_list) % 3 != 0:
220  
-                        raise template.TemplateSyntaxError, "Incorrect number of comma-separated arguments to '%s' tag" % self.tag_name
  222
+                        raise template.TemplateSyntaxError, "Incorrect number of comma-separated arguments to %r tag" % tokens[0]
221 223
                     for opt in option_list[::3]:
222 224
                         if not opt.isalnum():
223  
-                            raise template.TemplateSyntaxError, "Invalid photo directory name in '%s' tag: '%s'" % (self.tag_name, opt)
  225
+                            raise template.TemplateSyntaxError, "Invalid photo directory name in %r tag: '%s'" % (tokens[0], opt)
224 226
                     for opt in option_list[1::3] + option_list[2::3]:
225 227
                         if not opt.isdigit() or not (comments.MIN_PHOTO_DIMENSION <= int(opt) <= comments.MAX_PHOTO_DIMENSION):
226  
-                            raise template.TemplateSyntaxError, "Invalid photo dimension in '%s' tag: '%s'. Only values between %s and %s are allowed." % (self.tag_name, opt, comments.MIN_PHOTO_DIMENSION, comments.MAX_PHOTO_DIMENSION)
  228
+                            raise template.TemplateSyntaxError, "Invalid photo dimension in %r tag: '%s'. Only values between %s and %s are allowed." % (tokens[0], opt, comments.MIN_PHOTO_DIMENSION, comments.MAX_PHOTO_DIMENSION)
227 229
                     # VALIDATION ENDS #########################################
228 230
                     kwargs[option] = True
229 231
                     kwargs['photo_options'] = args
230 232
                 elif option in ('ratings_optional', 'ratings_required') and not self.free:
231 233
                     # VALIDATION ##############################################
232 234
                     if 2 < len(args.split('|')) > 9:
233  
-                        raise template.TemplateSyntaxError, "Incorrect number of '%s' options in '%s' tag. Use between 2 and 8." % (option, self.tag_name)
  235
+                        raise template.TemplateSyntaxError, "Incorrect number of '%s' options in %r tag. Use between 2 and 8." % (option, tokens[0])
234 236
                     if re.match('^scale:\d+\-\d+\:$', args.split('|')[0]):
235  
-                        raise template.TemplateSyntaxError, "Invalid 'scale' in '%s' tag's '%s' options" % (self.tag_name, option)
  237
+                        raise template.TemplateSyntaxError, "Invalid 'scale' in %r tag's '%s' options" % (tokens[0], option)
236 238
                     # VALIDATION ENDS #########################################
237 239
                     kwargs[option] = True
238 240
                     kwargs['rating_options'] = args
239 241
                 elif option in ('is_public'):
240 242
                     kwargs[option] = (args == 'true')
241 243
                 else:
242  
-                    raise template.TemplateSyntaxError, "'%s' tag got invalid parameter '%s'" % (self.tag_name, option)
  244
+                    raise template.TemplateSyntaxError, "%r tag got invalid parameter '%s'" % (tokens[0], option)
243 245
         return CommentFormNode(content_type, obj_id_lookup_var, obj_id, self.free, **kwargs)
244 246
 
245 247
 class DoCommentCount:
@@ -260,36 +262,36 @@ class DoCommentCount:
260 262
 
261 263
         {% get_comment_count for lcom.eventtimes 23 as comment_count %}
262 264
     """
263  
-    def __init__(self, free, tag_name):
264  
-        self.free, self.tag_name = free, tag_name
  265
+    def __init__(self, free):
  266
+        self.free = free
265 267
 
266 268
     def __call__(self, parser, token):
267 269
         tokens = token.contents.split()
268 270
         # Now tokens is a list like this:
269 271
         # ['get_comment_list', 'for', 'lcom.eventtimes', 'event.id', 'as', 'comment_list']
270 272
         if len(tokens) != 6:
271  
-            raise template.TemplateSyntaxError, "%s block tag requires 5 arguments" % self.tag_name
  273
+            raise template.TemplateSyntaxError, "%r tag requires 5 arguments" % tokens[0]
272 274
         if tokens[1] != 'for':
273  
-            raise template.TemplateSyntaxError, "Second argument in '%s' tag must be 'for'" % self.tag_name
  275
+            raise template.TemplateSyntaxError, "Second argument in %r tag must be 'for'" % tokens[0]
274 276
         try:
275 277
             package, module = tokens[2].split('.')
276 278
         except ValueError: # unpack list of wrong size
277  
-            raise template.TemplateSyntaxError, "Third argument in '%s' tag must be in the format 'package.module'" % self.tag_name
  279
+            raise template.TemplateSyntaxError, "Third argument in %r tag must be in the format 'package.module'" % tokens[0]
278 280
         try:
279 281
             content_type = contenttypes.get_object(package__label__exact=package, python_module_name__exact=module)
280 282
         except contenttypes.ContentTypeDoesNotExist:
281  
-            raise template.TemplateSyntaxError, "'%s' tag has invalid content-type '%s.%s'" % (self.tag_name, package, module)
  283
+            raise template.TemplateSyntaxError, "%r tag has invalid content-type '%s.%s'" % (tokens[0], package, module)
282 284
         var_name, obj_id = None, None
283 285
         if tokens[3].isdigit():
284 286
             obj_id = tokens[3]
285 287
             try: # ensure the object ID is valid
286 288
                 content_type.get_object_for_this_type(pk=obj_id)
287 289
             except ObjectDoesNotExist:
288  
-                raise template.TemplateSyntaxError, "'%s' tag refers to %s object with ID %s, which doesn't exist" % (self.tag_name, content_type.name, obj_id)
  290
+                raise template.TemplateSyntaxError, "%r tag refers to %s object with ID %s, which doesn't exist" % (tokens[0], content_type.name, obj_id)
289 291
         else:
290 292
             var_name = tokens[3]
291 293
         if tokens[4] != 'as':
292  
-            raise template.TemplateSyntaxError, "Fourth argument in '%s' must be 'as'" % self.tag_name
  294
+            raise template.TemplateSyntaxError, "Fourth argument in %r must be 'as'" % tokens[0]
293 295
         return CommentCountNode(package, module, var_name, obj_id, tokens[5], self.free)
294 296
 
295 297
 class DoGetCommentList:
@@ -315,49 +317,49 @@ class DoGetCommentList:
315 317
 
316 318
         {% get_comment_list for lcom.eventtimes event.id as comment_list reversed %}
317 319
     """
318  
-    def __init__(self, free, tag_name):
319  
-        self.free, self.tag_name = free, tag_name
  320
+    def __init__(self, free):
  321
+        self.free = free
320 322
 
321 323
     def __call__(self, parser, token):
322 324
         tokens = token.contents.split()
323 325
         # Now tokens is a list like this:
324 326
         # ['get_comment_list', 'for', 'lcom.eventtimes', 'event.id', 'as', 'comment_list']
325  
-        if not 6 <= len(tokens) <= 7:
326  
-            raise template.TemplateSyntaxError, "%s block tag requires 5 or 6 arguments" % self.tag_name
  327
+        if not len(tokens) in (6, 7):
  328
+            raise template.TemplateSyntaxError, "%r tag requires 5 or 6 arguments" % tokens[0]
327 329
         if tokens[1] != 'for':
328  
-            raise template.TemplateSyntaxError, "Second argument in '%s' tag must be 'for'" % self.tag_name
  330
+            raise template.TemplateSyntaxError, "Second argument in %r tag must be 'for'" % tokens[0]
329 331
         try:
330 332
             package, module = tokens[2].split('.')
331 333
         except ValueError: # unpack list of wrong size
332  
-            raise template.TemplateSyntaxError, "Third argument in '%s' tag must be in the format 'package.module'" % self.tag_name
  334
+            raise template.TemplateSyntaxError, "Third argument in %r tag must be in the format 'package.module'" % tokens[0]
333 335
         try:
334 336
             content_type = contenttypes.get_object(package__label__exact=package, python_module_name__exact=module)
335 337
         except contenttypes.ContentTypeDoesNotExist:
336  
-            raise template.TemplateSyntaxError, "'%s' tag has invalid content-type '%s.%s'" % (self.tag_name, package, module)
  338
+            raise template.TemplateSyntaxError, "%r tag has invalid content-type '%s.%s'" % (tokens[0], package, module)
337 339
         var_name, obj_id = None, None
338 340
         if tokens[3].isdigit():
339 341
             obj_id = tokens[3]
340 342
             try: # ensure the object ID is valid
341 343
                 content_type.get_object_for_this_type(pk=obj_id)
342 344
             except ObjectDoesNotExist:
343  
-                raise template.TemplateSyntaxError, "'%s' tag refers to %s object with ID %s, which doesn't exist" % (self.tag_name, content_type.name, obj_id)
  345
+                raise template.TemplateSyntaxError, "%r tag refers to %s object with ID %s, which doesn't exist" % (tokens[0], content_type.name, obj_id)
344 346
         else:
345 347
             var_name = tokens[3]
346 348
         if tokens[4] != 'as':
347  
-            raise template.TemplateSyntaxError, "Fourth argument in '%s' must be 'as'" % self.tag_name
  349
+            raise template.TemplateSyntaxError, "Fourth argument in %r must be 'as'" % tokens[0]
348 350
         if len(tokens) == 7:
349 351
             if tokens[6] != 'reversed':
350  
-                raise template.TemplateSyntaxError, "Final argument in '%s' must be 'reversed' if given" % self.tag_name
  352
+                raise template.TemplateSyntaxError, "Final argument in %r must be 'reversed' if given" % tokens[0]
351 353
             ordering = "-"
352 354
         else:
353 355
             ordering = ""
354 356
         return CommentListNode(package, module, var_name, obj_id, tokens[5], self.free, ordering)
355 357
 
356 358
 # registration comments
357  
-template.register_tag('get_comment_list',       DoGetCommentList(free=False, tag_name='get_comment_list'))
358  
-template.register_tag('comment_form',           DoCommentForm(free=False, tag_name='comment_form'))
359  
-template.register_tag('get_comment_count',      DoCommentCount(free=False, tag_name='get_comment_count'))
  359
+template.register_tag('get_comment_list', DoGetCommentList(False))
  360
+template.register_tag('comment_form', DoCommentForm(False))
  361
+template.register_tag('get_comment_count', DoCommentCount(False))
360 362
 # free comments
361  
-template.register_tag('get_free_comment_list',  DoGetCommentList(free=True, tag_name='get_free_comment_list'))
362  
-template.register_tag('free_comment_form',      DoCommentForm(free=True, tag_name='free_comment_form'))
363  
-template.register_tag('get_free_comment_count', DoCommentCount(free=True, tag_name='get_free_comment_count'))
  363
+template.register_tag('get_free_comment_list', DoGetCommentList(True))
  364
+template.register_tag('free_comment_form', DoCommentForm(True))
  365
+template.register_tag('get_free_comment_count', DoCommentCount(True))

0 notes on commit 5569c75

Please sign in to comment.
Something went wrong with that request. Please try again.