The passage of the pyramid makes my head in

I am trying to figure out a Pyramid bypass with this very simple example. What I haven't figured out yet is where to "enter" the Article object from db.

Be that as it may, /Article correctly finds and displays article_view , but this is useless. How / when / where do I use the following part of the URL to request a specific article from db? eg. /Article/5048230b2485d614ecec341d .

Any tips would be great!

INIT .py

 from pyramid.config import Configurator from pyramid.events import subscriber from pyramid.events import NewRequest import pymongo from otk.resources import Root def main(global_config, **settings): """ This function returns a WSGI application. """ config = Configurator(settings=settings, root_factory=Root) config.add_static_view('static', 'otk:static') # MongoDB def add_mongo_db(event): settings = event.request.registry.settings url = settings['mongodb.url'] db_name = settings['mongodb.db_name'] db = settings['mongodb_conn'][db_name] event.request.db = db db_uri = settings['mongodb.url'] MongoDB = pymongo.Connection if 'pyramid_debugtoolbar' in set(settings.values()): class MongoDB(pymongo.Connection): def __html__(self): return 'MongoDB: <b>{}></b>'.format(self) conn = MongoDB(db_uri) config.registry.settings['mongodb_conn'] = conn config.add_subscriber(add_mongo_db, NewRequest) config.include('pyramid_jinja2') config.include('pyramid_debugtoolbar') config.scan('otk') return config.make_wsgi_app() 

resources.py

 class Root(object): __name__ = None __parent__ = None def __init__(self, request): self.request = request def __getitem__(self, key): if key == 'Article': return Article(self.request) else: raise KeyError class Article: __name__ = '' __parent__ = Root def __init__(self, request): self.reqeust = request # so I guess in here I need to update the Article with # with the document I get from the db. How? def __getitem__(self, key): raise KeyError 

views.py

 from pyramid.view import view_config from otk.resources import * from pyramid.response import Response @view_config(context=Root, renderer='templates/index.jinja2') def index(request): return {'project':'OTK'} @view_config(context=Article, renderer='templates/view/article.jinja2') def article_view(context, request): # I end up with an instance of Article here as the context.. but # at the moment, the Article is empty return {} 
+4
source share
1 answer

You typically return an Article object from the URL crawl identification part.

What happens with the crawl is that for each element of the URL path, an object is scanned and a new current object is created to search for the next path element.

So, for Article root object needs something matching this name, and the result of this search is a new “current” object, and then 5048230b2485d614ecec341d looked at on this new object.

So, what you are looking for is a dispatcher object that searches for articles based on the longer identifier you passed in and returns your instances of Article :

 class Root(object): __name__ = None __parent__ = None def __init__(self, request): self.request = request def __getitem__(self, key): if key == 'articles': dispatch = ArticleDispatcher(self.request) dispatch.__name__ = key dispatch.__parent__ = self return dispatch raise KeyError(key) class ArticleDispatcher(object): __name__ = None __parent__ = None def __init__(self, request): self.request = request def __getitem__(self, key): # Get a hold of the database here: db = findDatabase(self.request) if db.exists(key): data = db.load(key) art = Article(data) art.__name__ = key art.__parent__ = self return art raise KeyError(key) class Article: __name__ = None __parent__ = None def __init__(self, data): self.data = data 

Notice how I returned ArticleDispatcher when you use the URL /articles path, as well as how I set the __name__ and __parent__ ; you will need those that can create URLs for these instances.

The returned Article object now contains the actual article data, and the view can access this information when rendering.

You really want to go and study the Pyramid Traversal tutorial, which explains this in more detail.

+7
source

Source: https://habr.com/ru/post/1432662/


All Articles