- Pyramid resource: In plain English
- class Product(mapping.Document):
- item = mapping.TextField()
- name = mapping.TextField()
- sizes = mapping.ListField()
- class Product(mapping.Document):
- __acl__ = [(Allow, AUTHENTICATED, 'view')]
- item = mapping.TextField()
- name = mapping.TextField()
- sizes = mapping.ListField()
- def __getitem__(self, key):
- return <something>
- @view_config(context=Product, permission="view")
- def view_product(context, request):
- pass # would do stuff
- /product/1
- class ProductContainer(object):
- """
- container = ProductContainer()
- container[1]
- >>> <Product(1)>
- """
- def __init__(self, request, name="product", parent=None):
- self.__name__ = name
- self.__parent__ = parent
- self._request = request
- def __getitem__(self, key):
- p = db.get_product(id=key)
- if not p:
- raise KeyError(key)
- else:
- p.__acl__ = [(Allow, Everyone,"view")]
- p.__name__ = key
- p.__parent__ = self
- return p
- config = Configurator()
- config.add_route(name="product",
- path="/product/*traverse",
- factory=ProductContainer)
- config.scan()
- application = config.make_wsgi_app()
- class RootFactory(object):
- def __init__(self, request):
- self._request = request
- self.__acl__ = [(Allow, Everyone, "view")] # todo: add more acls
- @view_config(permission="view", route_name="orders")
- def view_product(context, request):
- order_id, product_id = request.matchdict["order_id"], request.matchdict["product_id"]
- pass # do what you need to with the input, the security check already happened
- config = Configurator(root_factory=RootFactory)
- config.add_route(name="orders",
- path="/order/{order_id}/products/{product_id}")
- config.scan()
- application = config.make_wsgi_app()