Mercurial > kallithea
annotate rhodecode/model/__init__.py @ 3195:a50901f2108d beta
Added repository followers to the get_repo api function result.
author | aparkar <aparkar@icloud.com> |
---|---|
date | Tue, 22 Jan 2013 11:56:47 +1100 |
parents | 12fce5e499d5 |
children | 1f7b8c73c94a dc4644865e8b |
rev | line source |
---|---|
759
a7f50911a945
Models code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
752
diff
changeset
|
1 # -*- coding: utf-8 -*- |
a7f50911a945
Models code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
752
diff
changeset
|
2 """ |
811 | 3 rhodecode.model.__init__ |
4 ~~~~~~~~~~~~~~~~~~~~~~~~ | |
1203
6832ef664673
source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents:
1155
diff
changeset
|
5 |
759
a7f50911a945
Models code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
752
diff
changeset
|
6 The application's model objects |
1203
6832ef664673
source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents:
1155
diff
changeset
|
7 |
759
a7f50911a945
Models code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
752
diff
changeset
|
8 :created_on: Nov 25, 2010 |
a7f50911a945
Models code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
752
diff
changeset
|
9 :author: marcink |
1824
89efedac4e6c
2012 copyrights
Marcin Kuzminski <marcin@python-works.com>
parents:
1800
diff
changeset
|
10 :copyright: (C) 2010-2012 Marcin Kuzminski <marcin@python-works.com> |
759
a7f50911a945
Models code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
752
diff
changeset
|
11 :license: GPLv3, see COPYING for more details. |
1203
6832ef664673
source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents:
1155
diff
changeset
|
12 |
6832ef664673
source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents:
1155
diff
changeset
|
13 |
759
a7f50911a945
Models code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
752
diff
changeset
|
14 :example: |
1203
6832ef664673
source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents:
1155
diff
changeset
|
15 |
811 | 16 .. code-block:: python |
1203
6832ef664673
source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents:
1155
diff
changeset
|
17 |
811 | 18 from paste.deploy import appconfig |
19 from pylons import config | |
20 from sqlalchemy import engine_from_config | |
21 from rhodecode.config.environment import load_environment | |
1203
6832ef664673
source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents:
1155
diff
changeset
|
22 |
811 | 23 conf = appconfig('config:development.ini', relative_to = './../../') |
24 load_environment(conf.global_conf, conf.local_conf) | |
1203
6832ef664673
source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents:
1155
diff
changeset
|
25 |
811 | 26 engine = engine_from_config(config, 'sqlalchemy.') |
27 init_model(engine) | |
28 # RUN YOUR CODE HERE | |
1203
6832ef664673
source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents:
1155
diff
changeset
|
29 |
759
a7f50911a945
Models code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
752
diff
changeset
|
30 """ |
1206
a671db5bdd58
fixed license issue #149
Marcin Kuzminski <marcin@python-works.com>
parents:
1203
diff
changeset
|
31 # This program is free software: you can redistribute it and/or modify |
a671db5bdd58
fixed license issue #149
Marcin Kuzminski <marcin@python-works.com>
parents:
1203
diff
changeset
|
32 # it under the terms of the GNU General Public License as published by |
a671db5bdd58
fixed license issue #149
Marcin Kuzminski <marcin@python-works.com>
parents:
1203
diff
changeset
|
33 # the Free Software Foundation, either version 3 of the License, or |
a671db5bdd58
fixed license issue #149
Marcin Kuzminski <marcin@python-works.com>
parents:
1203
diff
changeset
|
34 # (at your option) any later version. |
1203
6832ef664673
source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents:
1155
diff
changeset
|
35 # |
759
a7f50911a945
Models code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
752
diff
changeset
|
36 # This program is distributed in the hope that it will be useful, |
a7f50911a945
Models code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
752
diff
changeset
|
37 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
a7f50911a945
Models code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
752
diff
changeset
|
38 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
a7f50911a945
Models code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
752
diff
changeset
|
39 # GNU General Public License for more details. |
1203
6832ef664673
source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents:
1155
diff
changeset
|
40 # |
759
a7f50911a945
Models code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
752
diff
changeset
|
41 # You should have received a copy of the GNU General Public License |
1206
a671db5bdd58
fixed license issue #149
Marcin Kuzminski <marcin@python-works.com>
parents:
1203
diff
changeset
|
42 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
759
a7f50911a945
Models code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents:
752
diff
changeset
|
43 |
0 | 44 import logging |
547
1e757ac98988
renamed project to rhodecode
Marcin Kuzminski <marcin@python-works.com>
parents:
473
diff
changeset
|
45 from rhodecode.model import meta |
2882
12fce5e499d5
obfuscate password in logs for engine connection string
Marcin Kuzminski <marcin@python-works.com>
parents:
2672
diff
changeset
|
46 from rhodecode.lib.utils2 import safe_str, obfuscate_url_pw |
1155
37a9421f32a0
Added handy methods to Repository model for fetching groups with parents
Marcin Kuzminski <marcin@python-works.com>
parents:
902
diff
changeset
|
47 |
0 | 48 log = logging.getLogger(__name__) |
49 | |
1271
aa7e45ad0cea
Fixed permissions for users groups, group can have create repo permission now.
Marcin Kuzminski <marcin@python-works.com>
parents:
1206
diff
changeset
|
50 |
0 | 51 def init_model(engine): |
1271
aa7e45ad0cea
Fixed permissions for users groups, group can have create repo permission now.
Marcin Kuzminski <marcin@python-works.com>
parents:
1206
diff
changeset
|
52 """ |
aa7e45ad0cea
Fixed permissions for users groups, group can have create repo permission now.
Marcin Kuzminski <marcin@python-works.com>
parents:
1206
diff
changeset
|
53 Initializes db session, bind the engine with the metadata, |
aa7e45ad0cea
Fixed permissions for users groups, group can have create repo permission now.
Marcin Kuzminski <marcin@python-works.com>
parents:
1206
diff
changeset
|
54 Call this before using any of the tables or classes in the model, |
aa7e45ad0cea
Fixed permissions for users groups, group can have create repo permission now.
Marcin Kuzminski <marcin@python-works.com>
parents:
1206
diff
changeset
|
55 preferably once in application start |
1203
6832ef664673
source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents:
1155
diff
changeset
|
56 |
811 | 57 :param engine: engine to bind to |
58 """ | |
2882
12fce5e499d5
obfuscate password in logs for engine connection string
Marcin Kuzminski <marcin@python-works.com>
parents:
2672
diff
changeset
|
59 engine_str = obfuscate_url_pw(str(engine.url)) |
12fce5e499d5
obfuscate password in logs for engine connection string
Marcin Kuzminski <marcin@python-works.com>
parents:
2672
diff
changeset
|
60 log.info("initializing db for %s" % engine_str) |
0 | 61 meta.Base.metadata.bind = engine |
752
89b9037d68b7
fixed Example celery config to ampq,
Marcin Kuzminski <marcin@python-works.com>
parents:
547
diff
changeset
|
62 |
1271
aa7e45ad0cea
Fixed permissions for users groups, group can have create repo permission now.
Marcin Kuzminski <marcin@python-works.com>
parents:
1206
diff
changeset
|
63 |
752
89b9037d68b7
fixed Example celery config to ampq,
Marcin Kuzminski <marcin@python-works.com>
parents:
547
diff
changeset
|
64 class BaseModel(object): |
1751
47c2a006d43b
Summary page downloads limited to zip.
Marcin Kuzminski <marcin@python-works.com>
parents:
1749
diff
changeset
|
65 """ |
47c2a006d43b
Summary page downloads limited to zip.
Marcin Kuzminski <marcin@python-works.com>
parents:
1749
diff
changeset
|
66 Base Model for all RhodeCode models, it adds sql alchemy session |
811 | 67 into instance of model |
1203
6832ef664673
source code cleanup: remove trailing white space, normalize file endings
Marcin Kuzminski <marcin@python-works.com>
parents:
1155
diff
changeset
|
68 |
811 | 69 :param sa: If passed it reuses this session instead of creating a new one |
70 """ | |
752
89b9037d68b7
fixed Example celery config to ampq,
Marcin Kuzminski <marcin@python-works.com>
parents:
547
diff
changeset
|
71 |
2521
e4d2271e1193
Added get_all method to BaseModel, requires to define cls variable in child models
Marcin Kuzminski <marcin@python-works.com>
parents:
2432
diff
changeset
|
72 cls = None # override in child class |
e4d2271e1193
Added get_all method to BaseModel, requires to define cls variable in child models
Marcin Kuzminski <marcin@python-works.com>
parents:
2432
diff
changeset
|
73 |
752
89b9037d68b7
fixed Example celery config to ampq,
Marcin Kuzminski <marcin@python-works.com>
parents:
547
diff
changeset
|
74 def __init__(self, sa=None): |
89b9037d68b7
fixed Example celery config to ampq,
Marcin Kuzminski <marcin@python-works.com>
parents:
547
diff
changeset
|
75 if sa is not None: |
89b9037d68b7
fixed Example celery config to ampq,
Marcin Kuzminski <marcin@python-works.com>
parents:
547
diff
changeset
|
76 self.sa = sa |
89b9037d68b7
fixed Example celery config to ampq,
Marcin Kuzminski <marcin@python-works.com>
parents:
547
diff
changeset
|
77 else: |
2521
e4d2271e1193
Added get_all method to BaseModel, requires to define cls variable in child models
Marcin Kuzminski <marcin@python-works.com>
parents:
2432
diff
changeset
|
78 self.sa = meta.Session() |
1713
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1393
diff
changeset
|
79 |
1982
87f0800abc7b
#227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents:
1976
diff
changeset
|
80 def _get_instance(self, cls, instance, callback=None): |
1713
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1393
diff
changeset
|
81 """ |
1982
87f0800abc7b
#227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents:
1976
diff
changeset
|
82 Get's instance of given cls using some simple lookup mechanism. |
1800 | 83 |
1713
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1393
diff
changeset
|
84 :param cls: class to fetch |
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1393
diff
changeset
|
85 :param instance: int or Instance |
1982
87f0800abc7b
#227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents:
1976
diff
changeset
|
86 :param callback: callback to call if all lookups failed |
1713
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1393
diff
changeset
|
87 """ |
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1393
diff
changeset
|
88 |
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1393
diff
changeset
|
89 if isinstance(instance, cls): |
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1393
diff
changeset
|
90 return instance |
2672
169807710db2
fixed possible unicode errors on repo get function
Marcin Kuzminski <marcin@python-works.com>
parents:
2521
diff
changeset
|
91 elif isinstance(instance, (int, long)) or safe_str(instance).isdigit(): |
1713
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1393
diff
changeset
|
92 return cls.get(instance) |
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1393
diff
changeset
|
93 else: |
54687aa00724
Tests updates, Session refactoring
Marcin Kuzminski <marcin@python-works.com>
parents:
1393
diff
changeset
|
94 if instance: |
1982
87f0800abc7b
#227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents:
1976
diff
changeset
|
95 if callback is None: |
87f0800abc7b
#227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents:
1976
diff
changeset
|
96 raise Exception( |
2149 | 97 'given object must be int, long or Instance of %s ' |
98 'got %s, no callback provided' % (cls, type(instance)) | |
1982
87f0800abc7b
#227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents:
1976
diff
changeset
|
99 ) |
87f0800abc7b
#227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents:
1976
diff
changeset
|
100 else: |
87f0800abc7b
#227 Initial version of repository groups permissions system
Marcin Kuzminski <marcin@python-works.com>
parents:
1976
diff
changeset
|
101 return callback(instance) |
2432
d3ac7491a5c8
Share common getter functions in base model, and remove duplicated functions from other models
Marcin Kuzminski <marcin@python-works.com>
parents:
2149
diff
changeset
|
102 |
d3ac7491a5c8
Share common getter functions in base model, and remove duplicated functions from other models
Marcin Kuzminski <marcin@python-works.com>
parents:
2149
diff
changeset
|
103 def _get_user(self, user): |
d3ac7491a5c8
Share common getter functions in base model, and remove duplicated functions from other models
Marcin Kuzminski <marcin@python-works.com>
parents:
2149
diff
changeset
|
104 """ |
d3ac7491a5c8
Share common getter functions in base model, and remove duplicated functions from other models
Marcin Kuzminski <marcin@python-works.com>
parents:
2149
diff
changeset
|
105 Helper method to get user by ID, or username fallback |
d3ac7491a5c8
Share common getter functions in base model, and remove duplicated functions from other models
Marcin Kuzminski <marcin@python-works.com>
parents:
2149
diff
changeset
|
106 |
d3ac7491a5c8
Share common getter functions in base model, and remove duplicated functions from other models
Marcin Kuzminski <marcin@python-works.com>
parents:
2149
diff
changeset
|
107 :param user: |
d3ac7491a5c8
Share common getter functions in base model, and remove duplicated functions from other models
Marcin Kuzminski <marcin@python-works.com>
parents:
2149
diff
changeset
|
108 :type user: UserID, username, or User instance |
d3ac7491a5c8
Share common getter functions in base model, and remove duplicated functions from other models
Marcin Kuzminski <marcin@python-works.com>
parents:
2149
diff
changeset
|
109 """ |
2521
e4d2271e1193
Added get_all method to BaseModel, requires to define cls variable in child models
Marcin Kuzminski <marcin@python-works.com>
parents:
2432
diff
changeset
|
110 from rhodecode.model.db import User |
2432
d3ac7491a5c8
Share common getter functions in base model, and remove duplicated functions from other models
Marcin Kuzminski <marcin@python-works.com>
parents:
2149
diff
changeset
|
111 return self._get_instance(User, user, |
d3ac7491a5c8
Share common getter functions in base model, and remove duplicated functions from other models
Marcin Kuzminski <marcin@python-works.com>
parents:
2149
diff
changeset
|
112 callback=User.get_by_username) |
d3ac7491a5c8
Share common getter functions in base model, and remove duplicated functions from other models
Marcin Kuzminski <marcin@python-works.com>
parents:
2149
diff
changeset
|
113 |
d3ac7491a5c8
Share common getter functions in base model, and remove duplicated functions from other models
Marcin Kuzminski <marcin@python-works.com>
parents:
2149
diff
changeset
|
114 def _get_repo(self, repository): |
d3ac7491a5c8
Share common getter functions in base model, and remove duplicated functions from other models
Marcin Kuzminski <marcin@python-works.com>
parents:
2149
diff
changeset
|
115 """ |
d3ac7491a5c8
Share common getter functions in base model, and remove duplicated functions from other models
Marcin Kuzminski <marcin@python-works.com>
parents:
2149
diff
changeset
|
116 Helper method to get repository by ID, or repository name |
d3ac7491a5c8
Share common getter functions in base model, and remove duplicated functions from other models
Marcin Kuzminski <marcin@python-works.com>
parents:
2149
diff
changeset
|
117 |
d3ac7491a5c8
Share common getter functions in base model, and remove duplicated functions from other models
Marcin Kuzminski <marcin@python-works.com>
parents:
2149
diff
changeset
|
118 :param repository: |
d3ac7491a5c8
Share common getter functions in base model, and remove duplicated functions from other models
Marcin Kuzminski <marcin@python-works.com>
parents:
2149
diff
changeset
|
119 :type repository: RepoID, repository name or Repository Instance |
d3ac7491a5c8
Share common getter functions in base model, and remove duplicated functions from other models
Marcin Kuzminski <marcin@python-works.com>
parents:
2149
diff
changeset
|
120 """ |
2521
e4d2271e1193
Added get_all method to BaseModel, requires to define cls variable in child models
Marcin Kuzminski <marcin@python-works.com>
parents:
2432
diff
changeset
|
121 from rhodecode.model.db import Repository |
2432
d3ac7491a5c8
Share common getter functions in base model, and remove duplicated functions from other models
Marcin Kuzminski <marcin@python-works.com>
parents:
2149
diff
changeset
|
122 return self._get_instance(Repository, repository, |
d3ac7491a5c8
Share common getter functions in base model, and remove duplicated functions from other models
Marcin Kuzminski <marcin@python-works.com>
parents:
2149
diff
changeset
|
123 callback=Repository.get_by_repo_name) |
d3ac7491a5c8
Share common getter functions in base model, and remove duplicated functions from other models
Marcin Kuzminski <marcin@python-works.com>
parents:
2149
diff
changeset
|
124 |
d3ac7491a5c8
Share common getter functions in base model, and remove duplicated functions from other models
Marcin Kuzminski <marcin@python-works.com>
parents:
2149
diff
changeset
|
125 def _get_perm(self, permission): |
d3ac7491a5c8
Share common getter functions in base model, and remove duplicated functions from other models
Marcin Kuzminski <marcin@python-works.com>
parents:
2149
diff
changeset
|
126 """ |
d3ac7491a5c8
Share common getter functions in base model, and remove duplicated functions from other models
Marcin Kuzminski <marcin@python-works.com>
parents:
2149
diff
changeset
|
127 Helper method to get permission by ID, or permission name |
d3ac7491a5c8
Share common getter functions in base model, and remove duplicated functions from other models
Marcin Kuzminski <marcin@python-works.com>
parents:
2149
diff
changeset
|
128 |
d3ac7491a5c8
Share common getter functions in base model, and remove duplicated functions from other models
Marcin Kuzminski <marcin@python-works.com>
parents:
2149
diff
changeset
|
129 :param permission: |
d3ac7491a5c8
Share common getter functions in base model, and remove duplicated functions from other models
Marcin Kuzminski <marcin@python-works.com>
parents:
2149
diff
changeset
|
130 :type permission: PermissionID, permission_name or Permission instance |
d3ac7491a5c8
Share common getter functions in base model, and remove duplicated functions from other models
Marcin Kuzminski <marcin@python-works.com>
parents:
2149
diff
changeset
|
131 """ |
2521
e4d2271e1193
Added get_all method to BaseModel, requires to define cls variable in child models
Marcin Kuzminski <marcin@python-works.com>
parents:
2432
diff
changeset
|
132 from rhodecode.model.db import Permission |
2432
d3ac7491a5c8
Share common getter functions in base model, and remove duplicated functions from other models
Marcin Kuzminski <marcin@python-works.com>
parents:
2149
diff
changeset
|
133 return self._get_instance(Permission, permission, |
d3ac7491a5c8
Share common getter functions in base model, and remove duplicated functions from other models
Marcin Kuzminski <marcin@python-works.com>
parents:
2149
diff
changeset
|
134 callback=Permission.get_by_key) |
2521
e4d2271e1193
Added get_all method to BaseModel, requires to define cls variable in child models
Marcin Kuzminski <marcin@python-works.com>
parents:
2432
diff
changeset
|
135 |
e4d2271e1193
Added get_all method to BaseModel, requires to define cls variable in child models
Marcin Kuzminski <marcin@python-works.com>
parents:
2432
diff
changeset
|
136 def get_all(self): |
e4d2271e1193
Added get_all method to BaseModel, requires to define cls variable in child models
Marcin Kuzminski <marcin@python-works.com>
parents:
2432
diff
changeset
|
137 """ |
e4d2271e1193
Added get_all method to BaseModel, requires to define cls variable in child models
Marcin Kuzminski <marcin@python-works.com>
parents:
2432
diff
changeset
|
138 Returns all instances of what is defined in `cls` class variable |
e4d2271e1193
Added get_all method to BaseModel, requires to define cls variable in child models
Marcin Kuzminski <marcin@python-works.com>
parents:
2432
diff
changeset
|
139 """ |
e4d2271e1193
Added get_all method to BaseModel, requires to define cls variable in child models
Marcin Kuzminski <marcin@python-works.com>
parents:
2432
diff
changeset
|
140 return self.cls.getAll() |