Show More
@@ -1,128 +1,138 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """ |
|
3 | 3 | rhodecode.model.__init__ |
|
4 | 4 | ~~~~~~~~~~~~~~~~~~~~~~~~ |
|
5 | 5 | |
|
6 | 6 | The application's model objects |
|
7 | 7 | |
|
8 | 8 | :created_on: Nov 25, 2010 |
|
9 | 9 | :author: marcink |
|
10 | 10 | :copyright: (C) 2010-2012 Marcin Kuzminski <marcin@python-works.com> |
|
11 | 11 | :license: GPLv3, see COPYING for more details. |
|
12 | 12 | |
|
13 | 13 | |
|
14 | 14 | :example: |
|
15 | 15 | |
|
16 | 16 | .. code-block:: python |
|
17 | 17 | |
|
18 | 18 | from paste.deploy import appconfig |
|
19 | 19 | from pylons import config |
|
20 | 20 | from sqlalchemy import engine_from_config |
|
21 | 21 | from rhodecode.config.environment import load_environment |
|
22 | 22 | |
|
23 | 23 | conf = appconfig('config:development.ini', relative_to = './../../') |
|
24 | 24 | load_environment(conf.global_conf, conf.local_conf) |
|
25 | 25 | |
|
26 | 26 | engine = engine_from_config(config, 'sqlalchemy.') |
|
27 | 27 | init_model(engine) |
|
28 | 28 | # RUN YOUR CODE HERE |
|
29 | 29 | |
|
30 | 30 | """ |
|
31 | 31 | # This program is free software: you can redistribute it and/or modify |
|
32 | 32 | # it under the terms of the GNU General Public License as published by |
|
33 | 33 | # the Free Software Foundation, either version 3 of the License, or |
|
34 | 34 | # (at your option) any later version. |
|
35 | 35 | # |
|
36 | 36 | # This program is distributed in the hope that it will be useful, |
|
37 | 37 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
38 | 38 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
39 | 39 | # GNU General Public License for more details. |
|
40 | 40 | # |
|
41 | 41 | # You should have received a copy of the GNU General Public License |
|
42 | 42 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
43 | 43 | |
|
44 | 44 | import logging |
|
45 | from rhodecode.model.db import User, Repository, Permission | |
|
46 | 45 | from rhodecode.model import meta |
|
47 | 46 | |
|
48 | 47 | log = logging.getLogger(__name__) |
|
49 | 48 | |
|
50 | 49 | |
|
51 | 50 | def init_model(engine): |
|
52 | 51 | """ |
|
53 | 52 | Initializes db session, bind the engine with the metadata, |
|
54 | 53 | Call this before using any of the tables or classes in the model, |
|
55 | 54 | preferably once in application start |
|
56 | 55 | |
|
57 | 56 | :param engine: engine to bind to |
|
58 | 57 | """ |
|
59 | 58 | log.info("initializing db for %s" % engine) |
|
60 | 59 | meta.Base.metadata.bind = engine |
|
61 | 60 | |
|
62 | 61 | |
|
63 | 62 | class BaseModel(object): |
|
64 | 63 | """ |
|
65 | 64 | Base Model for all RhodeCode models, it adds sql alchemy session |
|
66 | 65 | into instance of model |
|
67 | 66 | |
|
68 | 67 | :param sa: If passed it reuses this session instead of creating a new one |
|
69 | 68 | """ |
|
70 | 69 | |
|
70 | cls = None # override in child class | |
|
71 | ||
|
71 | 72 | def __init__(self, sa=None): |
|
72 | 73 | if sa is not None: |
|
73 | 74 | self.sa = sa |
|
74 | 75 | else: |
|
75 | self.sa = meta.Session | |
|
76 | self.sa = meta.Session() | |
|
76 | 77 | |
|
77 | 78 | def _get_instance(self, cls, instance, callback=None): |
|
78 | 79 | """ |
|
79 | 80 | Get's instance of given cls using some simple lookup mechanism. |
|
80 | 81 | |
|
81 | 82 | :param cls: class to fetch |
|
82 | 83 | :param instance: int or Instance |
|
83 | 84 | :param callback: callback to call if all lookups failed |
|
84 | 85 | """ |
|
85 | 86 | |
|
86 | 87 | if isinstance(instance, cls): |
|
87 | 88 | return instance |
|
88 | 89 | elif isinstance(instance, (int, long)) or str(instance).isdigit(): |
|
89 | 90 | return cls.get(instance) |
|
90 | 91 | else: |
|
91 | 92 | if instance: |
|
92 | 93 | if callback is None: |
|
93 | 94 | raise Exception( |
|
94 | 95 | 'given object must be int, long or Instance of %s ' |
|
95 | 96 | 'got %s, no callback provided' % (cls, type(instance)) |
|
96 | 97 | ) |
|
97 | 98 | else: |
|
98 | 99 | return callback(instance) |
|
99 | 100 | |
|
100 | 101 | def _get_user(self, user): |
|
101 | 102 | """ |
|
102 | 103 | Helper method to get user by ID, or username fallback |
|
103 | 104 | |
|
104 | 105 | :param user: |
|
105 | 106 | :type user: UserID, username, or User instance |
|
106 | 107 | """ |
|
108 | from rhodecode.model.db import User | |
|
107 | 109 | return self._get_instance(User, user, |
|
108 | 110 | callback=User.get_by_username) |
|
109 | 111 | |
|
110 | 112 | def _get_repo(self, repository): |
|
111 | 113 | """ |
|
112 | 114 | Helper method to get repository by ID, or repository name |
|
113 | 115 | |
|
114 | 116 | :param repository: |
|
115 | 117 | :type repository: RepoID, repository name or Repository Instance |
|
116 | 118 | """ |
|
119 | from rhodecode.model.db import Repository | |
|
117 | 120 | return self._get_instance(Repository, repository, |
|
118 | 121 | callback=Repository.get_by_repo_name) |
|
119 | 122 | |
|
120 | 123 | def _get_perm(self, permission): |
|
121 | 124 | """ |
|
122 | 125 | Helper method to get permission by ID, or permission name |
|
123 | 126 | |
|
124 | 127 | :param permission: |
|
125 | 128 | :type permission: PermissionID, permission_name or Permission instance |
|
126 | 129 | """ |
|
130 | from rhodecode.model.db import Permission | |
|
127 | 131 | return self._get_instance(Permission, permission, |
|
128 | 132 | callback=Permission.get_by_key) |
|
133 | ||
|
134 | def get_all(self): | |
|
135 | """ | |
|
136 | Returns all instances of what is defined in `cls` class variable | |
|
137 | """ | |
|
138 | return self.cls.getAll() |
General Comments 0
You need to be logged in to leave comments.
Login now