This repository has been archived by the owner on Jul 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 132
/
pylons.txt
69 lines (47 loc) · 2.28 KB
/
pylons.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
Pylons support
==============
You can very easily interface Mongokit with Pylons. This tutorial explains how to do this.
Note that there's a lot of ways to do the same thing. If you found another better solution,
please contact me, I'll update this tutorial.
Write all your models in ``model/``. In the ``model/__init__.py``, import all the module
you want to register and then add them to a list called `register_models`.
Example of ``model/__init__.py``::
from blog import Blog
from blogpost import BlogPost
register_models = [Blog, BlogPost]
Then go to the ``lib/app_globals.py`` and edit this file so it look like this::
from pylons import config
from <appname>.models import register_models
from mongokit import *
class Globals(object):
"""Globals acts as a container for objects available throughout the
life of the application
"""
def __init__(self):
"""One instance of Globals is created during application
initialization and is available during requests via the
'app_globals' variable
"""
self.connection = Connection(
host = config['db_host'],
port = int(config['db_port']),
)
self.db = self.connection[config['db_name']]
self.connection.register(register_models)
In this file, we create the connection (and optionally the db if we use one) and then
we register all our models.
Now, you can access the connection via the pylons.config :
config['pylons.app_globals'].connection
A more convenient way is to add the connection to the BaseController so you can access
it just with ``self.connection``. The file ``lib/base.py`` has to look like this::
from pylons.controllers import WSGIController
from pylons import config
import pylons
class BaseController(WSGIController):
connection = config['pylons.app_globals'].connection
def __call__(self, environ, start_response):
"""Invoke the Controller"""
# WSGIController.__call__ dispatches to the Controller method
# the request is routed to. This routing information is
# available in environ['pylons.routes_dict']
return WSGIController.__call__(self, environ, start_response)