my routes.py

4 views
Skip to first unread message

Sujan Shakya

unread,
Jan 6, 2010, 10:32:51 PM1/6/10
to web2py-users
I want to share my routes.py file. The case is that I want only one
application to be hosted on my site and I want to keep my urls clean.
So instead of having url http://crm.appspot.com/crm/default/list_persons,
I want to have url like http://crm.appspot.com/list_persons. In order
to achieve this behavior, I wrote the routes.py as:

# routes.py
import os, re

default_app = 'crm'

routes_in = []

regex = re.compile('^\w+$')
for app in os.listdir('applications'):
if regex.match(app):
routes_in.append(('/%s' % app, '/%s/default' % app))
routes_in.append(('/%s/(?P<path>.*)' % app, '/%s/\g<path>' %
app))

routes_out = (
('/%s/static/(?P<path>.*)' % default_app, '/static/\g<path>'),
('/%s/default/(?P<path>.*)' % default_app, '/\g<path>'),
)

routes_in.extend(
[(y.replace('\g<path>', '(?P<path>.*)'), x.replace('(?P<path>.*)',
'\g<path>')
) for (x, y) in routes_out]
)

Its working very good for me. And I can browse the list_companies page
with both urls http://crm.appspot.com/crm/default/list_companies and
http://crm.appspot.com/list_companies.
Does it have any performance overhead? Else why the routes.py is not
preferred?

mdipierro

unread,
Jan 6, 2010, 10:57:46 PM1/6/10
to web2py-users
No reason. No overhead

On Jan 6, 9:32 pm, Sujan Shakya <suzan.sha...@gmail.com> wrote:
> I want to share my routes.py file. The case is that I want only one
> application to be hosted on my site and I want to keep my urls clean.

> So instead of having urlhttp://crm.appspot.com/crm/default/list_persons,
> I want to have url likehttp://crm.appspot.com/list_persons. In order


> to achieve this behavior, I wrote the routes.py as:
>
> # routes.py
> import os, re
>
> default_app = 'crm'
>
> routes_in = []
>
> regex = re.compile('^\w+$')
> for app in os.listdir('applications'):
>     if regex.match(app):
>         routes_in.append(('/%s' % app, '/%s/default' % app))
>         routes_in.append(('/%s/(?P<path>.*)' % app, '/%s/\g<path>' %
> app))
>
> routes_out = (
>     ('/%s/static/(?P<path>.*)' % default_app, '/static/\g<path>'),
>     ('/%s/default/(?P<path>.*)' % default_app, '/\g<path>'),
> )
>
> routes_in.extend(
>     [(y.replace('\g<path>', '(?P<path>.*)'), x.replace('(?P<path>.*)',
> '\g<path>')
>     ) for (x, y) in routes_out]
> )
>
> Its working very good for me. And I can browse the list_companies page

> with both urlshttp://crm.appspot.com/crm/default/list_companiesandhttp://crm.appspot.com/list_companies.

Sujan Shakya

unread,
Jan 7, 2010, 12:33:01 AM1/7/10
to web2py-users
Thanks.
Reply all
Reply to author
Forward
0 new messages