Show More
@@ -1,300 +1,293 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-2010 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 | |
|
30 | 30 | from sqlalchemy import * |
|
31 | 31 | from sqlalchemy.exc import DatabaseError |
|
32 | 32 | from sqlalchemy.orm import relation, backref, class_mapper |
|
33 | 33 | from sqlalchemy.orm.session import Session |
|
34 | 34 | |
|
35 | 35 | from rhodecode.model.meta import Base |
|
36 | 36 | |
|
37 | 37 | log = logging.getLogger(__name__) |
|
38 | 38 | |
|
39 | 39 | class BaseModel(object): |
|
40 | 40 | |
|
41 | 41 | @classmethod |
|
42 | 42 | def _get_keys(cls): |
|
43 | 43 | """return column names for this model """ |
|
44 | 44 | return class_mapper(cls).c.keys() |
|
45 | 45 | |
|
46 | 46 | def get_dict(self): |
|
47 | 47 | """return dict with keys and values corresponding |
|
48 | 48 | to this model data """ |
|
49 | 49 | |
|
50 | 50 | d = {} |
|
51 | 51 | for k in self._get_keys(): |
|
52 | 52 | d[k] = getattr(self, k) |
|
53 | 53 | return d |
|
54 | 54 | |
|
55 | 55 | def get_appstruct(self): |
|
56 | 56 | """return list with keys and values tupples corresponding |
|
57 | 57 | to this model data """ |
|
58 | 58 | |
|
59 | 59 | l = [] |
|
60 | 60 | for k in self._get_keys(): |
|
61 | 61 | l.append((k, getattr(self, k),)) |
|
62 | 62 | return l |
|
63 | 63 | |
|
64 | 64 | def populate_obj(self, populate_dict): |
|
65 | 65 | """populate model with data from given populate_dict""" |
|
66 | 66 | |
|
67 | 67 | for k in self._get_keys(): |
|
68 | 68 | if k in populate_dict: |
|
69 | 69 | setattr(self, k, populate_dict[k]) |
|
70 | 70 | |
|
71 | 71 | class RhodeCodeSettings(Base, BaseModel): |
|
72 | 72 | __tablename__ = 'rhodecode_settings' |
|
73 | 73 | __table_args__ = (UniqueConstraint('app_settings_name'), {'useexisting':True}) |
|
74 | 74 | app_settings_id = Column("app_settings_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
75 | 75 | app_settings_name = Column("app_settings_name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
76 | 76 | app_settings_value = Column("app_settings_value", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
77 | 77 | |
|
78 | 78 | def __init__(self, k, v): |
|
79 | 79 | self.app_settings_name = k |
|
80 | 80 | self.app_settings_value = v |
|
81 | 81 | |
|
82 | 82 | def __repr__(self): |
|
83 | 83 | return "<%s('%s:%s')>" % (self.__class__.__name__, |
|
84 | 84 | self.app_settings_name, self.app_settings_value) |
|
85 | 85 | |
|
86 | 86 | class RhodeCodeUi(Base, BaseModel): |
|
87 | 87 | __tablename__ = 'rhodecode_ui' |
|
88 | 88 | __table_args__ = {'useexisting':True} |
|
89 | 89 | ui_id = Column("ui_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
90 | 90 | ui_section = Column("ui_section", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
91 | 91 | ui_key = Column("ui_key", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
92 | 92 | ui_value = Column("ui_value", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
93 | 93 | ui_active = Column("ui_active", Boolean(), nullable=True, unique=None, default=True) |
|
94 | 94 | |
|
95 | 95 | |
|
96 | 96 | class User(Base, BaseModel): |
|
97 | 97 | __tablename__ = 'users' |
|
98 | 98 | __table_args__ = (UniqueConstraint('username'), UniqueConstraint('email'), {'useexisting':True}) |
|
99 | 99 | user_id = Column("user_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
100 | 100 | username = Column("username", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
101 | 101 | password = Column("password", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
102 | 102 | active = Column("active", Boolean(), nullable=True, unique=None, default=None) |
|
103 | 103 | admin = Column("admin", Boolean(), nullable=True, unique=None, default=False) |
|
104 | 104 | name = Column("name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
105 | 105 | lastname = Column("lastname", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
106 | 106 | email = Column("email", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
107 | 107 | last_login = Column("last_login", DateTime(timezone=False), nullable=True, unique=None, default=None) |
|
108 | 108 | is_ldap = Column("is_ldap", Boolean(), nullable=False, unique=None, default=False) |
|
109 | 109 | |
|
110 | 110 | user_log = relation('UserLog', cascade='all') |
|
111 | 111 | user_perms = relation('UserToPerm', primaryjoin="User.user_id==UserToPerm.user_id", cascade='all') |
|
112 | 112 | |
|
113 | 113 | repositories = relation('Repository') |
|
114 | 114 | user_followers = relation('UserFollowing', primaryjoin='UserFollowing.follows_user_id==User.user_id', cascade='all') |
|
115 | 115 | |
|
116 | 116 | @property |
|
117 | 117 | def full_contact(self): |
|
118 | 118 | return '%s %s <%s>' % (self.name, self.lastname, self.email) |
|
119 | 119 | |
|
120 | 120 | def __repr__(self): |
|
121 | 121 | return "<%s('id:%s:%s')>" % (self.__class__.__name__, |
|
122 | 122 | self.user_id, self.username) |
|
123 | 123 | |
|
124 | 124 | def update_lastlogin(self): |
|
125 | 125 | """Update user lastlogin""" |
|
126 | 126 | |
|
127 | 127 | try: |
|
128 | 128 | session = Session.object_session(self) |
|
129 | 129 | self.last_login = datetime.datetime.now() |
|
130 | 130 | session.add(self) |
|
131 | 131 | session.commit() |
|
132 | 132 | log.debug('updated user %s lastlogin', self.username) |
|
133 | 133 | except (DatabaseError,): |
|
134 | 134 | session.rollback() |
|
135 | 135 | |
|
136 | 136 | |
|
137 | 137 | class UserLog(Base, BaseModel): |
|
138 | 138 | __tablename__ = 'user_logs' |
|
139 | 139 | __table_args__ = {'useexisting':True} |
|
140 | 140 | user_log_id = Column("user_log_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
141 |
user_id = Column("user_id", Integer(), ForeignKey( |
|
|
142 |
repository_id = Column("repository_id", Integer(length=None, convert_unicode=False, assert_unicode=None), ForeignKey( |
|
|
141 | user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None) | |
|
142 | repository_id = Column("repository_id", Integer(length=None, convert_unicode=False, assert_unicode=None), ForeignKey('repositories.repo_id'), nullable=False, unique=None, default=None) | |
|
143 | 143 | repository_name = Column("repository_name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
144 | 144 | user_ip = Column("user_ip", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
145 | 145 | action = Column("action", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
146 | 146 | action_date = Column("action_date", DateTime(timezone=False), nullable=True, unique=None, default=None) |
|
147 | 147 | |
|
148 | 148 | user = relation('User') |
|
149 | 149 | repository = relation('Repository') |
|
150 | 150 | |
|
151 | 151 | class Repository(Base, BaseModel): |
|
152 | 152 | __tablename__ = 'repositories' |
|
153 | 153 | __table_args__ = (UniqueConstraint('repo_name'), {'useexisting':True},) |
|
154 | 154 | repo_id = Column("repo_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
155 | 155 | repo_name = Column("repo_name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=False, unique=True, default=None) |
|
156 | 156 | repo_type = Column("repo_type", String(length=None, convert_unicode=False, assert_unicode=None), nullable=False, unique=False, default='hg') |
|
157 |
user_id = Column("user_id", Integer(), ForeignKey( |
|
|
157 | user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=False, default=None) | |
|
158 | 158 | private = Column("private", Boolean(), nullable=True, unique=None, default=None) |
|
159 | 159 | enable_statistics = Column("statistics", Boolean(), nullable=True, unique=None, default=True) |
|
160 | 160 | description = Column("description", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
161 |
fork_id = Column("fork_id", Integer(), ForeignKey( |
|
|
162 |
group_id = Column("group_id", Integer(), ForeignKey( |
|
|
161 | fork_id = Column("fork_id", Integer(), ForeignKey('repositories.repo_id'), nullable=True, unique=False, default=None) | |
|
162 | group_id = Column("group_id", Integer(), ForeignKey('groups.group_id'), nullable=True, unique=False, default=None) | |
|
163 | 163 | |
|
164 | 164 | user = relation('User') |
|
165 | 165 | fork = relation('Repository', remote_side=repo_id) |
|
166 | 166 | group = relation('Group') |
|
167 | 167 | repo_to_perm = relation('RepoToPerm', cascade='all') |
|
168 | 168 | stats = relation('Statistics', cascade='all', uselist=False) |
|
169 | 169 | |
|
170 | 170 | repo_followers = relation('UserFollowing', primaryjoin='UserFollowing.follows_repo_id==Repository.repo_id', cascade='all') |
|
171 | 171 | |
|
172 | ||
|
173 | 172 | def __repr__(self): |
|
174 | 173 | return "<%s('%s:%s')>" % (self.__class__.__name__, |
|
175 |
|
|
|
174 | self.repo_id, self.repo_name) | |
|
176 | 175 | |
|
177 | 176 | class Group(Base, BaseModel): |
|
178 | 177 | __tablename__ = 'groups' |
|
179 | 178 | __table_args__ = (UniqueConstraint('group_name'), {'useexisting':True},) |
|
180 | 179 | |
|
181 | 180 | group_id = Column("group_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
182 | 181 | group_name = Column("group_name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=False, unique=True, default=None) |
|
183 |
group_parent_id = Column("group_parent_id", Integer(), ForeignKey( |
|
|
182 | group_parent_id = Column("group_parent_id", Integer(), ForeignKey('groups.group_id'), nullable=True, unique=None, default=None) | |
|
184 | 183 | |
|
185 | 184 | parent_group = relation('Group', remote_side=group_id) |
|
186 | 185 | |
|
187 | 186 | |
|
188 | 187 | def __init__(self, group_name='', parent_group=None): |
|
189 | 188 | self.group_name = group_name |
|
190 | 189 | self.parent_group = parent_group |
|
191 | 190 | |
|
192 | 191 | def __repr__(self): |
|
193 | 192 | return "<%s('%s:%s')>" % (self.__class__.__name__, self.group_id, |
|
194 | 193 | self.group_name) |
|
195 | 194 | |
|
196 | 195 | class Permission(Base, BaseModel): |
|
197 | 196 | __tablename__ = 'permissions' |
|
198 | 197 | __table_args__ = {'useexisting':True} |
|
199 | 198 | permission_id = Column("permission_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
200 | 199 | permission_name = Column("permission_name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
201 | 200 | permission_longname = Column("permission_longname", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
202 | 201 | |
|
203 | 202 | def __repr__(self): |
|
204 | 203 | return "<%s('%s:%s')>" % (self.__class__.__name__, |
|
205 |
|
|
|
204 | self.permission_id, self.permission_name) | |
|
206 | 205 | |
|
207 | 206 | class RepoToPerm(Base, BaseModel): |
|
208 | 207 | __tablename__ = 'repo_to_perm' |
|
209 | 208 | __table_args__ = (UniqueConstraint('user_id', 'repository_id'), {'useexisting':True}) |
|
210 | 209 | repo_to_perm_id = Column("repo_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
211 |
user_id = Column("user_id", Integer(), ForeignKey( |
|
|
212 |
permission_id = Column("permission_id", Integer(), ForeignKey( |
|
|
213 |
repository_id = Column("repository_id", Integer(), ForeignKey( |
|
|
210 | user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None) | |
|
211 | permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None) | |
|
212 | repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=None, default=None) | |
|
214 | 213 | |
|
215 | 214 | user = relation('User') |
|
216 | 215 | permission = relation('Permission') |
|
217 | 216 | repository = relation('Repository') |
|
218 | 217 | |
|
219 | 218 | class UserToPerm(Base, BaseModel): |
|
220 | 219 | __tablename__ = 'user_to_perm' |
|
221 | 220 | __table_args__ = (UniqueConstraint('user_id', 'permission_id'), {'useexisting':True}) |
|
222 | 221 | user_to_perm_id = Column("user_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
223 |
user_id = Column("user_id", Integer(), ForeignKey( |
|
|
224 |
permission_id = Column("permission_id", Integer(), ForeignKey( |
|
|
222 | user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None) | |
|
223 | permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None) | |
|
225 | 224 | |
|
226 | 225 | user = relation('User') |
|
227 | 226 | permission = relation('Permission') |
|
228 | 227 | |
|
229 | 228 | class GroupToPerm(Base, BaseModel): |
|
230 | 229 | __tablename__ = 'group_to_perm' |
|
231 | 230 | __table_args__ = (UniqueConstraint('group_id', 'permission_id'), {'useexisting':True}) |
|
232 | 231 | |
|
233 | 232 | group_to_perm_id = Column("group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
234 |
user_id = Column("user_id", Integer(), ForeignKey( |
|
|
235 |
permission_id = Column("permission_id", Integer(), ForeignKey( |
|
|
236 |
group_id = Column("group_id", Integer(), ForeignKey( |
|
|
233 | user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None) | |
|
234 | permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None) | |
|
235 | group_id = Column("group_id", Integer(), ForeignKey('groups.group_id'), nullable=False, unique=None, default=None) | |
|
237 | 236 | |
|
238 | 237 | user = relation('User') |
|
239 | 238 | permission = relation('Permission') |
|
240 | 239 | group = relation('Group') |
|
241 | 240 | |
|
242 | ||
|
243 | ||
|
244 | ||
|
245 | ||
|
246 | ||
|
247 | 241 | class Statistics(Base, BaseModel): |
|
248 | 242 | __tablename__ = 'statistics' |
|
249 | 243 | __table_args__ = (UniqueConstraint('repository_id'), {'useexisting':True}) |
|
250 | 244 | stat_id = Column("stat_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
251 |
repository_id = Column("repository_id", Integer(), ForeignKey( |
|
|
245 | repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=True, default=None) | |
|
252 | 246 | stat_on_revision = Column("stat_on_revision", Integer(), nullable=False) |
|
253 | 247 | commit_activity = Column("commit_activity", LargeBinary(), nullable=False)#JSON data |
|
254 | 248 | commit_activity_combined = Column("commit_activity_combined", LargeBinary(), nullable=False)#JSON data |
|
255 | 249 | languages = Column("languages", LargeBinary(), nullable=False)#JSON data |
|
256 | 250 | |
|
257 | 251 | repository = relation('Repository', single_parent=True) |
|
258 | 252 | |
|
259 | 253 | class UserFollowing(Base, BaseModel): |
|
260 | 254 | __tablename__ = 'user_followings' |
|
261 | 255 | __table_args__ = (UniqueConstraint('user_id', 'follows_repository_id'), |
|
262 | 256 | UniqueConstraint('user_id', 'follows_user_id') |
|
263 | 257 | , {'useexisting':True}) |
|
264 | 258 | |
|
265 | 259 | user_following_id = Column("user_following_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
266 |
user_id = Column("user_id", Integer(), ForeignKey( |
|
|
267 |
follows_repo_id = Column("follows_repository_id", Integer(), ForeignKey( |
|
|
268 |
follows_user_id = Column("follows_user_id", Integer(), ForeignKey( |
|
|
260 | user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None) | |
|
261 | follows_repo_id = Column("follows_repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=True, unique=None, default=None) | |
|
262 | follows_user_id = Column("follows_user_id", Integer(), ForeignKey('users.user_id'), nullable=True, unique=None, default=None) | |
|
269 | 263 | |
|
270 | 264 | user = relation('User', primaryjoin='User.user_id==UserFollowing.user_id') |
|
271 | 265 | |
|
272 | 266 | follows_user = relation('User', primaryjoin='User.user_id==UserFollowing.follows_user_id') |
|
273 | 267 | follows_repository = relation('Repository') |
|
274 | 268 | |
|
275 | ||
|
276 | 269 | class CacheInvalidation(Base, BaseModel): |
|
277 | 270 | __tablename__ = 'cache_invalidation' |
|
278 | 271 | __table_args__ = (UniqueConstraint('cache_key'), {'useexisting':True}) |
|
279 | 272 | cache_id = Column("cache_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
280 | 273 | cache_key = Column("cache_key", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
281 | 274 | cache_args = Column("cache_args", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
282 | 275 | cache_active = Column("cache_active", Boolean(), nullable=True, unique=None, default=False) |
|
283 | 276 | |
|
284 | 277 | |
|
285 | 278 | def __init__(self, cache_key, cache_args=''): |
|
286 | 279 | self.cache_key = cache_key |
|
287 | 280 | self.cache_args = cache_args |
|
288 | 281 | self.cache_active = False |
|
289 | 282 | |
|
290 | 283 | def __repr__(self): |
|
291 | 284 | return "<%s('%s:%s')>" % (self.__class__.__name__, |
|
292 | 285 | self.cache_id, self.cache_key) |
|
293 | 286 | |
|
294 | 287 | class DbMigrateVersion(Base, BaseModel): |
|
295 | 288 | __tablename__ = 'db_migrate_version' |
|
296 | 289 | __table_args__ = {'useexisting':True} |
|
297 | 290 | repository_id = Column('repository_id', String(250), primary_key=True) |
|
298 | 291 | repository_path = Column('repository_path', Text) |
|
299 | 292 | version = Column('version', Integer) |
|
300 | 293 |
General Comments 0
You need to be logged in to leave comments.
Login now