##// END OF EJS Templates
Models code cleanups
marcink -
r759:a7f50911 beta
parent child Browse files
Show More
@@ -1,32 +1,57 b''
1 """The application's model objects"""
1 # -*- coding: utf-8 -*-
2 """
3 package.rhodecode.model.__init__
4 ~~~~~~~~~~~~~~
5 The application's model objects
6
7 :created_on: Nov 25, 2010
8 :author: marcink
9 :copyright: (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com>
10 :license: GPLv3, see COPYING for more details.
11
12
13 :example:
14 from paste.deploy import appconfig
15 from pylons import config
16 from sqlalchemy import engine_from_config
17 from rhodecode.config.environment import load_environment
18
19 conf = appconfig('config:development.ini', relative_to = './../../')
20 load_environment(conf.global_conf, conf.local_conf)
21
22 engine = engine_from_config(config, 'sqlalchemy.')
23 init_model(engine)
24 #RUN YOUR CODE HERE
25
26 """
27 # This program is free software; you can redistribute it and/or
28 # modify it under the terms of the GNU General Public License
29 # as published by the Free Software Foundation; version 2
30 # of the License or (at your opinion) any later version of the license.
31 #
32 # This program is distributed in the hope that it will be useful,
33 # but WITHOUT ANY WARRANTY; without even the implied warranty of
34 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 # GNU General Public License for more details.
36 #
37 # You should have received a copy of the GNU General Public License
38 # along with this program; if not, write to the Free Software
39 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
40 # MA 02110-1301, USA.
41
2 import logging
42 import logging
3 from rhodecode.model import meta
43 from rhodecode.model import meta
4 log = logging.getLogger(__name__)
44 log = logging.getLogger(__name__)
5
45
6 def init_model(engine):
46 def init_model(engine):
7 """Call me before using any of the tables or classes in the model"""
47 """Call me before using any of the tables or classes in the model"""
8 log.info("INITIALIZING DB MODELS")
48 log.info("initializing db models for %s", engine)
9 meta.Base.metadata.bind = engine
49 meta.Base.metadata.bind = engine
10 #meta.Base2.metadata.bind = engine2
11
12 #THIS IS A TEST FOR EXECUTING SCRIPT AND LOAD PYLONS APPLICATION GLOBALS
13 #from paste.deploy import appconfig
14 #from pylons import config
15 #from sqlalchemy import engine_from_config
16 #from rhodecode.config.environment import load_environment
17 #
18 #conf = appconfig('config:development.ini', relative_to = './../../')
19 #load_environment(conf.global_conf, conf.local_conf)
20 #
21 #engine = engine_from_config(config, 'sqlalchemy.')
22 #init_model(engine)
23 # DO SOMETHING
24
25
50
26 class BaseModel(object):
51 class BaseModel(object):
27
52
28 def __init__(self, sa=None):
53 def __init__(self, sa=None):
29 if sa is not None:
54 if sa is not None:
30 self.sa = sa
55 self.sa = sa
31 else:
56 else:
32 self.sa = meta.Session()
57 self.sa = meta.Session()
@@ -1,187 +1,215 b''
1 from rhodecode.model.meta import Base
1 # -*- coding: utf-8 -*-
2 """
3 package.rhodecode.model.db
4 ~~~~~~~~~~~~~~
5
6 Database Models for RhodeCode
7 :created_on: Apr 08, 2010
8 :author: marcink
9 :copyright: (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com>
10 :license: GPLv3, see COPYING for more details.
11 """
12 # This program is free software; you can redistribute it and/or
13 # modify it under the terms of the GNU General Public License
14 # as published by the Free Software Foundation; version 2
15 # of the License or (at your opinion) any later version of the license.
16 #
17 # This program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # GNU General Public License for more details.
21 #
22 # You should have received a copy of the GNU General Public License
23 # along with this program; if not, write to the Free Software
24 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
25 # MA 02110-1301, USA.
26 import logging
27 import datetime
28
2 from sqlalchemy import *
29 from sqlalchemy import *
30 from sqlalchemy.exc import DatabaseError
3 from sqlalchemy.orm import relation, backref
31 from sqlalchemy.orm import relation, backref
4 from sqlalchemy.orm.session import Session
32 from sqlalchemy.orm.session import Session
5 from vcs.utils.lazy import LazyProperty
33
6 import logging
34 from rhodecode.model.meta import Base
35
7 log = logging.getLogger(__name__)
36 log = logging.getLogger(__name__)
8
37
9 class RhodeCodeSettings(Base):
38 class RhodeCodeSettings(Base):
10 __tablename__ = 'rhodecode_settings'
39 __tablename__ = 'rhodecode_settings'
11 __table_args__ = (UniqueConstraint('app_settings_name'), {'useexisting':True})
40 __table_args__ = (UniqueConstraint('app_settings_name'), {'useexisting':True})
12 app_settings_id = Column("app_settings_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True)
41 app_settings_id = Column("app_settings_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True)
13 app_settings_name = Column("app_settings_name", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
42 app_settings_name = Column("app_settings_name", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
14 app_settings_value = Column("app_settings_value", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
43 app_settings_value = Column("app_settings_value", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
15
44
16 def __init__(self, k, v):
45 def __init__(self, k, v):
17 self.app_settings_name = k
46 self.app_settings_name = k
18 self.app_settings_value = v
47 self.app_settings_value = v
19
48
20 def __repr__(self):
49 def __repr__(self):
21 return "<RhodeCodeSetting('%s:%s')>" % (self.app_settings_name,
50 return "<RhodeCodeSetting('%s:%s')>" % (self.app_settings_name,
22 self.app_settings_value)
51 self.app_settings_value)
23
52
24 class RhodeCodeUi(Base):
53 class RhodeCodeUi(Base):
25 __tablename__ = 'rhodecode_ui'
54 __tablename__ = 'rhodecode_ui'
26 __table_args__ = {'useexisting':True}
55 __table_args__ = {'useexisting':True}
27 ui_id = Column("ui_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True)
56 ui_id = Column("ui_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True)
28 ui_section = Column("ui_section", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
57 ui_section = Column("ui_section", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
29 ui_key = Column("ui_key", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
58 ui_key = Column("ui_key", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
30 ui_value = Column("ui_value", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
59 ui_value = Column("ui_value", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
31 ui_active = Column("ui_active", BOOLEAN(), nullable=True, unique=None, default=True)
60 ui_active = Column("ui_active", BOOLEAN(), nullable=True, unique=None, default=True)
32
61
33
62
34 class User(Base):
63 class User(Base):
35 __tablename__ = 'users'
64 __tablename__ = 'users'
36 __table_args__ = (UniqueConstraint('username'), UniqueConstraint('email'), {'useexisting':True})
65 __table_args__ = (UniqueConstraint('username'), UniqueConstraint('email'), {'useexisting':True})
37 user_id = Column("user_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True)
66 user_id = Column("user_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True)
38 username = Column("username", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
67 username = Column("username", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
39 password = Column("password", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
68 password = Column("password", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
40 active = Column("active", BOOLEAN(), nullable=True, unique=None, default=None)
69 active = Column("active", BOOLEAN(), nullable=True, unique=None, default=None)
41 admin = Column("admin", BOOLEAN(), nullable=True, unique=None, default=False)
70 admin = Column("admin", BOOLEAN(), nullable=True, unique=None, default=False)
42 name = Column("name", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
71 name = Column("name", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
43 lastname = Column("lastname", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
72 lastname = Column("lastname", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
44 email = Column("email", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
73 email = Column("email", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
45 last_login = Column("last_login", DATETIME(timezone=False), nullable=True, unique=None, default=None)
74 last_login = Column("last_login", DATETIME(timezone=False), nullable=True, unique=None, default=None)
46 is_ldap = Column("is_ldap", BOOLEAN(), nullable=False, unique=None, default=False)
75 is_ldap = Column("is_ldap", BOOLEAN(), nullable=False, unique=None, default=False)
47
76
48 user_log = relation('UserLog', cascade='all')
77 user_log = relation('UserLog', cascade='all')
49 user_perms = relation('UserToPerm', primaryjoin="User.user_id==UserToPerm.user_id", cascade='all')
78 user_perms = relation('UserToPerm', primaryjoin="User.user_id==UserToPerm.user_id", cascade='all')
50
79
51 repositories = relation('Repository')
80 repositories = relation('Repository')
52 user_followers = relation('UserFollowing', primaryjoin='UserFollowing.follows_user_id==User.user_id', cascade='all')
81 user_followers = relation('UserFollowing', primaryjoin='UserFollowing.follows_user_id==User.user_id', cascade='all')
53
82
54 @LazyProperty
83 @property
55 def full_contact(self):
84 def full_contact(self):
56 return '%s %s <%s>' % (self.name, self.lastname, self.email)
85 return '%s %s <%s>' % (self.name, self.lastname, self.email)
57
86
58 def __repr__(self):
87 def __repr__(self):
59 return "<User('id:%s:%s')>" % (self.user_id, self.username)
88 return "<User('id:%s:%s')>" % (self.user_id, self.username)
60
89
61 def update_lastlogin(self):
90 def update_lastlogin(self):
62 """Update user lastlogin"""
91 """Update user lastlogin"""
63 import datetime
64
92
65 try:
93 try:
66 session = Session.object_session(self)
94 session = Session.object_session(self)
67 self.last_login = datetime.datetime.now()
95 self.last_login = datetime.datetime.now()
68 session.add(self)
96 session.add(self)
69 session.commit()
97 session.commit()
70 log.debug('updated user %s lastlogin', self.username)
98 log.debug('updated user %s lastlogin', self.username)
71 except Exception:
99 except (DatabaseError,):
72 session.rollback()
100 session.rollback()
73
101
74
102
75 class UserLog(Base):
103 class UserLog(Base):
76 __tablename__ = 'user_logs'
104 __tablename__ = 'user_logs'
77 __table_args__ = {'useexisting':True}
105 __table_args__ = {'useexisting':True}
78 user_log_id = Column("user_log_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True)
106 user_log_id = Column("user_log_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True)
79 user_id = Column("user_id", INTEGER(), ForeignKey(u'users.user_id'), nullable=False, unique=None, default=None)
107 user_id = Column("user_id", INTEGER(), ForeignKey(u'users.user_id'), nullable=False, unique=None, default=None)
80 repository_id = Column("repository_id", INTEGER(length=None, convert_unicode=False, assert_unicode=None), ForeignKey(u'repositories.repo_id'), nullable=False, unique=None, default=None)
108 repository_id = Column("repository_id", INTEGER(length=None, convert_unicode=False, assert_unicode=None), ForeignKey(u'repositories.repo_id'), nullable=False, unique=None, default=None)
81 repository_name = Column("repository_name", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
109 repository_name = Column("repository_name", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
82 user_ip = Column("user_ip", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
110 user_ip = Column("user_ip", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
83 action = Column("action", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
111 action = Column("action", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
84 action_date = Column("action_date", DATETIME(timezone=False), nullable=True, unique=None, default=None)
112 action_date = Column("action_date", DATETIME(timezone=False), nullable=True, unique=None, default=None)
85
113
86 user = relation('User')
114 user = relation('User')
87 repository = relation('Repository')
115 repository = relation('Repository')
88
116
89 class Repository(Base):
117 class Repository(Base):
90 __tablename__ = 'repositories'
118 __tablename__ = 'repositories'
91 __table_args__ = (UniqueConstraint('repo_name'), {'useexisting':True},)
119 __table_args__ = (UniqueConstraint('repo_name'), {'useexisting':True},)
92 repo_id = Column("repo_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True)
120 repo_id = Column("repo_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True)
93 repo_name = Column("repo_name", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=False, unique=True, default=None)
121 repo_name = Column("repo_name", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=False, unique=True, default=None)
94 repo_type = Column("repo_type", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=False, unique=False, default=None)
122 repo_type = Column("repo_type", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=False, unique=False, default=None)
95 user_id = Column("user_id", INTEGER(), ForeignKey(u'users.user_id'), nullable=False, unique=False, default=None)
123 user_id = Column("user_id", INTEGER(), ForeignKey(u'users.user_id'), nullable=False, unique=False, default=None)
96 private = Column("private", BOOLEAN(), nullable=True, unique=None, default=None)
124 private = Column("private", BOOLEAN(), nullable=True, unique=None, default=None)
97 description = Column("description", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
125 description = Column("description", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
98 fork_id = Column("fork_id", INTEGER(), ForeignKey(u'repositories.repo_id'), nullable=True, unique=False, default=None)
126 fork_id = Column("fork_id", INTEGER(), ForeignKey(u'repositories.repo_id'), nullable=True, unique=False, default=None)
99
127
100 user = relation('User')
128 user = relation('User')
101 fork = relation('Repository', remote_side=repo_id)
129 fork = relation('Repository', remote_side=repo_id)
102 repo_to_perm = relation('RepoToPerm', cascade='all')
130 repo_to_perm = relation('RepoToPerm', cascade='all')
103 stats = relation('Statistics', cascade='all', uselist=False)
131 stats = relation('Statistics', cascade='all', uselist=False)
104
132
105 repo_followers = relation('UserFollowing', primaryjoin='UserFollowing.follows_repo_id==Repository.repo_id', cascade='all')
133 repo_followers = relation('UserFollowing', primaryjoin='UserFollowing.follows_repo_id==Repository.repo_id', cascade='all')
106
134
107
135
108 def __repr__(self):
136 def __repr__(self):
109 return "<Repository('%s:%s')>" % (self.repo_id, self.repo_name)
137 return "<Repository('%s:%s')>" % (self.repo_id, self.repo_name)
110
138
111 class Permission(Base):
139 class Permission(Base):
112 __tablename__ = 'permissions'
140 __tablename__ = 'permissions'
113 __table_args__ = {'useexisting':True}
141 __table_args__ = {'useexisting':True}
114 permission_id = Column("permission_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True)
142 permission_id = Column("permission_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True)
115 permission_name = Column("permission_name", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
143 permission_name = Column("permission_name", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
116 permission_longname = Column("permission_longname", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
144 permission_longname = Column("permission_longname", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
117
145
118 def __repr__(self):
146 def __repr__(self):
119 return "<Permission('%s:%s')>" % (self.permission_id, self.permission_name)
147 return "<Permission('%s:%s')>" % (self.permission_id, self.permission_name)
120
148
121 class RepoToPerm(Base):
149 class RepoToPerm(Base):
122 __tablename__ = 'repo_to_perm'
150 __tablename__ = 'repo_to_perm'
123 __table_args__ = (UniqueConstraint('user_id', 'repository_id'), {'useexisting':True})
151 __table_args__ = (UniqueConstraint('user_id', 'repository_id'), {'useexisting':True})
124 repo_to_perm_id = Column("repo_to_perm_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True)
152 repo_to_perm_id = Column("repo_to_perm_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True)
125 user_id = Column("user_id", INTEGER(), ForeignKey(u'users.user_id'), nullable=False, unique=None, default=None)
153 user_id = Column("user_id", INTEGER(), ForeignKey(u'users.user_id'), nullable=False, unique=None, default=None)
126 permission_id = Column("permission_id", INTEGER(), ForeignKey(u'permissions.permission_id'), nullable=False, unique=None, default=None)
154 permission_id = Column("permission_id", INTEGER(), ForeignKey(u'permissions.permission_id'), nullable=False, unique=None, default=None)
127 repository_id = Column("repository_id", INTEGER(), ForeignKey(u'repositories.repo_id'), nullable=False, unique=None, default=None)
155 repository_id = Column("repository_id", INTEGER(), ForeignKey(u'repositories.repo_id'), nullable=False, unique=None, default=None)
128
156
129 user = relation('User')
157 user = relation('User')
130 permission = relation('Permission')
158 permission = relation('Permission')
131 repository = relation('Repository')
159 repository = relation('Repository')
132
160
133 class UserToPerm(Base):
161 class UserToPerm(Base):
134 __tablename__ = 'user_to_perm'
162 __tablename__ = 'user_to_perm'
135 __table_args__ = (UniqueConstraint('user_id', 'permission_id'), {'useexisting':True})
163 __table_args__ = (UniqueConstraint('user_id', 'permission_id'), {'useexisting':True})
136 user_to_perm_id = Column("user_to_perm_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True)
164 user_to_perm_id = Column("user_to_perm_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True)
137 user_id = Column("user_id", INTEGER(), ForeignKey(u'users.user_id'), nullable=False, unique=None, default=None)
165 user_id = Column("user_id", INTEGER(), ForeignKey(u'users.user_id'), nullable=False, unique=None, default=None)
138 permission_id = Column("permission_id", INTEGER(), ForeignKey(u'permissions.permission_id'), nullable=False, unique=None, default=None)
166 permission_id = Column("permission_id", INTEGER(), ForeignKey(u'permissions.permission_id'), nullable=False, unique=None, default=None)
139
167
140 user = relation('User')
168 user = relation('User')
141 permission = relation('Permission')
169 permission = relation('Permission')
142
170
143 class Statistics(Base):
171 class Statistics(Base):
144 __tablename__ = 'statistics'
172 __tablename__ = 'statistics'
145 __table_args__ = (UniqueConstraint('repository_id'), {'useexisting':True})
173 __table_args__ = (UniqueConstraint('repository_id'), {'useexisting':True})
146 stat_id = Column("stat_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True)
174 stat_id = Column("stat_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True)
147 repository_id = Column("repository_id", INTEGER(), ForeignKey(u'repositories.repo_id'), nullable=False, unique=True, default=None)
175 repository_id = Column("repository_id", INTEGER(), ForeignKey(u'repositories.repo_id'), nullable=False, unique=True, default=None)
148 stat_on_revision = Column("stat_on_revision", INTEGER(), nullable=False)
176 stat_on_revision = Column("stat_on_revision", INTEGER(), nullable=False)
149 commit_activity = Column("commit_activity", BLOB(), nullable=False)#JSON data
177 commit_activity = Column("commit_activity", BLOB(), nullable=False)#JSON data
150 commit_activity_combined = Column("commit_activity_combined", BLOB(), nullable=False)#JSON data
178 commit_activity_combined = Column("commit_activity_combined", BLOB(), nullable=False)#JSON data
151 languages = Column("languages", BLOB(), nullable=False)#JSON data
179 languages = Column("languages", BLOB(), nullable=False)#JSON data
152
180
153 repository = relation('Repository', single_parent=True)
181 repository = relation('Repository', single_parent=True)
154
182
155 class UserFollowing(Base):
183 class UserFollowing(Base):
156 __tablename__ = 'user_followings'
184 __tablename__ = 'user_followings'
157 __table_args__ = (UniqueConstraint('user_id', 'follows_repository_id'),
185 __table_args__ = (UniqueConstraint('user_id', 'follows_repository_id'),
158 UniqueConstraint('user_id', 'follows_user_id')
186 UniqueConstraint('user_id', 'follows_user_id')
159 , {'useexisting':True})
187 , {'useexisting':True})
160
188
161 user_following_id = Column("user_following_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True)
189 user_following_id = Column("user_following_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True)
162 user_id = Column("user_id", INTEGER(), ForeignKey(u'users.user_id'), nullable=False, unique=None, default=None)
190 user_id = Column("user_id", INTEGER(), ForeignKey(u'users.user_id'), nullable=False, unique=None, default=None)
163 follows_repo_id = Column("follows_repository_id", INTEGER(), ForeignKey(u'repositories.repo_id'), nullable=True, unique=None, default=None)
191 follows_repo_id = Column("follows_repository_id", INTEGER(), ForeignKey(u'repositories.repo_id'), nullable=True, unique=None, default=None)
164 follows_user_id = Column("follows_user_id", INTEGER(), ForeignKey(u'users.user_id'), nullable=True, unique=None, default=None)
192 follows_user_id = Column("follows_user_id", INTEGER(), ForeignKey(u'users.user_id'), nullable=True, unique=None, default=None)
165
193
166 user = relation('User', primaryjoin='User.user_id==UserFollowing.user_id')
194 user = relation('User', primaryjoin='User.user_id==UserFollowing.user_id')
167
195
168 follows_user = relation('User', primaryjoin='User.user_id==UserFollowing.follows_user_id')
196 follows_user = relation('User', primaryjoin='User.user_id==UserFollowing.follows_user_id')
169 follows_repository = relation('Repository')
197 follows_repository = relation('Repository')
170
198
171
199
172 class CacheInvalidation(Base):
200 class CacheInvalidation(Base):
173 __tablename__ = 'cache_invalidation'
201 __tablename__ = 'cache_invalidation'
174 __table_args__ = (UniqueConstraint('cache_key'), {'useexisting':True})
202 __table_args__ = (UniqueConstraint('cache_key'), {'useexisting':True})
175 cache_id = Column("cache_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True)
203 cache_id = Column("cache_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True)
176 cache_key = Column("cache_key", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
204 cache_key = Column("cache_key", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
177 cache_args = Column("cache_args", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
205 cache_args = Column("cache_args", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
178 cache_active = Column("cache_active", BOOLEAN(), nullable=True, unique=None, default=False)
206 cache_active = Column("cache_active", BOOLEAN(), nullable=True, unique=None, default=False)
179
207
180
208
181 def __init__(self, cache_key, cache_args=''):
209 def __init__(self, cache_key, cache_args=''):
182 self.cache_key = cache_key
210 self.cache_key = cache_key
183 self.cache_args = cache_args
211 self.cache_args = cache_args
184 self.cache_active = False
212 self.cache_active = False
185
213
186 def __repr__(self):
214 def __repr__(self):
187 return "<CacheInvalidation('%s:%s')>" % (self.cache_id, self.cache_key)
215 return "<CacheInvalidation('%s:%s')>" % (self.cache_id, self.cache_key)
@@ -1,96 +1,101 b''
1 #!/usr/bin/env python
1 # -*- coding: utf-8 -*-
2 # encoding: utf-8
2 """
3 # Model for permissions
3 package.rhodecode.model.permission
4 # Copyright (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com>
4 ~~~~~~~~~~~~~~
5
5
6 permissions model for RhodeCode
7 :created_on: Aug 20, 2010
8 :author: marcink
9 :copyright: (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com>
10 :license: GPLv3, see COPYING for more details.
11 """
6 # This program is free software; you can redistribute it and/or
12 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
13 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; version 2
14 # as published by the Free Software Foundation; version 2
9 # of the License or (at your opinion) any later version of the license.
15 # of the License or (at your opinion) any later version of the license.
10 #
16 #
11 # This program is distributed in the hope that it will be useful,
17 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
20 # GNU General Public License for more details.
15 #
21 #
16 # You should have received a copy of the GNU General Public License
22 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
23 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
24 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 # MA 02110-1301, USA.
25 # MA 02110-1301, USA.
20 """
26
21 Created on Aug 20, 2010
27 import logging
22 Model for permissions
28 import traceback
23 :author: marcink
29
24 """
30 from sqlalchemy.exc import DatabaseError
25
31
26 from rhodecode.model import BaseModel
32 from rhodecode.model import BaseModel
27 from rhodecode.model.db import User, Permission, UserToPerm, RepoToPerm
33 from rhodecode.model.db import User, Permission, UserToPerm, RepoToPerm
28 from rhodecode.model.caching_query import FromCache
34 from rhodecode.model.caching_query import FromCache
29 import logging
35
30 import traceback
31 log = logging.getLogger(__name__)
36 log = logging.getLogger(__name__)
32
37
33
38
34 class PermissionModel(BaseModel):
39 class PermissionModel(BaseModel):
35
40
36 def get_permission(self, permission_id, cache=False):
41 def get_permission(self, permission_id, cache=False):
37 perm = self.sa.query(Permission)
42 perm = self.sa.query(Permission)
38 if cache:
43 if cache:
39 perm = perm.options(FromCache("sql_cache_short",
44 perm = perm.options(FromCache("sql_cache_short",
40 "get_permission_%s" % permission_id))
45 "get_permission_%s" % permission_id))
41 return perm.get(permission_id)
46 return perm.get(permission_id)
42
47
43 def get_permission_by_name(self, name, cache=False):
48 def get_permission_by_name(self, name, cache=False):
44 perm = self.sa.query(Permission)\
49 perm = self.sa.query(Permission)\
45 .filter(Permission.permission_name == name)
50 .filter(Permission.permission_name == name)
46 if cache:
51 if cache:
47 perm = perm.options(FromCache("sql_cache_short",
52 perm = perm.options(FromCache("sql_cache_short",
48 "get_permission_%s" % name))
53 "get_permission_%s" % name))
49 return perm.scalar()
54 return perm.scalar()
50
55
51 def update(self, form_result):
56 def update(self, form_result):
52 perm_user = self.sa.query(User)\
57 perm_user = self.sa.query(User)\
53 .filter(User.username == form_result['perm_user_name']).scalar()
58 .filter(User.username == form_result['perm_user_name']).scalar()
54 u2p = self.sa.query(UserToPerm).filter(UserToPerm.user == perm_user).all()
59 u2p = self.sa.query(UserToPerm).filter(UserToPerm.user == perm_user).all()
55 if len(u2p) != 3:
60 if len(u2p) != 3:
56 raise Exception('Defined: %s should be 3 permissions for default'
61 raise Exception('Defined: %s should be 3 permissions for default'
57 ' user. This should not happen please verify'
62 ' user. This should not happen please verify'
58 ' your database' % len(u2p))
63 ' your database' % len(u2p))
59
64
60 try:
65 try:
61 #stage 1 change defaults
66 #stage 1 change defaults
62 for p in u2p:
67 for p in u2p:
63 if p.permission.permission_name.startswith('repository.'):
68 if p.permission.permission_name.startswith('repository.'):
64 p.permission = self.get_permission_by_name(
69 p.permission = self.get_permission_by_name(
65 form_result['default_perm'])
70 form_result['default_perm'])
66 self.sa.add(p)
71 self.sa.add(p)
67
72
68 if p.permission.permission_name.startswith('hg.register.'):
73 if p.permission.permission_name.startswith('hg.register.'):
69 p.permission = self.get_permission_by_name(
74 p.permission = self.get_permission_by_name(
70 form_result['default_register'])
75 form_result['default_register'])
71 self.sa.add(p)
76 self.sa.add(p)
72
77
73 if p.permission.permission_name.startswith('hg.create.'):
78 if p.permission.permission_name.startswith('hg.create.'):
74 p.permission = self.get_permission_by_name(
79 p.permission = self.get_permission_by_name(
75 form_result['default_create'])
80 form_result['default_create'])
76 self.sa.add(p)
81 self.sa.add(p)
77
82
78 #stage 2 update all default permissions for repos if checked
83 #stage 2 update all default permissions for repos if checked
79 if form_result['overwrite_default'] == True:
84 if form_result['overwrite_default'] == True:
80 for r2p in self.sa.query(RepoToPerm)\
85 for r2p in self.sa.query(RepoToPerm)\
81 .filter(RepoToPerm.user == perm_user).all():
86 .filter(RepoToPerm.user == perm_user).all():
82 r2p.permission = self.get_permission_by_name(
87 r2p.permission = self.get_permission_by_name(
83 form_result['default_perm'])
88 form_result['default_perm'])
84 self.sa.add(r2p)
89 self.sa.add(r2p)
85
90
86 #stage 3 set anonymous access
91 #stage 3 set anonymous access
87 if perm_user.username == 'default':
92 if perm_user.username == 'default':
88 perm_user.active = bool(form_result['anonymous'])
93 perm_user.active = bool(form_result['anonymous'])
89 self.sa.add(perm_user)
94 self.sa.add(perm_user)
90
95
91
96
92 self.sa.commit()
97 self.sa.commit()
93 except:
98 except (DatabaseError,):
94 log.error(traceback.format_exc())
99 log.error(traceback.format_exc())
95 self.sa.rollback()
100 self.sa.rollback()
96 raise
101 raise
General Comments 0
You need to be logged in to leave comments. Login now