Show More
@@ -1,287 +1,300 | |||||
1 | #!/usr/bin/env python |
|
1 | #!/usr/bin/env python | |
2 | # encoding: utf-8 |
|
2 | # encoding: utf-8 | |
3 | # database management for RhodeCode |
|
3 | # database management for RhodeCode | |
4 | # Copyright (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com> |
|
4 | # Copyright (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com> | |
5 | # |
|
5 | # | |
6 | # This program is free software; you can redistribute it and/or |
|
6 | # This program is free software; you can redistribute it and/or | |
7 | # modify it under the terms of the GNU General Public License |
|
7 | # modify it under the terms of the GNU General Public License | |
8 | # as published by the Free Software Foundation; version 2 |
|
8 | # as published by the Free Software Foundation; version 2 | |
9 | # of the License or (at your opinion) any later version of the license. |
|
9 | # of the License or (at your opinion) any later version of the license. | |
10 | # |
|
10 | # | |
11 | # This program is distributed in the hope that it will be useful, |
|
11 | # This program is distributed in the hope that it will be useful, | |
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | # GNU General Public License for more details. |
|
14 | # GNU General Public License for more details. | |
15 | # |
|
15 | # | |
16 | # You should have received a copy of the GNU General Public License |
|
16 | # You should have received a copy of the GNU General Public License | |
17 | # along with this program; if not, write to the Free Software |
|
17 | # along with this program; if not, write to the Free Software | |
18 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
18 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | |
19 | # MA 02110-1301, USA. |
|
19 | # MA 02110-1301, USA. | |
20 |
|
20 | |||
21 | """ |
|
21 | """ | |
22 | Created on April 10, 2010 |
|
22 | Created on April 10, 2010 | |
23 | database management and creation for RhodeCode |
|
23 | database management and creation for RhodeCode | |
24 | @author: marcink |
|
24 | @author: marcink | |
25 | """ |
|
25 | """ | |
26 |
|
26 | |||
27 | from os.path import dirname as dn, join as jn |
|
27 | from os.path import dirname as dn, join as jn | |
28 | import os |
|
28 | import os | |
29 | import sys |
|
29 | import sys | |
30 | import uuid |
|
30 | import uuid | |
31 |
|
31 | |||
32 | from rhodecode.lib.auth import get_crypt_password |
|
32 | from rhodecode.lib.auth import get_crypt_password | |
33 | from rhodecode.lib.utils import ask_ok |
|
33 | from rhodecode.lib.utils import ask_ok | |
34 | from rhodecode.model import init_model |
|
34 | from rhodecode.model import init_model | |
35 | from rhodecode.model.db import User, Permission, RhodeCodeUi, RhodeCodeSettings, \ |
|
35 | from rhodecode.model.db import User, Permission, RhodeCodeUi, RhodeCodeSettings, \ | |
36 | UserToPerm |
|
36 | UserToPerm | |
37 | from rhodecode.model import meta |
|
37 | from rhodecode.model import meta | |
38 | from sqlalchemy.engine import create_engine |
|
38 | from sqlalchemy.engine import create_engine | |
39 | import logging |
|
39 | import logging | |
40 |
|
40 | |||
41 | log = logging.getLogger(__name__) |
|
41 | log = logging.getLogger(__name__) | |
42 |
|
42 | |||
43 | class DbManage(object): |
|
43 | class DbManage(object): | |
44 | def __init__(self, log_sql, dbname, root, tests=False): |
|
44 | def __init__(self, log_sql, dbname, root, tests=False): | |
45 | self.dbname = dbname |
|
45 | self.dbname = dbname | |
46 | self.tests = tests |
|
46 | self.tests = tests | |
47 | self.root = root |
|
47 | self.root = root | |
48 | dburi = 'sqlite:////%s' % jn(self.root, self.dbname) |
|
48 | dburi = 'sqlite:////%s' % jn(self.root, self.dbname) | |
49 | engine = create_engine(dburi, echo=log_sql) |
|
49 | engine = create_engine(dburi, echo=log_sql) | |
50 | init_model(engine) |
|
50 | init_model(engine) | |
51 | self.sa = meta.Session() |
|
51 | self.sa = meta.Session() | |
52 | self.db_exists = False |
|
52 | self.db_exists = False | |
53 |
|
53 | |||
54 | def check_for_db(self, override): |
|
54 | def check_for_db(self, override): | |
55 | db_path = jn(self.root, self.dbname) |
|
55 | db_path = jn(self.root, self.dbname) | |
56 | log.info('checking for existing db in %s', db_path) |
|
56 | log.info('checking for existing db in %s', db_path) | |
57 | if os.path.isfile(db_path): |
|
57 | if os.path.isfile(db_path): | |
58 | self.db_exists = True |
|
58 | self.db_exists = True | |
59 | if not override: |
|
59 | if not override: | |
60 | raise Exception('database already exists') |
|
60 | raise Exception('database already exists') | |
61 |
|
61 | |||
62 | def create_tables(self, override=False): |
|
62 | def create_tables(self, override=False): | |
63 | """ |
|
63 | """ | |
64 | Create a auth database |
|
64 | Create a auth database | |
65 | """ |
|
65 | """ | |
66 | self.check_for_db(override) |
|
66 | self.check_for_db(override) | |
67 | if self.db_exists: |
|
67 | if self.db_exists: | |
68 | log.info("database exist and it's going to be destroyed") |
|
68 | log.info("database exist and it's going to be destroyed") | |
69 | if self.tests: |
|
69 | if self.tests: | |
70 | destroy = True |
|
70 | destroy = True | |
71 | else: |
|
71 | else: | |
72 | destroy = ask_ok('Are you sure to destroy old database ? [y/n]') |
|
72 | destroy = ask_ok('Are you sure to destroy old database ? [y/n]') | |
73 | if not destroy: |
|
73 | if not destroy: | |
74 | sys.exit() |
|
74 | sys.exit() | |
75 | if self.db_exists and destroy: |
|
75 | if self.db_exists and destroy: | |
76 | os.remove(jn(self.root, self.dbname)) |
|
76 | os.remove(jn(self.root, self.dbname)) | |
77 | checkfirst = not override |
|
77 | checkfirst = not override | |
78 | meta.Base.metadata.create_all(checkfirst=checkfirst) |
|
78 | meta.Base.metadata.create_all(checkfirst=checkfirst) | |
79 | log.info('Created tables for %s', self.dbname) |
|
79 | log.info('Created tables for %s', self.dbname) | |
80 |
|
80 | |||
81 | def admin_prompt(self, second=False): |
|
81 | def admin_prompt(self, second=False): | |
82 | if not self.tests: |
|
82 | if not self.tests: | |
83 | import getpass |
|
83 | import getpass | |
84 |
|
84 | |||
85 |
|
85 | |||
86 | def get_password(): |
|
86 | def get_password(): | |
87 | password = getpass.getpass('Specify admin password (min 6 chars):') |
|
87 | password = getpass.getpass('Specify admin password (min 6 chars):') | |
88 | confirm = getpass.getpass('Confirm password:') |
|
88 | confirm = getpass.getpass('Confirm password:') | |
89 |
|
89 | |||
90 | if password != confirm: |
|
90 | if password != confirm: | |
91 | log.error('passwords mismatch') |
|
91 | log.error('passwords mismatch') | |
92 | return False |
|
92 | return False | |
93 | if len(password) < 6: |
|
93 | if len(password) < 6: | |
94 | log.error('password is to short use at least 6 characters') |
|
94 | log.error('password is to short use at least 6 characters') | |
95 | return False |
|
95 | return False | |
96 |
|
96 | |||
97 | return password |
|
97 | return password | |
98 |
|
98 | |||
99 | username = raw_input('Specify admin username:') |
|
99 | username = raw_input('Specify admin username:') | |
100 |
|
100 | |||
101 | password = get_password() |
|
101 | password = get_password() | |
102 | if not password: |
|
102 | if not password: | |
103 | #second try |
|
103 | #second try | |
104 | password = get_password() |
|
104 | password = get_password() | |
105 | if not password: |
|
105 | if not password: | |
106 | sys.exit() |
|
106 | sys.exit() | |
107 |
|
107 | |||
108 | email = raw_input('Specify admin email:') |
|
108 | email = raw_input('Specify admin email:') | |
109 | self.create_user(username, password, email, True) |
|
109 | self.create_user(username, password, email, True) | |
110 | else: |
|
110 | else: | |
111 | log.info('creating admin and regular test users') |
|
111 | log.info('creating admin and regular test users') | |
112 | self.create_user('test_admin', 'test12', 'test_admin@mail.com', True) |
|
112 | self.create_user('test_admin', 'test12', 'test_admin@mail.com', True) | |
113 | self.create_user('test_regular', 'test12', 'test_regular@mail.com', False) |
|
113 | self.create_user('test_regular', 'test12', 'test_regular@mail.com', False) | |
114 | self.create_user('test_regular2', 'test12', 'test_regular2@mail.com', False) |
|
114 | self.create_user('test_regular2', 'test12', 'test_regular2@mail.com', False) | |
115 |
|
115 | |||
116 |
|
116 | |||
117 |
|
117 | |||
118 | def config_prompt(self, test_repo_path=''): |
|
118 | def config_prompt(self, test_repo_path=''): | |
119 | log.info('Setting up repositories config') |
|
119 | log.info('Setting up repositories config') | |
120 |
|
120 | |||
121 | if not self.tests and not test_repo_path: |
|
121 | if not self.tests and not test_repo_path: | |
122 | path = raw_input('Specify valid full path to your repositories' |
|
122 | path = raw_input('Specify valid full path to your repositories' | |
123 | ' you can change this later in application settings:') |
|
123 | ' you can change this later in application settings:') | |
124 | else: |
|
124 | else: | |
125 | path = test_repo_path |
|
125 | path = test_repo_path | |
126 |
|
126 | |||
127 | if not os.path.isdir(path): |
|
127 | if not os.path.isdir(path): | |
128 | log.error('You entered wrong path: %s', path) |
|
128 | log.error('You entered wrong path: %s', path) | |
129 | sys.exit() |
|
129 | sys.exit() | |
130 |
|
130 | |||
131 | hooks1 = RhodeCodeUi() |
|
131 | hooks1 = RhodeCodeUi() | |
132 | hooks1.ui_section = 'hooks' |
|
132 | hooks1.ui_section = 'hooks' | |
133 | hooks1.ui_key = 'changegroup.update' |
|
133 | hooks1.ui_key = 'changegroup.update' | |
134 | hooks1.ui_value = 'hg update >&2' |
|
134 | hooks1.ui_value = 'hg update >&2' | |
135 | hooks1.ui_active = False |
|
135 | hooks1.ui_active = False | |
136 |
|
136 | |||
137 | hooks2 = RhodeCodeUi() |
|
137 | hooks2 = RhodeCodeUi() | |
138 | hooks2.ui_section = 'hooks' |
|
138 | hooks2.ui_section = 'hooks' | |
139 | hooks2.ui_key = 'changegroup.repo_size' |
|
139 | hooks2.ui_key = 'changegroup.repo_size' | |
140 | hooks2.ui_value = 'python:rhodecode.lib.hooks.repo_size' |
|
140 | hooks2.ui_value = 'python:rhodecode.lib.hooks.repo_size' | |
141 |
|
141 | |||
|
142 | hooks3 = RhodeCodeUi() | |||
|
143 | hooks3.ui_section = 'hooks' | |||
|
144 | hooks3.ui_key = 'pretxnchangegroup.push_logger' | |||
|
145 | hooks3.ui_value = 'python:rhodecode.lib.hooks.log_push_action' | |||
|
146 | ||||
|
147 | hooks4 = RhodeCodeUi() | |||
|
148 | hooks4.ui_section = 'hooks' | |||
|
149 | hooks4.ui_key = 'preoutgoing.pull_logger' | |||
|
150 | hooks4.ui_value = 'python:rhodecode.lib.hooks.log_pull_action' | |||
|
151 | ||||
|
152 | ||||
142 | web1 = RhodeCodeUi() |
|
153 | web1 = RhodeCodeUi() | |
143 | web1.ui_section = 'web' |
|
154 | web1.ui_section = 'web' | |
144 | web1.ui_key = 'push_ssl' |
|
155 | web1.ui_key = 'push_ssl' | |
145 | web1.ui_value = 'false' |
|
156 | web1.ui_value = 'false' | |
146 |
|
157 | |||
147 | web2 = RhodeCodeUi() |
|
158 | web2 = RhodeCodeUi() | |
148 | web2.ui_section = 'web' |
|
159 | web2.ui_section = 'web' | |
149 | web2.ui_key = 'allow_archive' |
|
160 | web2.ui_key = 'allow_archive' | |
150 | web2.ui_value = 'gz zip bz2' |
|
161 | web2.ui_value = 'gz zip bz2' | |
151 |
|
162 | |||
152 | web3 = RhodeCodeUi() |
|
163 | web3 = RhodeCodeUi() | |
153 | web3.ui_section = 'web' |
|
164 | web3.ui_section = 'web' | |
154 | web3.ui_key = 'allow_push' |
|
165 | web3.ui_key = 'allow_push' | |
155 | web3.ui_value = '*' |
|
166 | web3.ui_value = '*' | |
156 |
|
167 | |||
157 | web4 = RhodeCodeUi() |
|
168 | web4 = RhodeCodeUi() | |
158 | web4.ui_section = 'web' |
|
169 | web4.ui_section = 'web' | |
159 | web4.ui_key = 'baseurl' |
|
170 | web4.ui_key = 'baseurl' | |
160 | web4.ui_value = '/' |
|
171 | web4.ui_value = '/' | |
161 |
|
172 | |||
162 | paths = RhodeCodeUi() |
|
173 | paths = RhodeCodeUi() | |
163 | paths.ui_section = 'paths' |
|
174 | paths.ui_section = 'paths' | |
164 | paths.ui_key = '/' |
|
175 | paths.ui_key = '/' | |
165 | paths.ui_value = path |
|
176 | paths.ui_value = path | |
166 |
|
177 | |||
167 |
|
178 | |||
168 | hgsettings1 = RhodeCodeSettings() |
|
179 | hgsettings1 = RhodeCodeSettings() | |
169 |
|
180 | |||
170 | hgsettings1.app_settings_name = 'realm' |
|
181 | hgsettings1.app_settings_name = 'realm' | |
171 | hgsettings1.app_settings_value = 'RhodeCode authentication' |
|
182 | hgsettings1.app_settings_value = 'RhodeCode authentication' | |
172 |
|
183 | |||
173 | hgsettings2 = RhodeCodeSettings() |
|
184 | hgsettings2 = RhodeCodeSettings() | |
174 | hgsettings2.app_settings_name = 'title' |
|
185 | hgsettings2.app_settings_name = 'title' | |
175 | hgsettings2.app_settings_value = 'RhodeCode' |
|
186 | hgsettings2.app_settings_value = 'RhodeCode' | |
176 |
|
187 | |||
177 | try: |
|
188 | try: | |
178 | self.sa.add(hooks1) |
|
189 | self.sa.add(hooks1) | |
179 | self.sa.add(hooks2) |
|
190 | self.sa.add(hooks2) | |
|
191 | self.sa.add(hooks3) | |||
|
192 | self.sa.add(hooks4) | |||
180 | self.sa.add(web1) |
|
193 | self.sa.add(web1) | |
181 | self.sa.add(web2) |
|
194 | self.sa.add(web2) | |
182 | self.sa.add(web3) |
|
195 | self.sa.add(web3) | |
183 | self.sa.add(web4) |
|
196 | self.sa.add(web4) | |
184 | self.sa.add(paths) |
|
197 | self.sa.add(paths) | |
185 | self.sa.add(hgsettings1) |
|
198 | self.sa.add(hgsettings1) | |
186 | self.sa.add(hgsettings2) |
|
199 | self.sa.add(hgsettings2) | |
187 | self.sa.commit() |
|
200 | self.sa.commit() | |
188 | except: |
|
201 | except: | |
189 | self.sa.rollback() |
|
202 | self.sa.rollback() | |
190 | raise |
|
203 | raise | |
191 | log.info('created ui config') |
|
204 | log.info('created ui config') | |
192 |
|
205 | |||
193 | def create_user(self, username, password, email='', admin=False): |
|
206 | def create_user(self, username, password, email='', admin=False): | |
194 | log.info('creating administrator user %s', username) |
|
207 | log.info('creating administrator user %s', username) | |
195 | new_user = User() |
|
208 | new_user = User() | |
196 | new_user.username = username |
|
209 | new_user.username = username | |
197 | new_user.password = get_crypt_password(password) |
|
210 | new_user.password = get_crypt_password(password) | |
198 | new_user.name = 'RhodeCode' |
|
211 | new_user.name = 'RhodeCode' | |
199 | new_user.lastname = 'Admin' |
|
212 | new_user.lastname = 'Admin' | |
200 | new_user.email = email |
|
213 | new_user.email = email | |
201 | new_user.admin = admin |
|
214 | new_user.admin = admin | |
202 | new_user.active = True |
|
215 | new_user.active = True | |
203 |
|
216 | |||
204 | try: |
|
217 | try: | |
205 | self.sa.add(new_user) |
|
218 | self.sa.add(new_user) | |
206 | self.sa.commit() |
|
219 | self.sa.commit() | |
207 | except: |
|
220 | except: | |
208 | self.sa.rollback() |
|
221 | self.sa.rollback() | |
209 | raise |
|
222 | raise | |
210 |
|
223 | |||
211 | def create_default_user(self): |
|
224 | def create_default_user(self): | |
212 | log.info('creating default user') |
|
225 | log.info('creating default user') | |
213 | #create default user for handling default permissions. |
|
226 | #create default user for handling default permissions. | |
214 | def_user = User() |
|
227 | def_user = User() | |
215 | def_user.username = 'default' |
|
228 | def_user.username = 'default' | |
216 | def_user.password = get_crypt_password(str(uuid.uuid1())[:8]) |
|
229 | def_user.password = get_crypt_password(str(uuid.uuid1())[:8]) | |
217 | def_user.name = 'default' |
|
230 | def_user.name = 'default' | |
218 | def_user.lastname = 'default' |
|
231 | def_user.lastname = 'default' | |
219 | def_user.email = 'default@default.com' |
|
232 | def_user.email = 'default@default.com' | |
220 | def_user.admin = False |
|
233 | def_user.admin = False | |
221 | def_user.active = False |
|
234 | def_user.active = False | |
222 | try: |
|
235 | try: | |
223 | self.sa.add(def_user) |
|
236 | self.sa.add(def_user) | |
224 | self.sa.commit() |
|
237 | self.sa.commit() | |
225 | except: |
|
238 | except: | |
226 | self.sa.rollback() |
|
239 | self.sa.rollback() | |
227 | raise |
|
240 | raise | |
228 |
|
241 | |||
229 | def create_permissions(self): |
|
242 | def create_permissions(self): | |
230 | #module.(access|create|change|delete)_[name] |
|
243 | #module.(access|create|change|delete)_[name] | |
231 | #module.(read|write|owner) |
|
244 | #module.(read|write|owner) | |
232 | perms = [('repository.none', 'Repository no access'), |
|
245 | perms = [('repository.none', 'Repository no access'), | |
233 | ('repository.read', 'Repository read access'), |
|
246 | ('repository.read', 'Repository read access'), | |
234 | ('repository.write', 'Repository write access'), |
|
247 | ('repository.write', 'Repository write access'), | |
235 | ('repository.admin', 'Repository admin access'), |
|
248 | ('repository.admin', 'Repository admin access'), | |
236 | ('hg.admin', 'Hg Administrator'), |
|
249 | ('hg.admin', 'Hg Administrator'), | |
237 | ('hg.create.repository', 'Repository create'), |
|
250 | ('hg.create.repository', 'Repository create'), | |
238 | ('hg.create.none', 'Repository creation disabled'), |
|
251 | ('hg.create.none', 'Repository creation disabled'), | |
239 | ('hg.register.none', 'Register disabled'), |
|
252 | ('hg.register.none', 'Register disabled'), | |
240 | ('hg.register.manual_activate', 'Register new user with rhodecode without manual activation'), |
|
253 | ('hg.register.manual_activate', 'Register new user with rhodecode without manual activation'), | |
241 | ('hg.register.auto_activate', 'Register new user with rhodecode without auto activation'), |
|
254 | ('hg.register.auto_activate', 'Register new user with rhodecode without auto activation'), | |
242 | ] |
|
255 | ] | |
243 |
|
256 | |||
244 | for p in perms: |
|
257 | for p in perms: | |
245 | new_perm = Permission() |
|
258 | new_perm = Permission() | |
246 | new_perm.permission_name = p[0] |
|
259 | new_perm.permission_name = p[0] | |
247 | new_perm.permission_longname = p[1] |
|
260 | new_perm.permission_longname = p[1] | |
248 | try: |
|
261 | try: | |
249 | self.sa.add(new_perm) |
|
262 | self.sa.add(new_perm) | |
250 | self.sa.commit() |
|
263 | self.sa.commit() | |
251 | except: |
|
264 | except: | |
252 | self.sa.rollback() |
|
265 | self.sa.rollback() | |
253 | raise |
|
266 | raise | |
254 |
|
267 | |||
255 | def populate_default_permissions(self): |
|
268 | def populate_default_permissions(self): | |
256 | log.info('creating default user permissions') |
|
269 | log.info('creating default user permissions') | |
257 |
|
270 | |||
258 | default_user = self.sa.query(User)\ |
|
271 | default_user = self.sa.query(User)\ | |
259 | .filter(User.username == 'default').scalar() |
|
272 | .filter(User.username == 'default').scalar() | |
260 |
|
273 | |||
261 | reg_perm = UserToPerm() |
|
274 | reg_perm = UserToPerm() | |
262 | reg_perm.user = default_user |
|
275 | reg_perm.user = default_user | |
263 | reg_perm.permission = self.sa.query(Permission)\ |
|
276 | reg_perm.permission = self.sa.query(Permission)\ | |
264 | .filter(Permission.permission_name == 'hg.register.manual_activate')\ |
|
277 | .filter(Permission.permission_name == 'hg.register.manual_activate')\ | |
265 | .scalar() |
|
278 | .scalar() | |
266 |
|
279 | |||
267 | create_repo_perm = UserToPerm() |
|
280 | create_repo_perm = UserToPerm() | |
268 | create_repo_perm.user = default_user |
|
281 | create_repo_perm.user = default_user | |
269 | create_repo_perm.permission = self.sa.query(Permission)\ |
|
282 | create_repo_perm.permission = self.sa.query(Permission)\ | |
270 | .filter(Permission.permission_name == 'hg.create.repository')\ |
|
283 | .filter(Permission.permission_name == 'hg.create.repository')\ | |
271 | .scalar() |
|
284 | .scalar() | |
272 |
|
285 | |||
273 | default_repo_perm = UserToPerm() |
|
286 | default_repo_perm = UserToPerm() | |
274 | default_repo_perm.user = default_user |
|
287 | default_repo_perm.user = default_user | |
275 | default_repo_perm.permission = self.sa.query(Permission)\ |
|
288 | default_repo_perm.permission = self.sa.query(Permission)\ | |
276 | .filter(Permission.permission_name == 'repository.read')\ |
|
289 | .filter(Permission.permission_name == 'repository.read')\ | |
277 | .scalar() |
|
290 | .scalar() | |
278 |
|
291 | |||
279 | try: |
|
292 | try: | |
280 | self.sa.add(reg_perm) |
|
293 | self.sa.add(reg_perm) | |
281 | self.sa.add(create_repo_perm) |
|
294 | self.sa.add(create_repo_perm) | |
282 | self.sa.add(default_repo_perm) |
|
295 | self.sa.add(default_repo_perm) | |
283 | self.sa.commit() |
|
296 | self.sa.commit() | |
284 | except: |
|
297 | except: | |
285 | self.sa.rollback() |
|
298 | self.sa.rollback() | |
286 | raise |
|
299 | raise | |
287 |
|
300 |
@@ -1,140 +1,136 | |||||
1 | from rhodecode.model.meta import Base |
|
1 | from rhodecode.model.meta import Base from sqlalchemy import * from | |
2 | from sqlalchemy import * |
|
2 | sqlalchemy.orm import relation, backref from sqlalchemy.orm.session import | |
3 | from sqlalchemy.orm import relation, backref |
|
3 | Session from vcs.utils.lazy import LazyProperty import logging | |
4 | from sqlalchemy.orm.session import Session |
|
|||
5 | from vcs.utils.lazy import LazyProperty |
|
|||
6 | import logging |
|
|||
7 |
|
4 | |||
8 | log = logging.getLogger(__name__) |
|
5 | log = logging.getLogger(__name__) | |
9 |
|
6 | |||
10 | class RhodeCodeSettings(Base): |
|
7 | class RhodeCodeSettings(Base): | |
11 | __tablename__ = 'rhodecode_settings' |
|
8 | __tablename__ = 'rhodecode_settings' | |
12 | __table_args__ = (UniqueConstraint('app_settings_name'), {'useexisting':True}) |
|
9 | __table_args__ = (UniqueConstraint('app_settings_name'), {'useexisting':True}) | |
13 | app_settings_id = Column("app_settings_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True) |
|
10 | app_settings_id = Column("app_settings_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True) | |
14 | app_settings_name = Column("app_settings_name", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
11 | app_settings_name = Column("app_settings_name", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) | |
15 | app_settings_value = Column("app_settings_value", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
12 | app_settings_value = Column("app_settings_value", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) | |
16 |
|
13 | |||
17 | class RhodeCodeUi(Base): |
|
14 | class RhodeCodeUi(Base): | |
18 | __tablename__ = 'rhodecode_ui' |
|
15 | __tablename__ = 'rhodecode_ui' | |
19 | __table_args__ = {'useexisting':True} |
|
16 | __table_args__ = {'useexisting':True} | |
20 | ui_id = Column("ui_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True) |
|
17 | ui_id = Column("ui_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True) | |
21 | ui_section = Column("ui_section", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
18 | ui_section = Column("ui_section", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) | |
22 | ui_key = Column("ui_key", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
19 | ui_key = Column("ui_key", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) | |
23 | ui_value = Column("ui_value", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
20 | ui_value = Column("ui_value", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) | |
24 | ui_active = Column("ui_active", BOOLEAN(), nullable=True, unique=None, default=True) |
|
21 | ui_active = Column("ui_active", BOOLEAN(), nullable=True, unique=None, default=True) | |
25 |
|
22 | |||
26 |
|
23 | |||
27 | class User(Base): |
|
24 | class User(Base): | |
28 | __tablename__ = 'users' |
|
25 | __tablename__ = 'users' | |
29 | __table_args__ = (UniqueConstraint('username'), UniqueConstraint('email'), {'useexisting':True}) |
|
26 | __table_args__ = (UniqueConstraint('username'), UniqueConstraint('email'), {'useexisting':True}) | |
30 | user_id = Column("user_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True) |
|
27 | user_id = Column("user_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True) | |
31 | username = Column("username", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
28 | username = Column("username", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) | |
32 | password = Column("password", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
29 | password = Column("password", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) | |
33 | active = Column("active", BOOLEAN(), nullable=True, unique=None, default=None) |
|
30 | active = Column("active", BOOLEAN(), nullable=True, unique=None, default=None) | |
34 | admin = Column("admin", BOOLEAN(), nullable=True, unique=None, default=False) |
|
31 | admin = Column("admin", BOOLEAN(), nullable=True, unique=None, default=False) | |
35 | name = Column("name", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
32 | name = Column("name", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) | |
36 | lastname = Column("lastname", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
33 | lastname = Column("lastname", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) | |
37 | email = Column("email", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
34 | email = Column("email", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) | |
38 | last_login = Column("last_login", DATETIME(timezone=False), nullable=True, unique=None, default=None) |
|
35 | last_login = Column("last_login", DATETIME(timezone=False), nullable=True, unique=None, default=None) | |
39 |
|
36 | |||
40 | user_log = relation('UserLog') |
|
37 | user_log = relation('UserLog') | |
41 | user_perms = relation('UserToPerm', primaryjoin="User.user_id==UserToPerm.user_id") |
|
38 | user_perms = relation('UserToPerm', primaryjoin="User.user_id==UserToPerm.user_id") | |
42 |
|
39 | |||
43 | @LazyProperty |
|
40 | @LazyProperty | |
44 | def full_contact(self): |
|
41 | def full_contact(self): | |
45 | return '%s %s <%s>' % (self.name, self.lastname, self.email) |
|
42 | return '%s %s <%s>' % (self.name, self.lastname, self.email) | |
46 |
|
43 | |||
47 | def __repr__(self): |
|
44 | def __repr__(self): | |
48 | return "<User('id:%s:%s')>" % (self.user_id, self.username) |
|
45 | return "<User('id:%s:%s')>" % (self.user_id, self.username) | |
49 |
|
46 | |||
50 | def update_lastlogin(self): |
|
47 | def update_lastlogin(self): | |
51 | """Update user lastlogin""" |
|
48 | """Update user lastlogin""" | |
52 | import datetime |
|
49 | import datetime | |
53 |
|
50 | |||
54 | try: |
|
51 | try: | |
55 | session = Session.object_session(self) |
|
52 | session = Session.object_session(self) | |
56 | self.last_login = datetime.datetime.now() |
|
53 | self.last_login = datetime.datetime.now() | |
57 | session.add(self) |
|
54 | session.add(self) | |
58 | session.commit() |
|
55 | session.commit() | |
59 | log.debug('updated user %s lastlogin', self.username) |
|
56 | log.debug('updated user %s lastlogin', self.username) | |
60 | except Exception: |
|
57 | except Exception: | |
61 | session.rollback() |
|
58 | session.rollback() | |
62 |
|
59 | |||
63 |
|
60 | |||
64 | class UserLog(Base): |
|
61 | class UserLog(Base): | |
65 | __tablename__ = 'user_logs' |
|
62 | __tablename__ = 'user_logs' | |
66 | __table_args__ = {'useexisting':True} |
|
63 | __table_args__ = {'useexisting':True} | |
67 | user_log_id = Column("user_log_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True) |
|
64 | user_log_id = Column("user_log_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True) | |
68 | user_id = Column("user_id", INTEGER(), ForeignKey(u'users.user_id'), nullable=False, unique=None, default=None) |
|
65 | user_id = Column("user_id", INTEGER(), ForeignKey(u'users.user_id'), nullable=False, unique=None, default=None) | |
69 | 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) |
|
66 | 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) | |
70 | repository_name = Column("repository_name", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
67 | repository_name = Column("repository_name", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) | |
71 | user_ip = Column("user_ip", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
68 | user_ip = Column("user_ip", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) | |
72 | action = Column("action", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
69 | action = Column("action", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) | |
73 | action_date = Column("action_date", DATETIME(timezone=False), nullable=True, unique=None, default=None) |
|
70 | action_date = Column("action_date", DATETIME(timezone=False), nullable=True, unique=None, default=None) | |
74 | revision = Column('revision', TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
|||
75 |
|
71 | |||
76 | user = relation('User') |
|
72 | user = relation('User') | |
77 | repository = relation('Repository') |
|
73 | repository = relation('Repository') | |
78 |
|
74 | |||
79 | class Repository(Base): |
|
75 | class Repository(Base): | |
80 | __tablename__ = 'repositories' |
|
76 | __tablename__ = 'repositories' | |
81 | __table_args__ = (UniqueConstraint('repo_name'), {'useexisting':True},) |
|
77 | __table_args__ = (UniqueConstraint('repo_name'), {'useexisting':True},) | |
82 | repo_id = Column("repo_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True) |
|
78 | repo_id = Column("repo_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True) | |
83 | repo_name = Column("repo_name", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=False, unique=True, default=None) |
|
79 | repo_name = Column("repo_name", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=False, unique=True, default=None) | |
84 | repo_type = Column("repo_type", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=False, unique=False, default=None) |
|
80 | repo_type = Column("repo_type", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=False, unique=False, default=None) | |
85 | user_id = Column("user_id", INTEGER(), ForeignKey(u'users.user_id'), nullable=False, unique=False, default=None) |
|
81 | user_id = Column("user_id", INTEGER(), ForeignKey(u'users.user_id'), nullable=False, unique=False, default=None) | |
86 | private = Column("private", BOOLEAN(), nullable=True, unique=None, default=None) |
|
82 | private = Column("private", BOOLEAN(), nullable=True, unique=None, default=None) | |
87 | description = Column("description", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
83 | description = Column("description", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) | |
88 | fork_id = Column("fork_id", INTEGER(), ForeignKey(u'repositories.repo_id'), nullable=True, unique=False, default=None) |
|
84 | fork_id = Column("fork_id", INTEGER(), ForeignKey(u'repositories.repo_id'), nullable=True, unique=False, default=None) | |
89 |
|
85 | |||
90 | user = relation('User') |
|
86 | user = relation('User') | |
91 | fork = relation('Repository', remote_side=repo_id) |
|
87 | fork = relation('Repository', remote_side=repo_id) | |
92 | repo_to_perm = relation('RepoToPerm', cascade='all') |
|
88 | repo_to_perm = relation('RepoToPerm', cascade='all') | |
93 |
|
89 | |||
94 | def __repr__(self): |
|
90 | def __repr__(self): | |
95 | return "<Repository('id:%s:%s')>" % (self.repo_id, self.repo_name) |
|
91 | return "<Repository('id:%s:%s')>" % (self.repo_id, self.repo_name) | |
96 |
|
92 | |||
97 | class Permission(Base): |
|
93 | class Permission(Base): | |
98 | __tablename__ = 'permissions' |
|
94 | __tablename__ = 'permissions' | |
99 | __table_args__ = {'useexisting':True} |
|
95 | __table_args__ = {'useexisting':True} | |
100 | permission_id = Column("permission_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True) |
|
96 | permission_id = Column("permission_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True) | |
101 | permission_name = Column("permission_name", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
97 | permission_name = Column("permission_name", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) | |
102 | permission_longname = Column("permission_longname", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
98 | permission_longname = Column("permission_longname", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) | |
103 |
|
99 | |||
104 | def __repr__(self): |
|
100 | def __repr__(self): | |
105 | return "<Permission('%s:%s')>" % (self.permission_id, self.permission_name) |
|
101 | return "<Permission('%s:%s')>" % (self.permission_id, self.permission_name) | |
106 |
|
102 | |||
107 | class RepoToPerm(Base): |
|
103 | class RepoToPerm(Base): | |
108 | __tablename__ = 'repo_to_perm' |
|
104 | __tablename__ = 'repo_to_perm' | |
109 | __table_args__ = (UniqueConstraint('user_id', 'repository_id'), {'useexisting':True}) |
|
105 | __table_args__ = (UniqueConstraint('user_id', 'repository_id'), {'useexisting':True}) | |
110 | repo_to_perm_id = Column("repo_to_perm_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True) |
|
106 | repo_to_perm_id = Column("repo_to_perm_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True) | |
111 | 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) | |
112 | permission_id = Column("permission_id", INTEGER(), ForeignKey(u'permissions.permission_id'), nullable=False, unique=None, default=None) |
|
108 | permission_id = Column("permission_id", INTEGER(), ForeignKey(u'permissions.permission_id'), nullable=False, unique=None, default=None) | |
113 | repository_id = Column("repository_id", INTEGER(), ForeignKey(u'repositories.repo_id'), nullable=False, unique=None, default=None) |
|
109 | repository_id = Column("repository_id", INTEGER(), ForeignKey(u'repositories.repo_id'), nullable=False, unique=None, default=None) | |
114 |
|
110 | |||
115 | user = relation('User') |
|
111 | user = relation('User') | |
116 | permission = relation('Permission') |
|
112 | permission = relation('Permission') | |
117 | repository = relation('Repository') |
|
113 | repository = relation('Repository') | |
118 |
|
114 | |||
119 | class UserToPerm(Base): |
|
115 | class UserToPerm(Base): | |
120 | __tablename__ = 'user_to_perm' |
|
116 | __tablename__ = 'user_to_perm' | |
121 | __table_args__ = (UniqueConstraint('user_id', 'permission_id'), {'useexisting':True}) |
|
117 | __table_args__ = (UniqueConstraint('user_id', 'permission_id'), {'useexisting':True}) | |
122 | user_to_perm_id = Column("user_to_perm_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True) |
|
118 | user_to_perm_id = Column("user_to_perm_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True) | |
123 | user_id = Column("user_id", INTEGER(), ForeignKey(u'users.user_id'), nullable=False, unique=None, default=None) |
|
119 | user_id = Column("user_id", INTEGER(), ForeignKey(u'users.user_id'), nullable=False, unique=None, default=None) | |
124 | permission_id = Column("permission_id", INTEGER(), ForeignKey(u'permissions.permission_id'), nullable=False, unique=None, default=None) |
|
120 | permission_id = Column("permission_id", INTEGER(), ForeignKey(u'permissions.permission_id'), nullable=False, unique=None, default=None) | |
125 |
|
121 | |||
126 | user = relation('User') |
|
122 | user = relation('User') | |
127 | permission = relation('Permission') |
|
123 | permission = relation('Permission') | |
128 |
|
124 | |||
129 | class Statistics(Base): |
|
125 | class Statistics(Base): | |
130 | __tablename__ = 'statistics' |
|
126 | __tablename__ = 'statistics' | |
131 | __table_args__ = (UniqueConstraint('repository_id'), {'useexisting':True}) |
|
127 | __table_args__ = (UniqueConstraint('repository_id'), {'useexisting':True}) | |
132 | stat_id = Column("stat_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True) |
|
128 | stat_id = Column("stat_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True) | |
133 | repository_id = Column("repository_id", INTEGER(), ForeignKey(u'repositories.repo_id'), nullable=False, unique=True, default=None) |
|
129 | repository_id = Column("repository_id", INTEGER(), ForeignKey(u'repositories.repo_id'), nullable=False, unique=True, default=None) | |
134 | stat_on_revision = Column("stat_on_revision", INTEGER(), nullable=False) |
|
130 | stat_on_revision = Column("stat_on_revision", INTEGER(), nullable=False) | |
135 | commit_activity = Column("commit_activity", BLOB(), nullable=False)#JSON data |
|
131 | commit_activity = Column("commit_activity", BLOB(), nullable=False)#JSON data | |
136 | commit_activity_combined = Column("commit_activity_combined", BLOB(), nullable=False)#JSON data |
|
132 | commit_activity_combined = Column("commit_activity_combined", BLOB(), nullable=False)#JSON data | |
137 | languages = Column("languages", BLOB(), nullable=False)#JSON data |
|
133 | languages = Column("languages", BLOB(), nullable=False)#JSON data | |
138 |
|
134 | |||
139 | repository = relation('Repository') |
|
135 | repository = relation('Repository') | |
140 |
|
136 |
General Comments 0
You need to be logged in to leave comments.
Login now