Ok, moving this code to decoration function really good.
And now do you want to add this template selection logic into third-party
apps you using in this project?

I have no idea.

As i remember this is called monkey-patching

>>> import test
>>> def heh(func):
...  def helper():
...   print "y"
...   func()
...  return helper
...
>>> test.a()
x
>>> test.a = heh(test.a)
>>> test.a()
y
x
>>>

you can remotely replace functions in any views.py with your own. For
example

from third_party_app import views as third_party_views
for attr_name in dir(third_party_views):
    view = getattr(third_party_views, attr_name)
    if callable(view):
        setattr(third_party_views, attr_name, decorator(view))

BUT! Third-party app is using django's get_template and select_template
functions to retrieve a template.
There we have function call with hard-coded template name. And we cannot
change it. We can monkey-patch function in the module but it also will have
no information about HttpRequest...

All I can see here is trying to decorate view with decorator setting global
variable in that views module to current request. Then decorating function
get_template to pick this global variable, use it and hope django is
non-threaded =)

2009/10/13 Nan <ringe...@gmail.com>

>
>
> Well, I'm using a theming system -- each user can choose a theme to
> use, and that choice is attached to their account.  Not just the base
> template but the inner templates for some parts of the site can vary
> from theme to theme, so I want to run something to the following
> effect:
>
> def my_r2r(request, template, context):
>  # template is a path like 'app/foo.html'
>  theme = request.user.get_profile().theme
>  theme_template = theme.adjust_theme_path(template)
>  t = loader.select_template((template, theme_template))
>  return HttpResponse(t.render(RequestContext(request, context)))
>
> A custom template loader would be a really neat solution, but I can't
> see any way to pass it the request in order for it to select the
> template.
>
>
>
>
> On Oct 12, 3:17 pm, Arthur Metasov <meta...@gmail.com> wrote:
> > You should carefully look at the code of django template loaders.
> > What do you mean? Dou you want template paths to be stored in the
> database
> > and dinamically change? Or do you want to use 3rd-party app but change
> > template dirs it is looking for?
> >
> > 2009/10/12 ringemup <ringe...@gmail.com>
> >
> >
> >
> > > I need to find a way to programmatically change the template path for
> > > every view in my project -- a decorator seems like a nice DRY way to
> > > accomplish that.  But what's the best way to apply it to views in
> > > third-party apps?
> >
> > > The only way to accomplish this that I can think of is to declare new
> > > decorated versions of the view functions in my own views.py and
> > > duplicate the urlpatterns into my own urlconf rather than including
> > > the apps' url patterns.  The new patterns will call the decorated
> > > functions.
> >
> > > Is there a cleaner or simpler way to do this?
> >
>

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

Reply via email to