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