#15791: New feature: callable objects can signal that templates should never 
call
them
-------------------------------------+-------------------------------------
               Reporter:  ejucovy    |        Owner:  nobody
                   Type:  New        |       Status:  new
  feature                            |    Component:  Template system
              Milestone:             |     Severity:  Normal
                Version:  SVN        |     Keywords:
             Resolution:             |    Has patch:  1
           Triage Stage:  Ready for  |  Needs tests:  0
  checkin                            |
    Needs documentation:  0          |
Patch needs improvement:  0          |
-------------------------------------+-------------------------------------
Changes (by lukeplant):

 * stage:  Unreviewed => Ready for checkin


Comment:

 It's unfortunate that we need this, as it seems a bit hacky.

 However, it is a genuine problem. An alternative solution is to convert
 callable objects to non-callable by some magic, so that the template
 system does not need to be altered. But that seems just as bad, and will
 probably have more gotchas.

 Your solution follows the precedent of `alters_data`, while being
 orthogonal to it (which I didn't realise at first), in that it allows
 attributes of the callable to be accessed, while `alters_data` does not.

 The 'nocall' filter or template tag would be much more work, and harder to
 implement, and I don't think are practical. For large batches of callable
 objects retrieved by some third-party library, I think a good enough
 workaround is to wrap the data in a simple generator that adds the
 'do_not_call_in_templates' attribute to instances where needed:

 {{{
 #!python

 def mark_not_callable(objects):
     for obj in objects:
         obj.do_not_call_in_templates = True
         # or possibly:
         obj.some_sub_object.do_not_call_in_templates = True
         yield obj

 def view(request):
     # ...
     context['objects'] = mark_not_callable(some_objects)
 }}}

 `'do_not_call_in_templates'` seems a bit verbose, but it's explicit and
 it's your bike-shed, so we'll leave that.

 Thank you for the tests for alters_data. There are tests for other
 callables in class 'Templates' in regressiontests/templates/tests.py, but
 not for alters_data that I can find, and the nature of the tests means it
 is good to explicitly test that the methods are not called, as you have
 done. The simple `test_callable` test you have added can be left, because
 it uses a class with a `__call__` method, while the existing test uses a
 lambda, and its conceivable that having both types of callable tested
 could be useful.

 For future reference, I found one tiny nit, which I'll fix when I commit -
 our coding style for templates is to do `{{ variable }}` and not
 `{{variable}}`.

 So, for all these reasons, marking accepted, and in fact, ready for
 checkin. Thank you very much!

-- 
Ticket URL: <http://code.djangoproject.com/ticket/15791#comment:3>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to