http://bitbucket.org/_amol_/tgext.mobilemiddleware
It is indeed quite simple but as I found it makes easier to manage
mobile devices I'm posting it here :D
The idea is very nice, but the implementation seems buggy. I have fixed a few bugs that related to people using Decoration the way you do, and it's unfortunately wrong to do it that way. IMHO a default request/thread-local way of using it would be great. By that I mean that once it's "finalized", all alterations should be scoped to the thread an request going on. But it's not that way right now, so...
The problem is that you modify the Decoration. But that can lead to race-conditions in multi-threaded environments.
There is a thread-safe implementation that I fixed, which is called "override_template". It's inside the decorators package. If you'd manage to make the implementation based on that, it should be ok. If not as such, take a look, and implement your approach accordingly.
Additionally, I think the implementation could benefit from a even more flexible approach, by making the template-decision based on a callable. I agree that just two templates are probably the right thing for 95% of all cases, but maybe passing a callable, which then would determine the template based on the user-agent would be a good idea.
Last but not least - no tests!
I hope you don't mind the constructive criticism.
Diez
Thank you Diez for pointing that out,
actually I wrote the code in about 1 hour before going to sleep and I
went to sleep with the strange feeling that it might have that problem
due to use_custom_format saving the format inside the controller
itself, but didn't have time/wanted to check it as I was tired :D
Also use_custom_format, due to the fact that it changes the decorated
function, switches the template permanently, which is not what I
suppose it should be expected to do.
I think that changing it to save the wanted custom format inside the
request instead of saving it inside the decorated function would be
preferred and solve both the issues.
Documentation suggests to call use_custom_format to switch between
custom formats, but doing that has indeed a set of problems from
switching the template forever to causing race conditions.
As you suggested I'm going to reimplement right now the expose_mobile
decorator by using override_template which avoids those problems due
to the fact that it actually saves the overridden template inside the
request itself. I didn't use it at the first time as custom_formats
are more comfortable as you register a short name to switch between
them.
Just commited it as
https://bitbucket.org/_amol_/tgext.mobilemiddleware/changeset/6cde17466cf4
and released new version on pypi.
If you want to suggest any other improvement feel free to fork on
bitbucket, I'll pull them back.
Next steps for me will be to implement correct support for multiple
expose_mobile and a set of test units to avoid breaking code in the
future.
Can you provide me how you installed it and on which version of python?
On Mon, Sep 5, 2011 at 3:58 PM, MattRock <matt.r...@gmail.com> wrote:
> Standard installation of tgext.middleware as per the documentation is
> resulting in a "name 'expose_mobile' is not defined" error.
>
> --
> You received this message because you are subscribed to the Google Groups
> "TurboGears" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/turbogears/-/im2ZfJTaXZgJ.
> To post to this group, send email to turbo...@googlegroups.com.
> To unsubscribe from this group, send email to
> turbogears+...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/turbogears?hl=en.
>