Show More
@@ -1,347 +1,325 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """ |
|
2 | """ | |
3 | rhodecode.model.db |
|
3 | rhodecode.model.db | |
4 | ~~~~~~~~~~~~~~~~~~ |
|
4 | ~~~~~~~~~~~~~~~~~~ | |
5 |
|
5 | |||
6 | Database Models for RhodeCode |
|
6 | Database Models for RhodeCode | |
7 |
|
7 | |||
8 | :created_on: Apr 08, 2010 |
|
8 | :created_on: Apr 08, 2010 | |
9 | :author: marcink |
|
9 | :author: marcink | |
10 | :copyright: (C) 2009-2011 Marcin Kuzminski <marcin@python-works.com> |
|
10 | :copyright: (C) 2009-2011 Marcin Kuzminski <marcin@python-works.com> | |
11 | :license: GPLv3, see COPYING for more details. |
|
11 | :license: GPLv3, see COPYING for more details. | |
12 | """ |
|
12 | """ | |
13 | # This program is free software; you can redistribute it and/or |
|
13 | # This program is free software; you can redistribute it and/or | |
14 | # modify it under the terms of the GNU General Public License |
|
14 | # modify it under the terms of the GNU General Public License | |
15 | # as published by the Free Software Foundation; version 2 |
|
15 | # as published by the Free Software Foundation; version 2 | |
16 | # of the License or (at your opinion) any later version of the license. |
|
16 | # of the License or (at your opinion) any later version of the license. | |
17 | # |
|
17 | # | |
18 | # This program is distributed in the hope that it will be useful, |
|
18 | # This program is distributed in the hope that it will be useful, | |
19 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 | # GNU General Public License for more details. |
|
21 | # GNU General Public License for more details. | |
22 | # |
|
22 | # | |
23 | # You should have received a copy of the GNU General Public License |
|
23 | # You should have received a copy of the GNU General Public License | |
24 | # along with this program; if not, write to the Free Software |
|
24 | # along with this program; if not, write to the Free Software | |
25 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
25 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | |
26 | # MA 02110-1301, USA. |
|
26 | # MA 02110-1301, USA. | |
27 | import logging |
|
27 | import logging | |
28 | import datetime |
|
28 | import datetime | |
29 | from datetime import date |
|
29 | 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) | |
76 | app_settings_name = Column("app_settings_name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
44 | app_settings_name = Column("app_settings_name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) | |
77 | app_settings_value = Column("app_settings_value", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
45 | app_settings_value = Column("app_settings_value", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) | |
78 |
|
46 | |||
79 | def __init__(self, k='', v=''): |
|
47 | def __init__(self, k='', v=''): | |
80 | self.app_settings_name = k |
|
48 | self.app_settings_name = k | |
81 | self.app_settings_value = v |
|
49 | self.app_settings_value = v | |
82 |
|
50 | |||
83 | def __repr__(self): |
|
51 | def __repr__(self): | |
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) | |
91 | ui_section = Column("ui_section", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
59 | ui_section = Column("ui_section", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) | |
92 | ui_key = Column("ui_key", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
60 | ui_key = Column("ui_key", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) | |
93 | ui_value = Column("ui_value", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
61 | ui_value = Column("ui_value", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) | |
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) | |
101 | username = Column("username", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
69 | username = Column("username", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) | |
102 | password = Column("password", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
70 | password = Column("password", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) | |
103 | active = Column("active", Boolean(), nullable=True, unique=None, default=None) |
|
71 | active = Column("active", Boolean(), nullable=True, unique=None, default=None) | |
104 | admin = Column("admin", Boolean(), nullable=True, unique=None, default=False) |
|
72 | admin = Column("admin", Boolean(), nullable=True, unique=None, default=False) | |
105 | name = Column("name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
73 | name = Column("name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) | |
106 | lastname = Column("lastname", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
74 | lastname = Column("lastname", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) | |
107 | email = Column("email", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
75 | email = Column("email", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) | |
108 | last_login = Column("last_login", DateTime(timezone=False), nullable=True, unique=None, default=None) |
|
76 | last_login = Column("last_login", DateTime(timezone=False), nullable=True, unique=None, default=None) | |
109 | ldap_dn = Column("ldap_dn", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
77 | ldap_dn = Column("ldap_dn", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) | |
110 |
|
78 | |||
111 | user_log = relationship('UserLog', cascade='all') |
|
79 | user_log = relationship('UserLog', cascade='all') | |
112 | user_perms = relationship('UserToPerm', primaryjoin="User.user_id==UserToPerm.user_id", cascade='all') |
|
80 | user_perms = relationship('UserToPerm', primaryjoin="User.user_id==UserToPerm.user_id", cascade='all') | |
113 |
|
81 | |||
114 | repositories = relationship('Repository') |
|
82 | repositories = relationship('Repository') | |
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 | |
126 |
|
94 | |||
127 | def __repr__(self): |
|
95 | def __repr__(self): | |
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 | |||
134 | try: |
|
107 | try: | |
135 | session = Session.object_session(self) |
|
108 | session = Session.object_session(self) | |
136 | self.last_login = datetime.datetime.now() |
|
109 | self.last_login = datetime.datetime.now() | |
137 | session.add(self) |
|
110 | session.add(self) | |
138 | session.commit() |
|
111 | session.commit() | |
139 | log.debug('updated user %s lastlogin', self.username) |
|
112 | log.debug('updated user %s lastlogin', self.username) | |
140 | except (DatabaseError,): |
|
113 | except (DatabaseError,): | |
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) | |
148 | user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None) |
|
121 | user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None) | |
149 | repository_id = Column("repository_id", Integer(length=None, convert_unicode=False, assert_unicode=None), ForeignKey('repositories.repo_id'), nullable=False, unique=None, default=None) |
|
122 | repository_id = Column("repository_id", Integer(length=None, convert_unicode=False, assert_unicode=None), ForeignKey('repositories.repo_id'), nullable=False, unique=None, default=None) | |
150 | repository_name = Column("repository_name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
123 | repository_name = Column("repository_name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) | |
151 | user_ip = Column("user_ip", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
124 | user_ip = Column("user_ip", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) | |
152 | action = Column("action", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
125 | action = Column("action", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) | |
153 | action_date = Column("action_date", DateTime(timezone=False), nullable=True, unique=None, default=None) |
|
126 | action_date = Column("action_date", DateTime(timezone=False), nullable=True, unique=None, default=None) | |
154 |
|
127 | |||
155 | @property |
|
128 | @property | |
156 | def action_as_day(self): |
|
129 | def action_as_day(self): | |
157 | return date(*self.action_date.timetuple()[:3]) |
|
130 | return date(*self.action_date.timetuple()[:3]) | |
158 |
|
131 | |||
159 | user = relationship('User') |
|
132 | user = relationship('User') | |
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 | |||
167 | users_group_id = Column("users_group_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
140 | users_group_id = Column("users_group_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) | |
168 | users_group_name = Column("users_group_name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=False, unique=True, default=None) |
|
141 | users_group_name = Column("users_group_name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=False, unique=True, default=None) | |
169 | users_group_active = Column("users_group_active", Boolean(), nullable=True, unique=None, default=None) |
|
142 | users_group_active = Column("users_group_active", Boolean(), nullable=True, unique=None, default=None) | |
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 | |||
177 | users_group_member_id = Column("users_group_member_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
150 | users_group_member_id = Column("users_group_member_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) | |
178 | users_group_id = Column("users_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None) |
|
151 | users_group_id = Column("users_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None) | |
179 | user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None) |
|
152 | user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None) | |
180 |
|
153 | |||
181 | user = relationship('User', lazy='joined') |
|
154 | user = relationship('User', lazy='joined') | |
182 | users_group = relationship('UsersGroup') |
|
155 | users_group = relationship('UsersGroup') | |
183 |
|
156 | |||
184 | def __init__(self, gr_id='', u_id=''): |
|
157 | def __init__(self, gr_id='', u_id=''): | |
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) | |
192 | repo_name = Column("repo_name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=False, unique=True, default=None) |
|
165 | repo_name = Column("repo_name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=False, unique=True, default=None) | |
193 | repo_type = Column("repo_type", String(length=None, convert_unicode=False, assert_unicode=None), nullable=False, unique=False, default='hg') |
|
166 | repo_type = Column("repo_type", String(length=None, convert_unicode=False, assert_unicode=None), nullable=False, unique=False, default='hg') | |
194 | user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=False, default=None) |
|
167 | user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=False, default=None) | |
195 | private = Column("private", Boolean(), nullable=True, unique=None, default=None) |
|
168 | private = Column("private", Boolean(), nullable=True, unique=None, default=None) | |
196 | enable_statistics = Column("statistics", Boolean(), nullable=True, unique=None, default=True) |
|
169 | enable_statistics = Column("statistics", Boolean(), nullable=True, unique=None, default=True) | |
197 | enable_downloads = Column("downloads", Boolean(), nullable=True, unique=None, default=True) |
|
170 | enable_downloads = Column("downloads", Boolean(), nullable=True, unique=None, default=True) | |
198 | description = Column("description", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
171 | description = Column("description", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) | |
199 | fork_id = Column("fork_id", Integer(), ForeignKey('repositories.repo_id'), nullable=True, unique=False, default=None) |
|
172 | fork_id = Column("fork_id", Integer(), ForeignKey('repositories.repo_id'), nullable=True, unique=False, default=None) | |
200 | group_id = Column("group_id", Integer(), ForeignKey('groups.group_id'), nullable=True, unique=False, default=None) |
|
173 | group_id = Column("group_id", Integer(), ForeignKey('groups.group_id'), nullable=True, unique=False, default=None) | |
201 |
|
174 | |||
202 | user = relationship('User') |
|
175 | user = relationship('User') | |
203 | fork = relationship('Repository', remote_side=repo_id) |
|
176 | fork = relationship('Repository', remote_side=repo_id) | |
204 | group = relationship('Group') |
|
177 | group = relationship('Group') | |
205 | repo_to_perm = relationship('RepoToPerm', cascade='all', order_by='RepoToPerm.repo_to_perm_id') |
|
178 | repo_to_perm = relationship('RepoToPerm', cascade='all', order_by='RepoToPerm.repo_to_perm_id') | |
206 | users_group_to_perm = relationship('UsersGroupToPerm', cascade='all') |
|
179 | users_group_to_perm = relationship('UsersGroupToPerm', cascade='all') | |
207 | stats = relationship('Statistics', cascade='all', uselist=False) |
|
180 | stats = relationship('Statistics', cascade='all', uselist=False) | |
208 |
|
181 | |||
209 | followers = relationship('UserFollowing', primaryjoin='UserFollowing.follows_repo_id==Repository.repo_id', cascade='all') |
|
182 | followers = relationship('UserFollowing', primaryjoin='UserFollowing.follows_repo_id==Repository.repo_id', cascade='all') | |
210 |
|
183 | |||
211 | logs = relationship('UserLog', cascade='all') |
|
184 | logs = relationship('UserLog', cascade='all') | |
212 |
|
185 | |||
213 | def __repr__(self): |
|
186 | def __repr__(self): | |
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 | |||
221 | group_id = Column("group_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
199 | group_id = Column("group_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) | |
222 | group_name = Column("group_name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=False, unique=True, default=None) |
|
200 | group_name = Column("group_name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=False, unique=True, default=None) | |
223 | group_parent_id = Column("group_parent_id", Integer(), ForeignKey('groups.group_id'), nullable=True, unique=None, default=None) |
|
201 | group_parent_id = Column("group_parent_id", Integer(), ForeignKey('groups.group_id'), nullable=True, unique=None, default=None) | |
224 |
|
202 | |||
225 | parent_group = relationship('Group', remote_side=group_id) |
|
203 | parent_group = relationship('Group', remote_side=group_id) | |
226 |
|
204 | |||
227 |
|
205 | |||
228 | def __init__(self, group_name='', parent_group=None): |
|
206 | def __init__(self, group_name='', parent_group=None): | |
229 | self.group_name = group_name |
|
207 | self.group_name = group_name | |
230 | self.parent_group = parent_group |
|
208 | self.parent_group = parent_group | |
231 |
|
209 | |||
232 | def __repr__(self): |
|
210 | def __repr__(self): | |
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) | |
240 | permission_name = Column("permission_name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
218 | permission_name = Column("permission_name", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) | |
241 | permission_longname = Column("permission_longname", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
219 | permission_longname = Column("permission_longname", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) | |
242 |
|
220 | |||
243 | def __repr__(self): |
|
221 | def __repr__(self): | |
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) | |
251 | user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None) |
|
229 | user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None) | |
252 | permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None) |
|
230 | permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None) | |
253 | repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=None, default=None) |
|
231 | repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=None, default=None) | |
254 |
|
232 | |||
255 | user = relationship('User') |
|
233 | user = relationship('User') | |
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) | |
263 | user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None) |
|
241 | user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None) | |
264 | permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None) |
|
242 | permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None) | |
265 |
|
243 | |||
266 | user = relationship('User') |
|
244 | user = relationship('User') | |
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) | |
274 | users_group_id = Column("users_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None) |
|
252 | users_group_id = Column("users_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None) | |
275 | permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None) |
|
253 | permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None) | |
276 | repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=None, default=None) |
|
254 | repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=None, default=None) | |
277 |
|
255 | |||
278 | users_group = relationship('UsersGroup') |
|
256 | users_group = relationship('UsersGroup') | |
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 | |||
286 | group_to_perm_id = Column("group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
264 | group_to_perm_id = Column("group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) | |
287 | user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None) |
|
265 | user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None) | |
288 | permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None) |
|
266 | permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None) | |
289 | group_id = Column("group_id", Integer(), ForeignKey('groups.group_id'), nullable=False, unique=None, default=None) |
|
267 | group_id = Column("group_id", Integer(), ForeignKey('groups.group_id'), nullable=False, unique=None, default=None) | |
290 |
|
268 | |||
291 | user = relationship('User') |
|
269 | user = relationship('User') | |
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) | |
299 | repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=True, default=None) |
|
277 | repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=True, default=None) | |
300 | stat_on_revision = Column("stat_on_revision", Integer(), nullable=False) |
|
278 | stat_on_revision = Column("stat_on_revision", Integer(), nullable=False) | |
301 | commit_activity = Column("commit_activity", LargeBinary(), nullable=False)#JSON data |
|
279 | commit_activity = Column("commit_activity", LargeBinary(), nullable=False)#JSON data | |
302 | commit_activity_combined = Column("commit_activity_combined", LargeBinary(), nullable=False)#JSON data |
|
280 | commit_activity_combined = Column("commit_activity_combined", LargeBinary(), nullable=False)#JSON data | |
303 | languages = Column("languages", LargeBinary(), nullable=False)#JSON data |
|
281 | languages = Column("languages", LargeBinary(), nullable=False)#JSON data | |
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') | |
311 | , {'useexisting':True}) |
|
289 | , {'useexisting':True}) | |
312 |
|
290 | |||
313 | user_following_id = Column("user_following_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) |
|
291 | user_following_id = Column("user_following_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) | |
314 | user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None) |
|
292 | user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None) | |
315 | follows_repo_id = Column("follows_repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=True, unique=None, default=None) |
|
293 | follows_repo_id = Column("follows_repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=True, unique=None, default=None) | |
316 | follows_user_id = Column("follows_user_id", Integer(), ForeignKey('users.user_id'), nullable=True, unique=None, default=None) |
|
294 | follows_user_id = Column("follows_user_id", Integer(), ForeignKey('users.user_id'), nullable=True, unique=None, default=None) | |
317 |
|
295 | |||
318 | user = relationship('User', primaryjoin='User.user_id==UserFollowing.user_id') |
|
296 | user = relationship('User', primaryjoin='User.user_id==UserFollowing.user_id') | |
319 |
|
297 | |||
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) | |
327 | cache_key = Column("cache_key", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
305 | cache_key = Column("cache_key", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) | |
328 | cache_args = Column("cache_args", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
306 | cache_args = Column("cache_args", String(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) | |
329 | cache_active = Column("cache_active", Boolean(), nullable=True, unique=None, default=False) |
|
307 | cache_active = Column("cache_active", Boolean(), nullable=True, unique=None, default=False) | |
330 |
|
308 | |||
331 |
|
309 | |||
332 | def __init__(self, cache_key, cache_args=''): |
|
310 | def __init__(self, cache_key, cache_args=''): | |
333 | self.cache_key = cache_key |
|
311 | self.cache_key = cache_key | |
334 | self.cache_args = cache_args |
|
312 | self.cache_args = cache_args | |
335 | self.cache_active = False |
|
313 | self.cache_active = False | |
336 |
|
314 | |||
337 | def __repr__(self): |
|
315 | def __repr__(self): | |
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) | |
345 | repository_path = Column('repository_path', Text) |
|
323 | repository_path = Column('repository_path', Text) | |
346 | version = Column('version', Integer) |
|
324 | version = Column('version', Integer) | |
347 |
|
325 |
@@ -1,26 +1,70 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() | |
9 |
|
11 | |||
10 | __all__ = ['Base', 'Session'] |
|
12 | __all__ = ['Base', 'Session'] | |
11 | # |
|
13 | # | |
12 | # SQLAlchemy session manager. Updated by model.init_model() |
|
14 | # SQLAlchemy session manager. Updated by model.init_model() | |
13 | # |
|
15 | # | |
14 | Session = scoped_session( |
|
16 | Session = scoped_session( | |
15 | sessionmaker( |
|
17 | sessionmaker( | |
16 | query_cls=caching_query.query_callable(cache_manager) |
|
18 | query_cls=caching_query.query_callable(cache_manager) | |
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