durusmail: qp: Re: qpy development ideas
qpy development ideas
2007-10-08
2007-10-09
Re: qpy development ideas
2007-10-10
2007-10-10
2007-10-14
2007-10-15
David K. Hess (2 parts)
2007-10-09
2007-10-09
2007-10-10
2007-10-10
2007-10-10
2007-10-10
2007-10-10
Re: qpy development ideas
dhess@fishsoftware.com
2007-10-18
> I've since seen David Hess's follow-on message about using
> decorators to encapsulate [html] with ensure_signed_in / header /
> footer - I can see some merit in that - one line of code instead of
> three. Mind you, the slightly briefer:
>
> @page
> def some_page [html] ():
>     o = get_some_obj()
>     ' stuff '
>     # ... code ...
>     ' yet more stuff '
>
> Doesn't deal with a common usage:
>
> def some_page [html] ():
>     o = get_some_obj()
>     header(title='%s : %s' % (
>         o.get_name(),
>         get_publisher().format_date(o.get_stamp())))
>     ' stuff '
>     # ... code ...
>     ' yet more stuff '
>     footer()

Excellent use case! Here's what I'd do:

def page(title="Default Title"):
    def decorator(content_generator):
        def full_page [html] (*args, **kwargs):
            ensure_signed_in()
            header(title=get_hit().get_info().get("title", title))
            content_generator(*args, **kwargs)
            footer()
        return full_page
    return decorator

@page()
def some_page [html] ():
    o = get_some_obj()
    get_hit().get_info()["title"] = '%s : %s' % (
        o.get_name(),
        get_publisher().format_date(o.get_stamp()))
    ' stuff '
    # ... code ...
    ' yet more stuff '

As an aside, my motivation is not briefness. I would like to be able to
more easily change the page structure of individual sections of my
application by switching out header and footer calls in just one place
without touching the "content_generator".

Additionally, I end up with greater confidence in the security of my
application since ensure_signed_in() is applied in just one place rather
than many.

Dave

reply