Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 24th, 2012  |  syntax: None  |  size: 1.92 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Pyramid resource: In plain English
  2. class Product(mapping.Document):
  3.   item = mapping.TextField()
  4.   name = mapping.TextField()
  5.   sizes = mapping.ListField()
  6.        
  7. class Product(mapping.Document):
  8.   __acl__ = [(Allow, AUTHENTICATED, 'view')]
  9.   item = mapping.TextField()
  10.   name = mapping.TextField()
  11.   sizes = mapping.ListField()
  12.  
  13.   def __getitem__(self, key):
  14.       return <something>
  15.        
  16. @view_config(context=Product, permission="view")
  17.   def view_product(context, request):
  18.       pass # would do stuff
  19.        
  20. /product/1
  21.        
  22. class ProductContainer(object):
  23.       """
  24.       container = ProductContainer()
  25.       container[1]
  26.       >>> <Product(1)>
  27.       """
  28.       def __init__(self, request, name="product", parent=None):
  29.           self.__name__ = name
  30.           self.__parent__ = parent
  31.           self._request = request
  32.  
  33.       def __getitem__(self, key):
  34.           p = db.get_product(id=key)
  35.  
  36.           if not p:
  37.               raise KeyError(key)
  38.           else:
  39.               p.__acl__ = [(Allow, Everyone,"view")]
  40.               p.__name__ = key
  41.               p.__parent__ = self
  42.               return p
  43.        
  44. config = Configurator()
  45.   config.add_route(name="product",
  46.                    path="/product/*traverse",
  47.                    factory=ProductContainer)
  48.   config.scan()
  49.   application = config.make_wsgi_app()
  50.        
  51. class RootFactory(object):
  52.       def __init__(self, request):
  53.           self._request = request
  54.           self.__acl__ = [(Allow, Everyone, "view")]  # todo: add more acls
  55.  
  56.  
  57.   @view_config(permission="view", route_name="orders")
  58.   def view_product(context, request):
  59.       order_id, product_id = request.matchdict["order_id"], request.matchdict["product_id"]
  60.       pass # do what you need to with the input, the security check already happened
  61.  
  62.   config = Configurator(root_factory=RootFactory)
  63.  
  64.   config.add_route(name="orders",
  65.                    path="/order/{order_id}/products/{product_id}")
  66.  
  67.   config.scan()
  68.   application = config.make_wsgi_app()