Show More
@@ -30,46 +30,14 b' from datetime import date' | |||||
30 |
|
30 | |||
31 | from sqlalchemy import * |
|
31 | from sqlalchemy import * | |
32 | from sqlalchemy.exc import DatabaseError |
|
32 | from sqlalchemy.exc import DatabaseError | |
33 |
from sqlalchemy.orm import relationship, backref |
|
33 | from sqlalchemy.orm import relationship, backref | |
34 | from sqlalchemy.orm.session import Session |
|
34 | from sqlalchemy.orm.session import Session | |
35 |
|
35 | |||
36 | from rhodecode.model.meta import Base |
|
36 | from rhodecode.model.meta import Base | |
37 |
|
37 | |||
38 | log = logging.getLogger(__name__) |
|
38 | log = logging.getLogger(__name__) | |
39 |
|
39 | |||
40 | class BaseModel(object): |
|
40 | class RhodeCodeSettings(Base): | |
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): |
|
|||
73 | __tablename__ = 'rhodecode_settings' |
|
41 | __tablename__ = 'rhodecode_settings' | |
74 | __table_args__ = (UniqueConstraint('app_settings_name'), {'useexisting':True}) |
|
42 | __table_args__ = (UniqueConstraint('app_settings_name'), {'useexisting':True}) | |
75 | app_settings_id = Column("app_settings_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
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 | return "<%s('%s:%s')>" % (self.__class__.__name__, |
|
52 | return "<%s('%s:%s')>" % (self.__class__.__name__, | |
85 | self.app_settings_name, self.app_settings_value) |
|
53 | self.app_settings_name, self.app_settings_value) | |
86 |
|
54 | |||
87 |
class RhodeCodeUi(Base |
|
55 | class RhodeCodeUi(Base): | |
88 | __tablename__ = 'rhodecode_ui' |
|
56 | __tablename__ = 'rhodecode_ui' | |
89 | __table_args__ = {'useexisting':True} |
|
57 | __table_args__ = {'useexisting':True} | |
90 | ui_id = Column("ui_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
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 | ui_active = Column("ui_active", Boolean(), nullable=True, unique=None, default=True) |
|
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 | __tablename__ = 'users' |
|
66 | __tablename__ = 'users' | |
99 | __table_args__ = (UniqueConstraint('username'), UniqueConstraint('email'), {'useexisting':True}) |
|
67 | __table_args__ = (UniqueConstraint('username'), UniqueConstraint('email'), {'useexisting':True}) | |
100 | user_id = Column("user_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
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 | user_followers = relationship('UserFollowing', primaryjoin='UserFollowing.follows_user_id==User.user_id', cascade='all') |
|
83 | user_followers = relationship('UserFollowing', primaryjoin='UserFollowing.follows_user_id==User.user_id', cascade='all') | |
116 |
|
84 | |||
117 | group_member = relationship('UsersGroupMember', cascade='all') |
|
85 | group_member = relationship('UsersGroupMember', cascade='all') | |
|
86 | ||||
118 | @property |
|
87 | @property | |
119 | def full_contact(self): |
|
88 | def full_contact(self): | |
120 | return '%s %s <%s>' % (self.name, self.lastname, self.email) |
|
89 | return '%s %s <%s>' % (self.name, self.lastname, self.email) | |
121 |
|
90 | |||
122 |
|
||||
123 | @property |
|
91 | @property | |
124 | def is_admin(self): |
|
92 | def is_admin(self): | |
125 | return self.admin |
|
93 | return self.admin | |
@@ -128,6 +96,11 b' class User(Base, BaseModel):' | |||||
128 | return "<%s('id:%s:%s')>" % (self.__class__.__name__, |
|
96 | return "<%s('id:%s:%s')>" % (self.__class__.__name__, | |
129 | self.user_id, self.username) |
|
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 | def update_lastlogin(self): |
|
104 | def update_lastlogin(self): | |
132 | """Update user lastlogin""" |
|
105 | """Update user lastlogin""" | |
133 |
|
106 | |||
@@ -141,7 +114,7 b' class User(Base, BaseModel):' | |||||
141 | session.rollback() |
|
114 | session.rollback() | |
142 |
|
115 | |||
143 |
|
116 | |||
144 |
class UserLog(Base |
|
117 | class UserLog(Base): | |
145 | __tablename__ = 'user_logs' |
|
118 | __tablename__ = 'user_logs' | |
146 | __table_args__ = {'useexisting':True} |
|
119 | __table_args__ = {'useexisting':True} | |
147 | user_log_id = Column("user_log_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
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 | repository = relationship('Repository') |
|
133 | repository = relationship('Repository') | |
161 |
|
134 | |||
162 |
|
135 | |||
163 |
class UsersGroup(Base |
|
136 | class UsersGroup(Base): | |
164 | __tablename__ = 'users_groups' |
|
137 | __tablename__ = 'users_groups' | |
165 | __table_args__ = {'useexisting':True} |
|
138 | __table_args__ = {'useexisting':True} | |
166 |
|
139 | |||
@@ -170,7 +143,7 b' class UsersGroup(Base, BaseModel):' | |||||
170 |
|
143 | |||
171 | members = relationship('UsersGroupMember', cascade="all, delete, delete-orphan", lazy="joined") |
|
144 | members = relationship('UsersGroupMember', cascade="all, delete, delete-orphan", lazy="joined") | |
172 |
|
145 | |||
173 |
class UsersGroupMember(Base |
|
146 | class UsersGroupMember(Base): | |
174 | __tablename__ = 'users_groups_members' |
|
147 | __tablename__ = 'users_groups_members' | |
175 | __table_args__ = {'useexisting':True} |
|
148 | __table_args__ = {'useexisting':True} | |
176 |
|
149 | |||
@@ -185,7 +158,7 b' class UsersGroupMember(Base, BaseModel):' | |||||
185 | self.users_group_id = gr_id |
|
158 | self.users_group_id = gr_id | |
186 | self.user_id = u_id |
|
159 | self.user_id = u_id | |
187 |
|
160 | |||
188 |
class Repository(Base |
|
161 | class Repository(Base): | |
189 | __tablename__ = 'repositories' |
|
162 | __tablename__ = 'repositories' | |
190 | __table_args__ = (UniqueConstraint('repo_name'), {'useexisting':True},) |
|
163 | __table_args__ = (UniqueConstraint('repo_name'), {'useexisting':True},) | |
191 | repo_id = Column("repo_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
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 | return "<%s('%s:%s')>" % (self.__class__.__name__, |
|
187 | return "<%s('%s:%s')>" % (self.__class__.__name__, | |
215 | self.repo_id, self.repo_name) |
|
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 | __tablename__ = 'groups' |
|
196 | __tablename__ = 'groups' | |
219 | __table_args__ = (UniqueConstraint('group_name'), {'useexisting':True},) |
|
197 | __table_args__ = (UniqueConstraint('group_name'), {'useexisting':True},) | |
220 |
|
198 | |||
@@ -233,7 +211,7 b' class Group(Base, BaseModel):' | |||||
233 | return "<%s('%s:%s')>" % (self.__class__.__name__, self.group_id, |
|
211 | return "<%s('%s:%s')>" % (self.__class__.__name__, self.group_id, | |
234 | self.group_name) |
|
212 | self.group_name) | |
235 |
|
213 | |||
236 |
class Permission(Base |
|
214 | class Permission(Base): | |
237 | __tablename__ = 'permissions' |
|
215 | __tablename__ = 'permissions' | |
238 | __table_args__ = {'useexisting':True} |
|
216 | __table_args__ = {'useexisting':True} | |
239 | permission_id = Column("permission_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
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 | return "<%s('%s:%s')>" % (self.__class__.__name__, |
|
222 | return "<%s('%s:%s')>" % (self.__class__.__name__, | |
245 | self.permission_id, self.permission_name) |
|
223 | self.permission_id, self.permission_name) | |
246 |
|
224 | |||
247 |
class RepoToPerm(Base |
|
225 | class RepoToPerm(Base): | |
248 | __tablename__ = 'repo_to_perm' |
|
226 | __tablename__ = 'repo_to_perm' | |
249 | __table_args__ = (UniqueConstraint('user_id', 'repository_id'), {'useexisting':True}) |
|
227 | __table_args__ = (UniqueConstraint('user_id', 'repository_id'), {'useexisting':True}) | |
250 | repo_to_perm_id = Column("repo_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
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 | permission = relationship('Permission') |
|
234 | permission = relationship('Permission') | |
257 | repository = relationship('Repository') |
|
235 | repository = relationship('Repository') | |
258 |
|
236 | |||
259 |
class UserToPerm(Base |
|
237 | class UserToPerm(Base): | |
260 | __tablename__ = 'user_to_perm' |
|
238 | __tablename__ = 'user_to_perm' | |
261 | __table_args__ = (UniqueConstraint('user_id', 'permission_id'), {'useexisting':True}) |
|
239 | __table_args__ = (UniqueConstraint('user_id', 'permission_id'), {'useexisting':True}) | |
262 | user_to_perm_id = Column("user_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
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 | permission = relationship('Permission') |
|
245 | permission = relationship('Permission') | |
268 |
|
246 | |||
269 |
|
247 | |||
270 |
class UsersGroupToPerm(Base |
|
248 | class UsersGroupToPerm(Base): | |
271 | __tablename__ = 'users_group_to_perm' |
|
249 | __tablename__ = 'users_group_to_perm' | |
272 | __table_args__ = (UniqueConstraint('users_group_id', 'permission_id'), {'useexisting':True}) |
|
250 | __table_args__ = (UniqueConstraint('users_group_id', 'permission_id'), {'useexisting':True}) | |
273 | users_group_to_perm_id = Column("users_group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
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 | permission = relationship('Permission') |
|
257 | permission = relationship('Permission') | |
280 | repository = relationship('Repository') |
|
258 | repository = relationship('Repository') | |
281 |
|
259 | |||
282 |
class GroupToPerm(Base |
|
260 | class GroupToPerm(Base): | |
283 | __tablename__ = 'group_to_perm' |
|
261 | __tablename__ = 'group_to_perm' | |
284 | __table_args__ = (UniqueConstraint('group_id', 'permission_id'), {'useexisting':True}) |
|
262 | __table_args__ = (UniqueConstraint('group_id', 'permission_id'), {'useexisting':True}) | |
285 |
|
263 | |||
@@ -292,7 +270,7 b' class GroupToPerm(Base, BaseModel):' | |||||
292 | permission = relationship('Permission') |
|
270 | permission = relationship('Permission') | |
293 | group = relationship('Group') |
|
271 | group = relationship('Group') | |
294 |
|
272 | |||
295 |
class Statistics(Base |
|
273 | class Statistics(Base): | |
296 | __tablename__ = 'statistics' |
|
274 | __tablename__ = 'statistics' | |
297 | __table_args__ = (UniqueConstraint('repository_id'), {'useexisting':True}) |
|
275 | __table_args__ = (UniqueConstraint('repository_id'), {'useexisting':True}) | |
298 | stat_id = Column("stat_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
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 | repository = relationship('Repository', single_parent=True) |
|
283 | repository = relationship('Repository', single_parent=True) | |
306 |
|
284 | |||
307 |
class UserFollowing(Base |
|
285 | class UserFollowing(Base): | |
308 | __tablename__ = 'user_followings' |
|
286 | __tablename__ = 'user_followings' | |
309 | __table_args__ = (UniqueConstraint('user_id', 'follows_repository_id'), |
|
287 | __table_args__ = (UniqueConstraint('user_id', 'follows_repository_id'), | |
310 | UniqueConstraint('user_id', 'follows_user_id') |
|
288 | UniqueConstraint('user_id', 'follows_user_id') | |
@@ -320,7 +298,7 b' class UserFollowing(Base, BaseModel):' | |||||
320 | follows_user = relationship('User', primaryjoin='User.user_id==UserFollowing.follows_user_id') |
|
298 | follows_user = relationship('User', primaryjoin='User.user_id==UserFollowing.follows_user_id') | |
321 | follows_repository = relationship('Repository', order_by='Repository.repo_name') |
|
299 | follows_repository = relationship('Repository', order_by='Repository.repo_name') | |
322 |
|
300 | |||
323 |
class CacheInvalidation(Base |
|
301 | class CacheInvalidation(Base): | |
324 | __tablename__ = 'cache_invalidation' |
|
302 | __tablename__ = 'cache_invalidation' | |
325 | __table_args__ = (UniqueConstraint('cache_key'), {'useexisting':True}) |
|
303 | __table_args__ = (UniqueConstraint('cache_key'), {'useexisting':True}) | |
326 | cache_id = Column("cache_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
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 | return "<%s('%s:%s')>" % (self.__class__.__name__, |
|
316 | return "<%s('%s:%s')>" % (self.__class__.__name__, | |
339 | self.cache_id, self.cache_key) |
|
317 | self.cache_id, self.cache_key) | |
340 |
|
318 | |||
341 |
class DbMigrateVersion(Base |
|
319 | class DbMigrateVersion(Base): | |
342 | __tablename__ = 'db_migrate_version' |
|
320 | __tablename__ = 'db_migrate_version' | |
343 | __table_args__ = {'useexisting':True} |
|
321 | __table_args__ = {'useexisting':True} | |
344 | repository_id = Column('repository_id', String(250), primary_key=True) |
|
322 | repository_id = Column('repository_id', String(250), primary_key=True) |
@@ -1,8 +1,10 b'' | |||||
1 | """SQLAlchemy Metadata and Session object""" |
|
1 | """SQLAlchemy Metadata and Session object""" | |
2 | from sqlalchemy.ext.declarative import declarative_base |
|
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 | from rhodecode.model import caching_query |
|
6 | from rhodecode.model import caching_query | |
5 | from beaker import cache |
|
7 | ||
6 |
|
8 | |||
7 | # Beaker CacheManager. A home base for cache configurations. |
|
9 | # Beaker CacheManager. A home base for cache configurations. | |
8 | cache_manager = cache.CacheManager() |
|
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 | # The declarative Base |
|
66 | # The declarative Base | |
21 | Base = declarative_base() |
|
67 | Base = declarative_base(cls=BaseModel) | |
22 | #For another db... |
|
|||
23 | #Base2 = declarative_base() |
|
|||
24 |
|
68 | |||
25 | #to use cache use this in query |
|
69 | #to use cache use this in query | |
26 | #.options(FromCache("sqlalchemy_cache_type", "cachekey")) |
|
70 | #.options(FromCache("sqlalchemy_cache_type", "cachekey")) |
General Comments 0
You need to be logged in to leave comments.
Login now