##// END OF EJS Templates
fixed Session problems in model class functions...
marcink -
r1081:70bf0be5 beta
parent child Browse files
Show More
@@ -1,325 +1,336 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 33 from sqlalchemy.orm import relationship, backref
34 from sqlalchemy.orm.session import Session
34 from sqlalchemy.orm.interfaces import MapperExtension
35 35
36 from rhodecode.model.meta import Base
36 from rhodecode.model.meta import Base, Session
37 37
38 38 log = logging.getLogger(__name__)
39 39
40 #==============================================================================
41 # MAPPER EXTENSIONS
42 #==============================================================================
43
44 class RepositoryMapper(MapperExtension):
45 def after_update(self, mapper, connection, instance):
46 pass
47
48
40 49 class RhodeCodeSettings(Base):
41 50 __tablename__ = 'rhodecode_settings'
42 51 __table_args__ = (UniqueConstraint('app_settings_name'), {'useexisting':True})
43 52 app_settings_id = Column("app_settings_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
44 53 app_settings_name = Column("app_settings_name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
45 54 app_settings_value = Column("app_settings_value", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
46 55
47 56 def __init__(self, k='', v=''):
48 57 self.app_settings_name = k
49 58 self.app_settings_value = v
50 59
51 60 def __repr__(self):
52 61 return "<%s('%s:%s')>" % (self.__class__.__name__,
53 62 self.app_settings_name, self.app_settings_value)
54 63
55 64 class RhodeCodeUi(Base):
56 65 __tablename__ = 'rhodecode_ui'
57 66 __table_args__ = {'useexisting':True}
58 67 ui_id = Column("ui_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
59 68 ui_section = Column("ui_section", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
60 69 ui_key = Column("ui_key", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
61 70 ui_value = Column("ui_value", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
62 71 ui_active = Column("ui_active", Boolean(), nullable=True, unique=None, default=True)
63 72
64 73
65 74 class User(Base):
66 75 __tablename__ = 'users'
67 76 __table_args__ = (UniqueConstraint('username'), UniqueConstraint('email'), {'useexisting':True})
68 77 user_id = Column("user_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
69 78 username = Column("username", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
70 79 password = Column("password", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
71 80 active = Column("active", Boolean(), nullable=True, unique=None, default=None)
72 81 admin = Column("admin", Boolean(), nullable=True, unique=None, default=False)
73 82 name = Column("name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
74 83 lastname = Column("lastname", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
75 84 email = Column("email", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
76 85 last_login = Column("last_login", DateTime(timezone=False), nullable=True, unique=None, default=None)
77 86 ldap_dn = Column("ldap_dn", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
78 87
79 88 user_log = relationship('UserLog', cascade='all')
80 89 user_perms = relationship('UserToPerm', primaryjoin="User.user_id==UserToPerm.user_id", cascade='all')
81 90
82 91 repositories = relationship('Repository')
83 92 user_followers = relationship('UserFollowing', primaryjoin='UserFollowing.follows_user_id==User.user_id', cascade='all')
84 93
85 94 group_member = relationship('UsersGroupMember', cascade='all')
86 95
87 96 @property
88 97 def full_contact(self):
89 98 return '%s %s <%s>' % (self.name, self.lastname, self.email)
90 99
91 100 @property
92 101 def is_admin(self):
93 102 return self.admin
94 103
95 104 def __repr__(self):
96 105 return "<%s('id:%s:%s')>" % (self.__class__.__name__,
97 106 self.user_id, self.username)
98 107
99 108 @classmethod
100 109 def by_username(cls, username):
101 110 return Session.query(cls).filter(cls.username == username).one()
102 111
103 112
104 113 def update_lastlogin(self):
105 114 """Update user lastlogin"""
106 115
107 116 try:
108 117 session = Session.object_session(self)
109 118 self.last_login = datetime.datetime.now()
110 119 session.add(self)
111 120 session.commit()
112 121 log.debug('updated user %s lastlogin', self.username)
113 122 except (DatabaseError,):
114 123 session.rollback()
115 124
116 125
117 126 class UserLog(Base):
118 127 __tablename__ = 'user_logs'
119 128 __table_args__ = {'useexisting':True}
120 129 user_log_id = Column("user_log_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
121 130 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
122 131 repository_id = Column("repository_id", Integer(length=None, convert_unicode=False, assert_unicode=None), ForeignKey('repositories.repo_id'), nullable=False, unique=None, default=None)
123 132 repository_name = Column("repository_name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
124 133 user_ip = Column("user_ip", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
125 134 action = Column("action", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
126 135 action_date = Column("action_date", DateTime(timezone=False), nullable=True, unique=None, default=None)
127 136
128 137 @property
129 138 def action_as_day(self):
130 139 return date(*self.action_date.timetuple()[:3])
131 140
132 141 user = relationship('User')
133 142 repository = relationship('Repository')
134 143
135 144
136 145 class UsersGroup(Base):
137 146 __tablename__ = 'users_groups'
138 147 __table_args__ = {'useexisting':True}
139 148
140 149 users_group_id = Column("users_group_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
141 150 users_group_name = Column("users_group_name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=False, unique=True, default=None)
142 151 users_group_active = Column("users_group_active", Boolean(), nullable=True, unique=None, default=None)
143 152
144 153 members = relationship('UsersGroupMember', cascade="all, delete, delete-orphan", lazy="joined")
145 154
146 155 class UsersGroupMember(Base):
147 156 __tablename__ = 'users_groups_members'
148 157 __table_args__ = {'useexisting':True}
149 158
150 159 users_group_member_id = Column("users_group_member_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
151 160 users_group_id = Column("users_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None)
152 161 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
153 162
154 163 user = relationship('User', lazy='joined')
155 164 users_group = relationship('UsersGroup')
156 165
157 166 def __init__(self, gr_id='', u_id=''):
158 167 self.users_group_id = gr_id
159 168 self.user_id = u_id
160 169
161 170 class Repository(Base):
162 171 __tablename__ = 'repositories'
163 172 __table_args__ = (UniqueConstraint('repo_name'), {'useexisting':True},)
173 __mapper_args__ = {'extension':RepositoryMapper()}
174
164 175 repo_id = Column("repo_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
165 176 repo_name = Column("repo_name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=False, unique=True, default=None)
166 177 repo_type = Column("repo_type", String(length=None, convert_unicode=False, assert_unicode=None), nullable=False, unique=False, default='hg')
167 178 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=False, default=None)
168 179 private = Column("private", Boolean(), nullable=True, unique=None, default=None)
169 180 enable_statistics = Column("statistics", Boolean(), nullable=True, unique=None, default=True)
170 181 enable_downloads = Column("downloads", Boolean(), nullable=True, unique=None, default=True)
171 182 description = Column("description", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
172 183 fork_id = Column("fork_id", Integer(), ForeignKey('repositories.repo_id'), nullable=True, unique=False, default=None)
173 184 group_id = Column("group_id", Integer(), ForeignKey('groups.group_id'), nullable=True, unique=False, default=None)
174 185
175 186 user = relationship('User')
176 187 fork = relationship('Repository', remote_side=repo_id)
177 188 group = relationship('Group')
178 189 repo_to_perm = relationship('RepoToPerm', cascade='all', order_by='RepoToPerm.repo_to_perm_id')
179 190 users_group_to_perm = relationship('UsersGroupToPerm', cascade='all')
180 191 stats = relationship('Statistics', cascade='all', uselist=False)
181 192
182 193 followers = relationship('UserFollowing', primaryjoin='UserFollowing.follows_repo_id==Repository.repo_id', cascade='all')
183 194
184 195 logs = relationship('UserLog', cascade='all')
185 196
186 197 def __repr__(self):
187 198 return "<%s('%s:%s')>" % (self.__class__.__name__,
188 199 self.repo_id, self.repo_name)
189 200
190 201 @classmethod
191 202 def by_repo_name(cls, repo_name):
192 203 return Session.query(cls).filter(cls.repo_name == repo_name).one()
193 204
194 205
195 206 class Group(Base):
196 207 __tablename__ = 'groups'
197 208 __table_args__ = (UniqueConstraint('group_name'), {'useexisting':True},)
198 209
199 210 group_id = Column("group_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
200 211 group_name = Column("group_name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=False, unique=True, default=None)
201 212 group_parent_id = Column("group_parent_id", Integer(), ForeignKey('groups.group_id'), nullable=True, unique=None, default=None)
202 213
203 214 parent_group = relationship('Group', remote_side=group_id)
204 215
205 216
206 217 def __init__(self, group_name='', parent_group=None):
207 218 self.group_name = group_name
208 219 self.parent_group = parent_group
209 220
210 221 def __repr__(self):
211 222 return "<%s('%s:%s')>" % (self.__class__.__name__, self.group_id,
212 223 self.group_name)
213 224
214 225 class Permission(Base):
215 226 __tablename__ = 'permissions'
216 227 __table_args__ = {'useexisting':True}
217 228 permission_id = Column("permission_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
218 229 permission_name = Column("permission_name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
219 230 permission_longname = Column("permission_longname", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
220 231
221 232 def __repr__(self):
222 233 return "<%s('%s:%s')>" % (self.__class__.__name__,
223 234 self.permission_id, self.permission_name)
224 235
225 236 class RepoToPerm(Base):
226 237 __tablename__ = 'repo_to_perm'
227 238 __table_args__ = (UniqueConstraint('user_id', 'repository_id'), {'useexisting':True})
228 239 repo_to_perm_id = Column("repo_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
229 240 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
230 241 permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
231 242 repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=None, default=None)
232 243
233 244 user = relationship('User')
234 245 permission = relationship('Permission')
235 246 repository = relationship('Repository')
236 247
237 248 class UserToPerm(Base):
238 249 __tablename__ = 'user_to_perm'
239 250 __table_args__ = (UniqueConstraint('user_id', 'permission_id'), {'useexisting':True})
240 251 user_to_perm_id = Column("user_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
241 252 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
242 253 permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
243 254
244 255 user = relationship('User')
245 256 permission = relationship('Permission')
246 257
247 258
248 259 class UsersGroupToPerm(Base):
249 260 __tablename__ = 'users_group_to_perm'
250 261 __table_args__ = (UniqueConstraint('users_group_id', 'permission_id'), {'useexisting':True})
251 262 users_group_to_perm_id = Column("users_group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
252 263 users_group_id = Column("users_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None)
253 264 permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
254 265 repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=None, default=None)
255 266
256 267 users_group = relationship('UsersGroup')
257 268 permission = relationship('Permission')
258 269 repository = relationship('Repository')
259 270
260 271 class GroupToPerm(Base):
261 272 __tablename__ = 'group_to_perm'
262 273 __table_args__ = (UniqueConstraint('group_id', 'permission_id'), {'useexisting':True})
263 274
264 275 group_to_perm_id = Column("group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
265 276 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
266 277 permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
267 278 group_id = Column("group_id", Integer(), ForeignKey('groups.group_id'), nullable=False, unique=None, default=None)
268 279
269 280 user = relationship('User')
270 281 permission = relationship('Permission')
271 282 group = relationship('Group')
272 283
273 284 class Statistics(Base):
274 285 __tablename__ = 'statistics'
275 286 __table_args__ = (UniqueConstraint('repository_id'), {'useexisting':True})
276 287 stat_id = Column("stat_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
277 288 repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=True, default=None)
278 289 stat_on_revision = Column("stat_on_revision", Integer(), nullable=False)
279 290 commit_activity = Column("commit_activity", LargeBinary(), nullable=False)#JSON data
280 291 commit_activity_combined = Column("commit_activity_combined", LargeBinary(), nullable=False)#JSON data
281 292 languages = Column("languages", LargeBinary(), nullable=False)#JSON data
282 293
283 294 repository = relationship('Repository', single_parent=True)
284 295
285 296 class UserFollowing(Base):
286 297 __tablename__ = 'user_followings'
287 298 __table_args__ = (UniqueConstraint('user_id', 'follows_repository_id'),
288 299 UniqueConstraint('user_id', 'follows_user_id')
289 300 , {'useexisting':True})
290 301
291 302 user_following_id = Column("user_following_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
292 303 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
293 304 follows_repo_id = Column("follows_repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=True, unique=None, default=None)
294 305 follows_user_id = Column("follows_user_id", Integer(), ForeignKey('users.user_id'), nullable=True, unique=None, default=None)
295 306
296 307 user = relationship('User', primaryjoin='User.user_id==UserFollowing.user_id')
297 308
298 309 follows_user = relationship('User', primaryjoin='User.user_id==UserFollowing.follows_user_id')
299 310 follows_repository = relationship('Repository', order_by='Repository.repo_name')
300 311
301 312 class CacheInvalidation(Base):
302 313 __tablename__ = 'cache_invalidation'
303 314 __table_args__ = (UniqueConstraint('cache_key'), {'useexisting':True})
304 315 cache_id = Column("cache_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
305 316 cache_key = Column("cache_key", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
306 317 cache_args = Column("cache_args", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
307 318 cache_active = Column("cache_active", Boolean(), nullable=True, unique=None, default=False)
308 319
309 320
310 321 def __init__(self, cache_key, cache_args=''):
311 322 self.cache_key = cache_key
312 323 self.cache_args = cache_args
313 324 self.cache_active = False
314 325
315 326 def __repr__(self):
316 327 return "<%s('%s:%s')>" % (self.__class__.__name__,
317 328 self.cache_id, self.cache_key)
318 329
319 330 class DbMigrateVersion(Base):
320 331 __tablename__ = 'db_migrate_version'
321 332 __table_args__ = {'useexisting':True}
322 333 repository_id = Column('repository_id', String(250), primary_key=True)
323 334 repository_path = Column('repository_path', Text)
324 335 version = Column('version', Integer)
325 336
General Comments 0
You need to be logged in to leave comments. Login now