App / Subapp Organization

112 views
Skip to first unread message

Tony

unread,
Oct 12, 2011, 11:07:39 AM10/12/11
to web.py
Hello,

I'm trying to write a REST-style backend using webpy and wondered if
anyone had suggestions or examples. The app deals with data sets in
the 10k to 100k range of similar items, with the need to select out
views of the data with different groupings and orders based on the
properties of the items and return JSON for client side parsing. So
what I wanted to do was something like this:

* /{container-uuid}/ (general listing of items)
* /{container-uuid}/attribute (listing of items with the attribute)
* /{container-uuid}/attribute/value (listing of items with the
specific attribute value)
* /{container-uuid}/{item-id} (summary listing of single item)
* /{container-uuid}/{item-id}/detailed (detailed listing of a
single item)

I wanted to use the GET parameters for iterating through the result
sets (using jqGrid for the HTML view) to handle skip, offset, sort
order, etc. When I started to try doing this I ran into an
inconsistency in the subapp regex handling, basically you can't use
regex in subapps:

https://github.com/webpy/webpy/issues/12

Trying to keep everything in one giant python script makes the code
difficult to read and it will take some careful planning to break
things out into libraries or functions. Either that or I'll have to
put all the parameters into GET/POST/DELETE variables which is not
what I want to do either. Has anyone got a good template or example
for doing this kind of thing?

Tony

Ben Corneau

unread,
Oct 12, 2011, 10:10:25 PM10/12/11
to we...@googlegroups.com
You can keep the url map in one single file but still define the classes in separate files.  You could probably even push the mapping for those urls into the 'subapp' and append it's urls to the main url.  You would still need the full url, unlike a true subapp, and you would need to be careful with the append because order matters.

I agree it would be nice to be able to use a regex to match on a url and pass it to a subapp.

code.py
----------------------------------------
import web
import container

urls = (
"/containers/(\w+)/",                 container.FullList,
"/containers/(\w+)/attribute/" ,      container.Attributes,
"/containers/(\w+)/attribute/value/", container.AttributeValue,
"/containers/(\w+)/items/(\w+)/",     container.Item,
"/main", "Main",
)

app = web.application(urls, globals())

class Main:
    def GET(self):
        return 'Main App page'

if __name__ == "__main__":
    app.run()

container.py
------------------
class FullList:
    def GET(self, container_id):
        return "Full list. container:%s"%(container_id)
class Attributes:
    def GET(self, container_id):
        return "Attributes. container:%s"%(container_id)
class AttributeValue:
    def GET(self, container_id):
        return "AttributeValue. container:%s"%(container_id)
class Item:
    def GET(self, container_id, item_id):
        return "Item. container:%s item:%s"%(container_id, item_id)






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


andrei

unread,
Oct 13, 2011, 4:17:25 AM10/13/11
to web.py
I use web.auto_application() so controller classes define their path
themselves

# app.py

app = web.auto_application()

# controller/__init__.py

import sys
sys.path.append("..")

__all__ = [
"auth",
"application",
"documents",
"blocks",
"pages",
]

# controller/auth.py

from app import app

class Users(app.page):

path="/a/users/?"

@auth.restrict(role="admin")
def GET(self):
users = db.select("users", where="NOT is_deleted", order="id
DESC")
user_form = userForm()
return render.auth.list(users, user_form)

# code.py

import os
import sys

sys.path.append(os.path.abspath(os.path.dirname(__file__)))

from app import app
from controller import *

if __name__ == "__main__":
app.run()
else:
application = app.wsgifunc()

Tony

unread,
Oct 17, 2011, 11:18:30 AM10/17/11
to web.py
On Oct 12, 10:10 pm, Ben Corneau <bencorn...@gmail.com> wrote:
> You can keep the url map in one single file but still define the classes
> in separate files.  You could probably even push the mapping for those urls
> into the 'subapp' and append it's urls to the main url.  You would still
> need the full url, unlike a true subapp, and you would need to be careful
> with the append because order matters.

That is a great idea! Thanks so much for the time to share it.

Tony

Tony

unread,
Oct 17, 2011, 11:20:11 AM10/17/11
to web.py
On Oct 13, 4:17 am, andrei <andre...@gmail.com> wrote:
> I use web.auto_application() so controller classes define their path
> themselves

That is a very clever approach to take. Thanks for sharing it! Between
you and Ben, I may be able to accomplish my original design now
without the drawbacks I mentioned.

Tony
Reply all
Reply to author
Forward
0 new messages