Show More
@@ -30,46 +30,14 b' 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) |
@@ -84,7 +52,7 b' class RhodeCodeSettings(Base, BaseModel)' | |||
|
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) |
@@ -94,7 +62,7 b' class RhodeCodeUi(Base, BaseModel):' | |||
|
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) |
@@ -115,11 +83,11 b' class User(Base, BaseModel):' | |||
|
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 |
@@ -128,6 +96,11 b' class User(Base, BaseModel):' | |||
|
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 | |
@@ -141,7 +114,7 b' class User(Base, BaseModel):' | |||
|
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) |
@@ -160,7 +133,7 b' class UserLog(Base, BaseModel):' | |||
|
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 | |
@@ -170,7 +143,7 b' class UsersGroup(Base, BaseModel):' | |||
|
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 | |
@@ -185,7 +158,7 b' class UsersGroupMember(Base, BaseModel):' | |||
|
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) |
@@ -214,7 +187,12 b' class Repository(Base, BaseModel):' | |||
|
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 | |
@@ -233,7 +211,7 b' class Group(Base, BaseModel):' | |||
|
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) |
@@ -244,7 +222,7 b' class Permission(Base, BaseModel):' | |||
|
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) |
@@ -256,7 +234,7 b' class RepoToPerm(Base, BaseModel):' | |||
|
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) |
@@ -267,7 +245,7 b' class UserToPerm(Base, BaseModel):' | |||
|
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) |
@@ -279,7 +257,7 b' class UsersGroupToPerm(Base, BaseModel):' | |||
|
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 | |
@@ -292,7 +270,7 b' class GroupToPerm(Base, BaseModel):' | |||
|
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) |
@@ -304,7 +282,7 b' class Statistics(Base, BaseModel):' | |||
|
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') |
@@ -320,7 +298,7 b' class UserFollowing(Base, BaseModel):' | |||
|
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) |
@@ -338,7 +316,7 b' class CacheInvalidation(Base, BaseModel)' | |||
|
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) |
@@ -1,8 +1,10 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() |
@@ -17,10 +19,52 b' Session = scoped_session(' | |||
|
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