Show More
@@ -1,134 +1,135 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2010-2018 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 | import logging |
|
23 | 23 | |
|
24 | import rhodecode | |
|
24 | 25 | from rhodecode.model import meta, db |
|
25 | 26 | from rhodecode.lib.utils2 import obfuscate_url_pw, get_encryption_key |
|
26 | 27 | |
|
27 | 28 | log = logging.getLogger(__name__) |
|
28 | 29 | |
|
29 | 30 | |
|
30 | 31 | def init_model(engine, encryption_key=None): |
|
31 | 32 | """ |
|
32 | 33 | Initializes db session, bind the engine with the metadata, |
|
33 | 34 | Call this before using any of the tables or classes in the model, |
|
34 | 35 | preferably once in application start |
|
35 | 36 | |
|
36 | 37 | :param engine: engine to bind to |
|
37 | 38 | """ |
|
38 | 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 | 41 | meta.Base.metadata.bind = engine |
|
41 | 42 | db.ENCRYPTION_KEY = encryption_key |
|
42 | 43 | |
|
43 | 44 | |
|
44 | 45 | def init_model_encryption(migration_models, config=None): |
|
45 | 46 | from pyramid.threadlocal import get_current_registry |
|
46 | 47 | config = config or get_current_registry().settings |
|
47 | 48 | migration_models.ENCRYPTION_KEY = get_encryption_key(config) |
|
48 | 49 | db.ENCRYPTION_KEY = get_encryption_key(config) |
|
49 | 50 | |
|
50 | 51 | |
|
51 | 52 | class BaseModel(object): |
|
52 | 53 | """ |
|
53 | 54 | Base Model for all RhodeCode models, it adds sql alchemy session |
|
54 | 55 | into instance of model |
|
55 | 56 | |
|
56 | 57 | :param sa: If passed it reuses this session instead of creating a new one |
|
57 | 58 | """ |
|
58 | 59 | |
|
59 | 60 | cls = None # override in child class |
|
60 | 61 | |
|
61 | 62 | def __init__(self, sa=None): |
|
62 | 63 | if sa is not None: |
|
63 | 64 | self.sa = sa |
|
64 | 65 | else: |
|
65 | 66 | self.sa = meta.Session() |
|
66 | 67 | |
|
67 | 68 | def _get_instance(self, cls, instance, callback=None): |
|
68 | 69 | """ |
|
69 | 70 | Gets instance of given cls using some simple lookup mechanism. |
|
70 | 71 | |
|
71 | 72 | :param cls: classes to fetch |
|
72 | 73 | :param instance: int or Instance |
|
73 | 74 | :param callback: callback to call if all lookups failed |
|
74 | 75 | """ |
|
75 | 76 | |
|
76 | 77 | if isinstance(instance, cls): |
|
77 | 78 | return instance |
|
78 | 79 | elif isinstance(instance, (int, long)): |
|
79 | 80 | if isinstance(cls, tuple): |
|
80 | 81 | # if we pass multi instances we pick first to .get() |
|
81 | 82 | cls = cls[0] |
|
82 | 83 | return cls.get(instance) |
|
83 | 84 | else: |
|
84 | 85 | if instance: |
|
85 | 86 | if callback is None: |
|
86 | 87 | raise Exception( |
|
87 | 88 | 'given object must be int, long or Instance of %s ' |
|
88 | 89 | 'got %s, no callback provided' % (cls, type(instance)) |
|
89 | 90 | ) |
|
90 | 91 | else: |
|
91 | 92 | return callback(instance) |
|
92 | 93 | |
|
93 | 94 | def _get_user(self, user): |
|
94 | 95 | """ |
|
95 | 96 | Helper method to get user by ID, or username fallback |
|
96 | 97 | |
|
97 | 98 | :param user: UserID, username, or User instance |
|
98 | 99 | """ |
|
99 | 100 | return self._get_instance( |
|
100 | 101 | db.User, user, callback=db.User.get_by_username) |
|
101 | 102 | |
|
102 | 103 | def _get_user_group(self, user_group): |
|
103 | 104 | """ |
|
104 | 105 | Helper method to get user by ID, or username fallback |
|
105 | 106 | |
|
106 | 107 | :param user_group: UserGroupID, user_group_name, or UserGroup instance |
|
107 | 108 | """ |
|
108 | 109 | return self._get_instance( |
|
109 | 110 | db.UserGroup, user_group, callback=db.UserGroup.get_by_group_name) |
|
110 | 111 | |
|
111 | 112 | def _get_repo(self, repository): |
|
112 | 113 | """ |
|
113 | 114 | Helper method to get repository by ID, or repository name |
|
114 | 115 | |
|
115 | 116 | :param repository: RepoID, repository name or Repository Instance |
|
116 | 117 | """ |
|
117 | 118 | return self._get_instance( |
|
118 | 119 | db.Repository, repository, callback=db.Repository.get_by_repo_name) |
|
119 | 120 | |
|
120 | 121 | def _get_perm(self, permission): |
|
121 | 122 | """ |
|
122 | 123 | Helper method to get permission by ID, or permission name |
|
123 | 124 | |
|
124 | 125 | :param permission: PermissionID, permission_name or Permission instance |
|
125 | 126 | """ |
|
126 | 127 | return self._get_instance( |
|
127 | 128 | db.Permission, permission, callback=db.Permission.get_by_key) |
|
128 | 129 | |
|
129 | 130 | @classmethod |
|
130 | 131 | def get_all(cls): |
|
131 | 132 | """ |
|
132 | 133 | Returns all instances of what is defined in `cls` class variable |
|
133 | 134 | """ |
|
134 | 135 | return cls.cls.getAll() |
General Comments 0
You need to be logged in to leave comments.
Login now