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