librelist archives

« back to archive

Architecture discussion

Architecture discussion

From:
Ivan Kleshnin
Date:
2013-06-08 @ 16:37
Is it possible (or reliable) to have one view working with both backend and
frontend?

Say, i have a "blog" blueprint. I want to register it twice, dealing with
template auto-switching by using custom render_template.

*Flask has no ability to mount Blueprint to Blueprint (which could be
useful).*

When i register it twice (with '/blog' and '/admin/blog'endpoints),
werkzeug machinery enables
*default URL* feature
and immediately redirects from one to another. And i can't even patch
werkzeug.routing.Map cause i still need default redirect for powering
*/blog/page/1* to */blog/* auto-redirects.

*# myapp/blog/__init__.py*
...
@blueprint.route('/page/<int(1):page>')
@blueprint.route('/', defaults={'page': 1})
def index(page):
    ...
    return render_template('*/blog/index.html', **context)

...

*# myapp/__init__.py*
...
app.register_blueprint(package.blueprint, url_prefix='/blog')
app.register_blueprint(package.blueprint, url_prefix='/admin/blog')

So it's not a solution. I literally examined all entries on git-hub, tagged
with "flask" to see how people deal with such issue.
I examined all "skeleton" apps, all "bootstrap" apps an so on...

Looks like no one ever tried to create both "front-" and "back-" ended
solution *with* ability to inherit templates from each "zone"
and *without* view duplication. They all just inherit from one single
base.html (bounding to same design for site and admin zone).

It simply cannot be true. Am I wrong in the root of this approach and we
should always duplicate views and templates for "backend" and "frontend"
for some reason?

Re: [flask] Architecture discussion

From:
Simon Zimmermann
Date:
2013-06-10 @ 06:26
On Sat, Jun 8, 2013 at 6:37 PM, Ivan Kleshnin <ivan.kleshnin@gmail.com> wrote:
> Is it possible (or reliable) to have one view working with both backend and
> frontend?
>
> Say, i have a "blog" blueprint. I want to register it twice, dealing with
> template auto-switching by using custom render_template.
>
> Flask has no ability to mount Blueprint to Blueprint (which could be
> useful).
>
> When i register it twice (with '/blog' and '/admin/blog' endpoints),
> werkzeug machinery enables default URL feature
> and immediately redirects from one to another. And i can't even patch
> werkzeug.routing.Map cause i still need default redirect for powering
> /blog/page/1 to /blog/ auto-redirects.
>
> # myapp/blog/__init__.py
> ...
> @blueprint.route('/page/<int(1):page>')
> @blueprint.route('/', defaults={'page': 1})
> def index(page):
>     ...
>     return render_template('*/blog/index.html', **context)

@blueprint.route('/page/<int(1):page>')
@blueprint.route('/')
def index(page=1):
    ...
    return render_template('*/blog/index.html', **context)

The above snipet would fix your issue. Haven't looked at the
"defaults" parameter, but it seems to do the behavior you described.