金字塔资源:简明英语 [英] Pyramid resource: In plain English

查看:150
本文介绍了金字塔资源:简明英语的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've been reading on the ways to implement authorization (and authentication) to my newly created Pyramid application. I keep bumping into the concept called "Resource". I am using python-couchdb in my application and not using RDBMS at all, hence no SQLAlchemy. If I create a Product object like so:

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

有人可以告诉我这是否也称为资源吗?我一直在阅读金字塔的整个文档,但没有在哪里用简单明了的英语来解释资源一词的(也许我只是愚蠢的).如果这是资源,那是否意味着我只是将ACL内容粘贴在这里,如下所示:

Can someone please tell me if this is also called the resource? I've been reading the entire documentation of Pyramids, but no where does it explain the term resource in plain simple english (maybe I'm just stupid). If this is the resource, does this mean I just stick my ACL stuff in here like so:

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

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

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

If I were to also use Traversal, does this mean I add the getitem function in my python-couchdb Product class/resource?

对不起,它真的与所有新术语混淆(我来自Pylons 0.9.7).

Sorry, it's just really confusing with all the new terms (I came from Pylons 0.9.7).

谢谢.

推荐答案

我认为您缺少的部分是遍历部分.是产品 资源?好吧,这取决于您的遍历产生的结果, 可以生产产品.....

I think the piece you are missing is the traversal part. Is Product the resource? Well it depends on what your traversal produces, it could produce products.....

也许最好将其从视图中浏览回去 创建应用程序时如何配置它...

Perhaps it might be best to walk this through from the view back to how it gets configured when the application is created...

这是一个典型的视图.

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

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

So this view gets called when context is an instance of Product. AND if the acl attribute of that instance has the "view" permission. So how would an instance of Product become context?

这就是遍历魔力进来的地方. 遍历只是字典的字典.所以这的一种方法 如果您有一个类似

This is where the magic of traversal comes in. The very logic of traversal is simply a dictionary of dictionaries. So one way that this could work for you is if you had a url like

/product/1

不知何故,某些资源需要通过 url确定上下文,以便可以确定视图.如果什么 我们有类似...

Somehow, some resource needs to be traversed by the segments of the url to determine a context so that a view can be determined. What if we had something like...

  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是一个对象 就像字典一样. "名称"和"父母" 金字塔需要属性,以便生成网址 正确工作的方法.

Now this is covered in the documentation and I'm attempting to boil it down to the basics you need to know. The ProductContainer is an object that behaves like a dictionary. The "name" and "parent" attributes are required by pyramid in order for the url generation methods to work right.

所以现在我们有了可以遍历的资源.我们如何知道 金字塔遍历ProductContainer?我们通过 配置器对象.

So now we have a resource that can be traversed. How do we tell pyramid to traverse ProductContainer? We do that through the Configurator object.

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

factory参数需要一个可调用对象,并将其传递给当前对象 要求.碰巧ProductContainer. init 会做 很好.

The factory parameter expects a callable and it hands it the current request. It just so happens that ProductContainer.init will do that just fine.

对于这样一个简单的例子,这似乎有点多,但希望 您可以想象各种可能性.这种模式允许非常 精细的权限模型.

This might seem a little much for such a simple example, but hopefully you can imagine the possibilities. This pattern allows for very granular permission models.

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

If you don't want/need a very granular permission model such as row level acl's you probably don't need traversal, instead you can use routes with a single root factory.

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

注意:我是从内存中获取代码示例的,显然您需要所有必要的导入等.换句话说,这不能用作复制/粘贴

note: I did the code example from memory, obviously you need all the necessary imports etc. in other words this isn't going to work as a copy/paste

这篇关于金字塔资源:简明英语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆