Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a Pyramid "model" also a Pyramid "resource"?

I'm currently in the process of learning how to use the Python Pyramid web framework, and have found the documentation to be quite excellent.

I have, however, hit a stumbling block when it comes to distinguishing the idea of a "model" (i.e. a class defined under SQLAlchemy's declarative system) from the idea of a "resource" (i.e. a means of defining access control lists on views for use with Pyramid's auth system).

I understand the above statements seem to show that I already understand the difference, but I'm having trouble understanding whether I should be making models resources (by adding the __acl__ attribute directly in the model class) or creating a separate resource class (which has the proper __parent__ and __name__ attributes) which represents the access to a view which uses the model.

Any guidance is appreciated.

like image 446
Benjamin Crawford Ctrl-Alt-Tut Avatar asked Oct 17 '22 06:10

Benjamin Crawford Ctrl-Alt-Tut


1 Answers

If an application's domain model is hierarchically, Pyramid offers the idea of resources to build a resource tree. Traversal is used to map URLs to code and to identify a resource within the resource tree. You typically do not use resources and traversal when working with a relational database.

Excerpt from "Defending the design - Pyramid Does Traversal, and I Don't Like Traversal"

In Pyramid, traversal is the act of resolving a URL path to a resource object in a resource tree. Some people are uncomfortable with this notion, and believe it is wrong. Thankfully if you use Pyramid and you don't want to model your application in terms of a resource tree, you needn't use it at all. Instead use URL dispatch to map URL paths to views.

Relational databases aren't naturally hierarchical, so traversing one like a tree is not possible.

You can be assured that if you don't want to understand traversal, you don't have to. You can happily build Pyramid applications with only URL dispatch.

Excerpt from Resources

A resource is an object that represents a "place" in a tree related to your application. (...) A resource tree is a set of nested dictionary-like objects which you can use to represent your website's structure.

In an application which uses traversal to map URLs to code, the resource tree structure is used heavily to map each URL to a view callable. When traversal is used, Pyramid will walk through the resource tree by traversing through its nested dictionary structure in order to find a context resource. Once a context resource is found, the context resource and data in the request will be used to find a view callable.

In an application which uses URL dispatch, the resource tree is only used indirectly, and is often "invisible" to the developer. (...) This root resource sometimes has security declarations attached to it, but is not required to have any. In general, the resource tree is much less important in applications that use URL dispatch than applications that use traversal.

I think the topic is covered extensively in the docs.

  • Resources
  • Much ado about traversal
  • Traversal
  • Combining Traversal and URL Dispatch

I used to recommend a project to emphasize Pyramid’s capabilities.

  • Pyramid Auth Demo

My humble opinion: You do not need to understand both concepts entirely and upfront to adopt Pyramid framework for your first projects. Go for URL Dispatch and SQLAlchemy when working with a relational database.

Excerpt - Pyramid Provides Too Many "Rails"

By design, Pyramid is not a particularly opinionated web framework. Pyramid provides some features that other web frameworks do not. These are features meant for use cases that might not make sense to you if you're building a simple (...) web application.

like image 102
Sascha Gottfried Avatar answered Oct 20 '22 11:10

Sascha Gottfried