##// END OF EJS Templates
added user group to perm table
marcink -
r958:7d1483f3 beta
parent child Browse files
Show More
@@ -1,321 +1,331
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
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
121 121 @property
122 122 def is_admin(self):
123 123 return self.admin
124 124
125 125 def __repr__(self):
126 126 return "<%s('id:%s:%s')>" % (self.__class__.__name__,
127 127 self.user_id, self.username)
128 128
129 129 def update_lastlogin(self):
130 130 """Update user lastlogin"""
131 131
132 132 try:
133 133 session = Session.object_session(self)
134 134 self.last_login = datetime.datetime.now()
135 135 session.add(self)
136 136 session.commit()
137 137 log.debug('updated user %s lastlogin', self.username)
138 138 except (DatabaseError,):
139 139 session.rollback()
140 140
141 141
142 142 class UserLog(Base, BaseModel):
143 143 __tablename__ = 'user_logs'
144 144 __table_args__ = {'useexisting':True}
145 145 user_log_id = Column("user_log_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
146 146 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
147 147 repository_id = Column("repository_id", Integer(length=None, convert_unicode=False, assert_unicode=None), ForeignKey('repositories.repo_id'), nullable=False, unique=None, default=None)
148 148 repository_name = Column("repository_name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
149 149 user_ip = Column("user_ip", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
150 150 action = Column("action", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
151 151 action_date = Column("action_date", DateTime(timezone=False), nullable=True, unique=None, default=None)
152 152
153 153 user = relation('User')
154 154 repository = relation('Repository')
155 155
156 156
157 157 class UsersGroup(Base, BaseModel):
158 158 __tablename__ = 'users_groups'
159 159 __table_args__ = {'useexisting':True}
160 160
161 161 user_group_id = Column("users_groups_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
162 162 user_group_name = Column("user_group_name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=False, unique=True, default=None)
163 163
164 164
165 165 members = relation('UsersGroupMember')
166 166
167 167
168 168 class UsersGroupMember(Base, BaseModel):
169 169 __tablename__ = 'users_groups_members'
170 170 __table_args__ = {'useexisting':True}
171 171
172 172 user_groups_members_id = Column("user_groups_members_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
173 173 user_group_id = Column("user_group_id", Integer(), ForeignKey('users_groups.users_groups_id'), nullable=False, unique=None, default=None)
174 174 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
175 175
176 176 user = relation('User')
177 177 users_group = relation('UsersGroup')
178 178
179 179 class Repository(Base, BaseModel):
180 180 __tablename__ = 'repositories'
181 181 __table_args__ = (UniqueConstraint('repo_name'), {'useexisting':True},)
182 182 repo_id = Column("repo_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
183 183 repo_name = Column("repo_name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=False, unique=True, default=None)
184 184 repo_type = Column("repo_type", String(length=None, convert_unicode=False, assert_unicode=None), nullable=False, unique=False, default='hg')
185 185 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=False, default=None)
186 186 private = Column("private", Boolean(), nullable=True, unique=None, default=None)
187 187 enable_statistics = Column("statistics", Boolean(), nullable=True, unique=None, default=True)
188 188 description = Column("description", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
189 189 fork_id = Column("fork_id", Integer(), ForeignKey('repositories.repo_id'), nullable=True, unique=False, default=None)
190 190 group_id = Column("group_id", Integer(), ForeignKey('groups.group_id'), nullable=True, unique=False, default=None)
191 191
192 192 user = relation('User')
193 193 fork = relation('Repository', remote_side=repo_id)
194 194 group = relation('Group')
195 195 repo_to_perm = relation('RepoToPerm', cascade='all')
196 196 stats = relation('Statistics', cascade='all', uselist=False)
197 197
198 198 repo_followers = relation('UserFollowing', primaryjoin='UserFollowing.follows_repo_id==Repository.repo_id', cascade='all')
199 199
200 200 def __repr__(self):
201 201 return "<%s('%s:%s')>" % (self.__class__.__name__,
202 202 self.repo_id, self.repo_name)
203 203
204 204 class Group(Base, BaseModel):
205 205 __tablename__ = 'groups'
206 206 __table_args__ = (UniqueConstraint('group_name'), {'useexisting':True},)
207 207
208 208 group_id = Column("group_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
209 209 group_name = Column("group_name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=False, unique=True, default=None)
210 210 group_parent_id = Column("group_parent_id", Integer(), ForeignKey('groups.group_id'), nullable=True, unique=None, default=None)
211 211
212 212 parent_group = relation('Group', remote_side=group_id)
213 213
214 214
215 215 def __init__(self, group_name='', parent_group=None):
216 216 self.group_name = group_name
217 217 self.parent_group = parent_group
218 218
219 219 def __repr__(self):
220 220 return "<%s('%s:%s')>" % (self.__class__.__name__, self.group_id,
221 221 self.group_name)
222 222
223 223 class Permission(Base, BaseModel):
224 224 __tablename__ = 'permissions'
225 225 __table_args__ = {'useexisting':True}
226 226 permission_id = Column("permission_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
227 227 permission_name = Column("permission_name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
228 228 permission_longname = Column("permission_longname", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
229 229
230 230 def __repr__(self):
231 231 return "<%s('%s:%s')>" % (self.__class__.__name__,
232 232 self.permission_id, self.permission_name)
233 233
234 234 class RepoToPerm(Base, BaseModel):
235 235 __tablename__ = 'repo_to_perm'
236 236 __table_args__ = (UniqueConstraint('user_id', 'repository_id'), {'useexisting':True})
237 237 repo_to_perm_id = Column("repo_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
238 238 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
239 239 permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
240 240 repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=None, default=None)
241 241
242 242 user = relation('User')
243 243 permission = relation('Permission')
244 244 repository = relation('Repository')
245 245
246 246 class UserToPerm(Base, BaseModel):
247 247 __tablename__ = 'user_to_perm'
248 248 __table_args__ = (UniqueConstraint('user_id', 'permission_id'), {'useexisting':True})
249 249 user_to_perm_id = Column("user_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
250 250 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
251 251 permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
252 252
253 253 user = relation('User')
254 254 permission = relation('Permission')
255 255
256 class UsersGroupToPerm(Base, BaseModel):
257 __tablename__ = 'users_group_to_perm'
258 __table_args__ = (UniqueConstraint('users_group_id', 'permission_id'), {'useexisting':True})
259 users_group_to_perm_id = Column("users_group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
260 users_group_id = Column("users_group_id", Integer(), ForeignKey('users_groups.users_groups_id'), nullable=False, unique=None, default=None)
261 permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
262
263 users_group = relation('UsersGroup')
264 permission = relation('Permission')
265
256 266 class GroupToPerm(Base, BaseModel):
257 267 __tablename__ = 'group_to_perm'
258 268 __table_args__ = (UniqueConstraint('group_id', 'permission_id'), {'useexisting':True})
259 269
260 270 group_to_perm_id = Column("group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
261 271 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
262 272 permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
263 273 group_id = Column("group_id", Integer(), ForeignKey('groups.group_id'), nullable=False, unique=None, default=None)
264 274
265 275 user = relation('User')
266 276 permission = relation('Permission')
267 277 group = relation('Group')
268 278
269 279 class Statistics(Base, BaseModel):
270 280 __tablename__ = 'statistics'
271 281 __table_args__ = (UniqueConstraint('repository_id'), {'useexisting':True})
272 282 stat_id = Column("stat_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
273 283 repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=True, default=None)
274 284 stat_on_revision = Column("stat_on_revision", Integer(), nullable=False)
275 285 commit_activity = Column("commit_activity", LargeBinary(), nullable=False)#JSON data
276 286 commit_activity_combined = Column("commit_activity_combined", LargeBinary(), nullable=False)#JSON data
277 287 languages = Column("languages", LargeBinary(), nullable=False)#JSON data
278 288
279 289 repository = relation('Repository', single_parent=True)
280 290
281 291 class UserFollowing(Base, BaseModel):
282 292 __tablename__ = 'user_followings'
283 293 __table_args__ = (UniqueConstraint('user_id', 'follows_repository_id'),
284 294 UniqueConstraint('user_id', 'follows_user_id')
285 295 , {'useexisting':True})
286 296
287 297 user_following_id = Column("user_following_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
288 298 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
289 299 follows_repo_id = Column("follows_repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=True, unique=None, default=None)
290 300 follows_user_id = Column("follows_user_id", Integer(), ForeignKey('users.user_id'), nullable=True, unique=None, default=None)
291 301
292 302 user = relation('User', primaryjoin='User.user_id==UserFollowing.user_id')
293 303
294 304 follows_user = relation('User', primaryjoin='User.user_id==UserFollowing.follows_user_id')
295 305 follows_repository = relation('Repository')
296 306
297 307 class CacheInvalidation(Base, BaseModel):
298 308 __tablename__ = 'cache_invalidation'
299 309 __table_args__ = (UniqueConstraint('cache_key'), {'useexisting':True})
300 310 cache_id = Column("cache_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
301 311 cache_key = Column("cache_key", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
302 312 cache_args = Column("cache_args", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
303 313 cache_active = Column("cache_active", Boolean(), nullable=True, unique=None, default=False)
304 314
305 315
306 316 def __init__(self, cache_key, cache_args=''):
307 317 self.cache_key = cache_key
308 318 self.cache_args = cache_args
309 319 self.cache_active = False
310 320
311 321 def __repr__(self):
312 322 return "<%s('%s:%s')>" % (self.__class__.__name__,
313 323 self.cache_id, self.cache_key)
314 324
315 325 class DbMigrateVersion(Base, BaseModel):
316 326 __tablename__ = 'db_migrate_version'
317 327 __table_args__ = {'useexisting':True}
318 328 repository_id = Column('repository_id', String(250), primary_key=True)
319 329 repository_path = Column('repository_path', Text)
320 330 version = Column('version', Integer)
321 331
General Comments 0
You need to be logged in to leave comments. Login now