Using Custom Configs in Pyramid Views

Viewed 222

I'm working on learning Pyramid, and I'm trying to use a custom configuration from my development.ini file in one of my views. In this example, "ldap_server".

development.ini:

[app:main]
use = egg:myapp
pyramid.reload_templates = true
pyramid.includes =
    pyramid_debugtoolbar

ldap_server = 10.10.10.10

[server:main]
use = egg:waitress#main
listen = 0.0.0.0:6543

I find I'm able to access the value of "ldap_server" from within my main function in myapp/__init__.py. (The example below will print "The ldap server is 10.10.10.10" upon startup via pserve development.ini.)

from pyramid.config import Configurator

def main(global_config, **settings):
    config = Configurator(settings=settings)
    config.include('pyramid_jinja2')
    config.include('.routes')
    config.add_static_view(name='static', path='myapp:static')
    config.scan('.views')
    config.scan('.templates')

    ldap_server = settings.get('ldap_server')
    print('The ldap server is ' + ldap_server)

    return config.make_wsgi_app()

That said, I want to use this value in one of my views. I'm struggling to find documentation on how to do this. I've read a couple of documents, but am still struggling to grasp this.

Docs:

https://docs.pylonsproject.org/projects/pyramid/en/latest/narr/environment.html#adding-a-custom-setting

Specifically, I'm trying to understand which pyramid functions to import in my view, and how to access the 'ldap_server' value I defined in development.ini.

Here is the current snippet from myapp/views/login.py:

from pyramid.view import view_config, view_defaults
import ldap

@view_defaults(renderer='../templates/login.jinja2')
class TutorialViews(object):
    def __init__(self, request):
        self.request = request
        self.view_name = 'login'

    @view_config(route_name='login')
    def login_page(self):

        ldap_server = [ get value from development.ini ]

        do ldappy stuff...

I've been searching for hours, and can't figure out how to do it. This seems like it would be a trivial thing to accomplish. If someone could give me an example of how to do this, or point me to some docs that provide examples, I'd be extremely grateful.

UPDATE:

Below is the working solution (login.py), just in case someone like me (who is new to Pyramid) comes across this later. Just one line of change. Thanks to Sergey for his explanation in the answer below.

from pyramid.view import view_config, view_defaults
import ldap

@view_defaults(renderer='../templates/login.jinja2')
class TutorialViews(object):
    def __init__(self, request):
        self.request = request
        self.view_name = 'login'

    @view_config(route_name='login')
    def login_page(self):

        ldap_server = self.request.registry.settings['ldap_server']

        do ldappy stuff...
1 Answers

I think you should be able to access it via request.registry.settings:

If the settings argument is passed, it should be a Python dictionary representing the deployment settings for this application. These are later retrievable using the pyramid.registry.Registry.settings attribute (aka request.registry.settings).

https://docs.pylonsproject.org/projects/pyramid/en/latest/api/config.html

Related