##// END OF EJS Templates
db: ported init_model to pyramid.
marcink -
r2342:8478dd31 default
parent child Browse files
Show More
@@ -1,154 +1,155 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 # Copyright (C) 2010-2017 RhodeCode GmbH
4 4 #
5 5 # This program is free software: you can redistribute it and/or modify
6 6 # it under the terms of the GNU Affero General Public License, version 3
7 7 # (only), as published by the Free Software Foundation.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU Affero General Public License
15 15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 16 #
17 17 # This program is dual-licensed. If you wish to learn more about the
18 18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20 20
21 21 """
22 22 The application's model objects
23 23
24 24 :example:
25 25
26 26 .. code-block:: python
27 27
28 28 from paste.deploy import appconfig
29 29 from pylons import config
30 30 from sqlalchemy import engine_from_config
31 31 from rhodecode.config.environment import load_environment
32 32
33 33 conf = appconfig('config:development.ini', relative_to = './../../')
34 34 load_environment(conf.global_conf, conf.local_conf)
35 35
36 36 engine = engine_from_config(config, 'sqlalchemy.')
37 37 init_model(engine)
38 38 # RUN YOUR CODE HERE
39 39
40 40 """
41 41
42 42
43 43 import logging
44 44
45 45 from rhodecode.model import meta, db
46 46 from rhodecode.lib.utils2 import obfuscate_url_pw, get_encryption_key
47 47
48 48 log = logging.getLogger(__name__)
49 49
50 50
51 51 def init_model(engine, encryption_key=None):
52 52 """
53 53 Initializes db session, bind the engine with the metadata,
54 54 Call this before using any of the tables or classes in the model,
55 55 preferably once in application start
56 56
57 57 :param engine: engine to bind to
58 58 """
59 59 engine_str = obfuscate_url_pw(str(engine.url))
60 60 log.info("initializing db for %s", engine_str)
61 61 meta.Base.metadata.bind = engine
62 62 db.ENCRYPTION_KEY = encryption_key
63 63
64 64
65 def init_model_encryption(migration_models):
66 from pylons import config
65 def init_model_encryption(migration_models, config=None):
66 from pyramid.threadlocal import get_current_registry
67 config = config or get_current_registry().settings
67 68 migration_models.ENCRYPTION_KEY = get_encryption_key(config)
68 69 db.ENCRYPTION_KEY = get_encryption_key(config)
69 70
70 71
71 72 class BaseModel(object):
72 73 """
73 74 Base Model for all RhodeCode models, it adds sql alchemy session
74 75 into instance of model
75 76
76 77 :param sa: If passed it reuses this session instead of creating a new one
77 78 """
78 79
79 80 cls = None # override in child class
80 81
81 82 def __init__(self, sa=None):
82 83 if sa is not None:
83 84 self.sa = sa
84 85 else:
85 86 self.sa = meta.Session()
86 87
87 88 def _get_instance(self, cls, instance, callback=None):
88 89 """
89 90 Gets instance of given cls using some simple lookup mechanism.
90 91
91 92 :param cls: classes to fetch
92 93 :param instance: int or Instance
93 94 :param callback: callback to call if all lookups failed
94 95 """
95 96
96 97 if isinstance(instance, cls):
97 98 return instance
98 99 elif isinstance(instance, (int, long)):
99 100 if isinstance(cls, tuple):
100 101 # if we pass multi instances we pick first to .get()
101 102 cls = cls[0]
102 103 return cls.get(instance)
103 104 else:
104 105 if instance:
105 106 if callback is None:
106 107 raise Exception(
107 108 'given object must be int, long or Instance of %s '
108 109 'got %s, no callback provided' % (cls, type(instance))
109 110 )
110 111 else:
111 112 return callback(instance)
112 113
113 114 def _get_user(self, user):
114 115 """
115 116 Helper method to get user by ID, or username fallback
116 117
117 118 :param user: UserID, username, or User instance
118 119 """
119 120 return self._get_instance(
120 121 db.User, user, callback=db.User.get_by_username)
121 122
122 123 def _get_user_group(self, user_group):
123 124 """
124 125 Helper method to get user by ID, or username fallback
125 126
126 127 :param user_group: UserGroupID, user_group_name, or UserGroup instance
127 128 """
128 129 return self._get_instance(
129 130 db.UserGroup, user_group, callback=db.UserGroup.get_by_group_name)
130 131
131 132 def _get_repo(self, repository):
132 133 """
133 134 Helper method to get repository by ID, or repository name
134 135
135 136 :param repository: RepoID, repository name or Repository Instance
136 137 """
137 138 return self._get_instance(
138 139 db.Repository, repository, callback=db.Repository.get_by_repo_name)
139 140
140 141 def _get_perm(self, permission):
141 142 """
142 143 Helper method to get permission by ID, or permission name
143 144
144 145 :param permission: PermissionID, permission_name or Permission instance
145 146 """
146 147 return self._get_instance(
147 148 db.Permission, permission, callback=db.Permission.get_by_key)
148 149
149 150 @classmethod
150 151 def get_all(cls):
151 152 """
152 153 Returns all instances of what is defined in `cls` class variable
153 154 """
154 155 return cls.cls.getAll()
General Comments 0
You need to be logged in to leave comments. Login now