python - Pyramid 资源 : In plain English

标签 python pyramid couchdb-python

我一直在阅读如何对我新创建的 Pyramid 应用程序实现授权(和身份验证)。我不断碰到称为“资源”的概念。我在我的应用程序中使用 python-couchdb 而根本不使用 RDBMS,因此没有 SQLAlchemy。如果我像这样创建一个 Product 对象:

class Product(mapping.Document):
  item = mapping.TextField()
  name = mapping.TextField()
  sizes = mapping.ListField()

谁能告诉我这是否也称为资源?我一直在阅读 Pyramids 的整个文档,但它没有在哪里用简单的英语解释术语资源(也许我只是愚蠢)。如果这是资源,这是否意味着我只是把我的 ACL 东西放在这里:

class Product(mapping.Document):
  __acl__ = [(Allow, AUTHENTICATED, 'view')]
  item = mapping.TextField()
  name = mapping.TextField()
  sizes = mapping.ListField()

  def __getitem__(self, key):
      return <something>

如果我也使用 Traversal,这是否意味着我在我的 python-couchdb 产品类/资源中添加了 getitem 函数?

抱歉,所有新术语真的很困惑(我来自 Pylons 0.9.7)。

提前致谢。

最佳答案

我认为您缺少的部分是遍历部分。是产品 资源?好吧,这取决于你的遍历产生了什么,它 可以生产产品.....

也许最好从 View 中走一遍 创建应用程序时如何配置...

这是一个典型的 View 。

  @view_config(context=Product, permission="view")
  def view_product(context, request):
      pass # would do stuff  

因此,当上下文是 Product 的实例时,将调用此 View 。和 如果该实例的 acl 属性具有“ View ” 允许。那么 Product 的实例如何成为上下文?

这就是遍历的魔力所在。 遍历只是字典的字典。所以一种方式,这 可以为你工作是如果你有一个像

/product/1

不知何故,一些资源需要被 url 来确定上下文,以便可以确定 View 。如果 我们有类似...

  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

现在这已经包含在文档中,我正在尝试将其煮沸 深入了解您需要了解的基础知识。 ProductContainer 是一个对象 它的行为就像一本字典。 “姓名”和“ parent ” Pyramid 需要属性才能生成 url 正确工作的方法。

所以现在我们有了一个可以遍历的资源。我们怎么说 遍历 ProductContainer 的 Pyramid ?我们通过 配置器对象。

  config = Configurator()
  config.add_route(name="product",
                   path="/product/*traverse",
                   factory=ProductContainer)
  config.scan()
  application = config.make_wsgi_app()

工厂参数需要一个可调用对象并将当前的 要求。碰巧 ProductContainer.init 会做 那就好了。

对于这样一个简单的例子来说,这可能看起来有点多,但希望 你可以想象的可能性。这种模式允许非常 精细的权限模型。

如果您不想/不需要非常精细的权限模型,例如行 级别 acl 的你可能不需要遍历,而是你可以使用 具有单个根工厂的路由。

  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()

注意:我根据内存做了代码示例,显然你需要所有必要的导入等。换句话说,这不能作为复制/粘贴工作

关于python - Pyramid 资源 : In plain English,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9539702/

相关文章:

c++ - 带有qt声子的视频播放器(使用python)

python - Groupby 和基于特定行值的计算

python - Kotti 中的内容元素可以有多个通用 subview 吗?

python - 使用 python 将 tgz 存储到 couchdb

python - CouchDB-Python:如何使用 “_show” 和 “_list” 函数?

python - 使用 CouchDB 进行 Flask 分页

Python从包中导入函数

python - 从 Python 内核调用 C++ CUDA 设备函数

python - 如何检查 Pyramid (pylons 2)中哪些权限授权失败?

python - 变色龙 ZPT 模板