Show More
@@ -1,347 +1,325 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """ |
|
3 | 3 | rhodecode.model.db |
|
4 | 4 | ~~~~~~~~~~~~~~~~~~ |
|
5 | 5 | |
|
6 | 6 | Database Models for RhodeCode |
|
7 | 7 | |
|
8 | 8 | :created_on: Apr 08, 2010 |
|
9 | 9 | :author: marcink |
|
10 | 10 | :copyright: (C) 2009-2011 Marcin Kuzminski <marcin@python-works.com> |
|
11 | 11 | :license: GPLv3, see COPYING for more details. |
|
12 | 12 | """ |
|
13 | 13 | # This program is free software; you can redistribute it and/or |
|
14 | 14 | # modify it under the terms of the GNU General Public License |
|
15 | 15 | # as published by the Free Software Foundation; version 2 |
|
16 | 16 | # of the License or (at your opinion) any later version of the license. |
|
17 | 17 | # |
|
18 | 18 | # This program is distributed in the hope that it will be useful, |
|
19 | 19 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
20 | 20 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
21 | 21 | # GNU General Public License for more details. |
|
22 | 22 | # |
|
23 | 23 | # You should have received a copy of the GNU General Public License |
|
24 | 24 | # along with this program; if not, write to the Free Software |
|
25 | 25 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
26 | 26 | # MA 02110-1301, USA. |
|
27 | 27 | import logging |
|
28 | 28 | import datetime |
|
29 | 29 | from datetime import date |
|
30 | 30 | |
|
31 | 31 | from sqlalchemy import * |
|
32 | 32 | from sqlalchemy.exc import DatabaseError |
|
33 |
from sqlalchemy.orm import relationship, backref |
|
|
33 | from sqlalchemy.orm import relationship, backref | |
|
34 | 34 | from sqlalchemy.orm.session import Session |
|
35 | 35 | |
|
36 | 36 | from rhodecode.model.meta import Base |
|
37 | 37 | |
|
38 | 38 | log = logging.getLogger(__name__) |
|
39 | 39 | |
|
40 | class BaseModel(object): | |
|
41 | ||
|
42 | @classmethod | |
|
43 | def _get_keys(cls): | |
|
44 | """return column names for this model """ | |
|
45 | return class_mapper(cls).c.keys() | |
|
46 | ||
|
47 | def get_dict(self): | |
|
48 | """return dict with keys and values corresponding | |
|
49 | to this model data """ | |
|
50 | ||
|
51 | d = {} | |
|
52 | for k in self._get_keys(): | |
|
53 | d[k] = getattr(self, k) | |
|
54 | return d | |
|
55 | ||
|
56 | def get_appstruct(self): | |
|
57 | """return list with keys and values tupples corresponding | |
|
58 | to this model data """ | |
|
59 | ||
|
60 | l = [] | |
|
61 | for k in self._get_keys(): | |
|
62 | l.append((k, getattr(self, k),)) | |
|
63 | return l | |
|
64 | ||
|
65 | def populate_obj(self, populate_dict): | |
|
66 | """populate model with data from given populate_dict""" | |
|
67 | ||
|
68 | for k in self._get_keys(): | |
|
69 | if k in populate_dict: | |
|
70 | setattr(self, k, populate_dict[k]) | |
|
71 | ||
|
72 | class RhodeCodeSettings(Base, BaseModel): | |
|
40 | class RhodeCodeSettings(Base): | |
|
73 | 41 | __tablename__ = 'rhodecode_settings' |
|
74 | 42 | __table_args__ = (UniqueConstraint('app_settings_name'), {'useexisting':True}) |
|
75 | 43 | app_settings_id = Column("app_settings_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
76 | 44 | app_settings_name = Column("app_settings_name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
77 | 45 | app_settings_value = Column("app_settings_value", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
78 | 46 | |
|
79 | 47 | def __init__(self, k='', v=''): |
|
80 | 48 | self.app_settings_name = k |
|
81 | 49 | self.app_settings_value = v |
|
82 | 50 | |
|
83 | 51 | def __repr__(self): |
|
84 | 52 | return "<%s('%s:%s')>" % (self.__class__.__name__, |
|
85 | 53 | self.app_settings_name, self.app_settings_value) |
|
86 | 54 | |
|
87 |
class RhodeCodeUi(Base |
|
|
55 | class RhodeCodeUi(Base): | |
|
88 | 56 | __tablename__ = 'rhodecode_ui' |
|
89 | 57 | __table_args__ = {'useexisting':True} |
|
90 | 58 | ui_id = Column("ui_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
91 | 59 | ui_section = Column("ui_section", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
92 | 60 | ui_key = Column("ui_key", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
93 | 61 | ui_value = Column("ui_value", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
94 | 62 | ui_active = Column("ui_active", Boolean(), nullable=True, unique=None, default=True) |
|
95 | 63 | |
|
96 | 64 | |
|
97 |
class User(Base |
|
|
65 | class User(Base): | |
|
98 | 66 | __tablename__ = 'users' |
|
99 | 67 | __table_args__ = (UniqueConstraint('username'), UniqueConstraint('email'), {'useexisting':True}) |
|
100 | 68 | user_id = Column("user_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
101 | 69 | username = Column("username", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
102 | 70 | password = Column("password", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
103 | 71 | active = Column("active", Boolean(), nullable=True, unique=None, default=None) |
|
104 | 72 | admin = Column("admin", Boolean(), nullable=True, unique=None, default=False) |
|
105 | 73 | name = Column("name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
106 | 74 | lastname = Column("lastname", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
107 | 75 | email = Column("email", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
108 | 76 | last_login = Column("last_login", DateTime(timezone=False), nullable=True, unique=None, default=None) |
|
109 | 77 | ldap_dn = Column("ldap_dn", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
110 | 78 | |
|
111 | 79 | user_log = relationship('UserLog', cascade='all') |
|
112 | 80 | user_perms = relationship('UserToPerm', primaryjoin="User.user_id==UserToPerm.user_id", cascade='all') |
|
113 | 81 | |
|
114 | 82 | repositories = relationship('Repository') |
|
115 | 83 | user_followers = relationship('UserFollowing', primaryjoin='UserFollowing.follows_user_id==User.user_id', cascade='all') |
|
116 | 84 | |
|
117 | 85 | group_member = relationship('UsersGroupMember', cascade='all') |
|
86 | ||
|
118 | 87 | @property |
|
119 | 88 | def full_contact(self): |
|
120 | 89 | return '%s %s <%s>' % (self.name, self.lastname, self.email) |
|
121 | 90 | |
|
122 | ||
|
123 | 91 | @property |
|
124 | 92 | def is_admin(self): |
|
125 | 93 | return self.admin |
|
126 | 94 | |
|
127 | 95 | def __repr__(self): |
|
128 | 96 | return "<%s('id:%s:%s')>" % (self.__class__.__name__, |
|
129 | 97 | self.user_id, self.username) |
|
130 | 98 | |
|
99 | @classmethod | |
|
100 | def by_username(cls, username): | |
|
101 | return Session.query(cls).filter(cls.username == username).one() | |
|
102 | ||
|
103 | ||
|
131 | 104 | def update_lastlogin(self): |
|
132 | 105 | """Update user lastlogin""" |
|
133 | 106 | |
|
134 | 107 | try: |
|
135 | 108 | session = Session.object_session(self) |
|
136 | 109 | self.last_login = datetime.datetime.now() |
|
137 | 110 | session.add(self) |
|
138 | 111 | session.commit() |
|
139 | 112 | log.debug('updated user %s lastlogin', self.username) |
|
140 | 113 | except (DatabaseError,): |
|
141 | 114 | session.rollback() |
|
142 | 115 | |
|
143 | 116 | |
|
144 |
class UserLog(Base |
|
|
117 | class UserLog(Base): | |
|
145 | 118 | __tablename__ = 'user_logs' |
|
146 | 119 | __table_args__ = {'useexisting':True} |
|
147 | 120 | user_log_id = Column("user_log_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
148 | 121 | user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None) |
|
149 | 122 | repository_id = Column("repository_id", Integer(length=None, convert_unicode=False, assert_unicode=None), ForeignKey('repositories.repo_id'), nullable=False, unique=None, default=None) |
|
150 | 123 | repository_name = Column("repository_name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
151 | 124 | user_ip = Column("user_ip", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
152 | 125 | action = Column("action", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
153 | 126 | action_date = Column("action_date", DateTime(timezone=False), nullable=True, unique=None, default=None) |
|
154 | 127 | |
|
155 | 128 | @property |
|
156 | 129 | def action_as_day(self): |
|
157 | 130 | return date(*self.action_date.timetuple()[:3]) |
|
158 | 131 | |
|
159 | 132 | user = relationship('User') |
|
160 | 133 | repository = relationship('Repository') |
|
161 | 134 | |
|
162 | 135 | |
|
163 |
class UsersGroup(Base |
|
|
136 | class UsersGroup(Base): | |
|
164 | 137 | __tablename__ = 'users_groups' |
|
165 | 138 | __table_args__ = {'useexisting':True} |
|
166 | 139 | |
|
167 | 140 | users_group_id = Column("users_group_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
168 | 141 | users_group_name = Column("users_group_name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=False, unique=True, default=None) |
|
169 | 142 | users_group_active = Column("users_group_active", Boolean(), nullable=True, unique=None, default=None) |
|
170 | 143 | |
|
171 | 144 | members = relationship('UsersGroupMember', cascade="all, delete, delete-orphan", lazy="joined") |
|
172 | 145 | |
|
173 |
class UsersGroupMember(Base |
|
|
146 | class UsersGroupMember(Base): | |
|
174 | 147 | __tablename__ = 'users_groups_members' |
|
175 | 148 | __table_args__ = {'useexisting':True} |
|
176 | 149 | |
|
177 | 150 | users_group_member_id = Column("users_group_member_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
178 | 151 | users_group_id = Column("users_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None) |
|
179 | 152 | user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None) |
|
180 | 153 | |
|
181 | 154 | user = relationship('User', lazy='joined') |
|
182 | 155 | users_group = relationship('UsersGroup') |
|
183 | 156 | |
|
184 | 157 | def __init__(self, gr_id='', u_id=''): |
|
185 | 158 | self.users_group_id = gr_id |
|
186 | 159 | self.user_id = u_id |
|
187 | 160 | |
|
188 |
class Repository(Base |
|
|
161 | class Repository(Base): | |
|
189 | 162 | __tablename__ = 'repositories' |
|
190 | 163 | __table_args__ = (UniqueConstraint('repo_name'), {'useexisting':True},) |
|
191 | 164 | repo_id = Column("repo_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
192 | 165 | repo_name = Column("repo_name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=False, unique=True, default=None) |
|
193 | 166 | repo_type = Column("repo_type", String(length=None, convert_unicode=False, assert_unicode=None), nullable=False, unique=False, default='hg') |
|
194 | 167 | user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=False, default=None) |
|
195 | 168 | private = Column("private", Boolean(), nullable=True, unique=None, default=None) |
|
196 | 169 | enable_statistics = Column("statistics", Boolean(), nullable=True, unique=None, default=True) |
|
197 | 170 | enable_downloads = Column("downloads", Boolean(), nullable=True, unique=None, default=True) |
|
198 | 171 | description = Column("description", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
199 | 172 | fork_id = Column("fork_id", Integer(), ForeignKey('repositories.repo_id'), nullable=True, unique=False, default=None) |
|
200 | 173 | group_id = Column("group_id", Integer(), ForeignKey('groups.group_id'), nullable=True, unique=False, default=None) |
|
201 | 174 | |
|
202 | 175 | user = relationship('User') |
|
203 | 176 | fork = relationship('Repository', remote_side=repo_id) |
|
204 | 177 | group = relationship('Group') |
|
205 | 178 | repo_to_perm = relationship('RepoToPerm', cascade='all', order_by='RepoToPerm.repo_to_perm_id') |
|
206 | 179 | users_group_to_perm = relationship('UsersGroupToPerm', cascade='all') |
|
207 | 180 | stats = relationship('Statistics', cascade='all', uselist=False) |
|
208 | 181 | |
|
209 | 182 | followers = relationship('UserFollowing', primaryjoin='UserFollowing.follows_repo_id==Repository.repo_id', cascade='all') |
|
210 | 183 | |
|
211 | 184 | logs = relationship('UserLog', cascade='all') |
|
212 | 185 | |
|
213 | 186 | def __repr__(self): |
|
214 | 187 | return "<%s('%s:%s')>" % (self.__class__.__name__, |
|
215 | 188 | self.repo_id, self.repo_name) |
|
216 | 189 | |
|
217 | class Group(Base, BaseModel): | |
|
190 | @classmethod | |
|
191 | def by_repo_name(cls, repo_name): | |
|
192 | return Session.query(cls).filter(cls.repo_name == repo_name).one() | |
|
193 | ||
|
194 | ||
|
195 | class Group(Base): | |
|
218 | 196 | __tablename__ = 'groups' |
|
219 | 197 | __table_args__ = (UniqueConstraint('group_name'), {'useexisting':True},) |
|
220 | 198 | |
|
221 | 199 | group_id = Column("group_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
222 | 200 | group_name = Column("group_name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=False, unique=True, default=None) |
|
223 | 201 | group_parent_id = Column("group_parent_id", Integer(), ForeignKey('groups.group_id'), nullable=True, unique=None, default=None) |
|
224 | 202 | |
|
225 | 203 | parent_group = relationship('Group', remote_side=group_id) |
|
226 | 204 | |
|
227 | 205 | |
|
228 | 206 | def __init__(self, group_name='', parent_group=None): |
|
229 | 207 | self.group_name = group_name |
|
230 | 208 | self.parent_group = parent_group |
|
231 | 209 | |
|
232 | 210 | def __repr__(self): |
|
233 | 211 | return "<%s('%s:%s')>" % (self.__class__.__name__, self.group_id, |
|
234 | 212 | self.group_name) |
|
235 | 213 | |
|
236 |
class Permission(Base |
|
|
214 | class Permission(Base): | |
|
237 | 215 | __tablename__ = 'permissions' |
|
238 | 216 | __table_args__ = {'useexisting':True} |
|
239 | 217 | permission_id = Column("permission_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
240 | 218 | permission_name = Column("permission_name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
241 | 219 | permission_longname = Column("permission_longname", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
242 | 220 | |
|
243 | 221 | def __repr__(self): |
|
244 | 222 | return "<%s('%s:%s')>" % (self.__class__.__name__, |
|
245 | 223 | self.permission_id, self.permission_name) |
|
246 | 224 | |
|
247 |
class RepoToPerm(Base |
|
|
225 | class RepoToPerm(Base): | |
|
248 | 226 | __tablename__ = 'repo_to_perm' |
|
249 | 227 | __table_args__ = (UniqueConstraint('user_id', 'repository_id'), {'useexisting':True}) |
|
250 | 228 | repo_to_perm_id = Column("repo_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
251 | 229 | user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None) |
|
252 | 230 | permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None) |
|
253 | 231 | repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=None, default=None) |
|
254 | 232 | |
|
255 | 233 | user = relationship('User') |
|
256 | 234 | permission = relationship('Permission') |
|
257 | 235 | repository = relationship('Repository') |
|
258 | 236 | |
|
259 |
class UserToPerm(Base |
|
|
237 | class UserToPerm(Base): | |
|
260 | 238 | __tablename__ = 'user_to_perm' |
|
261 | 239 | __table_args__ = (UniqueConstraint('user_id', 'permission_id'), {'useexisting':True}) |
|
262 | 240 | user_to_perm_id = Column("user_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
263 | 241 | user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None) |
|
264 | 242 | permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None) |
|
265 | 243 | |
|
266 | 244 | user = relationship('User') |
|
267 | 245 | permission = relationship('Permission') |
|
268 | 246 | |
|
269 | 247 | |
|
270 |
class UsersGroupToPerm(Base |
|
|
248 | class UsersGroupToPerm(Base): | |
|
271 | 249 | __tablename__ = 'users_group_to_perm' |
|
272 | 250 | __table_args__ = (UniqueConstraint('users_group_id', 'permission_id'), {'useexisting':True}) |
|
273 | 251 | users_group_to_perm_id = Column("users_group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
274 | 252 | users_group_id = Column("users_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None) |
|
275 | 253 | permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None) |
|
276 | 254 | repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=None, default=None) |
|
277 | 255 | |
|
278 | 256 | users_group = relationship('UsersGroup') |
|
279 | 257 | permission = relationship('Permission') |
|
280 | 258 | repository = relationship('Repository') |
|
281 | 259 | |
|
282 |
class GroupToPerm(Base |
|
|
260 | class GroupToPerm(Base): | |
|
283 | 261 | __tablename__ = 'group_to_perm' |
|
284 | 262 | __table_args__ = (UniqueConstraint('group_id', 'permission_id'), {'useexisting':True}) |
|
285 | 263 | |
|
286 | 264 | group_to_perm_id = Column("group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
287 | 265 | user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None) |
|
288 | 266 | permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None) |
|
289 | 267 | group_id = Column("group_id", Integer(), ForeignKey('groups.group_id'), nullable=False, unique=None, default=None) |
|
290 | 268 | |
|
291 | 269 | user = relationship('User') |
|
292 | 270 | permission = relationship('Permission') |
|
293 | 271 | group = relationship('Group') |
|
294 | 272 | |
|
295 |
class Statistics(Base |
|
|
273 | class Statistics(Base): | |
|
296 | 274 | __tablename__ = 'statistics' |
|
297 | 275 | __table_args__ = (UniqueConstraint('repository_id'), {'useexisting':True}) |
|
298 | 276 | stat_id = Column("stat_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
299 | 277 | repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=True, default=None) |
|
300 | 278 | stat_on_revision = Column("stat_on_revision", Integer(), nullable=False) |
|
301 | 279 | commit_activity = Column("commit_activity", LargeBinary(), nullable=False)#JSON data |
|
302 | 280 | commit_activity_combined = Column("commit_activity_combined", LargeBinary(), nullable=False)#JSON data |
|
303 | 281 | languages = Column("languages", LargeBinary(), nullable=False)#JSON data |
|
304 | 282 | |
|
305 | 283 | repository = relationship('Repository', single_parent=True) |
|
306 | 284 | |
|
307 |
class UserFollowing(Base |
|
|
285 | class UserFollowing(Base): | |
|
308 | 286 | __tablename__ = 'user_followings' |
|
309 | 287 | __table_args__ = (UniqueConstraint('user_id', 'follows_repository_id'), |
|
310 | 288 | UniqueConstraint('user_id', 'follows_user_id') |
|
311 | 289 | , {'useexisting':True}) |
|
312 | 290 | |
|
313 | 291 | user_following_id = Column("user_following_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
314 | 292 | user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None) |
|
315 | 293 | follows_repo_id = Column("follows_repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=True, unique=None, default=None) |
|
316 | 294 | follows_user_id = Column("follows_user_id", Integer(), ForeignKey('users.user_id'), nullable=True, unique=None, default=None) |
|
317 | 295 | |
|
318 | 296 | user = relationship('User', primaryjoin='User.user_id==UserFollowing.user_id') |
|
319 | 297 | |
|
320 | 298 | follows_user = relationship('User', primaryjoin='User.user_id==UserFollowing.follows_user_id') |
|
321 | 299 | follows_repository = relationship('Repository', order_by='Repository.repo_name') |
|
322 | 300 | |
|
323 |
class CacheInvalidation(Base |
|
|
301 | class CacheInvalidation(Base): | |
|
324 | 302 | __tablename__ = 'cache_invalidation' |
|
325 | 303 | __table_args__ = (UniqueConstraint('cache_key'), {'useexisting':True}) |
|
326 | 304 | cache_id = Column("cache_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
327 | 305 | cache_key = Column("cache_key", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
328 | 306 | cache_args = Column("cache_args", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
329 | 307 | cache_active = Column("cache_active", Boolean(), nullable=True, unique=None, default=False) |
|
330 | 308 | |
|
331 | 309 | |
|
332 | 310 | def __init__(self, cache_key, cache_args=''): |
|
333 | 311 | self.cache_key = cache_key |
|
334 | 312 | self.cache_args = cache_args |
|
335 | 313 | self.cache_active = False |
|
336 | 314 | |
|
337 | 315 | def __repr__(self): |
|
338 | 316 | return "<%s('%s:%s')>" % (self.__class__.__name__, |
|
339 | 317 | self.cache_id, self.cache_key) |
|
340 | 318 | |
|
341 |
class DbMigrateVersion(Base |
|
|
319 | class DbMigrateVersion(Base): | |
|
342 | 320 | __tablename__ = 'db_migrate_version' |
|
343 | 321 | __table_args__ = {'useexisting':True} |
|
344 | 322 | repository_id = Column('repository_id', String(250), primary_key=True) |
|
345 | 323 | repository_path = Column('repository_path', Text) |
|
346 | 324 | version = Column('version', Integer) |
|
347 | 325 |
@@ -1,26 +1,70 b'' | |||
|
1 | 1 | """SQLAlchemy Metadata and Session object""" |
|
2 | 2 | from sqlalchemy.ext.declarative import declarative_base |
|
3 | from sqlalchemy.orm import scoped_session, sessionmaker | |
|
3 | from sqlalchemy.orm import scoped_session, sessionmaker, class_mapper | |
|
4 | from beaker import cache | |
|
5 | ||
|
4 | 6 | from rhodecode.model import caching_query |
|
5 | from beaker import cache | |
|
7 | ||
|
6 | 8 | |
|
7 | 9 | # Beaker CacheManager. A home base for cache configurations. |
|
8 | 10 | cache_manager = cache.CacheManager() |
|
9 | 11 | |
|
10 | 12 | __all__ = ['Base', 'Session'] |
|
11 | 13 | # |
|
12 | 14 | # SQLAlchemy session manager. Updated by model.init_model() |
|
13 | 15 | # |
|
14 | 16 | Session = scoped_session( |
|
15 | 17 | sessionmaker( |
|
16 | 18 | query_cls=caching_query.query_callable(cache_manager) |
|
17 | 19 | ) |
|
18 | 20 | ) |
|
19 | 21 | |
|
22 | class BaseModel(object): | |
|
23 | """Base Model for all classess | |
|
24 | ||
|
25 | """ | |
|
26 | ||
|
27 | @classmethod | |
|
28 | def _get_keys(cls): | |
|
29 | """return column names for this model """ | |
|
30 | return class_mapper(cls).c.keys() | |
|
31 | ||
|
32 | def get_dict(self): | |
|
33 | """return dict with keys and values corresponding | |
|
34 | to this model data """ | |
|
35 | ||
|
36 | d = {} | |
|
37 | for k in self._get_keys(): | |
|
38 | d[k] = getattr(self, k) | |
|
39 | return d | |
|
40 | ||
|
41 | def get_appstruct(self): | |
|
42 | """return list with keys and values tupples corresponding | |
|
43 | to this model data """ | |
|
44 | ||
|
45 | l = [] | |
|
46 | for k in self._get_keys(): | |
|
47 | l.append((k, getattr(self, k),)) | |
|
48 | return l | |
|
49 | ||
|
50 | def populate_obj(self, populate_dict): | |
|
51 | """populate model with data from given populate_dict""" | |
|
52 | ||
|
53 | for k in self._get_keys(): | |
|
54 | if k in populate_dict: | |
|
55 | setattr(self, k, populate_dict[k]) | |
|
56 | ||
|
57 | @classmethod | |
|
58 | def query(cls): | |
|
59 | return Session.query(cls) | |
|
60 | ||
|
61 | @classmethod | |
|
62 | def get(cls, id_): | |
|
63 | return Session.query(cls).get(id_) | |
|
64 | ||
|
65 | ||
|
20 | 66 | # The declarative Base |
|
21 | Base = declarative_base() | |
|
22 | #For another db... | |
|
23 | #Base2 = declarative_base() | |
|
67 | Base = declarative_base(cls=BaseModel) | |
|
24 | 68 | |
|
25 | 69 | #to use cache use this in query |
|
26 | 70 | #.options(FromCache("sqlalchemy_cache_type", "cachekey")) |
General Comments 0
You need to be logged in to leave comments.
Login now