Show More
@@ -1,128 +1,128 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | # This program is free software: you can redistribute it and/or modify |
|
2 | # This program is free software: you can redistribute it and/or modify | |
3 | # it under the terms of the GNU General Public License as published by |
|
3 | # it under the terms of the GNU General Public License as published by | |
4 | # the Free Software Foundation, either version 3 of the License, or |
|
4 | # the Free Software Foundation, either version 3 of the License, or | |
5 | # (at your option) any later version. |
|
5 | # (at your option) any later version. | |
6 | # |
|
6 | # | |
7 | # This program is distributed in the hope that it will be useful, |
|
7 | # This program is distributed in the hope that it will be useful, | |
8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
10 | # GNU General Public License for more details. |
|
10 | # GNU General Public License for more details. | |
11 | # |
|
11 | # | |
12 | # You should have received a copy of the GNU General Public License |
|
12 | # You should have received a copy of the GNU General Public License | |
13 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
13 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
14 | """ |
|
14 | """ | |
15 | kallithea.bin.kallithea_api |
|
15 | kallithea.bin.kallithea_api | |
16 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
16 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
17 |
|
17 | |||
18 | Api CLI client for Kallithea |
|
18 | Api CLI client for Kallithea | |
19 |
|
19 | |||
20 | This file was forked by the Kallithea project in July 2014. |
|
20 | This file was forked by the Kallithea project in July 2014. | |
21 | Original author and date, and relevant copyright and licensing information is below: |
|
21 | Original author and date, and relevant copyright and licensing information is below: | |
22 | :created_on: Jun 3, 2012 |
|
22 | :created_on: Jun 3, 2012 | |
23 | :author: marcink |
|
23 | :author: marcink | |
24 | :copyright: (c) 2013 RhodeCode GmbH, and others. |
|
24 | :copyright: (c) 2013 RhodeCode GmbH, and others. | |
25 | :license: GPLv3, see LICENSE.md for more details. |
|
25 | :license: GPLv3, see LICENSE.md for more details. | |
26 | """ |
|
26 | """ | |
27 |
|
27 | |||
28 | from __future__ import print_function |
|
28 | from __future__ import print_function | |
29 |
|
29 | |||
30 | import argparse |
|
30 | import argparse | |
31 | import sys |
|
31 | import sys | |
32 |
|
32 | |||
33 | from kallithea.bin.base import FORMAT_JSON, FORMAT_PRETTY, RcConf, api_call, json |
|
33 | from kallithea.bin.base import FORMAT_JSON, FORMAT_PRETTY, RcConf, api_call, json | |
34 |
|
34 | |||
35 |
|
35 | |||
36 | def argparser(argv): |
|
36 | def argparser(argv): | |
37 | usage = ( |
|
37 | usage = ( | |
38 | "kallithea-api [-h] [--format=FORMAT] [--apikey=APIKEY] [--apihost=APIHOST] " |
|
38 | "kallithea-api [-h] [--format=FORMAT] [--apikey=APIKEY] [--apihost=APIHOST] " | |
39 | "[--config=CONFIG] [--save-config] " |
|
39 | "[--config=CONFIG] [--save-config] " | |
40 | "METHOD <key:val> <key2:val> ...\n" |
|
40 | "METHOD <key:val> <key2:val> ...\n" | |
41 | "Create config file: kallithea-api --apikey=<key> --apihost=http://kallithea.example.com --save-config" |
|
41 | "Create config file: kallithea-api --apikey=<key> --apihost=http://kallithea.example.com --save-config" | |
42 | ) |
|
42 | ) | |
43 |
|
43 | |||
44 | parser = argparse.ArgumentParser(description='Kallithea API cli', |
|
44 | parser = argparse.ArgumentParser(description='Kallithea API cli', | |
45 | usage=usage) |
|
45 | usage=usage) | |
46 |
|
46 | |||
47 | ## config |
|
47 | ## config | |
48 | group = parser.add_argument_group('config') |
|
48 | group = parser.add_argument_group('config') | |
49 | group.add_argument('--apikey', help='api access key') |
|
49 | group.add_argument('--apikey', help='api access key') | |
50 | group.add_argument('--apihost', help='api host') |
|
50 | group.add_argument('--apihost', help='api host') | |
51 | group.add_argument('--config', help='config file') |
|
51 | group.add_argument('--config', help='config file') | |
52 | group.add_argument('--save-config', action='store_true', help='save the given config into a file') |
|
52 | group.add_argument('--save-config', action='store_true', help='save the given config into a file') | |
53 |
|
53 | |||
54 | group = parser.add_argument_group('API') |
|
54 | group = parser.add_argument_group('API') | |
55 | group.add_argument('method', metavar='METHOD', nargs='?', type=str, default=None, |
|
55 | group.add_argument('method', metavar='METHOD', nargs='?', type=str, default=None, | |
56 | help='API method name to call followed by key:value attributes', |
|
56 | help='API method name to call followed by key:value attributes', | |
57 | ) |
|
57 | ) | |
58 | group.add_argument('--format', dest='format', type=str, |
|
58 | group.add_argument('--format', dest='format', type=str, | |
59 | help='output format default: `%s` can ' |
|
59 | help='output format default: `%s` can ' | |
60 | 'be also `%s`' % (FORMAT_PRETTY, FORMAT_JSON), |
|
60 | 'be also `%s`' % (FORMAT_PRETTY, FORMAT_JSON), | |
61 | default=FORMAT_PRETTY |
|
61 | default=FORMAT_PRETTY | |
62 | ) |
|
62 | ) | |
63 | args, other = parser.parse_known_args() |
|
63 | args, other = parser.parse_known_args() | |
64 | return parser, args, other |
|
64 | return parser, args, other | |
65 |
|
65 | |||
66 |
|
66 | |||
67 | def main(argv=None): |
|
67 | def main(argv=None): | |
68 | """ |
|
68 | """ | |
69 | Main execution function for cli |
|
69 | Main execution function for cli | |
70 |
|
70 | |||
71 | :param argv: |
|
71 | :param argv: | |
72 | """ |
|
72 | """ | |
73 | if argv is None: |
|
73 | if argv is None: | |
74 | argv = sys.argv |
|
74 | argv = sys.argv | |
75 |
|
75 | |||
76 | conf = None |
|
76 | conf = None | |
77 | parser, args, other = argparser(argv) |
|
77 | parser, args, other = argparser(argv) | |
78 |
|
78 | |||
79 | api_credentials_given = (args.apikey and args.apihost) |
|
79 | api_credentials_given = (args.apikey and args.apihost) | |
80 | if args.save_config: |
|
80 | if args.save_config: | |
81 | if not api_credentials_given: |
|
81 | if not api_credentials_given: | |
82 | raise parser.error('--save-config requires --apikey and --apihost') |
|
82 | raise parser.error('--save-config requires --apikey and --apihost') | |
83 | conf = RcConf(config_location=args.config, |
|
83 | conf = RcConf(config_location=args.config, | |
84 | autocreate=True, config={'apikey': args.apikey, |
|
84 | autocreate=True, config={'apikey': args.apikey, | |
85 | 'apihost': args.apihost}) |
|
85 | 'apihost': args.apihost}) | |
86 | sys.exit() |
|
86 | sys.exit() | |
87 |
|
87 | |||
88 | if not conf: |
|
88 | if not conf: | |
89 | conf = RcConf(config_location=args.config, autoload=True) |
|
89 | conf = RcConf(config_location=args.config, autoload=True) | |
90 | if not conf: |
|
90 | if not conf: | |
91 | if not api_credentials_given: |
|
91 | if not api_credentials_given: | |
92 | parser.error('Could not find config file and missing ' |
|
92 | parser.error('Could not find config file and missing ' | |
93 | '--apikey or --apihost in params') |
|
93 | '--apikey or --apihost in params') | |
94 |
|
94 | |||
95 | apikey = args.apikey or conf['apikey'] |
|
95 | apikey = args.apikey or conf['apikey'] | |
96 | apihost = args.apihost or conf['apihost'] |
|
96 | apihost = args.apihost or conf['apihost'] | |
97 | method = args.method |
|
97 | method = args.method | |
98 |
|
98 | |||
99 | # if we don't have method here it's an error |
|
99 | # if we don't have method here it's an error | |
100 | if not method: |
|
100 | if not method: | |
101 | parser.error('Please specify method name') |
|
101 | parser.error('Please specify method name') | |
102 |
|
102 | |||
103 | try: |
|
103 | try: | |
104 |
margs = dict( |
|
104 | margs = dict(s.split(':', 1) for s in other) | |
105 | except ValueError: |
|
105 | except ValueError: | |
106 | sys.stderr.write('Error parsing arguments \n') |
|
106 | sys.stderr.write('Error parsing arguments \n') | |
107 | sys.exit() |
|
107 | sys.exit() | |
108 | if args.format == FORMAT_PRETTY: |
|
108 | if args.format == FORMAT_PRETTY: | |
109 | print('Calling method %s => %s' % (method, apihost)) |
|
109 | print('Calling method %s => %s' % (method, apihost)) | |
110 |
|
110 | |||
111 | json_resp = api_call(apikey, apihost, method, **margs) |
|
111 | json_resp = api_call(apikey, apihost, method, **margs) | |
112 | error_prefix = '' |
|
112 | error_prefix = '' | |
113 | if json_resp['error']: |
|
113 | if json_resp['error']: | |
114 | error_prefix = 'ERROR:' |
|
114 | error_prefix = 'ERROR:' | |
115 | json_data = json_resp['error'] |
|
115 | json_data = json_resp['error'] | |
116 | else: |
|
116 | else: | |
117 | json_data = json_resp['result'] |
|
117 | json_data = json_resp['result'] | |
118 | if args.format == FORMAT_JSON: |
|
118 | if args.format == FORMAT_JSON: | |
119 | print(json.dumps(json_data)) |
|
119 | print(json.dumps(json_data)) | |
120 | elif args.format == FORMAT_PRETTY: |
|
120 | elif args.format == FORMAT_PRETTY: | |
121 | print('Server response \n%s%s' % ( |
|
121 | print('Server response \n%s%s' % ( | |
122 | error_prefix, json.dumps(json_data, indent=4, sort_keys=True) |
|
122 | error_prefix, json.dumps(json_data, indent=4, sort_keys=True) | |
123 | )) |
|
123 | )) | |
124 | return 0 |
|
124 | return 0 | |
125 |
|
125 | |||
126 |
|
126 | |||
127 | if __name__ == '__main__': |
|
127 | if __name__ == '__main__': | |
128 | sys.exit(main(sys.argv)) |
|
128 | sys.exit(main(sys.argv)) |
@@ -1,401 +1,398 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | # This program is free software: you can redistribute it and/or modify |
|
2 | # This program is free software: you can redistribute it and/or modify | |
3 | # it under the terms of the GNU General Public License as published by |
|
3 | # it under the terms of the GNU General Public License as published by | |
4 | # the Free Software Foundation, either version 3 of the License, or |
|
4 | # the Free Software Foundation, either version 3 of the License, or | |
5 | # (at your option) any later version. |
|
5 | # (at your option) any later version. | |
6 | # |
|
6 | # | |
7 | # This program is distributed in the hope that it will be useful, |
|
7 | # This program is distributed in the hope that it will be useful, | |
8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
10 | # GNU General Public License for more details. |
|
10 | # GNU General Public License for more details. | |
11 | # |
|
11 | # | |
12 | # You should have received a copy of the GNU General Public License |
|
12 | # You should have received a copy of the GNU General Public License | |
13 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
13 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
14 | """ |
|
14 | """ | |
15 | kallithea.controllers.admin.repo_groups |
|
15 | kallithea.controllers.admin.repo_groups | |
16 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
16 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
17 |
|
17 | |||
18 | Repository groups controller for Kallithea |
|
18 | Repository groups controller for Kallithea | |
19 |
|
19 | |||
20 | This file was forked by the Kallithea project in July 2014. |
|
20 | This file was forked by the Kallithea project in July 2014. | |
21 | Original author and date, and relevant copyright and licensing information is below: |
|
21 | Original author and date, and relevant copyright and licensing information is below: | |
22 | :created_on: Mar 23, 2010 |
|
22 | :created_on: Mar 23, 2010 | |
23 | :author: marcink |
|
23 | :author: marcink | |
24 | :copyright: (c) 2013 RhodeCode GmbH, and others. |
|
24 | :copyright: (c) 2013 RhodeCode GmbH, and others. | |
25 | :license: GPLv3, see LICENSE.md for more details. |
|
25 | :license: GPLv3, see LICENSE.md for more details. | |
26 | """ |
|
26 | """ | |
27 |
|
27 | |||
28 | import itertools |
|
|||
29 | import logging |
|
28 | import logging | |
30 | import traceback |
|
29 | import traceback | |
31 |
|
30 | |||
32 | import formencode |
|
31 | import formencode | |
33 | from formencode import htmlfill |
|
32 | from formencode import htmlfill | |
34 | from tg import app_globals, request |
|
33 | from tg import app_globals, request | |
35 | from tg import tmpl_context as c |
|
34 | from tg import tmpl_context as c | |
36 | from tg.i18n import ugettext as _ |
|
35 | from tg.i18n import ugettext as _ | |
37 | from tg.i18n import ungettext |
|
36 | from tg.i18n import ungettext | |
38 | from webob.exc import HTTPForbidden, HTTPFound, HTTPInternalServerError, HTTPNotFound |
|
37 | from webob.exc import HTTPForbidden, HTTPFound, HTTPInternalServerError, HTTPNotFound | |
39 |
|
38 | |||
40 | import kallithea |
|
39 | import kallithea | |
41 | from kallithea.config.routing import url |
|
40 | from kallithea.config.routing import url | |
42 | from kallithea.lib import helpers as h |
|
41 | from kallithea.lib import helpers as h | |
43 | from kallithea.lib.auth import HasPermissionAny, HasRepoGroupPermissionLevel, HasRepoGroupPermissionLevelDecorator, LoginRequired |
|
42 | from kallithea.lib.auth import HasPermissionAny, HasRepoGroupPermissionLevel, HasRepoGroupPermissionLevelDecorator, LoginRequired | |
44 | from kallithea.lib.base import BaseController, render |
|
43 | from kallithea.lib.base import BaseController, render | |
45 | from kallithea.lib.utils2 import safe_int |
|
44 | from kallithea.lib.utils2 import safe_int, safe_unicode | |
46 | from kallithea.model.db import RepoGroup, Repository |
|
45 | from kallithea.model.db import RepoGroup, Repository | |
47 | from kallithea.model.forms import RepoGroupForm, RepoGroupPermsForm |
|
46 | from kallithea.model.forms import RepoGroupForm, RepoGroupPermsForm | |
48 | from kallithea.model.meta import Session |
|
47 | from kallithea.model.meta import Session | |
49 | from kallithea.model.repo import RepoModel |
|
48 | from kallithea.model.repo import RepoModel | |
50 | from kallithea.model.repo_group import RepoGroupModel |
|
49 | from kallithea.model.repo_group import RepoGroupModel | |
51 | from kallithea.model.scm import AvailableRepoGroupChoices, RepoGroupList |
|
50 | from kallithea.model.scm import AvailableRepoGroupChoices, RepoGroupList | |
52 |
|
51 | |||
53 |
|
52 | |||
54 | log = logging.getLogger(__name__) |
|
53 | log = logging.getLogger(__name__) | |
55 |
|
54 | |||
56 |
|
55 | |||
57 | class RepoGroupsController(BaseController): |
|
56 | class RepoGroupsController(BaseController): | |
58 |
|
57 | |||
59 | @LoginRequired(allow_default_user=True) |
|
58 | @LoginRequired(allow_default_user=True) | |
60 | def _before(self, *args, **kwargs): |
|
59 | def _before(self, *args, **kwargs): | |
61 | super(RepoGroupsController, self)._before(*args, **kwargs) |
|
60 | super(RepoGroupsController, self)._before(*args, **kwargs) | |
62 |
|
61 | |||
63 | def __load_defaults(self, extras=(), exclude=()): |
|
62 | def __load_defaults(self, extras=(), exclude=()): | |
64 | """extras is used for keeping current parent ignoring permissions |
|
63 | """extras is used for keeping current parent ignoring permissions | |
65 | exclude is used for not moving group to itself TODO: also exclude descendants |
|
64 | exclude is used for not moving group to itself TODO: also exclude descendants | |
66 | Note: only admin can create top level groups |
|
65 | Note: only admin can create top level groups | |
67 | """ |
|
66 | """ | |
68 | repo_groups = AvailableRepoGroupChoices([], 'admin', extras) |
|
67 | repo_groups = AvailableRepoGroupChoices([], 'admin', extras) | |
69 | exclude_group_ids = set(rg.group_id for rg in exclude) |
|
68 | exclude_group_ids = set(rg.group_id for rg in exclude) | |
70 | c.repo_groups = [rg for rg in repo_groups |
|
69 | c.repo_groups = [rg for rg in repo_groups | |
71 | if rg[0] not in exclude_group_ids] |
|
70 | if rg[0] not in exclude_group_ids] | |
72 |
|
71 | |||
73 | def __load_data(self, group_id): |
|
72 | def __load_data(self, group_id): | |
74 | """ |
|
73 | """ | |
75 | Load defaults settings for edit, and update |
|
74 | Load defaults settings for edit, and update | |
76 |
|
75 | |||
77 | :param group_id: |
|
76 | :param group_id: | |
78 | """ |
|
77 | """ | |
79 | repo_group = RepoGroup.get_or_404(group_id) |
|
78 | repo_group = RepoGroup.get_or_404(group_id) | |
80 | data = repo_group.get_dict() |
|
79 | data = repo_group.get_dict() | |
81 | data['group_name'] = repo_group.name |
|
80 | data['group_name'] = repo_group.name | |
82 |
|
81 | |||
83 | # fill repository group users |
|
82 | # fill repository group users | |
84 | for p in repo_group.repo_group_to_perm: |
|
83 | for p in repo_group.repo_group_to_perm: | |
85 | data.update({'u_perm_%s' % p.user.username: |
|
84 | data.update({'u_perm_%s' % p.user.username: | |
86 | p.permission.permission_name}) |
|
85 | p.permission.permission_name}) | |
87 |
|
86 | |||
88 | # fill repository group groups |
|
87 | # fill repository group groups | |
89 | for p in repo_group.users_group_to_perm: |
|
88 | for p in repo_group.users_group_to_perm: | |
90 | data.update({'g_perm_%s' % p.users_group.users_group_name: |
|
89 | data.update({'g_perm_%s' % p.users_group.users_group_name: | |
91 | p.permission.permission_name}) |
|
90 | p.permission.permission_name}) | |
92 |
|
91 | |||
93 | return data |
|
92 | return data | |
94 |
|
93 | |||
95 | def _revoke_perms_on_yourself(self, form_result): |
|
94 | def _revoke_perms_on_yourself(self, form_result): | |
96 | _up = filter(lambda u: request.authuser.username == u[0], |
|
95 | _up = filter(lambda u: request.authuser.username == u[0], | |
97 | form_result['perms_updates']) |
|
96 | form_result['perms_updates']) | |
98 | _new = filter(lambda u: request.authuser.username == u[0], |
|
97 | _new = filter(lambda u: request.authuser.username == u[0], | |
99 | form_result['perms_new']) |
|
98 | form_result['perms_new']) | |
100 | if _new and _new[0][1] != 'group.admin' or _up and _up[0][1] != 'group.admin': |
|
99 | if _new and _new[0][1] != 'group.admin' or _up and _up[0][1] != 'group.admin': | |
101 | return True |
|
100 | return True | |
102 | return False |
|
101 | return False | |
103 |
|
102 | |||
104 | def index(self, format='html'): |
|
103 | def index(self, format='html'): | |
105 | _list = RepoGroup.query(sorted=True).all() |
|
104 | _list = RepoGroup.query(sorted=True).all() | |
106 | group_iter = RepoGroupList(_list, perm_level='admin') |
|
105 | group_iter = RepoGroupList(_list, perm_level='admin') | |
107 | repo_groups_data = [] |
|
106 | repo_groups_data = [] | |
108 | total_records = len(group_iter) |
|
107 | total_records = len(group_iter) | |
109 | _tmpl_lookup = app_globals.mako_lookup |
|
108 | _tmpl_lookup = app_globals.mako_lookup | |
110 | template = _tmpl_lookup.get_template('data_table/_dt_elements.html') |
|
109 | template = _tmpl_lookup.get_template('data_table/_dt_elements.html') | |
111 |
|
110 | |||
112 | repo_group_name = lambda repo_group_name, children_groups: ( |
|
111 | repo_group_name = lambda repo_group_name, children_groups: ( | |
113 | template.get_def("repo_group_name") |
|
112 | template.get_def("repo_group_name") | |
114 | .render(repo_group_name, children_groups, _=_, h=h, c=c) |
|
113 | .render(repo_group_name, children_groups, _=_, h=h, c=c) | |
115 | ) |
|
114 | ) | |
116 | repo_group_actions = lambda repo_group_id, repo_group_name, gr_count: ( |
|
115 | repo_group_actions = lambda repo_group_id, repo_group_name, gr_count: ( | |
117 | template.get_def("repo_group_actions") |
|
116 | template.get_def("repo_group_actions") | |
118 | .render(repo_group_id, repo_group_name, gr_count, _=_, h=h, c=c, |
|
117 | .render(repo_group_id, repo_group_name, gr_count, _=_, h=h, c=c, | |
119 | ungettext=ungettext) |
|
118 | ungettext=ungettext) | |
120 | ) |
|
119 | ) | |
121 |
|
120 | |||
122 | for repo_gr in group_iter: |
|
121 | for repo_gr in group_iter: | |
123 | children_groups = map(h.safe_unicode, |
|
122 | children_groups = [safe_unicode(g.name) for g in repo_gr.parents] + [safe_unicode(repo_gr.name)] | |
124 | itertools.chain((g.name for g in repo_gr.parents), |
|
|||
125 | (x.name for x in [repo_gr]))) |
|
|||
126 | repo_count = repo_gr.repositories.count() |
|
123 | repo_count = repo_gr.repositories.count() | |
127 | repo_groups_data.append({ |
|
124 | repo_groups_data.append({ | |
128 | "raw_name": repo_gr.group_name, |
|
125 | "raw_name": repo_gr.group_name, | |
129 | "group_name": repo_group_name(repo_gr.group_name, children_groups), |
|
126 | "group_name": repo_group_name(repo_gr.group_name, children_groups), | |
130 | "desc": h.escape(repo_gr.group_description), |
|
127 | "desc": h.escape(repo_gr.group_description), | |
131 | "repos": repo_count, |
|
128 | "repos": repo_count, | |
132 | "owner": h.person(repo_gr.owner), |
|
129 | "owner": h.person(repo_gr.owner), | |
133 | "action": repo_group_actions(repo_gr.group_id, repo_gr.group_name, |
|
130 | "action": repo_group_actions(repo_gr.group_id, repo_gr.group_name, | |
134 | repo_count) |
|
131 | repo_count) | |
135 | }) |
|
132 | }) | |
136 |
|
133 | |||
137 | c.data = { |
|
134 | c.data = { | |
138 | "sort": None, |
|
135 | "sort": None, | |
139 | "dir": "asc", |
|
136 | "dir": "asc", | |
140 | "records": repo_groups_data |
|
137 | "records": repo_groups_data | |
141 | } |
|
138 | } | |
142 |
|
139 | |||
143 | return render('admin/repo_groups/repo_groups.html') |
|
140 | return render('admin/repo_groups/repo_groups.html') | |
144 |
|
141 | |||
145 | def create(self): |
|
142 | def create(self): | |
146 | self.__load_defaults() |
|
143 | self.__load_defaults() | |
147 |
|
144 | |||
148 | # permissions for can create group based on parent_id are checked |
|
145 | # permissions for can create group based on parent_id are checked | |
149 | # here in the Form |
|
146 | # here in the Form | |
150 | repo_group_form = RepoGroupForm(repo_groups=c.repo_groups) |
|
147 | repo_group_form = RepoGroupForm(repo_groups=c.repo_groups) | |
151 | try: |
|
148 | try: | |
152 | form_result = repo_group_form.to_python(dict(request.POST)) |
|
149 | form_result = repo_group_form.to_python(dict(request.POST)) | |
153 | gr = RepoGroupModel().create( |
|
150 | gr = RepoGroupModel().create( | |
154 | group_name=form_result['group_name'], |
|
151 | group_name=form_result['group_name'], | |
155 | group_description=form_result['group_description'], |
|
152 | group_description=form_result['group_description'], | |
156 | parent=form_result['parent_group_id'], |
|
153 | parent=form_result['parent_group_id'], | |
157 | owner=request.authuser.user_id, # TODO: make editable |
|
154 | owner=request.authuser.user_id, # TODO: make editable | |
158 | copy_permissions=form_result['group_copy_permissions'] |
|
155 | copy_permissions=form_result['group_copy_permissions'] | |
159 | ) |
|
156 | ) | |
160 | Session().commit() |
|
157 | Session().commit() | |
161 | # TODO: in future action_logger(, '', '', '') |
|
158 | # TODO: in future action_logger(, '', '', '') | |
162 | except formencode.Invalid as errors: |
|
159 | except formencode.Invalid as errors: | |
163 | return htmlfill.render( |
|
160 | return htmlfill.render( | |
164 | render('admin/repo_groups/repo_group_add.html'), |
|
161 | render('admin/repo_groups/repo_group_add.html'), | |
165 | defaults=errors.value, |
|
162 | defaults=errors.value, | |
166 | errors=errors.error_dict or {}, |
|
163 | errors=errors.error_dict or {}, | |
167 | prefix_error=False, |
|
164 | prefix_error=False, | |
168 | encoding="UTF-8", |
|
165 | encoding="UTF-8", | |
169 | force_defaults=False) |
|
166 | force_defaults=False) | |
170 | except Exception: |
|
167 | except Exception: | |
171 | log.error(traceback.format_exc()) |
|
168 | log.error(traceback.format_exc()) | |
172 | h.flash(_('Error occurred during creation of repository group %s') |
|
169 | h.flash(_('Error occurred during creation of repository group %s') | |
173 | % request.POST.get('group_name'), category='error') |
|
170 | % request.POST.get('group_name'), category='error') | |
174 | parent_group_id = form_result['parent_group_id'] |
|
171 | parent_group_id = form_result['parent_group_id'] | |
175 | # TODO: maybe we should get back to the main view, not the admin one |
|
172 | # TODO: maybe we should get back to the main view, not the admin one | |
176 | raise HTTPFound(location=url('repos_groups', parent_group=parent_group_id)) |
|
173 | raise HTTPFound(location=url('repos_groups', parent_group=parent_group_id)) | |
177 | h.flash(_('Created repository group %s') % gr.group_name, |
|
174 | h.flash(_('Created repository group %s') % gr.group_name, | |
178 | category='success') |
|
175 | category='success') | |
179 | raise HTTPFound(location=url('repos_group_home', group_name=gr.group_name)) |
|
176 | raise HTTPFound(location=url('repos_group_home', group_name=gr.group_name)) | |
180 |
|
177 | |||
181 | def new(self): |
|
178 | def new(self): | |
182 | if HasPermissionAny('hg.admin')('group create'): |
|
179 | if HasPermissionAny('hg.admin')('group create'): | |
183 | # we're global admin, we're ok and we can create TOP level groups |
|
180 | # we're global admin, we're ok and we can create TOP level groups | |
184 | pass |
|
181 | pass | |
185 | else: |
|
182 | else: | |
186 | # we pass in parent group into creation form, thus we know |
|
183 | # we pass in parent group into creation form, thus we know | |
187 | # what would be the group, we can check perms here ! |
|
184 | # what would be the group, we can check perms here ! | |
188 | group_id = safe_int(request.GET.get('parent_group')) |
|
185 | group_id = safe_int(request.GET.get('parent_group')) | |
189 | group = RepoGroup.get(group_id) if group_id else None |
|
186 | group = RepoGroup.get(group_id) if group_id else None | |
190 | group_name = group.group_name if group else None |
|
187 | group_name = group.group_name if group else None | |
191 | if HasRepoGroupPermissionLevel('admin')(group_name, 'group create'): |
|
188 | if HasRepoGroupPermissionLevel('admin')(group_name, 'group create'): | |
192 | pass |
|
189 | pass | |
193 | else: |
|
190 | else: | |
194 | raise HTTPForbidden() |
|
191 | raise HTTPForbidden() | |
195 |
|
192 | |||
196 | self.__load_defaults() |
|
193 | self.__load_defaults() | |
197 | return render('admin/repo_groups/repo_group_add.html') |
|
194 | return render('admin/repo_groups/repo_group_add.html') | |
198 |
|
195 | |||
199 | @HasRepoGroupPermissionLevelDecorator('admin') |
|
196 | @HasRepoGroupPermissionLevelDecorator('admin') | |
200 | def update(self, group_name): |
|
197 | def update(self, group_name): | |
201 | c.repo_group = RepoGroup.guess_instance(group_name) |
|
198 | c.repo_group = RepoGroup.guess_instance(group_name) | |
202 | self.__load_defaults(extras=[c.repo_group.parent_group], |
|
199 | self.__load_defaults(extras=[c.repo_group.parent_group], | |
203 | exclude=[c.repo_group]) |
|
200 | exclude=[c.repo_group]) | |
204 |
|
201 | |||
205 | # TODO: kill allow_empty_group - it is only used for redundant form validation! |
|
202 | # TODO: kill allow_empty_group - it is only used for redundant form validation! | |
206 | if HasPermissionAny('hg.admin')('group edit'): |
|
203 | if HasPermissionAny('hg.admin')('group edit'): | |
207 | # we're global admin, we're ok and we can create TOP level groups |
|
204 | # we're global admin, we're ok and we can create TOP level groups | |
208 | allow_empty_group = True |
|
205 | allow_empty_group = True | |
209 | elif not c.repo_group.parent_group: |
|
206 | elif not c.repo_group.parent_group: | |
210 | allow_empty_group = True |
|
207 | allow_empty_group = True | |
211 | else: |
|
208 | else: | |
212 | allow_empty_group = False |
|
209 | allow_empty_group = False | |
213 | repo_group_form = RepoGroupForm( |
|
210 | repo_group_form = RepoGroupForm( | |
214 | edit=True, |
|
211 | edit=True, | |
215 | old_data=c.repo_group.get_dict(), |
|
212 | old_data=c.repo_group.get_dict(), | |
216 | repo_groups=c.repo_groups, |
|
213 | repo_groups=c.repo_groups, | |
217 | can_create_in_root=allow_empty_group, |
|
214 | can_create_in_root=allow_empty_group, | |
218 | )() |
|
215 | )() | |
219 | try: |
|
216 | try: | |
220 | form_result = repo_group_form.to_python(dict(request.POST)) |
|
217 | form_result = repo_group_form.to_python(dict(request.POST)) | |
221 |
|
218 | |||
222 | new_gr = RepoGroupModel().update(group_name, form_result) |
|
219 | new_gr = RepoGroupModel().update(group_name, form_result) | |
223 | Session().commit() |
|
220 | Session().commit() | |
224 | h.flash(_('Updated repository group %s') |
|
221 | h.flash(_('Updated repository group %s') | |
225 | % form_result['group_name'], category='success') |
|
222 | % form_result['group_name'], category='success') | |
226 | # we now have new name ! |
|
223 | # we now have new name ! | |
227 | group_name = new_gr.group_name |
|
224 | group_name = new_gr.group_name | |
228 | # TODO: in future action_logger(, '', '', '') |
|
225 | # TODO: in future action_logger(, '', '', '') | |
229 | except formencode.Invalid as errors: |
|
226 | except formencode.Invalid as errors: | |
230 | c.active = 'settings' |
|
227 | c.active = 'settings' | |
231 | return htmlfill.render( |
|
228 | return htmlfill.render( | |
232 | render('admin/repo_groups/repo_group_edit.html'), |
|
229 | render('admin/repo_groups/repo_group_edit.html'), | |
233 | defaults=errors.value, |
|
230 | defaults=errors.value, | |
234 | errors=errors.error_dict or {}, |
|
231 | errors=errors.error_dict or {}, | |
235 | prefix_error=False, |
|
232 | prefix_error=False, | |
236 | encoding="UTF-8", |
|
233 | encoding="UTF-8", | |
237 | force_defaults=False) |
|
234 | force_defaults=False) | |
238 | except Exception: |
|
235 | except Exception: | |
239 | log.error(traceback.format_exc()) |
|
236 | log.error(traceback.format_exc()) | |
240 | h.flash(_('Error occurred during update of repository group %s') |
|
237 | h.flash(_('Error occurred during update of repository group %s') | |
241 | % request.POST.get('group_name'), category='error') |
|
238 | % request.POST.get('group_name'), category='error') | |
242 |
|
239 | |||
243 | raise HTTPFound(location=url('edit_repo_group', group_name=group_name)) |
|
240 | raise HTTPFound(location=url('edit_repo_group', group_name=group_name)) | |
244 |
|
241 | |||
245 | @HasRepoGroupPermissionLevelDecorator('admin') |
|
242 | @HasRepoGroupPermissionLevelDecorator('admin') | |
246 | def delete(self, group_name): |
|
243 | def delete(self, group_name): | |
247 | gr = c.repo_group = RepoGroup.guess_instance(group_name) |
|
244 | gr = c.repo_group = RepoGroup.guess_instance(group_name) | |
248 | repos = gr.repositories.all() |
|
245 | repos = gr.repositories.all() | |
249 | if repos: |
|
246 | if repos: | |
250 | h.flash(_('This group contains %s repositories and cannot be ' |
|
247 | h.flash(_('This group contains %s repositories and cannot be ' | |
251 | 'deleted') % len(repos), category='warning') |
|
248 | 'deleted') % len(repos), category='warning') | |
252 | raise HTTPFound(location=url('repos_groups')) |
|
249 | raise HTTPFound(location=url('repos_groups')) | |
253 |
|
250 | |||
254 | children = gr.children.all() |
|
251 | children = gr.children.all() | |
255 | if children: |
|
252 | if children: | |
256 | h.flash(_('This group contains %s subgroups and cannot be deleted' |
|
253 | h.flash(_('This group contains %s subgroups and cannot be deleted' | |
257 | % (len(children))), category='warning') |
|
254 | % (len(children))), category='warning') | |
258 | raise HTTPFound(location=url('repos_groups')) |
|
255 | raise HTTPFound(location=url('repos_groups')) | |
259 |
|
256 | |||
260 | try: |
|
257 | try: | |
261 | RepoGroupModel().delete(group_name) |
|
258 | RepoGroupModel().delete(group_name) | |
262 | Session().commit() |
|
259 | Session().commit() | |
263 | h.flash(_('Removed repository group %s') % group_name, |
|
260 | h.flash(_('Removed repository group %s') % group_name, | |
264 | category='success') |
|
261 | category='success') | |
265 | # TODO: in future action_logger(, '', '', '') |
|
262 | # TODO: in future action_logger(, '', '', '') | |
266 | except Exception: |
|
263 | except Exception: | |
267 | log.error(traceback.format_exc()) |
|
264 | log.error(traceback.format_exc()) | |
268 | h.flash(_('Error occurred during deletion of repository group %s') |
|
265 | h.flash(_('Error occurred during deletion of repository group %s') | |
269 | % group_name, category='error') |
|
266 | % group_name, category='error') | |
270 |
|
267 | |||
271 | if gr.parent_group: |
|
268 | if gr.parent_group: | |
272 | raise HTTPFound(location=url('repos_group_home', group_name=gr.parent_group.group_name)) |
|
269 | raise HTTPFound(location=url('repos_group_home', group_name=gr.parent_group.group_name)) | |
273 | raise HTTPFound(location=url('repos_groups')) |
|
270 | raise HTTPFound(location=url('repos_groups')) | |
274 |
|
271 | |||
275 | def show_by_name(self, group_name): |
|
272 | def show_by_name(self, group_name): | |
276 | """ |
|
273 | """ | |
277 | This is a proxy that does a lookup group_name -> id, and shows |
|
274 | This is a proxy that does a lookup group_name -> id, and shows | |
278 | the group by id view instead |
|
275 | the group by id view instead | |
279 | """ |
|
276 | """ | |
280 | group_name = group_name.rstrip('/') |
|
277 | group_name = group_name.rstrip('/') | |
281 | id_ = RepoGroup.get_by_group_name(group_name) |
|
278 | id_ = RepoGroup.get_by_group_name(group_name) | |
282 | if id_: |
|
279 | if id_: | |
283 | return self.show(group_name) |
|
280 | return self.show(group_name) | |
284 | raise HTTPNotFound |
|
281 | raise HTTPNotFound | |
285 |
|
282 | |||
286 | @HasRepoGroupPermissionLevelDecorator('read') |
|
283 | @HasRepoGroupPermissionLevelDecorator('read') | |
287 | def show(self, group_name): |
|
284 | def show(self, group_name): | |
288 | c.active = 'settings' |
|
285 | c.active = 'settings' | |
289 |
|
286 | |||
290 | c.group = c.repo_group = RepoGroup.guess_instance(group_name) |
|
287 | c.group = c.repo_group = RepoGroup.guess_instance(group_name) | |
291 |
|
288 | |||
292 | groups = RepoGroup.query(sorted=True).filter_by(parent_group=c.group).all() |
|
289 | groups = RepoGroup.query(sorted=True).filter_by(parent_group=c.group).all() | |
293 | repo_groups_list = self.scm_model.get_repo_groups(groups) |
|
290 | repo_groups_list = self.scm_model.get_repo_groups(groups) | |
294 |
|
291 | |||
295 | repos_list = Repository.query(sorted=True).filter_by(group=c.group).all() |
|
292 | repos_list = Repository.query(sorted=True).filter_by(group=c.group).all() | |
296 | c.data = RepoModel().get_repos_as_dict(repos_list, |
|
293 | c.data = RepoModel().get_repos_as_dict(repos_list, | |
297 | repo_groups_list=repo_groups_list, |
|
294 | repo_groups_list=repo_groups_list, | |
298 | short_name=True) |
|
295 | short_name=True) | |
299 |
|
296 | |||
300 | return render('admin/repo_groups/repo_group_show.html') |
|
297 | return render('admin/repo_groups/repo_group_show.html') | |
301 |
|
298 | |||
302 | @HasRepoGroupPermissionLevelDecorator('admin') |
|
299 | @HasRepoGroupPermissionLevelDecorator('admin') | |
303 | def edit(self, group_name): |
|
300 | def edit(self, group_name): | |
304 | c.active = 'settings' |
|
301 | c.active = 'settings' | |
305 |
|
302 | |||
306 | c.repo_group = RepoGroup.guess_instance(group_name) |
|
303 | c.repo_group = RepoGroup.guess_instance(group_name) | |
307 | self.__load_defaults(extras=[c.repo_group.parent_group], |
|
304 | self.__load_defaults(extras=[c.repo_group.parent_group], | |
308 | exclude=[c.repo_group]) |
|
305 | exclude=[c.repo_group]) | |
309 | defaults = self.__load_data(c.repo_group.group_id) |
|
306 | defaults = self.__load_data(c.repo_group.group_id) | |
310 |
|
307 | |||
311 | return htmlfill.render( |
|
308 | return htmlfill.render( | |
312 | render('admin/repo_groups/repo_group_edit.html'), |
|
309 | render('admin/repo_groups/repo_group_edit.html'), | |
313 | defaults=defaults, |
|
310 | defaults=defaults, | |
314 | encoding="UTF-8", |
|
311 | encoding="UTF-8", | |
315 | force_defaults=False |
|
312 | force_defaults=False | |
316 | ) |
|
313 | ) | |
317 |
|
314 | |||
318 | @HasRepoGroupPermissionLevelDecorator('admin') |
|
315 | @HasRepoGroupPermissionLevelDecorator('admin') | |
319 | def edit_repo_group_advanced(self, group_name): |
|
316 | def edit_repo_group_advanced(self, group_name): | |
320 | c.active = 'advanced' |
|
317 | c.active = 'advanced' | |
321 | c.repo_group = RepoGroup.guess_instance(group_name) |
|
318 | c.repo_group = RepoGroup.guess_instance(group_name) | |
322 |
|
319 | |||
323 | return render('admin/repo_groups/repo_group_edit.html') |
|
320 | return render('admin/repo_groups/repo_group_edit.html') | |
324 |
|
321 | |||
325 | @HasRepoGroupPermissionLevelDecorator('admin') |
|
322 | @HasRepoGroupPermissionLevelDecorator('admin') | |
326 | def edit_repo_group_perms(self, group_name): |
|
323 | def edit_repo_group_perms(self, group_name): | |
327 | c.active = 'perms' |
|
324 | c.active = 'perms' | |
328 | c.repo_group = RepoGroup.guess_instance(group_name) |
|
325 | c.repo_group = RepoGroup.guess_instance(group_name) | |
329 | self.__load_defaults() |
|
326 | self.__load_defaults() | |
330 | defaults = self.__load_data(c.repo_group.group_id) |
|
327 | defaults = self.__load_data(c.repo_group.group_id) | |
331 |
|
328 | |||
332 | return htmlfill.render( |
|
329 | return htmlfill.render( | |
333 | render('admin/repo_groups/repo_group_edit.html'), |
|
330 | render('admin/repo_groups/repo_group_edit.html'), | |
334 | defaults=defaults, |
|
331 | defaults=defaults, | |
335 | encoding="UTF-8", |
|
332 | encoding="UTF-8", | |
336 | force_defaults=False |
|
333 | force_defaults=False | |
337 | ) |
|
334 | ) | |
338 |
|
335 | |||
339 | @HasRepoGroupPermissionLevelDecorator('admin') |
|
336 | @HasRepoGroupPermissionLevelDecorator('admin') | |
340 | def update_perms(self, group_name): |
|
337 | def update_perms(self, group_name): | |
341 | """ |
|
338 | """ | |
342 | Update permissions for given repository group |
|
339 | Update permissions for given repository group | |
343 |
|
340 | |||
344 | :param group_name: |
|
341 | :param group_name: | |
345 | """ |
|
342 | """ | |
346 |
|
343 | |||
347 | c.repo_group = RepoGroup.guess_instance(group_name) |
|
344 | c.repo_group = RepoGroup.guess_instance(group_name) | |
348 | valid_recursive_choices = ['none', 'repos', 'groups', 'all'] |
|
345 | valid_recursive_choices = ['none', 'repos', 'groups', 'all'] | |
349 | form_result = RepoGroupPermsForm(valid_recursive_choices)().to_python(request.POST) |
|
346 | form_result = RepoGroupPermsForm(valid_recursive_choices)().to_python(request.POST) | |
350 | if not request.authuser.is_admin: |
|
347 | if not request.authuser.is_admin: | |
351 | if self._revoke_perms_on_yourself(form_result): |
|
348 | if self._revoke_perms_on_yourself(form_result): | |
352 | msg = _('Cannot revoke permission for yourself as admin') |
|
349 | msg = _('Cannot revoke permission for yourself as admin') | |
353 | h.flash(msg, category='warning') |
|
350 | h.flash(msg, category='warning') | |
354 | raise HTTPFound(location=url('edit_repo_group_perms', group_name=group_name)) |
|
351 | raise HTTPFound(location=url('edit_repo_group_perms', group_name=group_name)) | |
355 | recursive = form_result['recursive'] |
|
352 | recursive = form_result['recursive'] | |
356 | # iterate over all members(if in recursive mode) of this groups and |
|
353 | # iterate over all members(if in recursive mode) of this groups and | |
357 | # set the permissions ! |
|
354 | # set the permissions ! | |
358 | # this can be potentially heavy operation |
|
355 | # this can be potentially heavy operation | |
359 | RepoGroupModel()._update_permissions(c.repo_group, |
|
356 | RepoGroupModel()._update_permissions(c.repo_group, | |
360 | form_result['perms_new'], |
|
357 | form_result['perms_new'], | |
361 | form_result['perms_updates'], |
|
358 | form_result['perms_updates'], | |
362 | recursive) |
|
359 | recursive) | |
363 | # TODO: implement this |
|
360 | # TODO: implement this | |
364 | #action_logger(request.authuser, 'admin_changed_repo_permissions', |
|
361 | #action_logger(request.authuser, 'admin_changed_repo_permissions', | |
365 | # repo_name, request.ip_addr) |
|
362 | # repo_name, request.ip_addr) | |
366 | Session().commit() |
|
363 | Session().commit() | |
367 | h.flash(_('Repository group permissions updated'), category='success') |
|
364 | h.flash(_('Repository group permissions updated'), category='success') | |
368 | raise HTTPFound(location=url('edit_repo_group_perms', group_name=group_name)) |
|
365 | raise HTTPFound(location=url('edit_repo_group_perms', group_name=group_name)) | |
369 |
|
366 | |||
370 | @HasRepoGroupPermissionLevelDecorator('admin') |
|
367 | @HasRepoGroupPermissionLevelDecorator('admin') | |
371 | def delete_perms(self, group_name): |
|
368 | def delete_perms(self, group_name): | |
372 | try: |
|
369 | try: | |
373 | obj_type = request.POST.get('obj_type') |
|
370 | obj_type = request.POST.get('obj_type') | |
374 | obj_id = None |
|
371 | obj_id = None | |
375 | if obj_type == 'user': |
|
372 | if obj_type == 'user': | |
376 | obj_id = safe_int(request.POST.get('user_id')) |
|
373 | obj_id = safe_int(request.POST.get('user_id')) | |
377 | elif obj_type == 'user_group': |
|
374 | elif obj_type == 'user_group': | |
378 | obj_id = safe_int(request.POST.get('user_group_id')) |
|
375 | obj_id = safe_int(request.POST.get('user_group_id')) | |
379 |
|
376 | |||
380 | if not request.authuser.is_admin: |
|
377 | if not request.authuser.is_admin: | |
381 | if obj_type == 'user' and request.authuser.user_id == obj_id: |
|
378 | if obj_type == 'user' and request.authuser.user_id == obj_id: | |
382 | msg = _('Cannot revoke permission for yourself as admin') |
|
379 | msg = _('Cannot revoke permission for yourself as admin') | |
383 | h.flash(msg, category='warning') |
|
380 | h.flash(msg, category='warning') | |
384 | raise Exception('revoke admin permission on self') |
|
381 | raise Exception('revoke admin permission on self') | |
385 | recursive = request.POST.get('recursive', 'none') |
|
382 | recursive = request.POST.get('recursive', 'none') | |
386 | if obj_type == 'user': |
|
383 | if obj_type == 'user': | |
387 | RepoGroupModel().delete_permission(repo_group=group_name, |
|
384 | RepoGroupModel().delete_permission(repo_group=group_name, | |
388 | obj=obj_id, obj_type='user', |
|
385 | obj=obj_id, obj_type='user', | |
389 | recursive=recursive) |
|
386 | recursive=recursive) | |
390 | elif obj_type == 'user_group': |
|
387 | elif obj_type == 'user_group': | |
391 | RepoGroupModel().delete_permission(repo_group=group_name, |
|
388 | RepoGroupModel().delete_permission(repo_group=group_name, | |
392 | obj=obj_id, |
|
389 | obj=obj_id, | |
393 | obj_type='user_group', |
|
390 | obj_type='user_group', | |
394 | recursive=recursive) |
|
391 | recursive=recursive) | |
395 |
|
392 | |||
396 | Session().commit() |
|
393 | Session().commit() | |
397 | except Exception: |
|
394 | except Exception: | |
398 | log.error(traceback.format_exc()) |
|
395 | log.error(traceback.format_exc()) | |
399 | h.flash(_('An error occurred during revoking of permission'), |
|
396 | h.flash(_('An error occurred during revoking of permission'), | |
400 | category='error') |
|
397 | category='error') | |
401 | raise HTTPInternalServerError() |
|
398 | raise HTTPInternalServerError() |
@@ -1,269 +1,269 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | # This program is free software: you can redistribute it and/or modify |
|
2 | # This program is free software: you can redistribute it and/or modify | |
3 | # it under the terms of the GNU General Public License as published by |
|
3 | # it under the terms of the GNU General Public License as published by | |
4 | # the Free Software Foundation, either version 3 of the License, or |
|
4 | # the Free Software Foundation, either version 3 of the License, or | |
5 | # (at your option) any later version. |
|
5 | # (at your option) any later version. | |
6 | # |
|
6 | # | |
7 | # This program is distributed in the hope that it will be useful, |
|
7 | # This program is distributed in the hope that it will be useful, | |
8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
10 | # GNU General Public License for more details. |
|
10 | # GNU General Public License for more details. | |
11 | # |
|
11 | # | |
12 | # You should have received a copy of the GNU General Public License |
|
12 | # You should have received a copy of the GNU General Public License | |
13 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
13 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
14 | """ |
|
14 | """ | |
15 | kallithea.controllers.api |
|
15 | kallithea.controllers.api | |
16 | ~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
16 | ~~~~~~~~~~~~~~~~~~~~~~~~~ | |
17 |
|
17 | |||
18 | JSON RPC controller |
|
18 | JSON RPC controller | |
19 |
|
19 | |||
20 | This file was forked by the Kallithea project in July 2014. |
|
20 | This file was forked by the Kallithea project in July 2014. | |
21 | Original author and date, and relevant copyright and licensing information is below: |
|
21 | Original author and date, and relevant copyright and licensing information is below: | |
22 | :created_on: Aug 20, 2011 |
|
22 | :created_on: Aug 20, 2011 | |
23 | :author: marcink |
|
23 | :author: marcink | |
24 | :copyright: (c) 2013 RhodeCode GmbH, and others. |
|
24 | :copyright: (c) 2013 RhodeCode GmbH, and others. | |
25 | :license: GPLv3, see LICENSE.md for more details. |
|
25 | :license: GPLv3, see LICENSE.md for more details. | |
26 | """ |
|
26 | """ | |
27 |
|
27 | |||
28 | import inspect |
|
28 | import inspect | |
29 | import itertools |
|
29 | import itertools | |
30 | import logging |
|
30 | import logging | |
31 | import time |
|
31 | import time | |
32 | import traceback |
|
32 | import traceback | |
33 | import types |
|
33 | import types | |
34 |
|
34 | |||
35 | from tg import Response, TGController, request, response |
|
35 | from tg import Response, TGController, request, response | |
36 | from webob.exc import HTTPError, HTTPException |
|
36 | from webob.exc import HTTPError, HTTPException | |
37 |
|
37 | |||
38 | from kallithea.lib.auth import AuthUser |
|
38 | from kallithea.lib.auth import AuthUser | |
39 | from kallithea.lib.base import _get_access_path |
|
39 | from kallithea.lib.base import _get_access_path | |
40 | from kallithea.lib.base import _get_ip_addr as _get_ip |
|
40 | from kallithea.lib.base import _get_ip_addr as _get_ip | |
41 | from kallithea.lib.compat import json |
|
41 | from kallithea.lib.compat import json | |
42 | from kallithea.lib.utils2 import safe_str, safe_unicode |
|
42 | from kallithea.lib.utils2 import safe_str, safe_unicode | |
43 | from kallithea.model.db import User |
|
43 | from kallithea.model.db import User | |
44 |
|
44 | |||
45 |
|
45 | |||
46 | log = logging.getLogger('JSONRPC') |
|
46 | log = logging.getLogger('JSONRPC') | |
47 |
|
47 | |||
48 |
|
48 | |||
49 | class JSONRPCError(BaseException): |
|
49 | class JSONRPCError(BaseException): | |
50 |
|
50 | |||
51 | def __init__(self, message): |
|
51 | def __init__(self, message): | |
52 | self.message = message |
|
52 | self.message = message | |
53 | super(JSONRPCError, self).__init__() |
|
53 | super(JSONRPCError, self).__init__() | |
54 |
|
54 | |||
55 | def __str__(self): |
|
55 | def __str__(self): | |
56 | return safe_str(self.message) |
|
56 | return safe_str(self.message) | |
57 |
|
57 | |||
58 |
|
58 | |||
59 | class JSONRPCErrorResponse(Response, HTTPException): |
|
59 | class JSONRPCErrorResponse(Response, HTTPException): | |
60 | """ |
|
60 | """ | |
61 | Generate a Response object with a JSON-RPC error body |
|
61 | Generate a Response object with a JSON-RPC error body | |
62 | """ |
|
62 | """ | |
63 |
|
63 | |||
64 | def __init__(self, message=None, retid=None, code=None): |
|
64 | def __init__(self, message=None, retid=None, code=None): | |
65 | HTTPException.__init__(self, message, self) |
|
65 | HTTPException.__init__(self, message, self) | |
66 | Response.__init__(self, |
|
66 | Response.__init__(self, | |
67 | json_body=dict(id=retid, result=None, error=message), |
|
67 | json_body=dict(id=retid, result=None, error=message), | |
68 | status=code, |
|
68 | status=code, | |
69 | content_type='application/json') |
|
69 | content_type='application/json') | |
70 |
|
70 | |||
71 |
|
71 | |||
72 | class JSONRPCController(TGController): |
|
72 | class JSONRPCController(TGController): | |
73 | """ |
|
73 | """ | |
74 | A WSGI-speaking JSON-RPC controller class |
|
74 | A WSGI-speaking JSON-RPC controller class | |
75 |
|
75 | |||
76 | See the specification: |
|
76 | See the specification: | |
77 | <http://json-rpc.org/wiki/specification>`. |
|
77 | <http://json-rpc.org/wiki/specification>`. | |
78 |
|
78 | |||
79 | Valid controller return values should be json-serializable objects. |
|
79 | Valid controller return values should be json-serializable objects. | |
80 |
|
80 | |||
81 | Sub-classes should catch their exceptions and raise JSONRPCError |
|
81 | Sub-classes should catch their exceptions and raise JSONRPCError | |
82 | if they want to pass meaningful errors to the client. |
|
82 | if they want to pass meaningful errors to the client. | |
83 |
|
83 | |||
84 | """ |
|
84 | """ | |
85 |
|
85 | |||
86 | def _get_ip_addr(self, environ): |
|
86 | def _get_ip_addr(self, environ): | |
87 | return _get_ip(environ) |
|
87 | return _get_ip(environ) | |
88 |
|
88 | |||
89 | def _get_method_args(self): |
|
89 | def _get_method_args(self): | |
90 | """ |
|
90 | """ | |
91 | Return `self._rpc_args` to dispatched controller method |
|
91 | Return `self._rpc_args` to dispatched controller method | |
92 | chosen by __call__ |
|
92 | chosen by __call__ | |
93 | """ |
|
93 | """ | |
94 | return self._rpc_args |
|
94 | return self._rpc_args | |
95 |
|
95 | |||
96 | def _dispatch(self, state, remainder=None): |
|
96 | def _dispatch(self, state, remainder=None): | |
97 | """ |
|
97 | """ | |
98 | Parse the request body as JSON, look up the method on the |
|
98 | Parse the request body as JSON, look up the method on the | |
99 | controller and if it exists, dispatch to it. |
|
99 | controller and if it exists, dispatch to it. | |
100 | """ |
|
100 | """ | |
101 | # Since we are here we should respond as JSON |
|
101 | # Since we are here we should respond as JSON | |
102 | response.content_type = 'application/json' |
|
102 | response.content_type = 'application/json' | |
103 |
|
103 | |||
104 | environ = state.request.environ |
|
104 | environ = state.request.environ | |
105 | start = time.time() |
|
105 | start = time.time() | |
106 | ip_addr = self._get_ip_addr(environ) |
|
106 | ip_addr = self._get_ip_addr(environ) | |
107 | self._req_id = None |
|
107 | self._req_id = None | |
108 | if 'CONTENT_LENGTH' not in environ: |
|
108 | if 'CONTENT_LENGTH' not in environ: | |
109 | log.debug("No Content-Length") |
|
109 | log.debug("No Content-Length") | |
110 | raise JSONRPCErrorResponse(retid=self._req_id, |
|
110 | raise JSONRPCErrorResponse(retid=self._req_id, | |
111 | message="No Content-Length in request") |
|
111 | message="No Content-Length in request") | |
112 | else: |
|
112 | else: | |
113 | length = environ['CONTENT_LENGTH'] or 0 |
|
113 | length = environ['CONTENT_LENGTH'] or 0 | |
114 | length = int(environ['CONTENT_LENGTH']) |
|
114 | length = int(environ['CONTENT_LENGTH']) | |
115 | log.debug('Content-Length: %s', length) |
|
115 | log.debug('Content-Length: %s', length) | |
116 |
|
116 | |||
117 | if length == 0: |
|
117 | if length == 0: | |
118 | raise JSONRPCErrorResponse(retid=self._req_id, |
|
118 | raise JSONRPCErrorResponse(retid=self._req_id, | |
119 | message="Content-Length is 0") |
|
119 | message="Content-Length is 0") | |
120 |
|
120 | |||
121 | raw_body = environ['wsgi.input'].read(length) |
|
121 | raw_body = environ['wsgi.input'].read(length) | |
122 |
|
122 | |||
123 | try: |
|
123 | try: | |
124 | json_body = json.loads(raw_body) |
|
124 | json_body = json.loads(raw_body) | |
125 | except ValueError as e: |
|
125 | except ValueError as e: | |
126 | # catch JSON errors Here |
|
126 | # catch JSON errors Here | |
127 | raise JSONRPCErrorResponse(retid=self._req_id, |
|
127 | raise JSONRPCErrorResponse(retid=self._req_id, | |
128 | message="JSON parse error ERR:%s RAW:%r" |
|
128 | message="JSON parse error ERR:%s RAW:%r" | |
129 | % (e, raw_body)) |
|
129 | % (e, raw_body)) | |
130 |
|
130 | |||
131 | # check AUTH based on API key |
|
131 | # check AUTH based on API key | |
132 | try: |
|
132 | try: | |
133 | self._req_api_key = json_body['api_key'] |
|
133 | self._req_api_key = json_body['api_key'] | |
134 | self._req_id = json_body['id'] |
|
134 | self._req_id = json_body['id'] | |
135 | self._req_method = json_body['method'] |
|
135 | self._req_method = json_body['method'] | |
136 | self._request_params = json_body['args'] |
|
136 | self._request_params = json_body['args'] | |
137 | if not isinstance(self._request_params, dict): |
|
137 | if not isinstance(self._request_params, dict): | |
138 | self._request_params = {} |
|
138 | self._request_params = {} | |
139 |
|
139 | |||
140 | log.debug('method: %s, params: %s', |
|
140 | log.debug('method: %s, params: %s', | |
141 | self._req_method, self._request_params) |
|
141 | self._req_method, self._request_params) | |
142 | except KeyError as e: |
|
142 | except KeyError as e: | |
143 | raise JSONRPCErrorResponse(retid=self._req_id, |
|
143 | raise JSONRPCErrorResponse(retid=self._req_id, | |
144 | message='Incorrect JSON query missing %s' % e) |
|
144 | message='Incorrect JSON query missing %s' % e) | |
145 |
|
145 | |||
146 | # check if we can find this session using api_key |
|
146 | # check if we can find this session using api_key | |
147 | try: |
|
147 | try: | |
148 | u = User.get_by_api_key(self._req_api_key) |
|
148 | u = User.get_by_api_key(self._req_api_key) | |
149 | auth_user = AuthUser.make(dbuser=u, ip_addr=ip_addr) |
|
149 | auth_user = AuthUser.make(dbuser=u, ip_addr=ip_addr) | |
150 | if auth_user is None: |
|
150 | if auth_user is None: | |
151 | raise JSONRPCErrorResponse(retid=self._req_id, |
|
151 | raise JSONRPCErrorResponse(retid=self._req_id, | |
152 | message='Invalid API key') |
|
152 | message='Invalid API key') | |
153 | except Exception as e: |
|
153 | except Exception as e: | |
154 | raise JSONRPCErrorResponse(retid=self._req_id, |
|
154 | raise JSONRPCErrorResponse(retid=self._req_id, | |
155 | message='Invalid API key') |
|
155 | message='Invalid API key') | |
156 |
|
156 | |||
157 | request.authuser = auth_user |
|
157 | request.authuser = auth_user | |
158 | request.ip_addr = ip_addr |
|
158 | request.ip_addr = ip_addr | |
159 |
|
159 | |||
160 | self._error = None |
|
160 | self._error = None | |
161 | try: |
|
161 | try: | |
162 | self._func = self._find_method() |
|
162 | self._func = self._find_method() | |
163 | except AttributeError as e: |
|
163 | except AttributeError as e: | |
164 | raise JSONRPCErrorResponse(retid=self._req_id, |
|
164 | raise JSONRPCErrorResponse(retid=self._req_id, | |
165 | message=str(e)) |
|
165 | message=str(e)) | |
166 |
|
166 | |||
167 | # now that we have a method, add self._req_params to |
|
167 | # now that we have a method, add self._req_params to | |
168 | # self.kargs and dispatch control to WGIController |
|
168 | # self.kargs and dispatch control to WGIController | |
169 | argspec = inspect.getargspec(self._func) |
|
169 | argspec = inspect.getargspec(self._func) | |
170 | arglist = argspec[0][1:] |
|
170 | arglist = argspec[0][1:] | |
171 |
defaults = |
|
171 | defaults = [type(arg) for arg in argspec[3] or []] | |
172 | default_empty = type(NotImplemented) |
|
172 | default_empty = type(NotImplemented) | |
173 |
|
173 | |||
174 | # kw arguments required by this method |
|
174 | # kw arguments required by this method | |
175 | func_kwargs = dict(itertools.izip_longest(reversed(arglist), reversed(defaults), |
|
175 | func_kwargs = dict(itertools.izip_longest(reversed(arglist), reversed(defaults), | |
176 | fillvalue=default_empty)) |
|
176 | fillvalue=default_empty)) | |
177 |
|
177 | |||
178 | # This attribute will need to be first param of a method that uses |
|
178 | # This attribute will need to be first param of a method that uses | |
179 | # api_key, which is translated to instance of user at that name |
|
179 | # api_key, which is translated to instance of user at that name | |
180 | USER_SESSION_ATTR = 'apiuser' |
|
180 | USER_SESSION_ATTR = 'apiuser' | |
181 |
|
181 | |||
182 | # get our arglist and check if we provided them as args |
|
182 | # get our arglist and check if we provided them as args | |
183 | for arg, default in func_kwargs.iteritems(): |
|
183 | for arg, default in func_kwargs.iteritems(): | |
184 | if arg == USER_SESSION_ATTR: |
|
184 | if arg == USER_SESSION_ATTR: | |
185 | # USER_SESSION_ATTR is something translated from API key and |
|
185 | # USER_SESSION_ATTR is something translated from API key and | |
186 | # this is checked before so we don't need validate it |
|
186 | # this is checked before so we don't need validate it | |
187 | continue |
|
187 | continue | |
188 |
|
188 | |||
189 | # skip the required param check if it's default value is |
|
189 | # skip the required param check if it's default value is | |
190 | # NotImplementedType (default_empty) |
|
190 | # NotImplementedType (default_empty) | |
191 | if default == default_empty and arg not in self._request_params: |
|
191 | if default == default_empty and arg not in self._request_params: | |
192 | raise JSONRPCErrorResponse( |
|
192 | raise JSONRPCErrorResponse( | |
193 | retid=self._req_id, |
|
193 | retid=self._req_id, | |
194 | message='Missing non optional `%s` arg in JSON DATA' % arg, |
|
194 | message='Missing non optional `%s` arg in JSON DATA' % arg, | |
195 | ) |
|
195 | ) | |
196 |
|
196 | |||
197 | extra = set(self._request_params).difference(func_kwargs) |
|
197 | extra = set(self._request_params).difference(func_kwargs) | |
198 | if extra: |
|
198 | if extra: | |
199 | raise JSONRPCErrorResponse( |
|
199 | raise JSONRPCErrorResponse( | |
200 | retid=self._req_id, |
|
200 | retid=self._req_id, | |
201 | message='Unknown %s arg in JSON DATA' % |
|
201 | message='Unknown %s arg in JSON DATA' % | |
202 | ', '.join('`%s`' % arg for arg in extra), |
|
202 | ', '.join('`%s`' % arg for arg in extra), | |
203 | ) |
|
203 | ) | |
204 |
|
204 | |||
205 | self._rpc_args = {} |
|
205 | self._rpc_args = {} | |
206 | self._rpc_args.update(self._request_params) |
|
206 | self._rpc_args.update(self._request_params) | |
207 | self._rpc_args['action'] = self._req_method |
|
207 | self._rpc_args['action'] = self._req_method | |
208 | self._rpc_args['environ'] = environ |
|
208 | self._rpc_args['environ'] = environ | |
209 |
|
209 | |||
210 | log.info('IP: %s Request to %s time: %.3fs' % ( |
|
210 | log.info('IP: %s Request to %s time: %.3fs' % ( | |
211 | self._get_ip_addr(environ), |
|
211 | self._get_ip_addr(environ), | |
212 | safe_unicode(_get_access_path(environ)), time.time() - start) |
|
212 | safe_unicode(_get_access_path(environ)), time.time() - start) | |
213 | ) |
|
213 | ) | |
214 |
|
214 | |||
215 | state.set_action(self._rpc_call, []) |
|
215 | state.set_action(self._rpc_call, []) | |
216 | state.set_params(self._rpc_args) |
|
216 | state.set_params(self._rpc_args) | |
217 | return state |
|
217 | return state | |
218 |
|
218 | |||
219 | def _rpc_call(self, action, environ, **rpc_args): |
|
219 | def _rpc_call(self, action, environ, **rpc_args): | |
220 | """ |
|
220 | """ | |
221 | Call the specified RPC Method |
|
221 | Call the specified RPC Method | |
222 | """ |
|
222 | """ | |
223 | raw_response = '' |
|
223 | raw_response = '' | |
224 | try: |
|
224 | try: | |
225 | raw_response = getattr(self, action)(**rpc_args) |
|
225 | raw_response = getattr(self, action)(**rpc_args) | |
226 | if isinstance(raw_response, HTTPError): |
|
226 | if isinstance(raw_response, HTTPError): | |
227 | self._error = str(raw_response) |
|
227 | self._error = str(raw_response) | |
228 | except JSONRPCError as e: |
|
228 | except JSONRPCError as e: | |
229 | self._error = safe_str(e) |
|
229 | self._error = safe_str(e) | |
230 | except Exception as e: |
|
230 | except Exception as e: | |
231 | log.error('Encountered unhandled exception: %s', |
|
231 | log.error('Encountered unhandled exception: %s', | |
232 | traceback.format_exc(),) |
|
232 | traceback.format_exc(),) | |
233 | json_exc = JSONRPCError('Internal server error') |
|
233 | json_exc = JSONRPCError('Internal server error') | |
234 | self._error = safe_str(json_exc) |
|
234 | self._error = safe_str(json_exc) | |
235 |
|
235 | |||
236 | if self._error is not None: |
|
236 | if self._error is not None: | |
237 | raw_response = None |
|
237 | raw_response = None | |
238 |
|
238 | |||
239 | response = dict(id=self._req_id, result=raw_response, error=self._error) |
|
239 | response = dict(id=self._req_id, result=raw_response, error=self._error) | |
240 | try: |
|
240 | try: | |
241 | return json.dumps(response) |
|
241 | return json.dumps(response) | |
242 | except TypeError as e: |
|
242 | except TypeError as e: | |
243 | log.error('API FAILED. Error encoding response: %s', e) |
|
243 | log.error('API FAILED. Error encoding response: %s', e) | |
244 | return json.dumps( |
|
244 | return json.dumps( | |
245 | dict( |
|
245 | dict( | |
246 | id=self._req_id, |
|
246 | id=self._req_id, | |
247 | result=None, |
|
247 | result=None, | |
248 | error="Error encoding response" |
|
248 | error="Error encoding response" | |
249 | ) |
|
249 | ) | |
250 | ) |
|
250 | ) | |
251 |
|
251 | |||
252 | def _find_method(self): |
|
252 | def _find_method(self): | |
253 | """ |
|
253 | """ | |
254 | Return method named by `self._req_method` in controller if able |
|
254 | Return method named by `self._req_method` in controller if able | |
255 | """ |
|
255 | """ | |
256 | log.debug('Trying to find JSON-RPC method: %s', self._req_method) |
|
256 | log.debug('Trying to find JSON-RPC method: %s', self._req_method) | |
257 | if self._req_method.startswith('_'): |
|
257 | if self._req_method.startswith('_'): | |
258 | raise AttributeError("Method not allowed") |
|
258 | raise AttributeError("Method not allowed") | |
259 |
|
259 | |||
260 | try: |
|
260 | try: | |
261 | func = getattr(self, self._req_method, None) |
|
261 | func = getattr(self, self._req_method, None) | |
262 | except UnicodeEncodeError: |
|
262 | except UnicodeEncodeError: | |
263 | raise AttributeError("Problem decoding unicode in requested " |
|
263 | raise AttributeError("Problem decoding unicode in requested " | |
264 | "method name.") |
|
264 | "method name.") | |
265 |
|
265 | |||
266 | if isinstance(func, types.MethodType): |
|
266 | if isinstance(func, types.MethodType): | |
267 | return func |
|
267 | return func | |
268 | else: |
|
268 | else: | |
269 | raise AttributeError("No such method: %s" % (self._req_method,)) |
|
269 | raise AttributeError("No such method: %s" % (self._req_method,)) |
@@ -1,139 +1,139 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | # This program is free software: you can redistribute it and/or modify |
|
2 | # This program is free software: you can redistribute it and/or modify | |
3 | # it under the terms of the GNU General Public License as published by |
|
3 | # it under the terms of the GNU General Public License as published by | |
4 | # the Free Software Foundation, either version 3 of the License, or |
|
4 | # the Free Software Foundation, either version 3 of the License, or | |
5 | # (at your option) any later version. |
|
5 | # (at your option) any later version. | |
6 | # |
|
6 | # | |
7 | # This program is distributed in the hope that it will be useful, |
|
7 | # This program is distributed in the hope that it will be useful, | |
8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
10 | # GNU General Public License for more details. |
|
10 | # GNU General Public License for more details. | |
11 | # |
|
11 | # | |
12 | # You should have received a copy of the GNU General Public License |
|
12 | # You should have received a copy of the GNU General Public License | |
13 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
13 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
14 | """ |
|
14 | """ | |
15 | kallithea.controllers.feed |
|
15 | kallithea.controllers.feed | |
16 | ~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
16 | ~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
17 |
|
17 | |||
18 | Feed controller for Kallithea |
|
18 | Feed controller for Kallithea | |
19 |
|
19 | |||
20 | This file was forked by the Kallithea project in July 2014. |
|
20 | This file was forked by the Kallithea project in July 2014. | |
21 | Original author and date, and relevant copyright and licensing information is below: |
|
21 | Original author and date, and relevant copyright and licensing information is below: | |
22 | :created_on: Apr 23, 2010 |
|
22 | :created_on: Apr 23, 2010 | |
23 | :author: marcink |
|
23 | :author: marcink | |
24 | :copyright: (c) 2013 RhodeCode GmbH, and others. |
|
24 | :copyright: (c) 2013 RhodeCode GmbH, and others. | |
25 | :license: GPLv3, see LICENSE.md for more details. |
|
25 | :license: GPLv3, see LICENSE.md for more details. | |
26 | """ |
|
26 | """ | |
27 |
|
27 | |||
28 |
|
28 | |||
29 | import logging |
|
29 | import logging | |
30 |
|
30 | |||
31 | from beaker.cache import cache_region |
|
31 | from beaker.cache import cache_region | |
32 | from tg import response |
|
32 | from tg import response | |
33 | from tg import tmpl_context as c |
|
33 | from tg import tmpl_context as c | |
34 | from tg.i18n import ugettext as _ |
|
34 | from tg.i18n import ugettext as _ | |
35 | from webhelpers.feedgenerator import Atom1Feed, Rss201rev2Feed |
|
35 | from webhelpers.feedgenerator import Atom1Feed, Rss201rev2Feed | |
36 |
|
36 | |||
37 | from kallithea import CONFIG |
|
37 | from kallithea import CONFIG | |
38 | from kallithea.lib import helpers as h |
|
38 | from kallithea.lib import helpers as h | |
39 | from kallithea.lib.auth import HasRepoPermissionLevelDecorator, LoginRequired |
|
39 | from kallithea.lib.auth import HasRepoPermissionLevelDecorator, LoginRequired | |
40 | from kallithea.lib.base import BaseRepoController |
|
40 | from kallithea.lib.base import BaseRepoController | |
41 | from kallithea.lib.diffs import DiffProcessor |
|
41 | from kallithea.lib.diffs import DiffProcessor | |
42 | from kallithea.lib.utils2 import safe_int, safe_unicode, str2bool |
|
42 | from kallithea.lib.utils2 import safe_int, safe_unicode, str2bool | |
43 |
|
43 | |||
44 |
|
44 | |||
45 | log = logging.getLogger(__name__) |
|
45 | log = logging.getLogger(__name__) | |
46 |
|
46 | |||
47 |
|
47 | |||
48 | language = 'en-us' |
|
48 | language = 'en-us' | |
49 | ttl = "5" |
|
49 | ttl = "5" | |
50 |
|
50 | |||
51 |
|
51 | |||
52 | class FeedController(BaseRepoController): |
|
52 | class FeedController(BaseRepoController): | |
53 |
|
53 | |||
54 | @LoginRequired(allow_default_user=True) |
|
54 | @LoginRequired(allow_default_user=True) | |
55 | @HasRepoPermissionLevelDecorator('read') |
|
55 | @HasRepoPermissionLevelDecorator('read') | |
56 | def _before(self, *args, **kwargs): |
|
56 | def _before(self, *args, **kwargs): | |
57 | super(FeedController, self)._before(*args, **kwargs) |
|
57 | super(FeedController, self)._before(*args, **kwargs) | |
58 |
|
58 | |||
59 | def _get_title(self, cs): |
|
59 | def _get_title(self, cs): | |
60 | return h.shorter(cs.message, 160) |
|
60 | return h.shorter(cs.message, 160) | |
61 |
|
61 | |||
62 | def __get_desc(self, cs): |
|
62 | def __get_desc(self, cs): | |
63 | desc_msg = [(_('%s committed on %s') |
|
63 | desc_msg = [(_('%s committed on %s') | |
64 | % (h.person(cs.author), h.fmt_date(cs.date))) + '<br/>'] |
|
64 | % (h.person(cs.author), h.fmt_date(cs.date))) + '<br/>'] | |
65 | # branches, tags, bookmarks |
|
65 | # branches, tags, bookmarks | |
66 | for branch in cs.branches: |
|
66 | for branch in cs.branches: | |
67 | desc_msg.append('branch: %s<br/>' % branch) |
|
67 | desc_msg.append('branch: %s<br/>' % branch) | |
68 | for book in cs.bookmarks: |
|
68 | for book in cs.bookmarks: | |
69 | desc_msg.append('bookmark: %s<br/>' % book) |
|
69 | desc_msg.append('bookmark: %s<br/>' % book) | |
70 | for tag in cs.tags: |
|
70 | for tag in cs.tags: | |
71 | desc_msg.append('tag: %s<br/>' % tag) |
|
71 | desc_msg.append('tag: %s<br/>' % tag) | |
72 |
|
72 | |||
73 | changes = [] |
|
73 | changes = [] | |
74 | diff_limit = safe_int(CONFIG.get('rss_cut_off_limit', 32 * 1024)) |
|
74 | diff_limit = safe_int(CONFIG.get('rss_cut_off_limit', 32 * 1024)) | |
75 | raw_diff = cs.diff() |
|
75 | raw_diff = cs.diff() | |
76 | diff_processor = DiffProcessor(raw_diff, |
|
76 | diff_processor = DiffProcessor(raw_diff, | |
77 | diff_limit=diff_limit, |
|
77 | diff_limit=diff_limit, | |
78 | inline_diff=False) |
|
78 | inline_diff=False) | |
79 |
|
79 | |||
80 | for st in diff_processor.parsed: |
|
80 | for st in diff_processor.parsed: | |
81 | st.update({'added': st['stats']['added'], |
|
81 | st.update({'added': st['stats']['added'], | |
82 | 'removed': st['stats']['deleted']}) |
|
82 | 'removed': st['stats']['deleted']}) | |
83 | changes.append('\n %(operation)s %(filename)s ' |
|
83 | changes.append('\n %(operation)s %(filename)s ' | |
84 | '(%(added)s lines added, %(removed)s lines removed)' |
|
84 | '(%(added)s lines added, %(removed)s lines removed)' | |
85 | % st) |
|
85 | % st) | |
86 | if diff_processor.limited_diff: |
|
86 | if diff_processor.limited_diff: | |
87 | changes = changes + ['\n ' + |
|
87 | changes = changes + ['\n ' + | |
88 | _('Changeset was too big and was cut off...')] |
|
88 | _('Changeset was too big and was cut off...')] | |
89 |
|
89 | |||
90 | # rev link |
|
90 | # rev link | |
91 | _url = h.canonical_url('changeset_home', repo_name=c.db_repo.repo_name, |
|
91 | _url = h.canonical_url('changeset_home', repo_name=c.db_repo.repo_name, | |
92 | revision=cs.raw_id) |
|
92 | revision=cs.raw_id) | |
93 | desc_msg.append('changeset: <a href="%s">%s</a>' % (_url, cs.raw_id[:8])) |
|
93 | desc_msg.append('changeset: <a href="%s">%s</a>' % (_url, cs.raw_id[:8])) | |
94 |
|
94 | |||
95 | desc_msg.append('<pre>') |
|
95 | desc_msg.append('<pre>') | |
96 | desc_msg.append(h.urlify_text(cs.message)) |
|
96 | desc_msg.append(h.urlify_text(cs.message)) | |
97 | desc_msg.append('\n') |
|
97 | desc_msg.append('\n') | |
98 | desc_msg.extend(changes) |
|
98 | desc_msg.extend(changes) | |
99 | if str2bool(CONFIG.get('rss_include_diff', False)): |
|
99 | if str2bool(CONFIG.get('rss_include_diff', False)): | |
100 | desc_msg.append('\n\n') |
|
100 | desc_msg.append('\n\n') | |
101 | desc_msg.append(raw_diff) |
|
101 | desc_msg.append(raw_diff) | |
102 | desc_msg.append('</pre>') |
|
102 | desc_msg.append('</pre>') | |
103 |
return |
|
103 | return [safe_unicode(chunk) for chunk in desc_msg] | |
104 |
|
104 | |||
105 | def _feed(self, repo_name, kind, feed_factory): |
|
105 | def _feed(self, repo_name, kind, feed_factory): | |
106 | """Produce a simple feed""" |
|
106 | """Produce a simple feed""" | |
107 |
|
107 | |||
108 | @cache_region('long_term', '_get_feed_from_cache') |
|
108 | @cache_region('long_term', '_get_feed_from_cache') | |
109 | def _get_feed_from_cache(*_cache_keys): # parameters are not really used - only as caching key |
|
109 | def _get_feed_from_cache(*_cache_keys): # parameters are not really used - only as caching key | |
110 | feed = feed_factory( |
|
110 | feed = feed_factory( | |
111 | title=_('%s %s feed') % (c.site_name, repo_name), |
|
111 | title=_('%s %s feed') % (c.site_name, repo_name), | |
112 | link=h.canonical_url('summary_home', repo_name=repo_name), |
|
112 | link=h.canonical_url('summary_home', repo_name=repo_name), | |
113 | description=_('Changes on %s repository') % repo_name, |
|
113 | description=_('Changes on %s repository') % repo_name, | |
114 | language=language, |
|
114 | language=language, | |
115 | ttl=ttl, # rss only |
|
115 | ttl=ttl, # rss only | |
116 | ) |
|
116 | ) | |
117 |
|
117 | |||
118 | rss_items_per_page = safe_int(CONFIG.get('rss_items_per_page', 20)) |
|
118 | rss_items_per_page = safe_int(CONFIG.get('rss_items_per_page', 20)) | |
119 | for cs in reversed(list(c.db_repo_scm_instance[-rss_items_per_page:])): |
|
119 | for cs in reversed(list(c.db_repo_scm_instance[-rss_items_per_page:])): | |
120 | feed.add_item(title=self._get_title(cs), |
|
120 | feed.add_item(title=self._get_title(cs), | |
121 | link=h.canonical_url('changeset_home', repo_name=repo_name, |
|
121 | link=h.canonical_url('changeset_home', repo_name=repo_name, | |
122 | revision=cs.raw_id), |
|
122 | revision=cs.raw_id), | |
123 | author_name=cs.author, |
|
123 | author_name=cs.author, | |
124 | description=''.join(self.__get_desc(cs)), |
|
124 | description=''.join(self.__get_desc(cs)), | |
125 | pubdate=cs.date, |
|
125 | pubdate=cs.date, | |
126 | ) |
|
126 | ) | |
127 |
|
127 | |||
128 | response.content_type = feed.mime_type |
|
128 | response.content_type = feed.mime_type | |
129 | return feed.writeString('utf-8') |
|
129 | return feed.writeString('utf-8') | |
130 |
|
130 | |||
131 | return _get_feed_from_cache(repo_name, kind, c.db_repo.changeset_cache.get('raw_id')) |
|
131 | return _get_feed_from_cache(repo_name, kind, c.db_repo.changeset_cache.get('raw_id')) | |
132 |
|
132 | |||
133 | def atom(self, repo_name): |
|
133 | def atom(self, repo_name): | |
134 | """Produce a simple atom-1.0 feed""" |
|
134 | """Produce a simple atom-1.0 feed""" | |
135 | return self._feed(repo_name, 'ATOM', Atom1Feed) |
|
135 | return self._feed(repo_name, 'ATOM', Atom1Feed) | |
136 |
|
136 | |||
137 | def rss(self, repo_name): |
|
137 | def rss(self, repo_name): | |
138 | """Produce an rss2 feed via feedgenerator module""" |
|
138 | """Produce an rss2 feed via feedgenerator module""" | |
139 | return self._feed(repo_name, 'RSS', Rss201rev2Feed) |
|
139 | return self._feed(repo_name, 'RSS', Rss201rev2Feed) |
@@ -1,1326 +1,1326 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | # This program is free software: you can redistribute it and/or modify |
|
2 | # This program is free software: you can redistribute it and/or modify | |
3 | # it under the terms of the GNU General Public License as published by |
|
3 | # it under the terms of the GNU General Public License as published by | |
4 | # the Free Software Foundation, either version 3 of the License, or |
|
4 | # the Free Software Foundation, either version 3 of the License, or | |
5 | # (at your option) any later version. |
|
5 | # (at your option) any later version. | |
6 | # |
|
6 | # | |
7 | # This program is distributed in the hope that it will be useful, |
|
7 | # This program is distributed in the hope that it will be useful, | |
8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
10 | # GNU General Public License for more details. |
|
10 | # GNU General Public License for more details. | |
11 | # |
|
11 | # | |
12 | # You should have received a copy of the GNU General Public License |
|
12 | # You should have received a copy of the GNU General Public License | |
13 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
13 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
14 | """ |
|
14 | """ | |
15 | Helper functions |
|
15 | Helper functions | |
16 |
|
16 | |||
17 | Consists of functions to typically be used within templates, but also |
|
17 | Consists of functions to typically be used within templates, but also | |
18 | available to Controllers. This module is available to both as 'h'. |
|
18 | available to Controllers. This module is available to both as 'h'. | |
19 | """ |
|
19 | """ | |
20 | import hashlib |
|
20 | import hashlib | |
21 | import json |
|
21 | import json | |
22 | import logging |
|
22 | import logging | |
23 | import random |
|
23 | import random | |
24 | import re |
|
24 | import re | |
25 | import textwrap |
|
25 | import textwrap | |
26 | import urlparse |
|
26 | import urlparse | |
27 |
|
27 | |||
28 | from beaker.cache import cache_region |
|
28 | from beaker.cache import cache_region | |
29 | from pygments import highlight as code_highlight |
|
29 | from pygments import highlight as code_highlight | |
30 | from pygments.formatters.html import HtmlFormatter |
|
30 | from pygments.formatters.html import HtmlFormatter | |
31 | from tg.i18n import ugettext as _ |
|
31 | from tg.i18n import ugettext as _ | |
32 | from webhelpers2.html import HTML, escape, literal |
|
32 | from webhelpers2.html import HTML, escape, literal | |
33 | from webhelpers2.html.tags import NotGiven, Option, Options, _input, _make_safe_id_component, checkbox, end_form |
|
33 | from webhelpers2.html.tags import NotGiven, Option, Options, _input, _make_safe_id_component, checkbox, end_form | |
34 | from webhelpers2.html.tags import form as insecure_form |
|
34 | from webhelpers2.html.tags import form as insecure_form | |
35 | from webhelpers2.html.tags import hidden, link_to, password, radio |
|
35 | from webhelpers2.html.tags import hidden, link_to, password, radio | |
36 | from webhelpers2.html.tags import select as webhelpers2_select |
|
36 | from webhelpers2.html.tags import select as webhelpers2_select | |
37 | from webhelpers2.html.tags import submit, text, textarea |
|
37 | from webhelpers2.html.tags import submit, text, textarea | |
38 | from webhelpers2.number import format_byte_size |
|
38 | from webhelpers2.number import format_byte_size | |
39 | from webhelpers2.text import chop_at, truncate, wrap_paragraphs |
|
39 | from webhelpers2.text import chop_at, truncate, wrap_paragraphs | |
40 |
|
40 | |||
41 | from kallithea.config.routing import url |
|
41 | from kallithea.config.routing import url | |
42 | from kallithea.lib.annotate import annotate_highlight |
|
42 | from kallithea.lib.annotate import annotate_highlight | |
43 | #============================================================================== |
|
43 | #============================================================================== | |
44 | # PERMS |
|
44 | # PERMS | |
45 | #============================================================================== |
|
45 | #============================================================================== | |
46 | from kallithea.lib.auth import HasPermissionAny, HasRepoGroupPermissionLevel, HasRepoPermissionLevel |
|
46 | from kallithea.lib.auth import HasPermissionAny, HasRepoGroupPermissionLevel, HasRepoPermissionLevel | |
47 | from kallithea.lib.markup_renderer import url_re |
|
47 | from kallithea.lib.markup_renderer import url_re | |
48 | from kallithea.lib.pygmentsutils import get_custom_lexer |
|
48 | from kallithea.lib.pygmentsutils import get_custom_lexer | |
49 | from kallithea.lib.utils2 import MENTIONS_REGEX, AttributeDict |
|
49 | from kallithea.lib.utils2 import MENTIONS_REGEX, AttributeDict | |
50 | from kallithea.lib.utils2 import age as _age |
|
50 | from kallithea.lib.utils2 import age as _age | |
51 | from kallithea.lib.utils2 import credentials_filter, safe_int, safe_str, safe_unicode, str2bool, time_to_datetime |
|
51 | from kallithea.lib.utils2 import credentials_filter, safe_int, safe_str, safe_unicode, str2bool, time_to_datetime | |
52 | from kallithea.lib.vcs.backends.base import BaseChangeset, EmptyChangeset |
|
52 | from kallithea.lib.vcs.backends.base import BaseChangeset, EmptyChangeset | |
53 | from kallithea.lib.vcs.exceptions import ChangesetDoesNotExistError |
|
53 | from kallithea.lib.vcs.exceptions import ChangesetDoesNotExistError | |
54 | #============================================================================== |
|
54 | #============================================================================== | |
55 | # SCM FILTERS available via h. |
|
55 | # SCM FILTERS available via h. | |
56 | #============================================================================== |
|
56 | #============================================================================== | |
57 | from kallithea.lib.vcs.utils import author_email, author_name |
|
57 | from kallithea.lib.vcs.utils import author_email, author_name | |
58 |
|
58 | |||
59 |
|
59 | |||
60 | log = logging.getLogger(__name__) |
|
60 | log = logging.getLogger(__name__) | |
61 |
|
61 | |||
62 |
|
62 | |||
63 | def canonical_url(*args, **kargs): |
|
63 | def canonical_url(*args, **kargs): | |
64 | '''Like url(x, qualified=True), but returns url that not only is qualified |
|
64 | '''Like url(x, qualified=True), but returns url that not only is qualified | |
65 | but also canonical, as configured in canonical_url''' |
|
65 | but also canonical, as configured in canonical_url''' | |
66 | from kallithea import CONFIG |
|
66 | from kallithea import CONFIG | |
67 | try: |
|
67 | try: | |
68 | parts = CONFIG.get('canonical_url', '').split('://', 1) |
|
68 | parts = CONFIG.get('canonical_url', '').split('://', 1) | |
69 | kargs['host'] = parts[1] |
|
69 | kargs['host'] = parts[1] | |
70 | kargs['protocol'] = parts[0] |
|
70 | kargs['protocol'] = parts[0] | |
71 | except IndexError: |
|
71 | except IndexError: | |
72 | kargs['qualified'] = True |
|
72 | kargs['qualified'] = True | |
73 | return url(*args, **kargs) |
|
73 | return url(*args, **kargs) | |
74 |
|
74 | |||
75 |
|
75 | |||
76 | def canonical_hostname(): |
|
76 | def canonical_hostname(): | |
77 | '''Return canonical hostname of system''' |
|
77 | '''Return canonical hostname of system''' | |
78 | from kallithea import CONFIG |
|
78 | from kallithea import CONFIG | |
79 | try: |
|
79 | try: | |
80 | parts = CONFIG.get('canonical_url', '').split('://', 1) |
|
80 | parts = CONFIG.get('canonical_url', '').split('://', 1) | |
81 | return parts[1].split('/', 1)[0] |
|
81 | return parts[1].split('/', 1)[0] | |
82 | except IndexError: |
|
82 | except IndexError: | |
83 | parts = url('home', qualified=True).split('://', 1) |
|
83 | parts = url('home', qualified=True).split('://', 1) | |
84 | return parts[1].split('/', 1)[0] |
|
84 | return parts[1].split('/', 1)[0] | |
85 |
|
85 | |||
86 |
|
86 | |||
87 | def html_escape(s): |
|
87 | def html_escape(s): | |
88 | """Return string with all html escaped. |
|
88 | """Return string with all html escaped. | |
89 | This is also safe for javascript in html but not necessarily correct. |
|
89 | This is also safe for javascript in html but not necessarily correct. | |
90 | """ |
|
90 | """ | |
91 | return (s |
|
91 | return (s | |
92 | .replace('&', '&') |
|
92 | .replace('&', '&') | |
93 | .replace(">", ">") |
|
93 | .replace(">", ">") | |
94 | .replace("<", "<") |
|
94 | .replace("<", "<") | |
95 | .replace('"', """) |
|
95 | .replace('"', """) | |
96 | .replace("'", "'") # Note: this is HTML5 not HTML4 and might not work in mails |
|
96 | .replace("'", "'") # Note: this is HTML5 not HTML4 and might not work in mails | |
97 | ) |
|
97 | ) | |
98 |
|
98 | |||
99 | def js(value): |
|
99 | def js(value): | |
100 | """Convert Python value to the corresponding JavaScript representation. |
|
100 | """Convert Python value to the corresponding JavaScript representation. | |
101 |
|
101 | |||
102 | This is necessary to safely insert arbitrary values into HTML <script> |
|
102 | This is necessary to safely insert arbitrary values into HTML <script> | |
103 | sections e.g. using Mako template expression substitution. |
|
103 | sections e.g. using Mako template expression substitution. | |
104 |
|
104 | |||
105 | Note: Rather than using this function, it's preferable to avoid the |
|
105 | Note: Rather than using this function, it's preferable to avoid the | |
106 | insertion of values into HTML <script> sections altogether. Instead, |
|
106 | insertion of values into HTML <script> sections altogether. Instead, | |
107 | data should (to the extent possible) be passed to JavaScript using |
|
107 | data should (to the extent possible) be passed to JavaScript using | |
108 | data attributes or AJAX calls, eliminating the need for JS specific |
|
108 | data attributes or AJAX calls, eliminating the need for JS specific | |
109 | escaping. |
|
109 | escaping. | |
110 |
|
110 | |||
111 | Note: This is not safe for use in attributes (e.g. onclick), because |
|
111 | Note: This is not safe for use in attributes (e.g. onclick), because | |
112 | quotes are not escaped. |
|
112 | quotes are not escaped. | |
113 |
|
113 | |||
114 | Because the rules for parsing <script> varies between XHTML (where |
|
114 | Because the rules for parsing <script> varies between XHTML (where | |
115 | normal rules apply for any special characters) and HTML (where |
|
115 | normal rules apply for any special characters) and HTML (where | |
116 | entities are not interpreted, but the literal string "</script>" |
|
116 | entities are not interpreted, but the literal string "</script>" | |
117 | is forbidden), the function ensures that the result never contains |
|
117 | is forbidden), the function ensures that the result never contains | |
118 | '&', '<' and '>', thus making it safe in both those contexts (but |
|
118 | '&', '<' and '>', thus making it safe in both those contexts (but | |
119 | not in attributes). |
|
119 | not in attributes). | |
120 | """ |
|
120 | """ | |
121 | return literal( |
|
121 | return literal( | |
122 | ('(' + json.dumps(value) + ')') |
|
122 | ('(' + json.dumps(value) + ')') | |
123 | # In JSON, the following can only appear in string literals. |
|
123 | # In JSON, the following can only appear in string literals. | |
124 | .replace('&', r'\x26') |
|
124 | .replace('&', r'\x26') | |
125 | .replace('<', r'\x3c') |
|
125 | .replace('<', r'\x3c') | |
126 | .replace('>', r'\x3e') |
|
126 | .replace('>', r'\x3e') | |
127 | ) |
|
127 | ) | |
128 |
|
128 | |||
129 |
|
129 | |||
130 | def jshtml(val): |
|
130 | def jshtml(val): | |
131 | """HTML escapes a string value, then converts the resulting string |
|
131 | """HTML escapes a string value, then converts the resulting string | |
132 | to its corresponding JavaScript representation (see `js`). |
|
132 | to its corresponding JavaScript representation (see `js`). | |
133 |
|
133 | |||
134 | This is used when a plain-text string (possibly containing special |
|
134 | This is used when a plain-text string (possibly containing special | |
135 | HTML characters) will be used by a script in an HTML context (e.g. |
|
135 | HTML characters) will be used by a script in an HTML context (e.g. | |
136 | element.innerHTML or jQuery's 'html' method). |
|
136 | element.innerHTML or jQuery's 'html' method). | |
137 |
|
137 | |||
138 | If in doubt, err on the side of using `jshtml` over `js`, since it's |
|
138 | If in doubt, err on the side of using `jshtml` over `js`, since it's | |
139 | better to escape too much than too little. |
|
139 | better to escape too much than too little. | |
140 | """ |
|
140 | """ | |
141 | return js(escape(val)) |
|
141 | return js(escape(val)) | |
142 |
|
142 | |||
143 |
|
143 | |||
144 | def shorter(s, size=20, firstline=False, postfix='...'): |
|
144 | def shorter(s, size=20, firstline=False, postfix='...'): | |
145 | """Truncate s to size, including the postfix string if truncating. |
|
145 | """Truncate s to size, including the postfix string if truncating. | |
146 | If firstline, truncate at newline. |
|
146 | If firstline, truncate at newline. | |
147 | """ |
|
147 | """ | |
148 | if firstline: |
|
148 | if firstline: | |
149 | s = s.split('\n', 1)[0].rstrip() |
|
149 | s = s.split('\n', 1)[0].rstrip() | |
150 | if len(s) > size: |
|
150 | if len(s) > size: | |
151 | return s[:size - len(postfix)] + postfix |
|
151 | return s[:size - len(postfix)] + postfix | |
152 | return s |
|
152 | return s | |
153 |
|
153 | |||
154 |
|
154 | |||
155 | def reset(name, value, id=NotGiven, **attrs): |
|
155 | def reset(name, value, id=NotGiven, **attrs): | |
156 | """Create a reset button, similar to webhelpers2.html.tags.submit .""" |
|
156 | """Create a reset button, similar to webhelpers2.html.tags.submit .""" | |
157 | return _input("reset", name, value, id, attrs) |
|
157 | return _input("reset", name, value, id, attrs) | |
158 |
|
158 | |||
159 |
|
159 | |||
160 | def select(name, selected_values, options, id=NotGiven, **attrs): |
|
160 | def select(name, selected_values, options, id=NotGiven, **attrs): | |
161 | """Convenient wrapper of webhelpers2 to let it accept options as a tuple list""" |
|
161 | """Convenient wrapper of webhelpers2 to let it accept options as a tuple list""" | |
162 | if isinstance(options, list): |
|
162 | if isinstance(options, list): | |
163 | option_list = options |
|
163 | option_list = options | |
164 | # Handle old value,label lists ... where value also can be value,label lists |
|
164 | # Handle old value,label lists ... where value also can be value,label lists | |
165 | options = Options() |
|
165 | options = Options() | |
166 | for x in option_list: |
|
166 | for x in option_list: | |
167 | if isinstance(x, tuple) and len(x) == 2: |
|
167 | if isinstance(x, tuple) and len(x) == 2: | |
168 | value, label = x |
|
168 | value, label = x | |
169 | elif isinstance(x, basestring): |
|
169 | elif isinstance(x, basestring): | |
170 | value = label = x |
|
170 | value = label = x | |
171 | else: |
|
171 | else: | |
172 | log.error('invalid select option %r', x) |
|
172 | log.error('invalid select option %r', x) | |
173 | raise |
|
173 | raise | |
174 | if isinstance(value, list): |
|
174 | if isinstance(value, list): | |
175 | og = options.add_optgroup(label) |
|
175 | og = options.add_optgroup(label) | |
176 | for x in value: |
|
176 | for x in value: | |
177 | if isinstance(x, tuple) and len(x) == 2: |
|
177 | if isinstance(x, tuple) and len(x) == 2: | |
178 | group_value, group_label = x |
|
178 | group_value, group_label = x | |
179 | elif isinstance(x, basestring): |
|
179 | elif isinstance(x, basestring): | |
180 | group_value = group_label = x |
|
180 | group_value = group_label = x | |
181 | else: |
|
181 | else: | |
182 | log.error('invalid select option %r', x) |
|
182 | log.error('invalid select option %r', x) | |
183 | raise |
|
183 | raise | |
184 | og.add_option(group_label, group_value) |
|
184 | og.add_option(group_label, group_value) | |
185 | else: |
|
185 | else: | |
186 | options.add_option(label, value) |
|
186 | options.add_option(label, value) | |
187 | return webhelpers2_select(name, selected_values, options, id=id, **attrs) |
|
187 | return webhelpers2_select(name, selected_values, options, id=id, **attrs) | |
188 |
|
188 | |||
189 |
|
189 | |||
190 | safeid = _make_safe_id_component |
|
190 | safeid = _make_safe_id_component | |
191 |
|
191 | |||
192 |
|
192 | |||
193 | def FID(raw_id, path): |
|
193 | def FID(raw_id, path): | |
194 | """ |
|
194 | """ | |
195 | Creates a unique ID for filenode based on it's hash of path and revision |
|
195 | Creates a unique ID for filenode based on it's hash of path and revision | |
196 | it's safe to use in urls |
|
196 | it's safe to use in urls | |
197 |
|
197 | |||
198 | :param raw_id: |
|
198 | :param raw_id: | |
199 | :param path: |
|
199 | :param path: | |
200 | """ |
|
200 | """ | |
201 |
|
201 | |||
202 | return 'C-%s-%s' % (short_id(raw_id), hashlib.md5(safe_str(path)).hexdigest()[:12]) |
|
202 | return 'C-%s-%s' % (short_id(raw_id), hashlib.md5(safe_str(path)).hexdigest()[:12]) | |
203 |
|
203 | |||
204 |
|
204 | |||
205 | class _FilesBreadCrumbs(object): |
|
205 | class _FilesBreadCrumbs(object): | |
206 |
|
206 | |||
207 | def __call__(self, repo_name, rev, paths): |
|
207 | def __call__(self, repo_name, rev, paths): | |
208 | if isinstance(paths, str): |
|
208 | if isinstance(paths, str): | |
209 | paths = safe_unicode(paths) |
|
209 | paths = safe_unicode(paths) | |
210 | url_l = [link_to(repo_name, url('files_home', |
|
210 | url_l = [link_to(repo_name, url('files_home', | |
211 | repo_name=repo_name, |
|
211 | repo_name=repo_name, | |
212 | revision=rev, f_path=''), |
|
212 | revision=rev, f_path=''), | |
213 | class_='ypjax-link')] |
|
213 | class_='ypjax-link')] | |
214 | paths_l = paths.split('/') |
|
214 | paths_l = paths.split('/') | |
215 | for cnt, p in enumerate(paths_l): |
|
215 | for cnt, p in enumerate(paths_l): | |
216 | if p != '': |
|
216 | if p != '': | |
217 | url_l.append(link_to(p, |
|
217 | url_l.append(link_to(p, | |
218 | url('files_home', |
|
218 | url('files_home', | |
219 | repo_name=repo_name, |
|
219 | repo_name=repo_name, | |
220 | revision=rev, |
|
220 | revision=rev, | |
221 | f_path='/'.join(paths_l[:cnt + 1]) |
|
221 | f_path='/'.join(paths_l[:cnt + 1]) | |
222 | ), |
|
222 | ), | |
223 | class_='ypjax-link' |
|
223 | class_='ypjax-link' | |
224 | ) |
|
224 | ) | |
225 | ) |
|
225 | ) | |
226 |
|
226 | |||
227 | return literal('/'.join(url_l)) |
|
227 | return literal('/'.join(url_l)) | |
228 |
|
228 | |||
229 |
|
229 | |||
230 | files_breadcrumbs = _FilesBreadCrumbs() |
|
230 | files_breadcrumbs = _FilesBreadCrumbs() | |
231 |
|
231 | |||
232 |
|
232 | |||
233 | class CodeHtmlFormatter(HtmlFormatter): |
|
233 | class CodeHtmlFormatter(HtmlFormatter): | |
234 | """ |
|
234 | """ | |
235 | My code Html Formatter for source codes |
|
235 | My code Html Formatter for source codes | |
236 | """ |
|
236 | """ | |
237 |
|
237 | |||
238 | def wrap(self, source, outfile): |
|
238 | def wrap(self, source, outfile): | |
239 | return self._wrap_div(self._wrap_pre(self._wrap_code(source))) |
|
239 | return self._wrap_div(self._wrap_pre(self._wrap_code(source))) | |
240 |
|
240 | |||
241 | def _wrap_code(self, source): |
|
241 | def _wrap_code(self, source): | |
242 | for cnt, it in enumerate(source): |
|
242 | for cnt, it in enumerate(source): | |
243 | i, t = it |
|
243 | i, t = it | |
244 | t = '<span id="L%s">%s</span>' % (cnt + 1, t) |
|
244 | t = '<span id="L%s">%s</span>' % (cnt + 1, t) | |
245 | yield i, t |
|
245 | yield i, t | |
246 |
|
246 | |||
247 | def _wrap_tablelinenos(self, inner): |
|
247 | def _wrap_tablelinenos(self, inner): | |
248 | inner_lines = [] |
|
248 | inner_lines = [] | |
249 | lncount = 0 |
|
249 | lncount = 0 | |
250 | for t, line in inner: |
|
250 | for t, line in inner: | |
251 | if t: |
|
251 | if t: | |
252 | lncount += 1 |
|
252 | lncount += 1 | |
253 | inner_lines.append(line) |
|
253 | inner_lines.append(line) | |
254 |
|
254 | |||
255 | fl = self.linenostart |
|
255 | fl = self.linenostart | |
256 | mw = len(str(lncount + fl - 1)) |
|
256 | mw = len(str(lncount + fl - 1)) | |
257 | sp = self.linenospecial |
|
257 | sp = self.linenospecial | |
258 | st = self.linenostep |
|
258 | st = self.linenostep | |
259 | la = self.lineanchors |
|
259 | la = self.lineanchors | |
260 | aln = self.anchorlinenos |
|
260 | aln = self.anchorlinenos | |
261 | nocls = self.noclasses |
|
261 | nocls = self.noclasses | |
262 | if sp: |
|
262 | if sp: | |
263 | lines = [] |
|
263 | lines = [] | |
264 |
|
264 | |||
265 | for i in range(fl, fl + lncount): |
|
265 | for i in range(fl, fl + lncount): | |
266 | if i % st == 0: |
|
266 | if i % st == 0: | |
267 | if i % sp == 0: |
|
267 | if i % sp == 0: | |
268 | if aln: |
|
268 | if aln: | |
269 | lines.append('<a href="#%s%d" class="special">%*d</a>' % |
|
269 | lines.append('<a href="#%s%d" class="special">%*d</a>' % | |
270 | (la, i, mw, i)) |
|
270 | (la, i, mw, i)) | |
271 | else: |
|
271 | else: | |
272 | lines.append('<span class="special">%*d</span>' % (mw, i)) |
|
272 | lines.append('<span class="special">%*d</span>' % (mw, i)) | |
273 | else: |
|
273 | else: | |
274 | if aln: |
|
274 | if aln: | |
275 | lines.append('<a href="#%s%d">%*d</a>' % (la, i, mw, i)) |
|
275 | lines.append('<a href="#%s%d">%*d</a>' % (la, i, mw, i)) | |
276 | else: |
|
276 | else: | |
277 | lines.append('%*d' % (mw, i)) |
|
277 | lines.append('%*d' % (mw, i)) | |
278 | else: |
|
278 | else: | |
279 | lines.append('') |
|
279 | lines.append('') | |
280 | ls = '\n'.join(lines) |
|
280 | ls = '\n'.join(lines) | |
281 | else: |
|
281 | else: | |
282 | lines = [] |
|
282 | lines = [] | |
283 | for i in range(fl, fl + lncount): |
|
283 | for i in range(fl, fl + lncount): | |
284 | if i % st == 0: |
|
284 | if i % st == 0: | |
285 | if aln: |
|
285 | if aln: | |
286 | lines.append('<a href="#%s%d">%*d</a>' % (la, i, mw, i)) |
|
286 | lines.append('<a href="#%s%d">%*d</a>' % (la, i, mw, i)) | |
287 | else: |
|
287 | else: | |
288 | lines.append('%*d' % (mw, i)) |
|
288 | lines.append('%*d' % (mw, i)) | |
289 | else: |
|
289 | else: | |
290 | lines.append('') |
|
290 | lines.append('') | |
291 | ls = '\n'.join(lines) |
|
291 | ls = '\n'.join(lines) | |
292 |
|
292 | |||
293 | # in case you wonder about the seemingly redundant <div> here: since the |
|
293 | # in case you wonder about the seemingly redundant <div> here: since the | |
294 | # content in the other cell also is wrapped in a div, some browsers in |
|
294 | # content in the other cell also is wrapped in a div, some browsers in | |
295 | # some configurations seem to mess up the formatting... |
|
295 | # some configurations seem to mess up the formatting... | |
296 | if nocls: |
|
296 | if nocls: | |
297 | yield 0, ('<table class="%stable">' % self.cssclass + |
|
297 | yield 0, ('<table class="%stable">' % self.cssclass + | |
298 | '<tr><td><div class="linenodiv">' |
|
298 | '<tr><td><div class="linenodiv">' | |
299 | '<pre>' + ls + '</pre></div></td>' |
|
299 | '<pre>' + ls + '</pre></div></td>' | |
300 | '<td id="hlcode" class="code">') |
|
300 | '<td id="hlcode" class="code">') | |
301 | else: |
|
301 | else: | |
302 | yield 0, ('<table class="%stable">' % self.cssclass + |
|
302 | yield 0, ('<table class="%stable">' % self.cssclass + | |
303 | '<tr><td class="linenos"><div class="linenodiv">' |
|
303 | '<tr><td class="linenos"><div class="linenodiv">' | |
304 | '<pre>' + ls + '</pre></div></td>' |
|
304 | '<pre>' + ls + '</pre></div></td>' | |
305 | '<td id="hlcode" class="code">') |
|
305 | '<td id="hlcode" class="code">') | |
306 | yield 0, ''.join(inner_lines) |
|
306 | yield 0, ''.join(inner_lines) | |
307 | yield 0, '</td></tr></table>' |
|
307 | yield 0, '</td></tr></table>' | |
308 |
|
308 | |||
309 |
|
309 | |||
310 | _whitespace_re = re.compile(r'(\t)|( )(?=\n|</div>)') |
|
310 | _whitespace_re = re.compile(r'(\t)|( )(?=\n|</div>)') | |
311 |
|
311 | |||
312 |
|
312 | |||
313 | def _markup_whitespace(m): |
|
313 | def _markup_whitespace(m): | |
314 | groups = m.groups() |
|
314 | groups = m.groups() | |
315 | if groups[0]: |
|
315 | if groups[0]: | |
316 | return '<u>\t</u>' |
|
316 | return '<u>\t</u>' | |
317 | if groups[1]: |
|
317 | if groups[1]: | |
318 | return ' <i></i>' |
|
318 | return ' <i></i>' | |
319 |
|
319 | |||
320 |
|
320 | |||
321 | def markup_whitespace(s): |
|
321 | def markup_whitespace(s): | |
322 | return _whitespace_re.sub(_markup_whitespace, s) |
|
322 | return _whitespace_re.sub(_markup_whitespace, s) | |
323 |
|
323 | |||
324 |
|
324 | |||
325 | def pygmentize(filenode, **kwargs): |
|
325 | def pygmentize(filenode, **kwargs): | |
326 | """ |
|
326 | """ | |
327 | pygmentize function using pygments |
|
327 | pygmentize function using pygments | |
328 |
|
328 | |||
329 | :param filenode: |
|
329 | :param filenode: | |
330 | """ |
|
330 | """ | |
331 | lexer = get_custom_lexer(filenode.extension) or filenode.lexer |
|
331 | lexer = get_custom_lexer(filenode.extension) or filenode.lexer | |
332 | return literal(markup_whitespace( |
|
332 | return literal(markup_whitespace( | |
333 | code_highlight(filenode.content, lexer, CodeHtmlFormatter(**kwargs)))) |
|
333 | code_highlight(filenode.content, lexer, CodeHtmlFormatter(**kwargs)))) | |
334 |
|
334 | |||
335 |
|
335 | |||
336 | def pygmentize_annotation(repo_name, filenode, **kwargs): |
|
336 | def pygmentize_annotation(repo_name, filenode, **kwargs): | |
337 | """ |
|
337 | """ | |
338 | pygmentize function for annotation |
|
338 | pygmentize function for annotation | |
339 |
|
339 | |||
340 | :param filenode: |
|
340 | :param filenode: | |
341 | """ |
|
341 | """ | |
342 |
|
342 | |||
343 | color_dict = {} |
|
343 | color_dict = {} | |
344 |
|
344 | |||
345 | def gen_color(n=10000): |
|
345 | def gen_color(n=10000): | |
346 | """generator for getting n of evenly distributed colors using |
|
346 | """generator for getting n of evenly distributed colors using | |
347 | hsv color and golden ratio. It always return same order of colors |
|
347 | hsv color and golden ratio. It always return same order of colors | |
348 |
|
348 | |||
349 | :returns: RGB tuple |
|
349 | :returns: RGB tuple | |
350 | """ |
|
350 | """ | |
351 |
|
351 | |||
352 | def hsv_to_rgb(h, s, v): |
|
352 | def hsv_to_rgb(h, s, v): | |
353 | if s == 0.0: |
|
353 | if s == 0.0: | |
354 | return v, v, v |
|
354 | return v, v, v | |
355 | i = int(h * 6.0) # XXX assume int() truncates! |
|
355 | i = int(h * 6.0) # XXX assume int() truncates! | |
356 | f = (h * 6.0) - i |
|
356 | f = (h * 6.0) - i | |
357 | p = v * (1.0 - s) |
|
357 | p = v * (1.0 - s) | |
358 | q = v * (1.0 - s * f) |
|
358 | q = v * (1.0 - s * f) | |
359 | t = v * (1.0 - s * (1.0 - f)) |
|
359 | t = v * (1.0 - s * (1.0 - f)) | |
360 | i = i % 6 |
|
360 | i = i % 6 | |
361 | if i == 0: |
|
361 | if i == 0: | |
362 | return v, t, p |
|
362 | return v, t, p | |
363 | if i == 1: |
|
363 | if i == 1: | |
364 | return q, v, p |
|
364 | return q, v, p | |
365 | if i == 2: |
|
365 | if i == 2: | |
366 | return p, v, t |
|
366 | return p, v, t | |
367 | if i == 3: |
|
367 | if i == 3: | |
368 | return p, q, v |
|
368 | return p, q, v | |
369 | if i == 4: |
|
369 | if i == 4: | |
370 | return t, p, v |
|
370 | return t, p, v | |
371 | if i == 5: |
|
371 | if i == 5: | |
372 | return v, p, q |
|
372 | return v, p, q | |
373 |
|
373 | |||
374 | golden_ratio = 0.618033988749895 |
|
374 | golden_ratio = 0.618033988749895 | |
375 | h = 0.22717784590367374 |
|
375 | h = 0.22717784590367374 | |
376 |
|
376 | |||
377 | for _unused in xrange(n): |
|
377 | for _unused in xrange(n): | |
378 | h += golden_ratio |
|
378 | h += golden_ratio | |
379 | h %= 1 |
|
379 | h %= 1 | |
380 | HSV_tuple = [h, 0.95, 0.95] |
|
380 | HSV_tuple = [h, 0.95, 0.95] | |
381 | RGB_tuple = hsv_to_rgb(*HSV_tuple) |
|
381 | RGB_tuple = hsv_to_rgb(*HSV_tuple) | |
382 |
yield |
|
382 | yield [str(int(x * 256)) for x in RGB_tuple] | |
383 |
|
383 | |||
384 | cgenerator = gen_color() |
|
384 | cgenerator = gen_color() | |
385 |
|
385 | |||
386 | def get_color_string(cs): |
|
386 | def get_color_string(cs): | |
387 | if cs in color_dict: |
|
387 | if cs in color_dict: | |
388 | col = color_dict[cs] |
|
388 | col = color_dict[cs] | |
389 | else: |
|
389 | else: | |
390 | col = color_dict[cs] = cgenerator.next() |
|
390 | col = color_dict[cs] = cgenerator.next() | |
391 | return "color: rgb(%s)! important;" % (', '.join(col)) |
|
391 | return "color: rgb(%s)! important;" % (', '.join(col)) | |
392 |
|
392 | |||
393 | def url_func(repo_name): |
|
393 | def url_func(repo_name): | |
394 |
|
394 | |||
395 | def _url_func(changeset): |
|
395 | def _url_func(changeset): | |
396 | author = escape(changeset.author) |
|
396 | author = escape(changeset.author) | |
397 | date = changeset.date |
|
397 | date = changeset.date | |
398 | message = escape(changeset.message) |
|
398 | message = escape(changeset.message) | |
399 | tooltip_html = ("<b>Author:</b> %s<br/>" |
|
399 | tooltip_html = ("<b>Author:</b> %s<br/>" | |
400 | "<b>Date:</b> %s</b><br/>" |
|
400 | "<b>Date:</b> %s</b><br/>" | |
401 | "<b>Message:</b> %s") % (author, date, message) |
|
401 | "<b>Message:</b> %s") % (author, date, message) | |
402 |
|
402 | |||
403 | lnk_format = show_id(changeset) |
|
403 | lnk_format = show_id(changeset) | |
404 | uri = link_to( |
|
404 | uri = link_to( | |
405 | lnk_format, |
|
405 | lnk_format, | |
406 | url('changeset_home', repo_name=repo_name, |
|
406 | url('changeset_home', repo_name=repo_name, | |
407 | revision=changeset.raw_id), |
|
407 | revision=changeset.raw_id), | |
408 | style=get_color_string(changeset.raw_id), |
|
408 | style=get_color_string(changeset.raw_id), | |
409 | **{'data-toggle': 'popover', |
|
409 | **{'data-toggle': 'popover', | |
410 | 'data-content': tooltip_html} |
|
410 | 'data-content': tooltip_html} | |
411 | ) |
|
411 | ) | |
412 |
|
412 | |||
413 | uri += '\n' |
|
413 | uri += '\n' | |
414 | return uri |
|
414 | return uri | |
415 | return _url_func |
|
415 | return _url_func | |
416 |
|
416 | |||
417 | return literal(markup_whitespace(annotate_highlight(filenode, url_func(repo_name), **kwargs))) |
|
417 | return literal(markup_whitespace(annotate_highlight(filenode, url_func(repo_name), **kwargs))) | |
418 |
|
418 | |||
419 |
|
419 | |||
420 | class _Message(object): |
|
420 | class _Message(object): | |
421 | """A message returned by ``pop_flash_messages()``. |
|
421 | """A message returned by ``pop_flash_messages()``. | |
422 |
|
422 | |||
423 | Converting the message to a string returns the message text. Instances |
|
423 | Converting the message to a string returns the message text. Instances | |
424 | also have the following attributes: |
|
424 | also have the following attributes: | |
425 |
|
425 | |||
426 | * ``message``: the message text. |
|
426 | * ``message``: the message text. | |
427 | * ``category``: the category specified when the message was created. |
|
427 | * ``category``: the category specified when the message was created. | |
428 | """ |
|
428 | """ | |
429 |
|
429 | |||
430 | def __init__(self, category, message): |
|
430 | def __init__(self, category, message): | |
431 | self.category = category |
|
431 | self.category = category | |
432 | self.message = message |
|
432 | self.message = message | |
433 |
|
433 | |||
434 | def __str__(self): |
|
434 | def __str__(self): | |
435 | return self.message |
|
435 | return self.message | |
436 |
|
436 | |||
437 | __unicode__ = __str__ |
|
437 | __unicode__ = __str__ | |
438 |
|
438 | |||
439 | def __html__(self): |
|
439 | def __html__(self): | |
440 | return escape(safe_unicode(self.message)) |
|
440 | return escape(safe_unicode(self.message)) | |
441 |
|
441 | |||
442 |
|
442 | |||
443 | def _session_flash_messages(append=None, clear=False): |
|
443 | def _session_flash_messages(append=None, clear=False): | |
444 | """Manage a message queue in tg.session: return the current message queue |
|
444 | """Manage a message queue in tg.session: return the current message queue | |
445 | after appending the given message, and possibly clearing the queue.""" |
|
445 | after appending the given message, and possibly clearing the queue.""" | |
446 | key = 'flash' |
|
446 | key = 'flash' | |
447 | from tg import session |
|
447 | from tg import session | |
448 | if key in session: |
|
448 | if key in session: | |
449 | flash_messages = session[key] |
|
449 | flash_messages = session[key] | |
450 | else: |
|
450 | else: | |
451 | if append is None: # common fast path - also used for clearing empty queue |
|
451 | if append is None: # common fast path - also used for clearing empty queue | |
452 | return [] # don't bother saving |
|
452 | return [] # don't bother saving | |
453 | flash_messages = [] |
|
453 | flash_messages = [] | |
454 | session[key] = flash_messages |
|
454 | session[key] = flash_messages | |
455 | if append is not None and append not in flash_messages: |
|
455 | if append is not None and append not in flash_messages: | |
456 | flash_messages.append(append) |
|
456 | flash_messages.append(append) | |
457 | if clear: |
|
457 | if clear: | |
458 | session.pop(key, None) |
|
458 | session.pop(key, None) | |
459 | session.save() |
|
459 | session.save() | |
460 | return flash_messages |
|
460 | return flash_messages | |
461 |
|
461 | |||
462 |
|
462 | |||
463 | def flash(message, category=None, logf=None): |
|
463 | def flash(message, category=None, logf=None): | |
464 | """ |
|
464 | """ | |
465 | Show a message to the user _and_ log it through the specified function |
|
465 | Show a message to the user _and_ log it through the specified function | |
466 |
|
466 | |||
467 | category: notice (default), warning, error, success |
|
467 | category: notice (default), warning, error, success | |
468 | logf: a custom log function - such as log.debug |
|
468 | logf: a custom log function - such as log.debug | |
469 |
|
469 | |||
470 | logf defaults to log.info, unless category equals 'success', in which |
|
470 | logf defaults to log.info, unless category equals 'success', in which | |
471 | case logf defaults to log.debug. |
|
471 | case logf defaults to log.debug. | |
472 | """ |
|
472 | """ | |
473 | if logf is None: |
|
473 | if logf is None: | |
474 | logf = log.info |
|
474 | logf = log.info | |
475 | if category == 'success': |
|
475 | if category == 'success': | |
476 | logf = log.debug |
|
476 | logf = log.debug | |
477 |
|
477 | |||
478 | logf('Flash %s: %s', category, message) |
|
478 | logf('Flash %s: %s', category, message) | |
479 |
|
479 | |||
480 | _session_flash_messages(append=(category, message)) |
|
480 | _session_flash_messages(append=(category, message)) | |
481 |
|
481 | |||
482 |
|
482 | |||
483 | def pop_flash_messages(): |
|
483 | def pop_flash_messages(): | |
484 | """Return all accumulated messages and delete them from the session. |
|
484 | """Return all accumulated messages and delete them from the session. | |
485 |
|
485 | |||
486 | The return value is a list of ``Message`` objects. |
|
486 | The return value is a list of ``Message`` objects. | |
487 | """ |
|
487 | """ | |
488 | return [_Message(*m) for m in _session_flash_messages(clear=True)] |
|
488 | return [_Message(*m) for m in _session_flash_messages(clear=True)] | |
489 |
|
489 | |||
490 |
|
490 | |||
491 | age = lambda x, y=False: _age(x, y) |
|
491 | age = lambda x, y=False: _age(x, y) | |
492 | capitalize = lambda x: x.capitalize() |
|
492 | capitalize = lambda x: x.capitalize() | |
493 | email = author_email |
|
493 | email = author_email | |
494 | short_id = lambda x: x[:12] |
|
494 | short_id = lambda x: x[:12] | |
495 | hide_credentials = lambda x: ''.join(credentials_filter(x)) |
|
495 | hide_credentials = lambda x: ''.join(credentials_filter(x)) | |
496 |
|
496 | |||
497 |
|
497 | |||
498 | def show_id(cs): |
|
498 | def show_id(cs): | |
499 | """ |
|
499 | """ | |
500 | Configurable function that shows ID |
|
500 | Configurable function that shows ID | |
501 | by default it's r123:fffeeefffeee |
|
501 | by default it's r123:fffeeefffeee | |
502 |
|
502 | |||
503 | :param cs: changeset instance |
|
503 | :param cs: changeset instance | |
504 | """ |
|
504 | """ | |
505 | from kallithea import CONFIG |
|
505 | from kallithea import CONFIG | |
506 | def_len = safe_int(CONFIG.get('show_sha_length', 12)) |
|
506 | def_len = safe_int(CONFIG.get('show_sha_length', 12)) | |
507 | show_rev = str2bool(CONFIG.get('show_revision_number', False)) |
|
507 | show_rev = str2bool(CONFIG.get('show_revision_number', False)) | |
508 |
|
508 | |||
509 | raw_id = cs.raw_id[:def_len] |
|
509 | raw_id = cs.raw_id[:def_len] | |
510 | if show_rev: |
|
510 | if show_rev: | |
511 | return 'r%s:%s' % (cs.revision, raw_id) |
|
511 | return 'r%s:%s' % (cs.revision, raw_id) | |
512 | else: |
|
512 | else: | |
513 | return raw_id |
|
513 | return raw_id | |
514 |
|
514 | |||
515 |
|
515 | |||
516 | def fmt_date(date): |
|
516 | def fmt_date(date): | |
517 | if date: |
|
517 | if date: | |
518 | return date.strftime("%Y-%m-%d %H:%M:%S").decode('utf-8') |
|
518 | return date.strftime("%Y-%m-%d %H:%M:%S").decode('utf-8') | |
519 |
|
519 | |||
520 | return "" |
|
520 | return "" | |
521 |
|
521 | |||
522 |
|
522 | |||
523 | def is_git(repository): |
|
523 | def is_git(repository): | |
524 | if hasattr(repository, 'alias'): |
|
524 | if hasattr(repository, 'alias'): | |
525 | _type = repository.alias |
|
525 | _type = repository.alias | |
526 | elif hasattr(repository, 'repo_type'): |
|
526 | elif hasattr(repository, 'repo_type'): | |
527 | _type = repository.repo_type |
|
527 | _type = repository.repo_type | |
528 | else: |
|
528 | else: | |
529 | _type = repository |
|
529 | _type = repository | |
530 | return _type == 'git' |
|
530 | return _type == 'git' | |
531 |
|
531 | |||
532 |
|
532 | |||
533 | def is_hg(repository): |
|
533 | def is_hg(repository): | |
534 | if hasattr(repository, 'alias'): |
|
534 | if hasattr(repository, 'alias'): | |
535 | _type = repository.alias |
|
535 | _type = repository.alias | |
536 | elif hasattr(repository, 'repo_type'): |
|
536 | elif hasattr(repository, 'repo_type'): | |
537 | _type = repository.repo_type |
|
537 | _type = repository.repo_type | |
538 | else: |
|
538 | else: | |
539 | _type = repository |
|
539 | _type = repository | |
540 | return _type == 'hg' |
|
540 | return _type == 'hg' | |
541 |
|
541 | |||
542 |
|
542 | |||
543 | @cache_region('long_term', 'user_attr_or_none') |
|
543 | @cache_region('long_term', 'user_attr_or_none') | |
544 | def user_attr_or_none(author, show_attr): |
|
544 | def user_attr_or_none(author, show_attr): | |
545 | """Try to match email part of VCS committer string with a local user and return show_attr |
|
545 | """Try to match email part of VCS committer string with a local user and return show_attr | |
546 | - or return None if user not found""" |
|
546 | - or return None if user not found""" | |
547 | email = author_email(author) |
|
547 | email = author_email(author) | |
548 | if email: |
|
548 | if email: | |
549 | from kallithea.model.db import User |
|
549 | from kallithea.model.db import User | |
550 | user = User.get_by_email(email, cache=True) # cache will only use sql_cache_short |
|
550 | user = User.get_by_email(email, cache=True) # cache will only use sql_cache_short | |
551 | if user is not None: |
|
551 | if user is not None: | |
552 | return getattr(user, show_attr) |
|
552 | return getattr(user, show_attr) | |
553 | return None |
|
553 | return None | |
554 |
|
554 | |||
555 |
|
555 | |||
556 | def email_or_none(author): |
|
556 | def email_or_none(author): | |
557 | """Try to match email part of VCS committer string with a local user. |
|
557 | """Try to match email part of VCS committer string with a local user. | |
558 | Return primary email of user, email part of the specified author name, or None.""" |
|
558 | Return primary email of user, email part of the specified author name, or None.""" | |
559 | if not author: |
|
559 | if not author: | |
560 | return None |
|
560 | return None | |
561 | email = user_attr_or_none(author, 'email') |
|
561 | email = user_attr_or_none(author, 'email') | |
562 | if email is not None: |
|
562 | if email is not None: | |
563 | return email # always use user's main email address - not necessarily the one used to find user |
|
563 | return email # always use user's main email address - not necessarily the one used to find user | |
564 |
|
564 | |||
565 | # extract email from the commit string |
|
565 | # extract email from the commit string | |
566 | email = author_email(author) |
|
566 | email = author_email(author) | |
567 | if email: |
|
567 | if email: | |
568 | return email |
|
568 | return email | |
569 |
|
569 | |||
570 | # No valid email, not a valid user in the system, none! |
|
570 | # No valid email, not a valid user in the system, none! | |
571 | return None |
|
571 | return None | |
572 |
|
572 | |||
573 |
|
573 | |||
574 | def person(author, show_attr="username"): |
|
574 | def person(author, show_attr="username"): | |
575 | """Find the user identified by 'author', return one of the users attributes, |
|
575 | """Find the user identified by 'author', return one of the users attributes, | |
576 | default to the username attribute, None if there is no user""" |
|
576 | default to the username attribute, None if there is no user""" | |
577 | from kallithea.model.db import User |
|
577 | from kallithea.model.db import User | |
578 | # if author is already an instance use it for extraction |
|
578 | # if author is already an instance use it for extraction | |
579 | if isinstance(author, User): |
|
579 | if isinstance(author, User): | |
580 | return getattr(author, show_attr) |
|
580 | return getattr(author, show_attr) | |
581 |
|
581 | |||
582 | value = user_attr_or_none(author, show_attr) |
|
582 | value = user_attr_or_none(author, show_attr) | |
583 | if value is not None: |
|
583 | if value is not None: | |
584 | return value |
|
584 | return value | |
585 |
|
585 | |||
586 | # Still nothing? Just pass back the author name if any, else the email |
|
586 | # Still nothing? Just pass back the author name if any, else the email | |
587 | return author_name(author) or email(author) |
|
587 | return author_name(author) or email(author) | |
588 |
|
588 | |||
589 |
|
589 | |||
590 | def person_by_id(id_, show_attr="username"): |
|
590 | def person_by_id(id_, show_attr="username"): | |
591 | from kallithea.model.db import User |
|
591 | from kallithea.model.db import User | |
592 | # attr to return from fetched user |
|
592 | # attr to return from fetched user | |
593 | person_getter = lambda usr: getattr(usr, show_attr) |
|
593 | person_getter = lambda usr: getattr(usr, show_attr) | |
594 |
|
594 | |||
595 | # maybe it's an ID ? |
|
595 | # maybe it's an ID ? | |
596 | if str(id_).isdigit() or isinstance(id_, int): |
|
596 | if str(id_).isdigit() or isinstance(id_, int): | |
597 | id_ = int(id_) |
|
597 | id_ = int(id_) | |
598 | user = User.get(id_) |
|
598 | user = User.get(id_) | |
599 | if user is not None: |
|
599 | if user is not None: | |
600 | return person_getter(user) |
|
600 | return person_getter(user) | |
601 | return id_ |
|
601 | return id_ | |
602 |
|
602 | |||
603 |
|
603 | |||
604 | def boolicon(value): |
|
604 | def boolicon(value): | |
605 | """Returns boolean value of a value, represented as small html image of true/false |
|
605 | """Returns boolean value of a value, represented as small html image of true/false | |
606 | icons |
|
606 | icons | |
607 |
|
607 | |||
608 | :param value: value |
|
608 | :param value: value | |
609 | """ |
|
609 | """ | |
610 |
|
610 | |||
611 | if value: |
|
611 | if value: | |
612 | return HTML.tag('i', class_="icon-ok") |
|
612 | return HTML.tag('i', class_="icon-ok") | |
613 | else: |
|
613 | else: | |
614 | return HTML.tag('i', class_="icon-minus-circled") |
|
614 | return HTML.tag('i', class_="icon-minus-circled") | |
615 |
|
615 | |||
616 |
|
616 | |||
617 | def action_parser(user_log, feed=False, parse_cs=False): |
|
617 | def action_parser(user_log, feed=False, parse_cs=False): | |
618 | """ |
|
618 | """ | |
619 | This helper will action_map the specified string action into translated |
|
619 | This helper will action_map the specified string action into translated | |
620 | fancy names with icons and links |
|
620 | fancy names with icons and links | |
621 |
|
621 | |||
622 | :param user_log: user log instance |
|
622 | :param user_log: user log instance | |
623 | :param feed: use output for feeds (no html and fancy icons) |
|
623 | :param feed: use output for feeds (no html and fancy icons) | |
624 | :param parse_cs: parse Changesets into VCS instances |
|
624 | :param parse_cs: parse Changesets into VCS instances | |
625 | """ |
|
625 | """ | |
626 |
|
626 | |||
627 | action = user_log.action |
|
627 | action = user_log.action | |
628 | action_params = ' ' |
|
628 | action_params = ' ' | |
629 |
|
629 | |||
630 | x = action.split(':') |
|
630 | x = action.split(':') | |
631 |
|
631 | |||
632 | if len(x) > 1: |
|
632 | if len(x) > 1: | |
633 | action, action_params = x |
|
633 | action, action_params = x | |
634 |
|
634 | |||
635 | def get_cs_links(): |
|
635 | def get_cs_links(): | |
636 | revs_limit = 3 # display this amount always |
|
636 | revs_limit = 3 # display this amount always | |
637 | revs_top_limit = 50 # show upto this amount of changesets hidden |
|
637 | revs_top_limit = 50 # show upto this amount of changesets hidden | |
638 | revs_ids = action_params.split(',') |
|
638 | revs_ids = action_params.split(',') | |
639 | deleted = user_log.repository is None |
|
639 | deleted = user_log.repository is None | |
640 | if deleted: |
|
640 | if deleted: | |
641 | return ','.join(revs_ids) |
|
641 | return ','.join(revs_ids) | |
642 |
|
642 | |||
643 | repo_name = user_log.repository.repo_name |
|
643 | repo_name = user_log.repository.repo_name | |
644 |
|
644 | |||
645 | def lnk(rev, repo_name): |
|
645 | def lnk(rev, repo_name): | |
646 | lazy_cs = False |
|
646 | lazy_cs = False | |
647 | title_ = None |
|
647 | title_ = None | |
648 | url_ = '#' |
|
648 | url_ = '#' | |
649 | if isinstance(rev, BaseChangeset) or isinstance(rev, AttributeDict): |
|
649 | if isinstance(rev, BaseChangeset) or isinstance(rev, AttributeDict): | |
650 | if rev.op and rev.ref_name: |
|
650 | if rev.op and rev.ref_name: | |
651 | if rev.op == 'delete_branch': |
|
651 | if rev.op == 'delete_branch': | |
652 | lbl = _('Deleted branch: %s') % rev.ref_name |
|
652 | lbl = _('Deleted branch: %s') % rev.ref_name | |
653 | elif rev.op == 'tag': |
|
653 | elif rev.op == 'tag': | |
654 | lbl = _('Created tag: %s') % rev.ref_name |
|
654 | lbl = _('Created tag: %s') % rev.ref_name | |
655 | else: |
|
655 | else: | |
656 | lbl = 'Unknown operation %s' % rev.op |
|
656 | lbl = 'Unknown operation %s' % rev.op | |
657 | else: |
|
657 | else: | |
658 | lazy_cs = True |
|
658 | lazy_cs = True | |
659 | lbl = rev.short_id[:8] |
|
659 | lbl = rev.short_id[:8] | |
660 | url_ = url('changeset_home', repo_name=repo_name, |
|
660 | url_ = url('changeset_home', repo_name=repo_name, | |
661 | revision=rev.raw_id) |
|
661 | revision=rev.raw_id) | |
662 | else: |
|
662 | else: | |
663 | # changeset cannot be found - it might have been stripped or removed |
|
663 | # changeset cannot be found - it might have been stripped or removed | |
664 | lbl = rev[:12] |
|
664 | lbl = rev[:12] | |
665 | title_ = _('Changeset %s not found') % lbl |
|
665 | title_ = _('Changeset %s not found') % lbl | |
666 | if parse_cs: |
|
666 | if parse_cs: | |
667 | return link_to(lbl, url_, title=title_, **{'data-toggle': 'tooltip'}) |
|
667 | return link_to(lbl, url_, title=title_, **{'data-toggle': 'tooltip'}) | |
668 | return link_to(lbl, url_, class_='lazy-cs' if lazy_cs else '', |
|
668 | return link_to(lbl, url_, class_='lazy-cs' if lazy_cs else '', | |
669 | **{'data-raw_id': rev.raw_id, 'data-repo_name': repo_name}) |
|
669 | **{'data-raw_id': rev.raw_id, 'data-repo_name': repo_name}) | |
670 |
|
670 | |||
671 | def _get_op(rev_txt): |
|
671 | def _get_op(rev_txt): | |
672 | _op = None |
|
672 | _op = None | |
673 | _name = rev_txt |
|
673 | _name = rev_txt | |
674 | if len(rev_txt.split('=>')) == 2: |
|
674 | if len(rev_txt.split('=>')) == 2: | |
675 | _op, _name = rev_txt.split('=>') |
|
675 | _op, _name = rev_txt.split('=>') | |
676 | return _op, _name |
|
676 | return _op, _name | |
677 |
|
677 | |||
678 | revs = [] |
|
678 | revs = [] | |
679 | if len(filter(lambda v: v != '', revs_ids)) > 0: |
|
679 | if len(filter(lambda v: v != '', revs_ids)) > 0: | |
680 | repo = None |
|
680 | repo = None | |
681 | for rev in revs_ids[:revs_top_limit]: |
|
681 | for rev in revs_ids[:revs_top_limit]: | |
682 | _op, _name = _get_op(rev) |
|
682 | _op, _name = _get_op(rev) | |
683 |
|
683 | |||
684 | # we want parsed changesets, or new log store format is bad |
|
684 | # we want parsed changesets, or new log store format is bad | |
685 | if parse_cs: |
|
685 | if parse_cs: | |
686 | try: |
|
686 | try: | |
687 | if repo is None: |
|
687 | if repo is None: | |
688 | repo = user_log.repository.scm_instance |
|
688 | repo = user_log.repository.scm_instance | |
689 | _rev = repo.get_changeset(rev) |
|
689 | _rev = repo.get_changeset(rev) | |
690 | revs.append(_rev) |
|
690 | revs.append(_rev) | |
691 | except ChangesetDoesNotExistError: |
|
691 | except ChangesetDoesNotExistError: | |
692 | log.error('cannot find revision %s in this repo', rev) |
|
692 | log.error('cannot find revision %s in this repo', rev) | |
693 | revs.append(rev) |
|
693 | revs.append(rev) | |
694 | else: |
|
694 | else: | |
695 | _rev = AttributeDict({ |
|
695 | _rev = AttributeDict({ | |
696 | 'short_id': rev[:12], |
|
696 | 'short_id': rev[:12], | |
697 | 'raw_id': rev, |
|
697 | 'raw_id': rev, | |
698 | 'message': '', |
|
698 | 'message': '', | |
699 | 'op': _op, |
|
699 | 'op': _op, | |
700 | 'ref_name': _name |
|
700 | 'ref_name': _name | |
701 | }) |
|
701 | }) | |
702 | revs.append(_rev) |
|
702 | revs.append(_rev) | |
703 | cs_links = [" " + ', '.join( |
|
703 | cs_links = [" " + ', '.join( | |
704 | [lnk(rev, repo_name) for rev in revs[:revs_limit]] |
|
704 | [lnk(rev, repo_name) for rev in revs[:revs_limit]] | |
705 | )] |
|
705 | )] | |
706 | _op1, _name1 = _get_op(revs_ids[0]) |
|
706 | _op1, _name1 = _get_op(revs_ids[0]) | |
707 | _op2, _name2 = _get_op(revs_ids[-1]) |
|
707 | _op2, _name2 = _get_op(revs_ids[-1]) | |
708 |
|
708 | |||
709 | _rev = '%s...%s' % (_name1, _name2) |
|
709 | _rev = '%s...%s' % (_name1, _name2) | |
710 |
|
710 | |||
711 | compare_view = ( |
|
711 | compare_view = ( | |
712 | ' <div class="compare_view" data-toggle="tooltip" title="%s">' |
|
712 | ' <div class="compare_view" data-toggle="tooltip" title="%s">' | |
713 | '<a href="%s">%s</a> </div>' % ( |
|
713 | '<a href="%s">%s</a> </div>' % ( | |
714 | _('Show all combined changesets %s->%s') % ( |
|
714 | _('Show all combined changesets %s->%s') % ( | |
715 | revs_ids[0][:12], revs_ids[-1][:12] |
|
715 | revs_ids[0][:12], revs_ids[-1][:12] | |
716 | ), |
|
716 | ), | |
717 | url('changeset_home', repo_name=repo_name, |
|
717 | url('changeset_home', repo_name=repo_name, | |
718 | revision=_rev |
|
718 | revision=_rev | |
719 | ), |
|
719 | ), | |
720 | _('Compare view') |
|
720 | _('Compare view') | |
721 | ) |
|
721 | ) | |
722 | ) |
|
722 | ) | |
723 |
|
723 | |||
724 | # if we have exactly one more than normally displayed |
|
724 | # if we have exactly one more than normally displayed | |
725 | # just display it, takes less space than displaying |
|
725 | # just display it, takes less space than displaying | |
726 | # "and 1 more revisions" |
|
726 | # "and 1 more revisions" | |
727 | if len(revs_ids) == revs_limit + 1: |
|
727 | if len(revs_ids) == revs_limit + 1: | |
728 | cs_links.append(", " + lnk(revs[revs_limit], repo_name)) |
|
728 | cs_links.append(", " + lnk(revs[revs_limit], repo_name)) | |
729 |
|
729 | |||
730 | # hidden-by-default ones |
|
730 | # hidden-by-default ones | |
731 | if len(revs_ids) > revs_limit + 1: |
|
731 | if len(revs_ids) > revs_limit + 1: | |
732 | uniq_id = revs_ids[0] |
|
732 | uniq_id = revs_ids[0] | |
733 | html_tmpl = ( |
|
733 | html_tmpl = ( | |
734 | '<span> %s <a class="show_more" id="_%s" ' |
|
734 | '<span> %s <a class="show_more" id="_%s" ' | |
735 | 'href="#more">%s</a> %s</span>' |
|
735 | 'href="#more">%s</a> %s</span>' | |
736 | ) |
|
736 | ) | |
737 | if not feed: |
|
737 | if not feed: | |
738 | cs_links.append(html_tmpl % ( |
|
738 | cs_links.append(html_tmpl % ( | |
739 | _('and'), |
|
739 | _('and'), | |
740 | uniq_id, _('%s more') % (len(revs_ids) - revs_limit), |
|
740 | uniq_id, _('%s more') % (len(revs_ids) - revs_limit), | |
741 | _('revisions') |
|
741 | _('revisions') | |
742 | ) |
|
742 | ) | |
743 | ) |
|
743 | ) | |
744 |
|
744 | |||
745 | if not feed: |
|
745 | if not feed: | |
746 | html_tmpl = '<span id="%s" style="display:none">, %s </span>' |
|
746 | html_tmpl = '<span id="%s" style="display:none">, %s </span>' | |
747 | else: |
|
747 | else: | |
748 | html_tmpl = '<span id="%s"> %s </span>' |
|
748 | html_tmpl = '<span id="%s"> %s </span>' | |
749 |
|
749 | |||
750 | morelinks = ', '.join( |
|
750 | morelinks = ', '.join( | |
751 | [lnk(rev, repo_name) for rev in revs[revs_limit:]] |
|
751 | [lnk(rev, repo_name) for rev in revs[revs_limit:]] | |
752 | ) |
|
752 | ) | |
753 |
|
753 | |||
754 | if len(revs_ids) > revs_top_limit: |
|
754 | if len(revs_ids) > revs_top_limit: | |
755 | morelinks += ', ...' |
|
755 | morelinks += ', ...' | |
756 |
|
756 | |||
757 | cs_links.append(html_tmpl % (uniq_id, morelinks)) |
|
757 | cs_links.append(html_tmpl % (uniq_id, morelinks)) | |
758 | if len(revs) > 1: |
|
758 | if len(revs) > 1: | |
759 | cs_links.append(compare_view) |
|
759 | cs_links.append(compare_view) | |
760 | return ''.join(cs_links) |
|
760 | return ''.join(cs_links) | |
761 |
|
761 | |||
762 | def get_fork_name(): |
|
762 | def get_fork_name(): | |
763 | repo_name = action_params |
|
763 | repo_name = action_params | |
764 | url_ = url('summary_home', repo_name=repo_name) |
|
764 | url_ = url('summary_home', repo_name=repo_name) | |
765 | return _('Fork name %s') % link_to(action_params, url_) |
|
765 | return _('Fork name %s') % link_to(action_params, url_) | |
766 |
|
766 | |||
767 | def get_user_name(): |
|
767 | def get_user_name(): | |
768 | user_name = action_params |
|
768 | user_name = action_params | |
769 | return user_name |
|
769 | return user_name | |
770 |
|
770 | |||
771 | def get_users_group(): |
|
771 | def get_users_group(): | |
772 | group_name = action_params |
|
772 | group_name = action_params | |
773 | return group_name |
|
773 | return group_name | |
774 |
|
774 | |||
775 | def get_pull_request(): |
|
775 | def get_pull_request(): | |
776 | from kallithea.model.db import PullRequest |
|
776 | from kallithea.model.db import PullRequest | |
777 | pull_request_id = action_params |
|
777 | pull_request_id = action_params | |
778 | nice_id = PullRequest.make_nice_id(pull_request_id) |
|
778 | nice_id = PullRequest.make_nice_id(pull_request_id) | |
779 |
|
779 | |||
780 | deleted = user_log.repository is None |
|
780 | deleted = user_log.repository is None | |
781 | if deleted: |
|
781 | if deleted: | |
782 | repo_name = user_log.repository_name |
|
782 | repo_name = user_log.repository_name | |
783 | else: |
|
783 | else: | |
784 | repo_name = user_log.repository.repo_name |
|
784 | repo_name = user_log.repository.repo_name | |
785 |
|
785 | |||
786 | return link_to(_('Pull request %s') % nice_id, |
|
786 | return link_to(_('Pull request %s') % nice_id, | |
787 | url('pullrequest_show', repo_name=repo_name, |
|
787 | url('pullrequest_show', repo_name=repo_name, | |
788 | pull_request_id=pull_request_id)) |
|
788 | pull_request_id=pull_request_id)) | |
789 |
|
789 | |||
790 | def get_archive_name(): |
|
790 | def get_archive_name(): | |
791 | archive_name = action_params |
|
791 | archive_name = action_params | |
792 | return archive_name |
|
792 | return archive_name | |
793 |
|
793 | |||
794 | # action : translated str, callback(extractor), icon |
|
794 | # action : translated str, callback(extractor), icon | |
795 | action_map = { |
|
795 | action_map = { | |
796 | 'user_deleted_repo': (_('[deleted] repository'), |
|
796 | 'user_deleted_repo': (_('[deleted] repository'), | |
797 | None, 'icon-trashcan'), |
|
797 | None, 'icon-trashcan'), | |
798 | 'user_created_repo': (_('[created] repository'), |
|
798 | 'user_created_repo': (_('[created] repository'), | |
799 | None, 'icon-plus'), |
|
799 | None, 'icon-plus'), | |
800 | 'user_created_fork': (_('[created] repository as fork'), |
|
800 | 'user_created_fork': (_('[created] repository as fork'), | |
801 | None, 'icon-fork'), |
|
801 | None, 'icon-fork'), | |
802 | 'user_forked_repo': (_('[forked] repository'), |
|
802 | 'user_forked_repo': (_('[forked] repository'), | |
803 | get_fork_name, 'icon-fork'), |
|
803 | get_fork_name, 'icon-fork'), | |
804 | 'user_updated_repo': (_('[updated] repository'), |
|
804 | 'user_updated_repo': (_('[updated] repository'), | |
805 | None, 'icon-pencil'), |
|
805 | None, 'icon-pencil'), | |
806 | 'user_downloaded_archive': (_('[downloaded] archive from repository'), |
|
806 | 'user_downloaded_archive': (_('[downloaded] archive from repository'), | |
807 | get_archive_name, 'icon-download-cloud'), |
|
807 | get_archive_name, 'icon-download-cloud'), | |
808 | 'admin_deleted_repo': (_('[delete] repository'), |
|
808 | 'admin_deleted_repo': (_('[delete] repository'), | |
809 | None, 'icon-trashcan'), |
|
809 | None, 'icon-trashcan'), | |
810 | 'admin_created_repo': (_('[created] repository'), |
|
810 | 'admin_created_repo': (_('[created] repository'), | |
811 | None, 'icon-plus'), |
|
811 | None, 'icon-plus'), | |
812 | 'admin_forked_repo': (_('[forked] repository'), |
|
812 | 'admin_forked_repo': (_('[forked] repository'), | |
813 | None, 'icon-fork'), |
|
813 | None, 'icon-fork'), | |
814 | 'admin_updated_repo': (_('[updated] repository'), |
|
814 | 'admin_updated_repo': (_('[updated] repository'), | |
815 | None, 'icon-pencil'), |
|
815 | None, 'icon-pencil'), | |
816 | 'admin_created_user': (_('[created] user'), |
|
816 | 'admin_created_user': (_('[created] user'), | |
817 | get_user_name, 'icon-user'), |
|
817 | get_user_name, 'icon-user'), | |
818 | 'admin_updated_user': (_('[updated] user'), |
|
818 | 'admin_updated_user': (_('[updated] user'), | |
819 | get_user_name, 'icon-user'), |
|
819 | get_user_name, 'icon-user'), | |
820 | 'admin_created_users_group': (_('[created] user group'), |
|
820 | 'admin_created_users_group': (_('[created] user group'), | |
821 | get_users_group, 'icon-pencil'), |
|
821 | get_users_group, 'icon-pencil'), | |
822 | 'admin_updated_users_group': (_('[updated] user group'), |
|
822 | 'admin_updated_users_group': (_('[updated] user group'), | |
823 | get_users_group, 'icon-pencil'), |
|
823 | get_users_group, 'icon-pencil'), | |
824 | 'user_commented_revision': (_('[commented] on revision in repository'), |
|
824 | 'user_commented_revision': (_('[commented] on revision in repository'), | |
825 | get_cs_links, 'icon-comment'), |
|
825 | get_cs_links, 'icon-comment'), | |
826 | 'user_commented_pull_request': (_('[commented] on pull request for'), |
|
826 | 'user_commented_pull_request': (_('[commented] on pull request for'), | |
827 | get_pull_request, 'icon-comment'), |
|
827 | get_pull_request, 'icon-comment'), | |
828 | 'user_closed_pull_request': (_('[closed] pull request for'), |
|
828 | 'user_closed_pull_request': (_('[closed] pull request for'), | |
829 | get_pull_request, 'icon-ok'), |
|
829 | get_pull_request, 'icon-ok'), | |
830 | 'push': (_('[pushed] into'), |
|
830 | 'push': (_('[pushed] into'), | |
831 | get_cs_links, 'icon-move-up'), |
|
831 | get_cs_links, 'icon-move-up'), | |
832 | 'push_local': (_('[committed via Kallithea] into repository'), |
|
832 | 'push_local': (_('[committed via Kallithea] into repository'), | |
833 | get_cs_links, 'icon-pencil'), |
|
833 | get_cs_links, 'icon-pencil'), | |
834 | 'push_remote': (_('[pulled from remote] into repository'), |
|
834 | 'push_remote': (_('[pulled from remote] into repository'), | |
835 | get_cs_links, 'icon-move-up'), |
|
835 | get_cs_links, 'icon-move-up'), | |
836 | 'pull': (_('[pulled] from'), |
|
836 | 'pull': (_('[pulled] from'), | |
837 | None, 'icon-move-down'), |
|
837 | None, 'icon-move-down'), | |
838 | 'started_following_repo': (_('[started following] repository'), |
|
838 | 'started_following_repo': (_('[started following] repository'), | |
839 | None, 'icon-heart'), |
|
839 | None, 'icon-heart'), | |
840 | 'stopped_following_repo': (_('[stopped following] repository'), |
|
840 | 'stopped_following_repo': (_('[stopped following] repository'), | |
841 | None, 'icon-heart-empty'), |
|
841 | None, 'icon-heart-empty'), | |
842 | } |
|
842 | } | |
843 |
|
843 | |||
844 | action_str = action_map.get(action, action) |
|
844 | action_str = action_map.get(action, action) | |
845 | if feed: |
|
845 | if feed: | |
846 | action = action_str[0].replace('[', '').replace(']', '') |
|
846 | action = action_str[0].replace('[', '').replace(']', '') | |
847 | else: |
|
847 | else: | |
848 | action = action_str[0] \ |
|
848 | action = action_str[0] \ | |
849 | .replace('[', '<b>') \ |
|
849 | .replace('[', '<b>') \ | |
850 | .replace(']', '</b>') |
|
850 | .replace(']', '</b>') | |
851 |
|
851 | |||
852 | action_params_func = lambda: "" |
|
852 | action_params_func = lambda: "" | |
853 |
|
853 | |||
854 | if callable(action_str[1]): |
|
854 | if callable(action_str[1]): | |
855 | action_params_func = action_str[1] |
|
855 | action_params_func = action_str[1] | |
856 |
|
856 | |||
857 | def action_parser_icon(): |
|
857 | def action_parser_icon(): | |
858 | action = user_log.action |
|
858 | action = user_log.action | |
859 | action_params = None |
|
859 | action_params = None | |
860 | x = action.split(':') |
|
860 | x = action.split(':') | |
861 |
|
861 | |||
862 | if len(x) > 1: |
|
862 | if len(x) > 1: | |
863 | action, action_params = x |
|
863 | action, action_params = x | |
864 |
|
864 | |||
865 | ico = action_map.get(action, ['', '', ''])[2] |
|
865 | ico = action_map.get(action, ['', '', ''])[2] | |
866 | html = """<i class="%s"></i>""" % ico |
|
866 | html = """<i class="%s"></i>""" % ico | |
867 | return literal(html) |
|
867 | return literal(html) | |
868 |
|
868 | |||
869 | # returned callbacks we need to call to get |
|
869 | # returned callbacks we need to call to get | |
870 | return [lambda: literal(action), action_params_func, action_parser_icon] |
|
870 | return [lambda: literal(action), action_params_func, action_parser_icon] | |
871 |
|
871 | |||
872 |
|
872 | |||
873 | #============================================================================== |
|
873 | #============================================================================== | |
874 | # GRAVATAR URL |
|
874 | # GRAVATAR URL | |
875 | #============================================================================== |
|
875 | #============================================================================== | |
876 | def gravatar_div(email_address, cls='', size=30, **div_attributes): |
|
876 | def gravatar_div(email_address, cls='', size=30, **div_attributes): | |
877 | """Return an html literal with a span around a gravatar if they are enabled. |
|
877 | """Return an html literal with a span around a gravatar if they are enabled. | |
878 | Extra keyword parameters starting with 'div_' will get the prefix removed |
|
878 | Extra keyword parameters starting with 'div_' will get the prefix removed | |
879 | and '_' changed to '-' and be used as attributes on the div. The default |
|
879 | and '_' changed to '-' and be used as attributes on the div. The default | |
880 | class is 'gravatar'. |
|
880 | class is 'gravatar'. | |
881 | """ |
|
881 | """ | |
882 | from tg import tmpl_context as c |
|
882 | from tg import tmpl_context as c | |
883 | if not c.visual.use_gravatar: |
|
883 | if not c.visual.use_gravatar: | |
884 | return '' |
|
884 | return '' | |
885 | if 'div_class' not in div_attributes: |
|
885 | if 'div_class' not in div_attributes: | |
886 | div_attributes['div_class'] = "gravatar" |
|
886 | div_attributes['div_class'] = "gravatar" | |
887 | attributes = [] |
|
887 | attributes = [] | |
888 | for k, v in sorted(div_attributes.items()): |
|
888 | for k, v in sorted(div_attributes.items()): | |
889 | assert k.startswith('div_'), k |
|
889 | assert k.startswith('div_'), k | |
890 | attributes.append(' %s="%s"' % (k[4:].replace('_', '-'), escape(v))) |
|
890 | attributes.append(' %s="%s"' % (k[4:].replace('_', '-'), escape(v))) | |
891 | return literal("""<span%s>%s</span>""" % |
|
891 | return literal("""<span%s>%s</span>""" % | |
892 | (''.join(attributes), |
|
892 | (''.join(attributes), | |
893 | gravatar(email_address, cls=cls, size=size))) |
|
893 | gravatar(email_address, cls=cls, size=size))) | |
894 |
|
894 | |||
895 |
|
895 | |||
896 | def gravatar(email_address, cls='', size=30): |
|
896 | def gravatar(email_address, cls='', size=30): | |
897 | """return html element of the gravatar |
|
897 | """return html element of the gravatar | |
898 |
|
898 | |||
899 | This method will return an <img> with the resolution double the size (for |
|
899 | This method will return an <img> with the resolution double the size (for | |
900 | retina screens) of the image. If the url returned from gravatar_url is |
|
900 | retina screens) of the image. If the url returned from gravatar_url is | |
901 | empty then we fallback to using an icon. |
|
901 | empty then we fallback to using an icon. | |
902 |
|
902 | |||
903 | """ |
|
903 | """ | |
904 | from tg import tmpl_context as c |
|
904 | from tg import tmpl_context as c | |
905 | if not c.visual.use_gravatar: |
|
905 | if not c.visual.use_gravatar: | |
906 | return '' |
|
906 | return '' | |
907 |
|
907 | |||
908 | src = gravatar_url(email_address, size * 2) |
|
908 | src = gravatar_url(email_address, size * 2) | |
909 |
|
909 | |||
910 | if src: |
|
910 | if src: | |
911 | # here it makes sense to use style="width: ..." (instead of, say, a |
|
911 | # here it makes sense to use style="width: ..." (instead of, say, a | |
912 | # stylesheet) because we using this to generate a high-res (retina) size |
|
912 | # stylesheet) because we using this to generate a high-res (retina) size | |
913 | html = ('<i class="icon-gravatar {cls}"' |
|
913 | html = ('<i class="icon-gravatar {cls}"' | |
914 | ' style="font-size: {size}px;background-size: {size}px;background-image: url(\'{src}\')"' |
|
914 | ' style="font-size: {size}px;background-size: {size}px;background-image: url(\'{src}\')"' | |
915 | '></i>').format(cls=cls, size=size, src=src) |
|
915 | '></i>').format(cls=cls, size=size, src=src) | |
916 |
|
916 | |||
917 | else: |
|
917 | else: | |
918 | # if src is empty then there was no gravatar, so we use a font icon |
|
918 | # if src is empty then there was no gravatar, so we use a font icon | |
919 | html = ("""<i class="icon-user {cls}" style="font-size: {size}px;"></i>""" |
|
919 | html = ("""<i class="icon-user {cls}" style="font-size: {size}px;"></i>""" | |
920 | .format(cls=cls, size=size, src=src)) |
|
920 | .format(cls=cls, size=size, src=src)) | |
921 |
|
921 | |||
922 | return literal(html) |
|
922 | return literal(html) | |
923 |
|
923 | |||
924 |
|
924 | |||
925 | def gravatar_url(email_address, size=30, default=''): |
|
925 | def gravatar_url(email_address, size=30, default=''): | |
926 | # doh, we need to re-import those to mock it later |
|
926 | # doh, we need to re-import those to mock it later | |
927 | from kallithea.config.routing import url |
|
927 | from kallithea.config.routing import url | |
928 | from kallithea.model.db import User |
|
928 | from kallithea.model.db import User | |
929 | from tg import tmpl_context as c |
|
929 | from tg import tmpl_context as c | |
930 | if not c.visual.use_gravatar: |
|
930 | if not c.visual.use_gravatar: | |
931 | return "" |
|
931 | return "" | |
932 |
|
932 | |||
933 | _def = 'anonymous@kallithea-scm.org' # default gravatar |
|
933 | _def = 'anonymous@kallithea-scm.org' # default gravatar | |
934 | email_address = email_address or _def |
|
934 | email_address = email_address or _def | |
935 |
|
935 | |||
936 | if email_address == _def: |
|
936 | if email_address == _def: | |
937 | return default |
|
937 | return default | |
938 |
|
938 | |||
939 | parsed_url = urlparse.urlparse(url.current(qualified=True)) |
|
939 | parsed_url = urlparse.urlparse(url.current(qualified=True)) | |
940 | url = (c.visual.gravatar_url or User.DEFAULT_GRAVATAR_URL) \ |
|
940 | url = (c.visual.gravatar_url or User.DEFAULT_GRAVATAR_URL) \ | |
941 | .replace('{email}', email_address) \ |
|
941 | .replace('{email}', email_address) \ | |
942 | .replace('{md5email}', hashlib.md5(safe_str(email_address).lower()).hexdigest()) \ |
|
942 | .replace('{md5email}', hashlib.md5(safe_str(email_address).lower()).hexdigest()) \ | |
943 | .replace('{netloc}', parsed_url.netloc) \ |
|
943 | .replace('{netloc}', parsed_url.netloc) \ | |
944 | .replace('{scheme}', parsed_url.scheme) \ |
|
944 | .replace('{scheme}', parsed_url.scheme) \ | |
945 | .replace('{size}', safe_str(size)) |
|
945 | .replace('{size}', safe_str(size)) | |
946 | return url |
|
946 | return url | |
947 |
|
947 | |||
948 |
|
948 | |||
949 | def changed_tooltip(nodes): |
|
949 | def changed_tooltip(nodes): | |
950 | """ |
|
950 | """ | |
951 | Generates a html string for changed nodes in changeset page. |
|
951 | Generates a html string for changed nodes in changeset page. | |
952 | It limits the output to 30 entries |
|
952 | It limits the output to 30 entries | |
953 |
|
953 | |||
954 | :param nodes: LazyNodesGenerator |
|
954 | :param nodes: LazyNodesGenerator | |
955 | """ |
|
955 | """ | |
956 | if nodes: |
|
956 | if nodes: | |
957 | pref = ': <br/> ' |
|
957 | pref = ': <br/> ' | |
958 | suf = '' |
|
958 | suf = '' | |
959 | if len(nodes) > 30: |
|
959 | if len(nodes) > 30: | |
960 | suf = '<br/>' + _(' and %s more') % (len(nodes) - 30) |
|
960 | suf = '<br/>' + _(' and %s more') % (len(nodes) - 30) | |
961 | return literal(pref + '<br/> '.join([safe_unicode(x.path) |
|
961 | return literal(pref + '<br/> '.join([safe_unicode(x.path) | |
962 | for x in nodes[:30]]) + suf) |
|
962 | for x in nodes[:30]]) + suf) | |
963 | else: |
|
963 | else: | |
964 | return ': ' + _('No files') |
|
964 | return ': ' + _('No files') | |
965 |
|
965 | |||
966 |
|
966 | |||
967 | def fancy_file_stats(stats): |
|
967 | def fancy_file_stats(stats): | |
968 | """ |
|
968 | """ | |
969 | Displays a fancy two colored bar for number of added/deleted |
|
969 | Displays a fancy two colored bar for number of added/deleted | |
970 | lines of code on file |
|
970 | lines of code on file | |
971 |
|
971 | |||
972 | :param stats: two element list of added/deleted lines of code |
|
972 | :param stats: two element list of added/deleted lines of code | |
973 | """ |
|
973 | """ | |
974 | from kallithea.lib.diffs import NEW_FILENODE, DEL_FILENODE, \ |
|
974 | from kallithea.lib.diffs import NEW_FILENODE, DEL_FILENODE, \ | |
975 | MOD_FILENODE, RENAMED_FILENODE, CHMOD_FILENODE, BIN_FILENODE |
|
975 | MOD_FILENODE, RENAMED_FILENODE, CHMOD_FILENODE, BIN_FILENODE | |
976 |
|
976 | |||
977 | a, d = stats['added'], stats['deleted'] |
|
977 | a, d = stats['added'], stats['deleted'] | |
978 | width = 100 |
|
978 | width = 100 | |
979 |
|
979 | |||
980 | if stats['binary']: |
|
980 | if stats['binary']: | |
981 | # binary mode |
|
981 | # binary mode | |
982 | lbl = '' |
|
982 | lbl = '' | |
983 | bin_op = 1 |
|
983 | bin_op = 1 | |
984 |
|
984 | |||
985 | if BIN_FILENODE in stats['ops']: |
|
985 | if BIN_FILENODE in stats['ops']: | |
986 | lbl = 'bin+' |
|
986 | lbl = 'bin+' | |
987 |
|
987 | |||
988 | if NEW_FILENODE in stats['ops']: |
|
988 | if NEW_FILENODE in stats['ops']: | |
989 | lbl += _('new file') |
|
989 | lbl += _('new file') | |
990 | bin_op = NEW_FILENODE |
|
990 | bin_op = NEW_FILENODE | |
991 | elif MOD_FILENODE in stats['ops']: |
|
991 | elif MOD_FILENODE in stats['ops']: | |
992 | lbl += _('mod') |
|
992 | lbl += _('mod') | |
993 | bin_op = MOD_FILENODE |
|
993 | bin_op = MOD_FILENODE | |
994 | elif DEL_FILENODE in stats['ops']: |
|
994 | elif DEL_FILENODE in stats['ops']: | |
995 | lbl += _('del') |
|
995 | lbl += _('del') | |
996 | bin_op = DEL_FILENODE |
|
996 | bin_op = DEL_FILENODE | |
997 | elif RENAMED_FILENODE in stats['ops']: |
|
997 | elif RENAMED_FILENODE in stats['ops']: | |
998 | lbl += _('rename') |
|
998 | lbl += _('rename') | |
999 | bin_op = RENAMED_FILENODE |
|
999 | bin_op = RENAMED_FILENODE | |
1000 |
|
1000 | |||
1001 | # chmod can go with other operations |
|
1001 | # chmod can go with other operations | |
1002 | if CHMOD_FILENODE in stats['ops']: |
|
1002 | if CHMOD_FILENODE in stats['ops']: | |
1003 | _org_lbl = _('chmod') |
|
1003 | _org_lbl = _('chmod') | |
1004 | lbl += _org_lbl if lbl.endswith('+') else '+%s' % _org_lbl |
|
1004 | lbl += _org_lbl if lbl.endswith('+') else '+%s' % _org_lbl | |
1005 |
|
1005 | |||
1006 | #import ipdb;ipdb.set_trace() |
|
1006 | #import ipdb;ipdb.set_trace() | |
1007 | b_d = '<div class="bin bin%s progress-bar" style="width:100%%">%s</div>' % (bin_op, lbl) |
|
1007 | b_d = '<div class="bin bin%s progress-bar" style="width:100%%">%s</div>' % (bin_op, lbl) | |
1008 | b_a = '<div class="bin bin1" style="width:0%"></div>' |
|
1008 | b_a = '<div class="bin bin1" style="width:0%"></div>' | |
1009 | return literal('<div style="width:%spx" class="progress">%s%s</div>' % (width, b_a, b_d)) |
|
1009 | return literal('<div style="width:%spx" class="progress">%s%s</div>' % (width, b_a, b_d)) | |
1010 |
|
1010 | |||
1011 | t = stats['added'] + stats['deleted'] |
|
1011 | t = stats['added'] + stats['deleted'] | |
1012 | unit = float(width) / (t or 1) |
|
1012 | unit = float(width) / (t or 1) | |
1013 |
|
1013 | |||
1014 | # needs > 9% of width to be visible or 0 to be hidden |
|
1014 | # needs > 9% of width to be visible or 0 to be hidden | |
1015 | a_p = max(9, unit * a) if a > 0 else 0 |
|
1015 | a_p = max(9, unit * a) if a > 0 else 0 | |
1016 | d_p = max(9, unit * d) if d > 0 else 0 |
|
1016 | d_p = max(9, unit * d) if d > 0 else 0 | |
1017 | p_sum = a_p + d_p |
|
1017 | p_sum = a_p + d_p | |
1018 |
|
1018 | |||
1019 | if p_sum > width: |
|
1019 | if p_sum > width: | |
1020 | # adjust the percentage to be == 100% since we adjusted to 9 |
|
1020 | # adjust the percentage to be == 100% since we adjusted to 9 | |
1021 | if a_p > d_p: |
|
1021 | if a_p > d_p: | |
1022 | a_p = a_p - (p_sum - width) |
|
1022 | a_p = a_p - (p_sum - width) | |
1023 | else: |
|
1023 | else: | |
1024 | d_p = d_p - (p_sum - width) |
|
1024 | d_p = d_p - (p_sum - width) | |
1025 |
|
1025 | |||
1026 | a_v = a if a > 0 else '' |
|
1026 | a_v = a if a > 0 else '' | |
1027 | d_v = d if d > 0 else '' |
|
1027 | d_v = d if d > 0 else '' | |
1028 |
|
1028 | |||
1029 | d_a = '<div class="added progress-bar" style="width:%s%%">%s</div>' % ( |
|
1029 | d_a = '<div class="added progress-bar" style="width:%s%%">%s</div>' % ( | |
1030 | a_p, a_v |
|
1030 | a_p, a_v | |
1031 | ) |
|
1031 | ) | |
1032 | d_d = '<div class="deleted progress-bar" style="width:%s%%">%s</div>' % ( |
|
1032 | d_d = '<div class="deleted progress-bar" style="width:%s%%">%s</div>' % ( | |
1033 | d_p, d_v |
|
1033 | d_p, d_v | |
1034 | ) |
|
1034 | ) | |
1035 | return literal('<div class="progress" style="width:%spx">%s%s</div>' % (width, d_a, d_d)) |
|
1035 | return literal('<div class="progress" style="width:%spx">%s%s</div>' % (width, d_a, d_d)) | |
1036 |
|
1036 | |||
1037 |
|
1037 | |||
1038 | _URLIFY_RE = re.compile(r''' |
|
1038 | _URLIFY_RE = re.compile(r''' | |
1039 | # URL markup |
|
1039 | # URL markup | |
1040 | (?P<url>%s) | |
|
1040 | (?P<url>%s) | | |
1041 | # @mention markup |
|
1041 | # @mention markup | |
1042 | (?P<mention>%s) | |
|
1042 | (?P<mention>%s) | | |
1043 | # Changeset hash markup |
|
1043 | # Changeset hash markup | |
1044 | (?<!\w|[-_]) |
|
1044 | (?<!\w|[-_]) | |
1045 | (?P<hash>[0-9a-f]{12,40}) |
|
1045 | (?P<hash>[0-9a-f]{12,40}) | |
1046 | (?!\w|[-_]) | |
|
1046 | (?!\w|[-_]) | | |
1047 | # Markup of *bold text* |
|
1047 | # Markup of *bold text* | |
1048 | (?: |
|
1048 | (?: | |
1049 | (?:^|(?<=\s)) |
|
1049 | (?:^|(?<=\s)) | |
1050 | (?P<bold> [*] (?!\s) [^*\n]* (?<!\s) [*] ) |
|
1050 | (?P<bold> [*] (?!\s) [^*\n]* (?<!\s) [*] ) | |
1051 | (?![*\w]) |
|
1051 | (?![*\w]) | |
1052 | ) | |
|
1052 | ) | | |
1053 | # "Stylize" markup |
|
1053 | # "Stylize" markup | |
1054 | \[see\ \=>\ *(?P<seen>[a-zA-Z0-9\/\=\?\&\ \:\/\.\-]*)\] | |
|
1054 | \[see\ \=>\ *(?P<seen>[a-zA-Z0-9\/\=\?\&\ \:\/\.\-]*)\] | | |
1055 | \[license\ \=>\ *(?P<license>[a-zA-Z0-9\/\=\?\&\ \:\/\.\-]*)\] | |
|
1055 | \[license\ \=>\ *(?P<license>[a-zA-Z0-9\/\=\?\&\ \:\/\.\-]*)\] | | |
1056 | \[(?P<tagtype>requires|recommends|conflicts|base)\ \=>\ *(?P<tagvalue>[a-zA-Z0-9\-\/]*)\] | |
|
1056 | \[(?P<tagtype>requires|recommends|conflicts|base)\ \=>\ *(?P<tagvalue>[a-zA-Z0-9\-\/]*)\] | | |
1057 | \[(?:lang|language)\ \=>\ *(?P<lang>[a-zA-Z\-\/\#\+]*)\] | |
|
1057 | \[(?:lang|language)\ \=>\ *(?P<lang>[a-zA-Z\-\/\#\+]*)\] | | |
1058 | \[(?P<tag>[a-z]+)\] |
|
1058 | \[(?P<tag>[a-z]+)\] | |
1059 | ''' % (url_re.pattern, MENTIONS_REGEX.pattern), |
|
1059 | ''' % (url_re.pattern, MENTIONS_REGEX.pattern), | |
1060 | re.VERBOSE | re.MULTILINE | re.IGNORECASE) |
|
1060 | re.VERBOSE | re.MULTILINE | re.IGNORECASE) | |
1061 |
|
1061 | |||
1062 |
|
1062 | |||
1063 | def urlify_text(s, repo_name=None, link_=None, truncate=None, stylize=False, truncatef=truncate): |
|
1063 | def urlify_text(s, repo_name=None, link_=None, truncate=None, stylize=False, truncatef=truncate): | |
1064 | """ |
|
1064 | """ | |
1065 | Parses given text message and make literal html with markup. |
|
1065 | Parses given text message and make literal html with markup. | |
1066 | The text will be truncated to the specified length. |
|
1066 | The text will be truncated to the specified length. | |
1067 | Hashes are turned into changeset links to specified repository. |
|
1067 | Hashes are turned into changeset links to specified repository. | |
1068 | URLs links to what they say. |
|
1068 | URLs links to what they say. | |
1069 | Issues are linked to given issue-server. |
|
1069 | Issues are linked to given issue-server. | |
1070 | If link_ is provided, all text not already linking somewhere will link there. |
|
1070 | If link_ is provided, all text not already linking somewhere will link there. | |
1071 | """ |
|
1071 | """ | |
1072 |
|
1072 | |||
1073 | def _replace(match_obj): |
|
1073 | def _replace(match_obj): | |
1074 | url = match_obj.group('url') |
|
1074 | url = match_obj.group('url') | |
1075 | if url is not None: |
|
1075 | if url is not None: | |
1076 | return '<a href="%(url)s">%(url)s</a>' % {'url': url} |
|
1076 | return '<a href="%(url)s">%(url)s</a>' % {'url': url} | |
1077 | mention = match_obj.group('mention') |
|
1077 | mention = match_obj.group('mention') | |
1078 | if mention is not None: |
|
1078 | if mention is not None: | |
1079 | return '<b>%s</b>' % mention |
|
1079 | return '<b>%s</b>' % mention | |
1080 | hash_ = match_obj.group('hash') |
|
1080 | hash_ = match_obj.group('hash') | |
1081 | if hash_ is not None and repo_name is not None: |
|
1081 | if hash_ is not None and repo_name is not None: | |
1082 | from kallithea.config.routing import url # doh, we need to re-import url to mock it later |
|
1082 | from kallithea.config.routing import url # doh, we need to re-import url to mock it later | |
1083 | return '<a class="changeset_hash" href="%(url)s">%(hash)s</a>' % { |
|
1083 | return '<a class="changeset_hash" href="%(url)s">%(hash)s</a>' % { | |
1084 | 'url': url('changeset_home', repo_name=repo_name, revision=hash_), |
|
1084 | 'url': url('changeset_home', repo_name=repo_name, revision=hash_), | |
1085 | 'hash': hash_, |
|
1085 | 'hash': hash_, | |
1086 | } |
|
1086 | } | |
1087 | bold = match_obj.group('bold') |
|
1087 | bold = match_obj.group('bold') | |
1088 | if bold is not None: |
|
1088 | if bold is not None: | |
1089 | return '<b>*%s*</b>' % _urlify(bold[1:-1]) |
|
1089 | return '<b>*%s*</b>' % _urlify(bold[1:-1]) | |
1090 | if stylize: |
|
1090 | if stylize: | |
1091 | seen = match_obj.group('seen') |
|
1091 | seen = match_obj.group('seen') | |
1092 | if seen: |
|
1092 | if seen: | |
1093 | return '<div class="label label-meta" data-tag="see">see => %s</div>' % seen |
|
1093 | return '<div class="label label-meta" data-tag="see">see => %s</div>' % seen | |
1094 | license = match_obj.group('license') |
|
1094 | license = match_obj.group('license') | |
1095 | if license: |
|
1095 | if license: | |
1096 | return '<div class="label label-meta" data-tag="license"><a href="http://www.opensource.org/licenses/%s">%s</a></div>' % (license, license) |
|
1096 | return '<div class="label label-meta" data-tag="license"><a href="http://www.opensource.org/licenses/%s">%s</a></div>' % (license, license) | |
1097 | tagtype = match_obj.group('tagtype') |
|
1097 | tagtype = match_obj.group('tagtype') | |
1098 | if tagtype: |
|
1098 | if tagtype: | |
1099 | tagvalue = match_obj.group('tagvalue') |
|
1099 | tagvalue = match_obj.group('tagvalue') | |
1100 | return '<div class="label label-meta" data-tag="%s">%s => <a href="/%s">%s</a></div>' % (tagtype, tagtype, tagvalue, tagvalue) |
|
1100 | return '<div class="label label-meta" data-tag="%s">%s => <a href="/%s">%s</a></div>' % (tagtype, tagtype, tagvalue, tagvalue) | |
1101 | lang = match_obj.group('lang') |
|
1101 | lang = match_obj.group('lang') | |
1102 | if lang: |
|
1102 | if lang: | |
1103 | return '<div class="label label-meta" data-tag="lang">%s</div>' % lang |
|
1103 | return '<div class="label label-meta" data-tag="lang">%s</div>' % lang | |
1104 | tag = match_obj.group('tag') |
|
1104 | tag = match_obj.group('tag') | |
1105 | if tag: |
|
1105 | if tag: | |
1106 | return '<div class="label label-meta" data-tag="%s">%s</div>' % (tag, tag) |
|
1106 | return '<div class="label label-meta" data-tag="%s">%s</div>' % (tag, tag) | |
1107 | return match_obj.group(0) |
|
1107 | return match_obj.group(0) | |
1108 |
|
1108 | |||
1109 | def _urlify(s): |
|
1109 | def _urlify(s): | |
1110 | """ |
|
1110 | """ | |
1111 | Extract urls from text and make html links out of them |
|
1111 | Extract urls from text and make html links out of them | |
1112 | """ |
|
1112 | """ | |
1113 | return _URLIFY_RE.sub(_replace, s) |
|
1113 | return _URLIFY_RE.sub(_replace, s) | |
1114 |
|
1114 | |||
1115 | if truncate is None: |
|
1115 | if truncate is None: | |
1116 | s = s.rstrip() |
|
1116 | s = s.rstrip() | |
1117 | else: |
|
1117 | else: | |
1118 | s = truncatef(s, truncate, whole_word=True) |
|
1118 | s = truncatef(s, truncate, whole_word=True) | |
1119 | s = html_escape(s) |
|
1119 | s = html_escape(s) | |
1120 | s = _urlify(s) |
|
1120 | s = _urlify(s) | |
1121 | if repo_name is not None: |
|
1121 | if repo_name is not None: | |
1122 | s = urlify_issues(s, repo_name) |
|
1122 | s = urlify_issues(s, repo_name) | |
1123 | if link_ is not None: |
|
1123 | if link_ is not None: | |
1124 | # make href around everything that isn't a href already |
|
1124 | # make href around everything that isn't a href already | |
1125 | s = linkify_others(s, link_) |
|
1125 | s = linkify_others(s, link_) | |
1126 | s = s.replace('\r\n', '<br/>').replace('\n', '<br/>') |
|
1126 | s = s.replace('\r\n', '<br/>').replace('\n', '<br/>') | |
1127 | # Turn HTML5 into more valid HTML4 as required by some mail readers. |
|
1127 | # Turn HTML5 into more valid HTML4 as required by some mail readers. | |
1128 | # (This is not done in one step in html_escape, because character codes like |
|
1128 | # (This is not done in one step in html_escape, because character codes like | |
1129 | # { risk to be seen as an issue reference due to the presence of '#'.) |
|
1129 | # { risk to be seen as an issue reference due to the presence of '#'.) | |
1130 | s = s.replace("'", "'") |
|
1130 | s = s.replace("'", "'") | |
1131 | return literal(s) |
|
1131 | return literal(s) | |
1132 |
|
1132 | |||
1133 |
|
1133 | |||
1134 | def linkify_others(t, l): |
|
1134 | def linkify_others(t, l): | |
1135 | """Add a default link to html with links. |
|
1135 | """Add a default link to html with links. | |
1136 | HTML doesn't allow nesting of links, so the outer link must be broken up |
|
1136 | HTML doesn't allow nesting of links, so the outer link must be broken up | |
1137 | in pieces and give space for other links. |
|
1137 | in pieces and give space for other links. | |
1138 | """ |
|
1138 | """ | |
1139 | urls = re.compile(r'(\<a.*?\<\/a\>)',) |
|
1139 | urls = re.compile(r'(\<a.*?\<\/a\>)',) | |
1140 | links = [] |
|
1140 | links = [] | |
1141 | for e in urls.split(t): |
|
1141 | for e in urls.split(t): | |
1142 | if e.strip() and not urls.match(e): |
|
1142 | if e.strip() and not urls.match(e): | |
1143 | links.append('<a class="message-link" href="%s">%s</a>' % (l, e)) |
|
1143 | links.append('<a class="message-link" href="%s">%s</a>' % (l, e)) | |
1144 | else: |
|
1144 | else: | |
1145 | links.append(e) |
|
1145 | links.append(e) | |
1146 |
|
1146 | |||
1147 | return ''.join(links) |
|
1147 | return ''.join(links) | |
1148 |
|
1148 | |||
1149 |
|
1149 | |||
1150 | # Global variable that will hold the actual urlify_issues function body. |
|
1150 | # Global variable that will hold the actual urlify_issues function body. | |
1151 | # Will be set on first use when the global configuration has been read. |
|
1151 | # Will be set on first use when the global configuration has been read. | |
1152 | _urlify_issues_f = None |
|
1152 | _urlify_issues_f = None | |
1153 |
|
1153 | |||
1154 |
|
1154 | |||
1155 | def urlify_issues(newtext, repo_name): |
|
1155 | def urlify_issues(newtext, repo_name): | |
1156 | """Urlify issue references according to .ini configuration""" |
|
1156 | """Urlify issue references according to .ini configuration""" | |
1157 | global _urlify_issues_f |
|
1157 | global _urlify_issues_f | |
1158 | if _urlify_issues_f is None: |
|
1158 | if _urlify_issues_f is None: | |
1159 | from kallithea import CONFIG |
|
1159 | from kallithea import CONFIG | |
1160 | from kallithea.model.db import URL_SEP |
|
1160 | from kallithea.model.db import URL_SEP | |
1161 | assert CONFIG['sqlalchemy.url'] # make sure config has been loaded |
|
1161 | assert CONFIG['sqlalchemy.url'] # make sure config has been loaded | |
1162 |
|
1162 | |||
1163 | # Build chain of urlify functions, starting with not doing any transformation |
|
1163 | # Build chain of urlify functions, starting with not doing any transformation | |
1164 | tmp_urlify_issues_f = lambda s: s |
|
1164 | tmp_urlify_issues_f = lambda s: s | |
1165 |
|
1165 | |||
1166 | issue_pat_re = re.compile(r'issue_pat(.*)') |
|
1166 | issue_pat_re = re.compile(r'issue_pat(.*)') | |
1167 | for k in CONFIG.keys(): |
|
1167 | for k in CONFIG.keys(): | |
1168 | # Find all issue_pat* settings that also have corresponding server_link and prefix configuration |
|
1168 | # Find all issue_pat* settings that also have corresponding server_link and prefix configuration | |
1169 | m = issue_pat_re.match(k) |
|
1169 | m = issue_pat_re.match(k) | |
1170 | if m is None: |
|
1170 | if m is None: | |
1171 | continue |
|
1171 | continue | |
1172 | suffix = m.group(1) |
|
1172 | suffix = m.group(1) | |
1173 | issue_pat = CONFIG.get(k) |
|
1173 | issue_pat = CONFIG.get(k) | |
1174 | issue_server_link = CONFIG.get('issue_server_link%s' % suffix) |
|
1174 | issue_server_link = CONFIG.get('issue_server_link%s' % suffix) | |
1175 | issue_sub = CONFIG.get('issue_sub%s' % suffix) |
|
1175 | issue_sub = CONFIG.get('issue_sub%s' % suffix) | |
1176 | if not issue_pat or not issue_server_link or issue_sub is None: # issue_sub can be empty but should be present |
|
1176 | if not issue_pat or not issue_server_link or issue_sub is None: # issue_sub can be empty but should be present | |
1177 | log.error('skipping incomplete issue pattern %r: %r -> %r %r', suffix, issue_pat, issue_server_link, issue_sub) |
|
1177 | log.error('skipping incomplete issue pattern %r: %r -> %r %r', suffix, issue_pat, issue_server_link, issue_sub) | |
1178 | continue |
|
1178 | continue | |
1179 |
|
1179 | |||
1180 | # Wrap tmp_urlify_issues_f with substitution of this pattern, while making sure all loop variables (and compiled regexpes) are bound |
|
1180 | # Wrap tmp_urlify_issues_f with substitution of this pattern, while making sure all loop variables (and compiled regexpes) are bound | |
1181 | try: |
|
1181 | try: | |
1182 | issue_re = re.compile(issue_pat) |
|
1182 | issue_re = re.compile(issue_pat) | |
1183 | except re.error as e: |
|
1183 | except re.error as e: | |
1184 | log.error('skipping invalid issue pattern %r: %r -> %r %r. Error: %s', suffix, issue_pat, issue_server_link, issue_sub, str(e)) |
|
1184 | log.error('skipping invalid issue pattern %r: %r -> %r %r. Error: %s', suffix, issue_pat, issue_server_link, issue_sub, str(e)) | |
1185 | continue |
|
1185 | continue | |
1186 |
|
1186 | |||
1187 | log.debug('issue pattern %r: %r -> %r %r', suffix, issue_pat, issue_server_link, issue_sub) |
|
1187 | log.debug('issue pattern %r: %r -> %r %r', suffix, issue_pat, issue_server_link, issue_sub) | |
1188 |
|
1188 | |||
1189 | def issues_replace(match_obj, |
|
1189 | def issues_replace(match_obj, | |
1190 | issue_server_link=issue_server_link, issue_sub=issue_sub): |
|
1190 | issue_server_link=issue_server_link, issue_sub=issue_sub): | |
1191 | try: |
|
1191 | try: | |
1192 | issue_url = match_obj.expand(issue_server_link) |
|
1192 | issue_url = match_obj.expand(issue_server_link) | |
1193 | except (IndexError, re.error) as e: |
|
1193 | except (IndexError, re.error) as e: | |
1194 | log.error('invalid issue_url setting %r -> %r %r. Error: %s', issue_pat, issue_server_link, issue_sub, str(e)) |
|
1194 | log.error('invalid issue_url setting %r -> %r %r. Error: %s', issue_pat, issue_server_link, issue_sub, str(e)) | |
1195 | issue_url = issue_server_link |
|
1195 | issue_url = issue_server_link | |
1196 | issue_url = issue_url.replace('{repo}', repo_name) |
|
1196 | issue_url = issue_url.replace('{repo}', repo_name) | |
1197 | issue_url = issue_url.replace('{repo_name}', repo_name.split(URL_SEP)[-1]) |
|
1197 | issue_url = issue_url.replace('{repo_name}', repo_name.split(URL_SEP)[-1]) | |
1198 | # if issue_sub is empty use the matched issue reference verbatim |
|
1198 | # if issue_sub is empty use the matched issue reference verbatim | |
1199 | if not issue_sub: |
|
1199 | if not issue_sub: | |
1200 | issue_text = match_obj.group() |
|
1200 | issue_text = match_obj.group() | |
1201 | else: |
|
1201 | else: | |
1202 | try: |
|
1202 | try: | |
1203 | issue_text = match_obj.expand(issue_sub) |
|
1203 | issue_text = match_obj.expand(issue_sub) | |
1204 | except (IndexError, re.error) as e: |
|
1204 | except (IndexError, re.error) as e: | |
1205 | log.error('invalid issue_sub setting %r -> %r %r. Error: %s', issue_pat, issue_server_link, issue_sub, str(e)) |
|
1205 | log.error('invalid issue_sub setting %r -> %r %r. Error: %s', issue_pat, issue_server_link, issue_sub, str(e)) | |
1206 | issue_text = match_obj.group() |
|
1206 | issue_text = match_obj.group() | |
1207 |
|
1207 | |||
1208 | return ( |
|
1208 | return ( | |
1209 | '<a class="issue-tracker-link" href="%(url)s">' |
|
1209 | '<a class="issue-tracker-link" href="%(url)s">' | |
1210 | '%(text)s' |
|
1210 | '%(text)s' | |
1211 | '</a>' |
|
1211 | '</a>' | |
1212 | ) % { |
|
1212 | ) % { | |
1213 | 'url': issue_url, |
|
1213 | 'url': issue_url, | |
1214 | 'text': issue_text, |
|
1214 | 'text': issue_text, | |
1215 | } |
|
1215 | } | |
1216 | tmp_urlify_issues_f = (lambda s, |
|
1216 | tmp_urlify_issues_f = (lambda s, | |
1217 | issue_re=issue_re, issues_replace=issues_replace, chain_f=tmp_urlify_issues_f: |
|
1217 | issue_re=issue_re, issues_replace=issues_replace, chain_f=tmp_urlify_issues_f: | |
1218 | issue_re.sub(issues_replace, chain_f(s))) |
|
1218 | issue_re.sub(issues_replace, chain_f(s))) | |
1219 |
|
1219 | |||
1220 | # Set tmp function globally - atomically |
|
1220 | # Set tmp function globally - atomically | |
1221 | _urlify_issues_f = tmp_urlify_issues_f |
|
1221 | _urlify_issues_f = tmp_urlify_issues_f | |
1222 |
|
1222 | |||
1223 | return _urlify_issues_f(newtext) |
|
1223 | return _urlify_issues_f(newtext) | |
1224 |
|
1224 | |||
1225 |
|
1225 | |||
1226 | def render_w_mentions(source, repo_name=None): |
|
1226 | def render_w_mentions(source, repo_name=None): | |
1227 | """ |
|
1227 | """ | |
1228 | Render plain text with revision hashes and issue references urlified |
|
1228 | Render plain text with revision hashes and issue references urlified | |
1229 | and with @mention highlighting. |
|
1229 | and with @mention highlighting. | |
1230 | """ |
|
1230 | """ | |
1231 | s = safe_unicode(source) |
|
1231 | s = safe_unicode(source) | |
1232 | s = urlify_text(s, repo_name=repo_name) |
|
1232 | s = urlify_text(s, repo_name=repo_name) | |
1233 | return literal('<div class="formatted-fixed">%s</div>' % s) |
|
1233 | return literal('<div class="formatted-fixed">%s</div>' % s) | |
1234 |
|
1234 | |||
1235 |
|
1235 | |||
1236 | def short_ref(ref_type, ref_name): |
|
1236 | def short_ref(ref_type, ref_name): | |
1237 | if ref_type == 'rev': |
|
1237 | if ref_type == 'rev': | |
1238 | return short_id(ref_name) |
|
1238 | return short_id(ref_name) | |
1239 | return ref_name |
|
1239 | return ref_name | |
1240 |
|
1240 | |||
1241 |
|
1241 | |||
1242 | def link_to_ref(repo_name, ref_type, ref_name, rev=None): |
|
1242 | def link_to_ref(repo_name, ref_type, ref_name, rev=None): | |
1243 | """ |
|
1243 | """ | |
1244 | Return full markup for a href to changeset_home for a changeset. |
|
1244 | Return full markup for a href to changeset_home for a changeset. | |
1245 | If ref_type is branch it will link to changelog. |
|
1245 | If ref_type is branch it will link to changelog. | |
1246 | ref_name is shortened if ref_type is 'rev'. |
|
1246 | ref_name is shortened if ref_type is 'rev'. | |
1247 | if rev is specified show it too, explicitly linking to that revision. |
|
1247 | if rev is specified show it too, explicitly linking to that revision. | |
1248 | """ |
|
1248 | """ | |
1249 | txt = short_ref(ref_type, ref_name) |
|
1249 | txt = short_ref(ref_type, ref_name) | |
1250 | if ref_type == 'branch': |
|
1250 | if ref_type == 'branch': | |
1251 | u = url('changelog_home', repo_name=repo_name, branch=ref_name) |
|
1251 | u = url('changelog_home', repo_name=repo_name, branch=ref_name) | |
1252 | else: |
|
1252 | else: | |
1253 | u = url('changeset_home', repo_name=repo_name, revision=ref_name) |
|
1253 | u = url('changeset_home', repo_name=repo_name, revision=ref_name) | |
1254 | l = link_to(repo_name + '#' + txt, u) |
|
1254 | l = link_to(repo_name + '#' + txt, u) | |
1255 | if rev and ref_type != 'rev': |
|
1255 | if rev and ref_type != 'rev': | |
1256 | l = literal('%s (%s)' % (l, link_to(short_id(rev), url('changeset_home', repo_name=repo_name, revision=rev)))) |
|
1256 | l = literal('%s (%s)' % (l, link_to(short_id(rev), url('changeset_home', repo_name=repo_name, revision=rev)))) | |
1257 | return l |
|
1257 | return l | |
1258 |
|
1258 | |||
1259 |
|
1259 | |||
1260 | def changeset_status(repo, revision): |
|
1260 | def changeset_status(repo, revision): | |
1261 | from kallithea.model.changeset_status import ChangesetStatusModel |
|
1261 | from kallithea.model.changeset_status import ChangesetStatusModel | |
1262 | return ChangesetStatusModel().get_status(repo, revision) |
|
1262 | return ChangesetStatusModel().get_status(repo, revision) | |
1263 |
|
1263 | |||
1264 |
|
1264 | |||
1265 | def changeset_status_lbl(changeset_status): |
|
1265 | def changeset_status_lbl(changeset_status): | |
1266 | from kallithea.model.db import ChangesetStatus |
|
1266 | from kallithea.model.db import ChangesetStatus | |
1267 | return ChangesetStatus.get_status_lbl(changeset_status) |
|
1267 | return ChangesetStatus.get_status_lbl(changeset_status) | |
1268 |
|
1268 | |||
1269 |
|
1269 | |||
1270 | def get_permission_name(key): |
|
1270 | def get_permission_name(key): | |
1271 | from kallithea.model.db import Permission |
|
1271 | from kallithea.model.db import Permission | |
1272 | return dict(Permission.PERMS).get(key) |
|
1272 | return dict(Permission.PERMS).get(key) | |
1273 |
|
1273 | |||
1274 |
|
1274 | |||
1275 | def journal_filter_help(): |
|
1275 | def journal_filter_help(): | |
1276 | return _(textwrap.dedent(''' |
|
1276 | return _(textwrap.dedent(''' | |
1277 | Example filter terms: |
|
1277 | Example filter terms: | |
1278 | repository:vcs |
|
1278 | repository:vcs | |
1279 | username:developer |
|
1279 | username:developer | |
1280 | action:*push* |
|
1280 | action:*push* | |
1281 | ip:127.0.0.1 |
|
1281 | ip:127.0.0.1 | |
1282 | date:20120101 |
|
1282 | date:20120101 | |
1283 | date:[20120101100000 TO 20120102] |
|
1283 | date:[20120101100000 TO 20120102] | |
1284 |
|
1284 | |||
1285 | Generate wildcards using '*' character: |
|
1285 | Generate wildcards using '*' character: | |
1286 | "repository:vcs*" - search everything starting with 'vcs' |
|
1286 | "repository:vcs*" - search everything starting with 'vcs' | |
1287 | "repository:*vcs*" - search for repository containing 'vcs' |
|
1287 | "repository:*vcs*" - search for repository containing 'vcs' | |
1288 |
|
1288 | |||
1289 | Optional AND / OR operators in queries |
|
1289 | Optional AND / OR operators in queries | |
1290 | "repository:vcs OR repository:test" |
|
1290 | "repository:vcs OR repository:test" | |
1291 | "username:test AND repository:test*" |
|
1291 | "username:test AND repository:test*" | |
1292 | ''')) |
|
1292 | ''')) | |
1293 |
|
1293 | |||
1294 |
|
1294 | |||
1295 | def not_mapped_error(repo_name): |
|
1295 | def not_mapped_error(repo_name): | |
1296 | flash(_('%s repository is not mapped to db perhaps' |
|
1296 | flash(_('%s repository is not mapped to db perhaps' | |
1297 | ' it was created or renamed from the filesystem' |
|
1297 | ' it was created or renamed from the filesystem' | |
1298 | ' please run the application again' |
|
1298 | ' please run the application again' | |
1299 | ' in order to rescan repositories') % repo_name, category='error') |
|
1299 | ' in order to rescan repositories') % repo_name, category='error') | |
1300 |
|
1300 | |||
1301 |
|
1301 | |||
1302 | def ip_range(ip_addr): |
|
1302 | def ip_range(ip_addr): | |
1303 | from kallithea.model.db import UserIpMap |
|
1303 | from kallithea.model.db import UserIpMap | |
1304 | s, e = UserIpMap._get_ip_range(ip_addr) |
|
1304 | s, e = UserIpMap._get_ip_range(ip_addr) | |
1305 | return '%s - %s' % (s, e) |
|
1305 | return '%s - %s' % (s, e) | |
1306 |
|
1306 | |||
1307 |
|
1307 | |||
1308 | session_csrf_secret_name = "_session_csrf_secret_token" |
|
1308 | session_csrf_secret_name = "_session_csrf_secret_token" | |
1309 |
|
1309 | |||
1310 | def session_csrf_secret_token(): |
|
1310 | def session_csrf_secret_token(): | |
1311 | """Return (and create) the current session's CSRF protection token.""" |
|
1311 | """Return (and create) the current session's CSRF protection token.""" | |
1312 | from tg import session |
|
1312 | from tg import session | |
1313 | if not session_csrf_secret_name in session: |
|
1313 | if not session_csrf_secret_name in session: | |
1314 | session[session_csrf_secret_name] = str(random.getrandbits(128)) |
|
1314 | session[session_csrf_secret_name] = str(random.getrandbits(128)) | |
1315 | session.save() |
|
1315 | session.save() | |
1316 | return session[session_csrf_secret_name] |
|
1316 | return session[session_csrf_secret_name] | |
1317 |
|
1317 | |||
1318 | def form(url, method="post", **attrs): |
|
1318 | def form(url, method="post", **attrs): | |
1319 | """Like webhelpers.html.tags.form , but automatically adding |
|
1319 | """Like webhelpers.html.tags.form , but automatically adding | |
1320 | session_csrf_secret_token for POST. The secret is thus never leaked in GET |
|
1320 | session_csrf_secret_token for POST. The secret is thus never leaked in GET | |
1321 | URLs. |
|
1321 | URLs. | |
1322 | """ |
|
1322 | """ | |
1323 | form = insecure_form(url, method, **attrs) |
|
1323 | form = insecure_form(url, method, **attrs) | |
1324 | if method.lower() == 'get': |
|
1324 | if method.lower() == 'get': | |
1325 | return form |
|
1325 | return form | |
1326 | return form + HTML.div(hidden(session_csrf_secret_name, session_csrf_secret_token()), style="display: none;") |
|
1326 | return form + HTML.div(hidden(session_csrf_secret_name, session_csrf_secret_token()), style="display: none;") |
@@ -1,459 +1,459 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | # This program is free software: you can redistribute it and/or modify |
|
2 | # This program is free software: you can redistribute it and/or modify | |
3 | # it under the terms of the GNU General Public License as published by |
|
3 | # it under the terms of the GNU General Public License as published by | |
4 | # the Free Software Foundation, either version 3 of the License, or |
|
4 | # the Free Software Foundation, either version 3 of the License, or | |
5 | # (at your option) any later version. |
|
5 | # (at your option) any later version. | |
6 | # |
|
6 | # | |
7 | # This program is distributed in the hope that it will be useful, |
|
7 | # This program is distributed in the hope that it will be useful, | |
8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
10 | # GNU General Public License for more details. |
|
10 | # GNU General Public License for more details. | |
11 | # |
|
11 | # | |
12 | # You should have received a copy of the GNU General Public License |
|
12 | # You should have received a copy of the GNU General Public License | |
13 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
13 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
14 | """ |
|
14 | """ | |
15 | kallithea.lib.indexers.daemon |
|
15 | kallithea.lib.indexers.daemon | |
16 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
16 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
17 |
|
17 | |||
18 | A daemon will read from task table and run tasks |
|
18 | A daemon will read from task table and run tasks | |
19 |
|
19 | |||
20 | This file was forked by the Kallithea project in July 2014. |
|
20 | This file was forked by the Kallithea project in July 2014. | |
21 | Original author and date, and relevant copyright and licensing information is below: |
|
21 | Original author and date, and relevant copyright and licensing information is below: | |
22 | :created_on: Jan 26, 2010 |
|
22 | :created_on: Jan 26, 2010 | |
23 | :author: marcink |
|
23 | :author: marcink | |
24 | :copyright: (c) 2013 RhodeCode GmbH, and others. |
|
24 | :copyright: (c) 2013 RhodeCode GmbH, and others. | |
25 | :license: GPLv3, see LICENSE.md for more details. |
|
25 | :license: GPLv3, see LICENSE.md for more details. | |
26 | """ |
|
26 | """ | |
27 |
|
27 | |||
28 |
|
28 | |||
29 | import logging |
|
29 | import logging | |
30 | import os |
|
30 | import os | |
31 | import sys |
|
31 | import sys | |
32 | import traceback |
|
32 | import traceback | |
33 | from os.path import dirname |
|
33 | from os.path import dirname | |
34 | from shutil import rmtree |
|
34 | from shutil import rmtree | |
35 | from time import mktime |
|
35 | from time import mktime | |
36 |
|
36 | |||
37 | from whoosh.index import create_in, exists_in, open_dir |
|
37 | from whoosh.index import create_in, exists_in, open_dir | |
38 | from whoosh.qparser import QueryParser |
|
38 | from whoosh.qparser import QueryParser | |
39 |
|
39 | |||
40 | from kallithea.config.conf import INDEX_EXTENSIONS, INDEX_FILENAMES |
|
40 | from kallithea.config.conf import INDEX_EXTENSIONS, INDEX_FILENAMES | |
41 | from kallithea.lib.indexers import CHGSET_IDX_NAME, CHGSETS_SCHEMA, IDX_NAME, SCHEMA |
|
41 | from kallithea.lib.indexers import CHGSET_IDX_NAME, CHGSETS_SCHEMA, IDX_NAME, SCHEMA | |
42 | from kallithea.lib.utils2 import safe_str, safe_unicode |
|
42 | from kallithea.lib.utils2 import safe_str, safe_unicode | |
43 | from kallithea.lib.vcs.exceptions import ChangesetError, NodeDoesNotExistError, RepositoryError |
|
43 | from kallithea.lib.vcs.exceptions import ChangesetError, NodeDoesNotExistError, RepositoryError | |
44 | from kallithea.model.db import Repository |
|
44 | from kallithea.model.db import Repository | |
45 | from kallithea.model.scm import ScmModel |
|
45 | from kallithea.model.scm import ScmModel | |
46 |
|
46 | |||
47 |
|
47 | |||
48 | # Add location of top level folder to sys.path |
|
48 | # Add location of top level folder to sys.path | |
49 | project_path = dirname(dirname(dirname(dirname(os.path.realpath(__file__))))) |
|
49 | project_path = dirname(dirname(dirname(dirname(os.path.realpath(__file__))))) | |
50 | sys.path.append(project_path) |
|
50 | sys.path.append(project_path) | |
51 |
|
51 | |||
52 |
|
52 | |||
53 |
|
53 | |||
54 |
|
54 | |||
55 | log = logging.getLogger('whoosh_indexer') |
|
55 | log = logging.getLogger('whoosh_indexer') | |
56 |
|
56 | |||
57 |
|
57 | |||
58 | class WhooshIndexingDaemon(object): |
|
58 | class WhooshIndexingDaemon(object): | |
59 | """ |
|
59 | """ | |
60 | Daemon for atomic indexing jobs |
|
60 | Daemon for atomic indexing jobs | |
61 | """ |
|
61 | """ | |
62 |
|
62 | |||
63 | def __init__(self, indexname=IDX_NAME, index_location=None, |
|
63 | def __init__(self, indexname=IDX_NAME, index_location=None, | |
64 | repo_location=None, repo_list=None, |
|
64 | repo_location=None, repo_list=None, | |
65 | repo_update_list=None): |
|
65 | repo_update_list=None): | |
66 | self.indexname = indexname |
|
66 | self.indexname = indexname | |
67 |
|
67 | |||
68 | self.index_location = index_location |
|
68 | self.index_location = index_location | |
69 | if not index_location: |
|
69 | if not index_location: | |
70 | raise Exception('You have to provide index location') |
|
70 | raise Exception('You have to provide index location') | |
71 |
|
71 | |||
72 | self.repo_location = repo_location |
|
72 | self.repo_location = repo_location | |
73 | if not repo_location: |
|
73 | if not repo_location: | |
74 | raise Exception('You have to provide repositories location') |
|
74 | raise Exception('You have to provide repositories location') | |
75 |
|
75 | |||
76 | self.repo_paths = ScmModel().repo_scan(self.repo_location) |
|
76 | self.repo_paths = ScmModel().repo_scan(self.repo_location) | |
77 |
|
77 | |||
78 | # filter repo list |
|
78 | # filter repo list | |
79 | if repo_list: |
|
79 | if repo_list: | |
80 | # Fix non-ascii repo names to unicode |
|
80 | # Fix non-ascii repo names to unicode | |
81 |
repo_list = |
|
81 | repo_list = set(safe_unicode(repo_name) for repo_name in repo_list) | |
82 | self.filtered_repo_paths = {} |
|
82 | self.filtered_repo_paths = {} | |
83 | for repo_name, repo in self.repo_paths.items(): |
|
83 | for repo_name, repo in self.repo_paths.items(): | |
84 | if repo_name in repo_list: |
|
84 | if repo_name in repo_list: | |
85 | self.filtered_repo_paths[repo_name] = repo |
|
85 | self.filtered_repo_paths[repo_name] = repo | |
86 |
|
86 | |||
87 | self.repo_paths = self.filtered_repo_paths |
|
87 | self.repo_paths = self.filtered_repo_paths | |
88 |
|
88 | |||
89 | # filter update repo list |
|
89 | # filter update repo list | |
90 | self.filtered_repo_update_paths = {} |
|
90 | self.filtered_repo_update_paths = {} | |
91 | if repo_update_list: |
|
91 | if repo_update_list: | |
92 | self.filtered_repo_update_paths = {} |
|
92 | self.filtered_repo_update_paths = {} | |
93 | for repo_name, repo in self.repo_paths.items(): |
|
93 | for repo_name, repo in self.repo_paths.items(): | |
94 | if repo_name in repo_update_list: |
|
94 | if repo_name in repo_update_list: | |
95 | self.filtered_repo_update_paths[repo_name] = repo |
|
95 | self.filtered_repo_update_paths[repo_name] = repo | |
96 | self.repo_paths = self.filtered_repo_update_paths |
|
96 | self.repo_paths = self.filtered_repo_update_paths | |
97 |
|
97 | |||
98 | self.initial = True |
|
98 | self.initial = True | |
99 | if not os.path.isdir(self.index_location): |
|
99 | if not os.path.isdir(self.index_location): | |
100 | os.makedirs(self.index_location) |
|
100 | os.makedirs(self.index_location) | |
101 | log.info('Cannot run incremental index since it does not ' |
|
101 | log.info('Cannot run incremental index since it does not ' | |
102 | 'yet exist - running full build') |
|
102 | 'yet exist - running full build') | |
103 | elif not exists_in(self.index_location, IDX_NAME): |
|
103 | elif not exists_in(self.index_location, IDX_NAME): | |
104 | log.info('Running full index build, as the file content ' |
|
104 | log.info('Running full index build, as the file content ' | |
105 | 'index does not exist') |
|
105 | 'index does not exist') | |
106 | elif not exists_in(self.index_location, CHGSET_IDX_NAME): |
|
106 | elif not exists_in(self.index_location, CHGSET_IDX_NAME): | |
107 | log.info('Running full index build, as the changeset ' |
|
107 | log.info('Running full index build, as the changeset ' | |
108 | 'index does not exist') |
|
108 | 'index does not exist') | |
109 | else: |
|
109 | else: | |
110 | self.initial = False |
|
110 | self.initial = False | |
111 |
|
111 | |||
112 | def _get_index_revision(self, repo): |
|
112 | def _get_index_revision(self, repo): | |
113 | db_repo = Repository.get_by_repo_name(repo.name_unicode) |
|
113 | db_repo = Repository.get_by_repo_name(repo.name_unicode) | |
114 | landing_rev = 'tip' |
|
114 | landing_rev = 'tip' | |
115 | if db_repo: |
|
115 | if db_repo: | |
116 | _rev_type, _rev = db_repo.landing_rev |
|
116 | _rev_type, _rev = db_repo.landing_rev | |
117 | landing_rev = _rev |
|
117 | landing_rev = _rev | |
118 | return landing_rev |
|
118 | return landing_rev | |
119 |
|
119 | |||
120 | def _get_index_changeset(self, repo, index_rev=None): |
|
120 | def _get_index_changeset(self, repo, index_rev=None): | |
121 | if not index_rev: |
|
121 | if not index_rev: | |
122 | index_rev = self._get_index_revision(repo) |
|
122 | index_rev = self._get_index_revision(repo) | |
123 | cs = repo.get_changeset(index_rev) |
|
123 | cs = repo.get_changeset(index_rev) | |
124 | return cs |
|
124 | return cs | |
125 |
|
125 | |||
126 | def get_paths(self, repo): |
|
126 | def get_paths(self, repo): | |
127 | """ |
|
127 | """ | |
128 | recursive walk in root dir and return a set of all path in that dir |
|
128 | recursive walk in root dir and return a set of all path in that dir | |
129 | based on repository walk function |
|
129 | based on repository walk function | |
130 | """ |
|
130 | """ | |
131 | index_paths_ = set() |
|
131 | index_paths_ = set() | |
132 | try: |
|
132 | try: | |
133 | cs = self._get_index_changeset(repo) |
|
133 | cs = self._get_index_changeset(repo) | |
134 | for _topnode, _dirs, files in cs.walk('/'): |
|
134 | for _topnode, _dirs, files in cs.walk('/'): | |
135 | for f in files: |
|
135 | for f in files: | |
136 | index_paths_.add(os.path.join(safe_str(repo.path), safe_str(f.path))) |
|
136 | index_paths_.add(os.path.join(safe_str(repo.path), safe_str(f.path))) | |
137 |
|
137 | |||
138 | except RepositoryError: |
|
138 | except RepositoryError: | |
139 | log.debug(traceback.format_exc()) |
|
139 | log.debug(traceback.format_exc()) | |
140 | pass |
|
140 | pass | |
141 | return index_paths_ |
|
141 | return index_paths_ | |
142 |
|
142 | |||
143 | def get_node(self, repo, path, index_rev=None): |
|
143 | def get_node(self, repo, path, index_rev=None): | |
144 | """ |
|
144 | """ | |
145 | gets a filenode based on given full path. It operates on string for |
|
145 | gets a filenode based on given full path. It operates on string for | |
146 | hg git compatibility. |
|
146 | hg git compatibility. | |
147 |
|
147 | |||
148 | :param repo: scm repo instance |
|
148 | :param repo: scm repo instance | |
149 | :param path: full path including root location |
|
149 | :param path: full path including root location | |
150 | :return: FileNode |
|
150 | :return: FileNode | |
151 | """ |
|
151 | """ | |
152 | # FIXME: paths should be normalized ... or even better: don't include repo.path |
|
152 | # FIXME: paths should be normalized ... or even better: don't include repo.path | |
153 | path = safe_str(path) |
|
153 | path = safe_str(path) | |
154 | repo_path = safe_str(repo.path) |
|
154 | repo_path = safe_str(repo.path) | |
155 | assert path.startswith(repo_path) |
|
155 | assert path.startswith(repo_path) | |
156 | assert path[len(repo_path)] in (os.path.sep, os.path.altsep) |
|
156 | assert path[len(repo_path)] in (os.path.sep, os.path.altsep) | |
157 | node_path = path[len(repo_path) + 1:] |
|
157 | node_path = path[len(repo_path) + 1:] | |
158 | cs = self._get_index_changeset(repo, index_rev=index_rev) |
|
158 | cs = self._get_index_changeset(repo, index_rev=index_rev) | |
159 | node = cs.get_node(node_path) |
|
159 | node = cs.get_node(node_path) | |
160 | return node |
|
160 | return node | |
161 |
|
161 | |||
162 | def is_indexable_node(self, node): |
|
162 | def is_indexable_node(self, node): | |
163 | """ |
|
163 | """ | |
164 | Just index the content of chosen files, skipping binary files |
|
164 | Just index the content of chosen files, skipping binary files | |
165 | """ |
|
165 | """ | |
166 | return (node.extension in INDEX_EXTENSIONS or node.name in INDEX_FILENAMES) and \ |
|
166 | return (node.extension in INDEX_EXTENSIONS or node.name in INDEX_FILENAMES) and \ | |
167 | not node.is_binary |
|
167 | not node.is_binary | |
168 |
|
168 | |||
169 | def get_node_mtime(self, node): |
|
169 | def get_node_mtime(self, node): | |
170 | return mktime(node.last_changeset.date.timetuple()) |
|
170 | return mktime(node.last_changeset.date.timetuple()) | |
171 |
|
171 | |||
172 | def add_doc(self, writer, path, repo, repo_name, index_rev=None): |
|
172 | def add_doc(self, writer, path, repo, repo_name, index_rev=None): | |
173 | """ |
|
173 | """ | |
174 | Adding doc to writer this function itself fetches data from |
|
174 | Adding doc to writer this function itself fetches data from | |
175 | the instance of vcs backend |
|
175 | the instance of vcs backend | |
176 | """ |
|
176 | """ | |
177 | try: |
|
177 | try: | |
178 | node = self.get_node(repo, path, index_rev) |
|
178 | node = self.get_node(repo, path, index_rev) | |
179 | except (ChangesetError, NodeDoesNotExistError): |
|
179 | except (ChangesetError, NodeDoesNotExistError): | |
180 | log.debug(" >> %s - not found in %s %s", path, repo, index_rev) |
|
180 | log.debug(" >> %s - not found in %s %s", path, repo, index_rev) | |
181 | return 0, 0 |
|
181 | return 0, 0 | |
182 |
|
182 | |||
183 | indexed = indexed_w_content = 0 |
|
183 | indexed = indexed_w_content = 0 | |
184 | if self.is_indexable_node(node): |
|
184 | if self.is_indexable_node(node): | |
185 | u_content = node.content |
|
185 | u_content = node.content | |
186 | if not isinstance(u_content, unicode): |
|
186 | if not isinstance(u_content, unicode): | |
187 | log.warning(' >> %s - no text content', path) |
|
187 | log.warning(' >> %s - no text content', path) | |
188 | u_content = u'' |
|
188 | u_content = u'' | |
189 | else: |
|
189 | else: | |
190 | log.debug(' >> %s', path) |
|
190 | log.debug(' >> %s', path) | |
191 | indexed_w_content += 1 |
|
191 | indexed_w_content += 1 | |
192 |
|
192 | |||
193 | else: |
|
193 | else: | |
194 | log.debug(' >> %s - not indexable', path) |
|
194 | log.debug(' >> %s - not indexable', path) | |
195 | # just index file name without it's content |
|
195 | # just index file name without it's content | |
196 | u_content = u'' |
|
196 | u_content = u'' | |
197 | indexed += 1 |
|
197 | indexed += 1 | |
198 |
|
198 | |||
199 | p = safe_unicode(path) |
|
199 | p = safe_unicode(path) | |
200 | writer.add_document( |
|
200 | writer.add_document( | |
201 | fileid=p, |
|
201 | fileid=p, | |
202 | owner=unicode(repo.contact), |
|
202 | owner=unicode(repo.contact), | |
203 | repository_rawname=safe_unicode(repo_name), |
|
203 | repository_rawname=safe_unicode(repo_name), | |
204 | repository=safe_unicode(repo_name), |
|
204 | repository=safe_unicode(repo_name), | |
205 | path=p, |
|
205 | path=p, | |
206 | content=u_content, |
|
206 | content=u_content, | |
207 | modtime=self.get_node_mtime(node), |
|
207 | modtime=self.get_node_mtime(node), | |
208 | extension=node.extension |
|
208 | extension=node.extension | |
209 | ) |
|
209 | ) | |
210 | return indexed, indexed_w_content |
|
210 | return indexed, indexed_w_content | |
211 |
|
211 | |||
212 | def index_changesets(self, writer, repo_name, repo, start_rev=None): |
|
212 | def index_changesets(self, writer, repo_name, repo, start_rev=None): | |
213 | """ |
|
213 | """ | |
214 | Add all changeset in the vcs repo starting at start_rev |
|
214 | Add all changeset in the vcs repo starting at start_rev | |
215 | to the index writer |
|
215 | to the index writer | |
216 |
|
216 | |||
217 | :param writer: the whoosh index writer to add to |
|
217 | :param writer: the whoosh index writer to add to | |
218 | :param repo_name: name of the repository from whence the |
|
218 | :param repo_name: name of the repository from whence the | |
219 | changeset originates including the repository group |
|
219 | changeset originates including the repository group | |
220 | :param repo: the vcs repository instance to index changesets for, |
|
220 | :param repo: the vcs repository instance to index changesets for, | |
221 | the presumption is the repo has changesets to index |
|
221 | the presumption is the repo has changesets to index | |
222 | :param start_rev=None: the full sha id to start indexing from |
|
222 | :param start_rev=None: the full sha id to start indexing from | |
223 | if start_rev is None then index from the first changeset in |
|
223 | if start_rev is None then index from the first changeset in | |
224 | the repo |
|
224 | the repo | |
225 | """ |
|
225 | """ | |
226 |
|
226 | |||
227 | if start_rev is None: |
|
227 | if start_rev is None: | |
228 | start_rev = repo[0].raw_id |
|
228 | start_rev = repo[0].raw_id | |
229 |
|
229 | |||
230 | log.debug('Indexing changesets in %s, starting at rev %s', |
|
230 | log.debug('Indexing changesets in %s, starting at rev %s', | |
231 | repo_name, start_rev) |
|
231 | repo_name, start_rev) | |
232 |
|
232 | |||
233 | indexed = 0 |
|
233 | indexed = 0 | |
234 | cs_iter = repo.get_changesets(start=start_rev) |
|
234 | cs_iter = repo.get_changesets(start=start_rev) | |
235 | total = len(cs_iter) |
|
235 | total = len(cs_iter) | |
236 | for cs in cs_iter: |
|
236 | for cs in cs_iter: | |
237 | indexed += 1 |
|
237 | indexed += 1 | |
238 | log.debug(' >> %s %s/%s', cs, indexed, total) |
|
238 | log.debug(' >> %s %s/%s', cs, indexed, total) | |
239 | writer.add_document( |
|
239 | writer.add_document( | |
240 | raw_id=unicode(cs.raw_id), |
|
240 | raw_id=unicode(cs.raw_id), | |
241 | owner=unicode(repo.contact), |
|
241 | owner=unicode(repo.contact), | |
242 | date=cs._timestamp, |
|
242 | date=cs._timestamp, | |
243 | repository_rawname=safe_unicode(repo_name), |
|
243 | repository_rawname=safe_unicode(repo_name), | |
244 | repository=safe_unicode(repo_name), |
|
244 | repository=safe_unicode(repo_name), | |
245 | author=cs.author, |
|
245 | author=cs.author, | |
246 | message=cs.message, |
|
246 | message=cs.message, | |
247 | last=cs.last, |
|
247 | last=cs.last, | |
248 | added=u' '.join([safe_unicode(node.path) for node in cs.added]).lower(), |
|
248 | added=u' '.join([safe_unicode(node.path) for node in cs.added]).lower(), | |
249 | removed=u' '.join([safe_unicode(node.path) for node in cs.removed]).lower(), |
|
249 | removed=u' '.join([safe_unicode(node.path) for node in cs.removed]).lower(), | |
250 | changed=u' '.join([safe_unicode(node.path) for node in cs.changed]).lower(), |
|
250 | changed=u' '.join([safe_unicode(node.path) for node in cs.changed]).lower(), | |
251 | parents=u' '.join([cs.raw_id for cs in cs.parents]), |
|
251 | parents=u' '.join([cs.raw_id for cs in cs.parents]), | |
252 | ) |
|
252 | ) | |
253 |
|
253 | |||
254 | return indexed |
|
254 | return indexed | |
255 |
|
255 | |||
256 | def index_files(self, file_idx_writer, repo_name, repo): |
|
256 | def index_files(self, file_idx_writer, repo_name, repo): | |
257 | """ |
|
257 | """ | |
258 | Index files for given repo_name |
|
258 | Index files for given repo_name | |
259 |
|
259 | |||
260 | :param file_idx_writer: the whoosh index writer to add to |
|
260 | :param file_idx_writer: the whoosh index writer to add to | |
261 | :param repo_name: name of the repository we're indexing |
|
261 | :param repo_name: name of the repository we're indexing | |
262 | :param repo: instance of vcs repo |
|
262 | :param repo: instance of vcs repo | |
263 | """ |
|
263 | """ | |
264 | i_cnt = iwc_cnt = 0 |
|
264 | i_cnt = iwc_cnt = 0 | |
265 | log.debug('Building file index for %s @revision:%s', repo_name, |
|
265 | log.debug('Building file index for %s @revision:%s', repo_name, | |
266 | self._get_index_revision(repo)) |
|
266 | self._get_index_revision(repo)) | |
267 | index_rev = self._get_index_revision(repo) |
|
267 | index_rev = self._get_index_revision(repo) | |
268 | for idx_path in self.get_paths(repo): |
|
268 | for idx_path in self.get_paths(repo): | |
269 | i, iwc = self.add_doc(file_idx_writer, idx_path, repo, repo_name, index_rev) |
|
269 | i, iwc = self.add_doc(file_idx_writer, idx_path, repo, repo_name, index_rev) | |
270 | i_cnt += i |
|
270 | i_cnt += i | |
271 | iwc_cnt += iwc |
|
271 | iwc_cnt += iwc | |
272 |
|
272 | |||
273 | log.debug('added %s files %s with content for repo %s', |
|
273 | log.debug('added %s files %s with content for repo %s', | |
274 | i_cnt + iwc_cnt, iwc_cnt, repo.path) |
|
274 | i_cnt + iwc_cnt, iwc_cnt, repo.path) | |
275 | return i_cnt, iwc_cnt |
|
275 | return i_cnt, iwc_cnt | |
276 |
|
276 | |||
277 | def update_changeset_index(self): |
|
277 | def update_changeset_index(self): | |
278 | idx = open_dir(self.index_location, indexname=CHGSET_IDX_NAME) |
|
278 | idx = open_dir(self.index_location, indexname=CHGSET_IDX_NAME) | |
279 |
|
279 | |||
280 | with idx.searcher() as searcher: |
|
280 | with idx.searcher() as searcher: | |
281 | writer = idx.writer() |
|
281 | writer = idx.writer() | |
282 | writer_is_dirty = False |
|
282 | writer_is_dirty = False | |
283 | try: |
|
283 | try: | |
284 | indexed_total = 0 |
|
284 | indexed_total = 0 | |
285 | repo_name = None |
|
285 | repo_name = None | |
286 | for repo_name, repo in sorted(self.repo_paths.items()): |
|
286 | for repo_name, repo in sorted(self.repo_paths.items()): | |
287 | log.debug('Updating changeset index for repo %s', repo_name) |
|
287 | log.debug('Updating changeset index for repo %s', repo_name) | |
288 | # skip indexing if there aren't any revs in the repo |
|
288 | # skip indexing if there aren't any revs in the repo | |
289 | num_of_revs = len(repo) |
|
289 | num_of_revs = len(repo) | |
290 | if num_of_revs < 1: |
|
290 | if num_of_revs < 1: | |
291 | continue |
|
291 | continue | |
292 |
|
292 | |||
293 | qp = QueryParser('repository', schema=CHGSETS_SCHEMA) |
|
293 | qp = QueryParser('repository', schema=CHGSETS_SCHEMA) | |
294 | q = qp.parse(u"last:t AND %s" % repo_name) |
|
294 | q = qp.parse(u"last:t AND %s" % repo_name) | |
295 |
|
295 | |||
296 | results = searcher.search(q) |
|
296 | results = searcher.search(q) | |
297 |
|
297 | |||
298 | # default to scanning the entire repo |
|
298 | # default to scanning the entire repo | |
299 | last_rev = 0 |
|
299 | last_rev = 0 | |
300 | start_id = None |
|
300 | start_id = None | |
301 |
|
301 | |||
302 | if len(results) > 0: |
|
302 | if len(results) > 0: | |
303 | # assuming that there is only one result, if not this |
|
303 | # assuming that there is only one result, if not this | |
304 | # may require a full re-index. |
|
304 | # may require a full re-index. | |
305 | start_id = results[0]['raw_id'] |
|
305 | start_id = results[0]['raw_id'] | |
306 | last_rev = repo.get_changeset(revision=start_id).revision |
|
306 | last_rev = repo.get_changeset(revision=start_id).revision | |
307 |
|
307 | |||
308 | # there are new changesets to index or a new repo to index |
|
308 | # there are new changesets to index or a new repo to index | |
309 | if last_rev == 0 or num_of_revs > last_rev + 1: |
|
309 | if last_rev == 0 or num_of_revs > last_rev + 1: | |
310 | # delete the docs in the index for the previous |
|
310 | # delete the docs in the index for the previous | |
311 | # last changeset(s) |
|
311 | # last changeset(s) | |
312 | for hit in results: |
|
312 | for hit in results: | |
313 | q = qp.parse(u"last:t AND %s AND raw_id:%s" % |
|
313 | q = qp.parse(u"last:t AND %s AND raw_id:%s" % | |
314 | (repo_name, hit['raw_id'])) |
|
314 | (repo_name, hit['raw_id'])) | |
315 | writer.delete_by_query(q) |
|
315 | writer.delete_by_query(q) | |
316 |
|
316 | |||
317 | # index from the previous last changeset + all new ones |
|
317 | # index from the previous last changeset + all new ones | |
318 | indexed_total += self.index_changesets(writer, |
|
318 | indexed_total += self.index_changesets(writer, | |
319 | repo_name, repo, start_id) |
|
319 | repo_name, repo, start_id) | |
320 | writer_is_dirty = True |
|
320 | writer_is_dirty = True | |
321 | log.debug('indexed %s changesets for repo %s', |
|
321 | log.debug('indexed %s changesets for repo %s', | |
322 | indexed_total, repo_name |
|
322 | indexed_total, repo_name | |
323 | ) |
|
323 | ) | |
324 | finally: |
|
324 | finally: | |
325 | if writer_is_dirty: |
|
325 | if writer_is_dirty: | |
326 | log.debug('>> COMMITING CHANGES TO CHANGESET INDEX<<') |
|
326 | log.debug('>> COMMITING CHANGES TO CHANGESET INDEX<<') | |
327 | writer.commit(merge=True) |
|
327 | writer.commit(merge=True) | |
328 | log.debug('>>> FINISHED REBUILDING CHANGESET INDEX <<<') |
|
328 | log.debug('>>> FINISHED REBUILDING CHANGESET INDEX <<<') | |
329 | else: |
|
329 | else: | |
330 | log.debug('>> NOTHING TO COMMIT TO CHANGESET INDEX<<') |
|
330 | log.debug('>> NOTHING TO COMMIT TO CHANGESET INDEX<<') | |
331 |
|
331 | |||
332 | def update_file_index(self): |
|
332 | def update_file_index(self): | |
333 | log.debug((u'STARTING INCREMENTAL INDEXING UPDATE FOR EXTENSIONS %s ' |
|
333 | log.debug((u'STARTING INCREMENTAL INDEXING UPDATE FOR EXTENSIONS %s ' | |
334 | 'AND REPOS %s') % (INDEX_EXTENSIONS, self.repo_paths.keys())) |
|
334 | 'AND REPOS %s') % (INDEX_EXTENSIONS, self.repo_paths.keys())) | |
335 |
|
335 | |||
336 | idx = open_dir(self.index_location, indexname=self.indexname) |
|
336 | idx = open_dir(self.index_location, indexname=self.indexname) | |
337 | # The set of all paths in the index |
|
337 | # The set of all paths in the index | |
338 | indexed_paths = set() |
|
338 | indexed_paths = set() | |
339 | # The set of all paths we need to re-index |
|
339 | # The set of all paths we need to re-index | |
340 | to_index = set() |
|
340 | to_index = set() | |
341 |
|
341 | |||
342 | writer = idx.writer() |
|
342 | writer = idx.writer() | |
343 | writer_is_dirty = False |
|
343 | writer_is_dirty = False | |
344 | try: |
|
344 | try: | |
345 | with idx.reader() as reader: |
|
345 | with idx.reader() as reader: | |
346 |
|
346 | |||
347 | # Loop over the stored fields in the index |
|
347 | # Loop over the stored fields in the index | |
348 | for fields in reader.all_stored_fields(): |
|
348 | for fields in reader.all_stored_fields(): | |
349 | indexed_path = fields['path'] |
|
349 | indexed_path = fields['path'] | |
350 | indexed_repo_path = fields['repository'] |
|
350 | indexed_repo_path = fields['repository'] | |
351 | indexed_paths.add(indexed_path) |
|
351 | indexed_paths.add(indexed_path) | |
352 |
|
352 | |||
353 | if indexed_repo_path not in self.filtered_repo_update_paths: |
|
353 | if indexed_repo_path not in self.filtered_repo_update_paths: | |
354 | continue |
|
354 | continue | |
355 |
|
355 | |||
356 | repo = self.repo_paths[indexed_repo_path] |
|
356 | repo = self.repo_paths[indexed_repo_path] | |
357 |
|
357 | |||
358 | try: |
|
358 | try: | |
359 | node = self.get_node(repo, indexed_path) |
|
359 | node = self.get_node(repo, indexed_path) | |
360 | # Check if this file was changed since it was indexed |
|
360 | # Check if this file was changed since it was indexed | |
361 | indexed_time = fields['modtime'] |
|
361 | indexed_time = fields['modtime'] | |
362 | mtime = self.get_node_mtime(node) |
|
362 | mtime = self.get_node_mtime(node) | |
363 | if mtime > indexed_time: |
|
363 | if mtime > indexed_time: | |
364 | # The file has changed, delete it and add it to |
|
364 | # The file has changed, delete it and add it to | |
365 | # the list of files to reindex |
|
365 | # the list of files to reindex | |
366 | log.debug( |
|
366 | log.debug( | |
367 | 'adding to reindex list %s mtime: %s vs %s', |
|
367 | 'adding to reindex list %s mtime: %s vs %s', | |
368 | indexed_path, mtime, indexed_time |
|
368 | indexed_path, mtime, indexed_time | |
369 | ) |
|
369 | ) | |
370 | writer.delete_by_term('fileid', indexed_path) |
|
370 | writer.delete_by_term('fileid', indexed_path) | |
371 | writer_is_dirty = True |
|
371 | writer_is_dirty = True | |
372 |
|
372 | |||
373 | to_index.add(indexed_path) |
|
373 | to_index.add(indexed_path) | |
374 | except (ChangesetError, NodeDoesNotExistError): |
|
374 | except (ChangesetError, NodeDoesNotExistError): | |
375 | # This file was deleted since it was indexed |
|
375 | # This file was deleted since it was indexed | |
376 | log.debug('removing from index %s', indexed_path) |
|
376 | log.debug('removing from index %s', indexed_path) | |
377 | writer.delete_by_term('path', indexed_path) |
|
377 | writer.delete_by_term('path', indexed_path) | |
378 | writer_is_dirty = True |
|
378 | writer_is_dirty = True | |
379 |
|
379 | |||
380 | # Loop over the files in the filesystem |
|
380 | # Loop over the files in the filesystem | |
381 | # Assume we have a function that gathers the filenames of the |
|
381 | # Assume we have a function that gathers the filenames of the | |
382 | # documents to be indexed |
|
382 | # documents to be indexed | |
383 | ri_cnt_total = 0 # indexed |
|
383 | ri_cnt_total = 0 # indexed | |
384 | riwc_cnt_total = 0 # indexed with content |
|
384 | riwc_cnt_total = 0 # indexed with content | |
385 | for repo_name, repo in sorted(self.repo_paths.items()): |
|
385 | for repo_name, repo in sorted(self.repo_paths.items()): | |
386 | log.debug('Updating file index for repo %s', repo_name) |
|
386 | log.debug('Updating file index for repo %s', repo_name) | |
387 | # skip indexing if there aren't any revisions |
|
387 | # skip indexing if there aren't any revisions | |
388 | if len(repo) < 1: |
|
388 | if len(repo) < 1: | |
389 | continue |
|
389 | continue | |
390 | ri_cnt = 0 # indexed |
|
390 | ri_cnt = 0 # indexed | |
391 | riwc_cnt = 0 # indexed with content |
|
391 | riwc_cnt = 0 # indexed with content | |
392 | for path in self.get_paths(repo): |
|
392 | for path in self.get_paths(repo): | |
393 | path = safe_unicode(path) |
|
393 | path = safe_unicode(path) | |
394 | if path in to_index or path not in indexed_paths: |
|
394 | if path in to_index or path not in indexed_paths: | |
395 |
|
395 | |||
396 | # This is either a file that's changed, or a new file |
|
396 | # This is either a file that's changed, or a new file | |
397 | # that wasn't indexed before. So index it! |
|
397 | # that wasn't indexed before. So index it! | |
398 | i, iwc = self.add_doc(writer, path, repo, repo_name) |
|
398 | i, iwc = self.add_doc(writer, path, repo, repo_name) | |
399 | writer_is_dirty = True |
|
399 | writer_is_dirty = True | |
400 | ri_cnt += i |
|
400 | ri_cnt += i | |
401 | ri_cnt_total += 1 |
|
401 | ri_cnt_total += 1 | |
402 | riwc_cnt += iwc |
|
402 | riwc_cnt += iwc | |
403 | riwc_cnt_total += iwc |
|
403 | riwc_cnt_total += iwc | |
404 | log.debug('added %s files %s with content for repo %s', |
|
404 | log.debug('added %s files %s with content for repo %s', | |
405 | ri_cnt + riwc_cnt, riwc_cnt, repo.path |
|
405 | ri_cnt + riwc_cnt, riwc_cnt, repo.path | |
406 | ) |
|
406 | ) | |
407 | log.debug('indexed %s files in total and %s with content', |
|
407 | log.debug('indexed %s files in total and %s with content', | |
408 | ri_cnt_total, riwc_cnt_total |
|
408 | ri_cnt_total, riwc_cnt_total | |
409 | ) |
|
409 | ) | |
410 | finally: |
|
410 | finally: | |
411 | if writer_is_dirty: |
|
411 | if writer_is_dirty: | |
412 | log.debug('>> COMMITING CHANGES TO FILE INDEX <<') |
|
412 | log.debug('>> COMMITING CHANGES TO FILE INDEX <<') | |
413 | writer.commit(merge=True) |
|
413 | writer.commit(merge=True) | |
414 | log.debug('>>> FINISHED REBUILDING FILE INDEX <<<') |
|
414 | log.debug('>>> FINISHED REBUILDING FILE INDEX <<<') | |
415 | else: |
|
415 | else: | |
416 | log.debug('>> NOTHING TO COMMIT TO FILE INDEX <<') |
|
416 | log.debug('>> NOTHING TO COMMIT TO FILE INDEX <<') | |
417 | writer.cancel() |
|
417 | writer.cancel() | |
418 |
|
418 | |||
419 | def build_indexes(self): |
|
419 | def build_indexes(self): | |
420 | if os.path.exists(self.index_location): |
|
420 | if os.path.exists(self.index_location): | |
421 | log.debug('removing previous index') |
|
421 | log.debug('removing previous index') | |
422 | rmtree(self.index_location) |
|
422 | rmtree(self.index_location) | |
423 |
|
423 | |||
424 | if not os.path.exists(self.index_location): |
|
424 | if not os.path.exists(self.index_location): | |
425 | os.mkdir(self.index_location) |
|
425 | os.mkdir(self.index_location) | |
426 |
|
426 | |||
427 | chgset_idx = create_in(self.index_location, CHGSETS_SCHEMA, |
|
427 | chgset_idx = create_in(self.index_location, CHGSETS_SCHEMA, | |
428 | indexname=CHGSET_IDX_NAME) |
|
428 | indexname=CHGSET_IDX_NAME) | |
429 | chgset_idx_writer = chgset_idx.writer() |
|
429 | chgset_idx_writer = chgset_idx.writer() | |
430 |
|
430 | |||
431 | file_idx = create_in(self.index_location, SCHEMA, indexname=IDX_NAME) |
|
431 | file_idx = create_in(self.index_location, SCHEMA, indexname=IDX_NAME) | |
432 | file_idx_writer = file_idx.writer() |
|
432 | file_idx_writer = file_idx.writer() | |
433 | log.debug('BUILDING INDEX FOR EXTENSIONS %s ' |
|
433 | log.debug('BUILDING INDEX FOR EXTENSIONS %s ' | |
434 | 'AND REPOS %s' % (INDEX_EXTENSIONS, self.repo_paths.keys())) |
|
434 | 'AND REPOS %s' % (INDEX_EXTENSIONS, self.repo_paths.keys())) | |
435 |
|
435 | |||
436 | for repo_name, repo in sorted(self.repo_paths.items()): |
|
436 | for repo_name, repo in sorted(self.repo_paths.items()): | |
437 | log.debug('Updating indices for repo %s', repo_name) |
|
437 | log.debug('Updating indices for repo %s', repo_name) | |
438 | # skip indexing if there aren't any revisions |
|
438 | # skip indexing if there aren't any revisions | |
439 | if len(repo) < 1: |
|
439 | if len(repo) < 1: | |
440 | continue |
|
440 | continue | |
441 |
|
441 | |||
442 | self.index_files(file_idx_writer, repo_name, repo) |
|
442 | self.index_files(file_idx_writer, repo_name, repo) | |
443 | self.index_changesets(chgset_idx_writer, repo_name, repo) |
|
443 | self.index_changesets(chgset_idx_writer, repo_name, repo) | |
444 |
|
444 | |||
445 | log.debug('>> COMMITING CHANGES <<') |
|
445 | log.debug('>> COMMITING CHANGES <<') | |
446 | file_idx_writer.commit(merge=True) |
|
446 | file_idx_writer.commit(merge=True) | |
447 | chgset_idx_writer.commit(merge=True) |
|
447 | chgset_idx_writer.commit(merge=True) | |
448 | log.debug('>>> FINISHED BUILDING INDEX <<<') |
|
448 | log.debug('>>> FINISHED BUILDING INDEX <<<') | |
449 |
|
449 | |||
450 | def update_indexes(self): |
|
450 | def update_indexes(self): | |
451 | self.update_file_index() |
|
451 | self.update_file_index() | |
452 | self.update_changeset_index() |
|
452 | self.update_changeset_index() | |
453 |
|
453 | |||
454 | def run(self, full_index=False): |
|
454 | def run(self, full_index=False): | |
455 | """Run daemon""" |
|
455 | """Run daemon""" | |
456 | if full_index or self.initial: |
|
456 | if full_index or self.initial: | |
457 | self.build_indexes() |
|
457 | self.build_indexes() | |
458 | else: |
|
458 | else: | |
459 | self.update_indexes() |
|
459 | self.update_indexes() |
@@ -1,201 +1,201 b'' | |||||
1 | import datetime |
|
1 | import datetime | |
2 | import posixpath |
|
2 | import posixpath | |
3 | import stat |
|
3 | import stat | |
4 | import time |
|
4 | import time | |
5 |
|
5 | |||
6 | from dulwich import objects |
|
6 | from dulwich import objects | |
7 |
|
7 | |||
8 | from kallithea.lib.vcs.backends.base import BaseInMemoryChangeset |
|
8 | from kallithea.lib.vcs.backends.base import BaseInMemoryChangeset | |
9 | from kallithea.lib.vcs.exceptions import RepositoryError |
|
9 | from kallithea.lib.vcs.exceptions import RepositoryError | |
10 | from kallithea.lib.vcs.utils import safe_str |
|
10 | from kallithea.lib.vcs.utils import safe_str | |
11 |
|
11 | |||
12 |
|
12 | |||
13 | class GitInMemoryChangeset(BaseInMemoryChangeset): |
|
13 | class GitInMemoryChangeset(BaseInMemoryChangeset): | |
14 |
|
14 | |||
15 | def commit(self, message, author, parents=None, branch=None, date=None, |
|
15 | def commit(self, message, author, parents=None, branch=None, date=None, | |
16 | **kwargs): |
|
16 | **kwargs): | |
17 | """ |
|
17 | """ | |
18 | Performs in-memory commit (doesn't check workdir in any way) and |
|
18 | Performs in-memory commit (doesn't check workdir in any way) and | |
19 | returns newly created ``Changeset``. Updates repository's |
|
19 | returns newly created ``Changeset``. Updates repository's | |
20 | ``revisions``. |
|
20 | ``revisions``. | |
21 |
|
21 | |||
22 | :param message: message of the commit |
|
22 | :param message: message of the commit | |
23 | :param author: full username, i.e. "Joe Doe <joe.doe@example.com>" |
|
23 | :param author: full username, i.e. "Joe Doe <joe.doe@example.com>" | |
24 | :param parents: single parent or sequence of parents from which commit |
|
24 | :param parents: single parent or sequence of parents from which commit | |
25 | would be derived |
|
25 | would be derived | |
26 | :param date: ``datetime.datetime`` instance. Defaults to |
|
26 | :param date: ``datetime.datetime`` instance. Defaults to | |
27 | ``datetime.datetime.now()``. |
|
27 | ``datetime.datetime.now()``. | |
28 | :param branch: branch name, as string. If none given, default backend's |
|
28 | :param branch: branch name, as string. If none given, default backend's | |
29 | branch would be used. |
|
29 | branch would be used. | |
30 |
|
30 | |||
31 | :raises ``CommitError``: if any error occurs while committing |
|
31 | :raises ``CommitError``: if any error occurs while committing | |
32 | """ |
|
32 | """ | |
33 | self.check_integrity(parents) |
|
33 | self.check_integrity(parents) | |
34 |
|
34 | |||
35 | from .repository import GitRepository |
|
35 | from .repository import GitRepository | |
36 | if branch is None: |
|
36 | if branch is None: | |
37 | branch = GitRepository.DEFAULT_BRANCH_NAME |
|
37 | branch = GitRepository.DEFAULT_BRANCH_NAME | |
38 |
|
38 | |||
39 | repo = self.repository._repo |
|
39 | repo = self.repository._repo | |
40 | object_store = repo.object_store |
|
40 | object_store = repo.object_store | |
41 |
|
41 | |||
42 | ENCODING = "UTF-8" |
|
42 | ENCODING = "UTF-8" | |
43 |
|
43 | |||
44 | # Create tree and populates it with blobs |
|
44 | # Create tree and populates it with blobs | |
45 | commit_tree = self.parents[0] and repo[self.parents[0]._commit.tree] or \ |
|
45 | commit_tree = self.parents[0] and repo[self.parents[0]._commit.tree] or \ | |
46 | objects.Tree() |
|
46 | objects.Tree() | |
47 | for node in self.added + self.changed: |
|
47 | for node in self.added + self.changed: | |
48 | # Compute subdirs if needed |
|
48 | # Compute subdirs if needed | |
49 | dirpath, nodename = posixpath.split(node.path) |
|
49 | dirpath, nodename = posixpath.split(node.path) | |
50 |
dirnames = |
|
50 | dirnames = safe_str(dirpath).split('/') if dirpath else [] | |
51 | parent = commit_tree |
|
51 | parent = commit_tree | |
52 | ancestors = [('', parent)] |
|
52 | ancestors = [('', parent)] | |
53 |
|
53 | |||
54 | # Tries to dig for the deepest existing tree |
|
54 | # Tries to dig for the deepest existing tree | |
55 | while dirnames: |
|
55 | while dirnames: | |
56 | curdir = dirnames.pop(0) |
|
56 | curdir = dirnames.pop(0) | |
57 | try: |
|
57 | try: | |
58 | dir_id = parent[curdir][1] |
|
58 | dir_id = parent[curdir][1] | |
59 | except KeyError: |
|
59 | except KeyError: | |
60 | # put curdir back into dirnames and stops |
|
60 | # put curdir back into dirnames and stops | |
61 | dirnames.insert(0, curdir) |
|
61 | dirnames.insert(0, curdir) | |
62 | break |
|
62 | break | |
63 | else: |
|
63 | else: | |
64 | # If found, updates parent |
|
64 | # If found, updates parent | |
65 | parent = self.repository._repo[dir_id] |
|
65 | parent = self.repository._repo[dir_id] | |
66 | ancestors.append((curdir, parent)) |
|
66 | ancestors.append((curdir, parent)) | |
67 | # Now parent is deepest existing tree and we need to create subtrees |
|
67 | # Now parent is deepest existing tree and we need to create subtrees | |
68 | # for dirnames (in reverse order) [this only applies for nodes from added] |
|
68 | # for dirnames (in reverse order) [this only applies for nodes from added] | |
69 | new_trees = [] |
|
69 | new_trees = [] | |
70 |
|
70 | |||
71 | if not node.is_binary: |
|
71 | if not node.is_binary: | |
72 | content = node.content.encode(ENCODING) |
|
72 | content = node.content.encode(ENCODING) | |
73 | else: |
|
73 | else: | |
74 | content = node.content |
|
74 | content = node.content | |
75 | blob = objects.Blob.from_string(content) |
|
75 | blob = objects.Blob.from_string(content) | |
76 |
|
76 | |||
77 | node_path = node.name.encode(ENCODING) |
|
77 | node_path = node.name.encode(ENCODING) | |
78 | if dirnames: |
|
78 | if dirnames: | |
79 | # If there are trees which should be created we need to build |
|
79 | # If there are trees which should be created we need to build | |
80 | # them now (in reverse order) |
|
80 | # them now (in reverse order) | |
81 | reversed_dirnames = list(reversed(dirnames)) |
|
81 | reversed_dirnames = list(reversed(dirnames)) | |
82 | curtree = objects.Tree() |
|
82 | curtree = objects.Tree() | |
83 | curtree[node_path] = node.mode, blob.id |
|
83 | curtree[node_path] = node.mode, blob.id | |
84 | new_trees.append(curtree) |
|
84 | new_trees.append(curtree) | |
85 | for dirname in reversed_dirnames[:-1]: |
|
85 | for dirname in reversed_dirnames[:-1]: | |
86 | newtree = objects.Tree() |
|
86 | newtree = objects.Tree() | |
87 | #newtree.add(stat.S_IFDIR, dirname, curtree.id) |
|
87 | #newtree.add(stat.S_IFDIR, dirname, curtree.id) | |
88 | newtree[dirname] = stat.S_IFDIR, curtree.id |
|
88 | newtree[dirname] = stat.S_IFDIR, curtree.id | |
89 | new_trees.append(newtree) |
|
89 | new_trees.append(newtree) | |
90 | curtree = newtree |
|
90 | curtree = newtree | |
91 | parent[reversed_dirnames[-1]] = stat.S_IFDIR, curtree.id |
|
91 | parent[reversed_dirnames[-1]] = stat.S_IFDIR, curtree.id | |
92 | else: |
|
92 | else: | |
93 | parent.add(name=node_path, mode=node.mode, hexsha=blob.id) |
|
93 | parent.add(name=node_path, mode=node.mode, hexsha=blob.id) | |
94 |
|
94 | |||
95 | new_trees.append(parent) |
|
95 | new_trees.append(parent) | |
96 | # Update ancestors |
|
96 | # Update ancestors | |
97 | for parent, tree, path in reversed([(a[1], b[1], b[0]) for a, b in |
|
97 | for parent, tree, path in reversed([(a[1], b[1], b[0]) for a, b in | |
98 | zip(ancestors, ancestors[1:])] |
|
98 | zip(ancestors, ancestors[1:])] | |
99 | ): |
|
99 | ): | |
100 | parent[path] = stat.S_IFDIR, tree.id |
|
100 | parent[path] = stat.S_IFDIR, tree.id | |
101 | object_store.add_object(tree) |
|
101 | object_store.add_object(tree) | |
102 |
|
102 | |||
103 | object_store.add_object(blob) |
|
103 | object_store.add_object(blob) | |
104 | for tree in new_trees: |
|
104 | for tree in new_trees: | |
105 | object_store.add_object(tree) |
|
105 | object_store.add_object(tree) | |
106 | for node in self.removed: |
|
106 | for node in self.removed: | |
107 | paths = node.path.split('/') |
|
107 | paths = node.path.split('/') | |
108 | tree = commit_tree |
|
108 | tree = commit_tree | |
109 | trees = [tree] |
|
109 | trees = [tree] | |
110 | # Traverse deep into the forest... |
|
110 | # Traverse deep into the forest... | |
111 | for path in paths: |
|
111 | for path in paths: | |
112 | try: |
|
112 | try: | |
113 | obj = self.repository._repo[tree[path][1]] |
|
113 | obj = self.repository._repo[tree[path][1]] | |
114 | if isinstance(obj, objects.Tree): |
|
114 | if isinstance(obj, objects.Tree): | |
115 | trees.append(obj) |
|
115 | trees.append(obj) | |
116 | tree = obj |
|
116 | tree = obj | |
117 | except KeyError: |
|
117 | except KeyError: | |
118 | break |
|
118 | break | |
119 | # Cut down the blob and all rotten trees on the way back... |
|
119 | # Cut down the blob and all rotten trees on the way back... | |
120 | for path, tree in reversed(zip(paths, trees)): |
|
120 | for path, tree in reversed(zip(paths, trees)): | |
121 | del tree[path] |
|
121 | del tree[path] | |
122 | if tree: |
|
122 | if tree: | |
123 | # This tree still has elements - don't remove it or any |
|
123 | # This tree still has elements - don't remove it or any | |
124 | # of it's parents |
|
124 | # of it's parents | |
125 | break |
|
125 | break | |
126 |
|
126 | |||
127 | object_store.add_object(commit_tree) |
|
127 | object_store.add_object(commit_tree) | |
128 |
|
128 | |||
129 | # Create commit |
|
129 | # Create commit | |
130 | commit = objects.Commit() |
|
130 | commit = objects.Commit() | |
131 | commit.tree = commit_tree.id |
|
131 | commit.tree = commit_tree.id | |
132 | commit.parents = [p._commit.id for p in self.parents if p] |
|
132 | commit.parents = [p._commit.id for p in self.parents if p] | |
133 | commit.author = commit.committer = safe_str(author) |
|
133 | commit.author = commit.committer = safe_str(author) | |
134 | commit.encoding = ENCODING |
|
134 | commit.encoding = ENCODING | |
135 | commit.message = safe_str(message) |
|
135 | commit.message = safe_str(message) | |
136 |
|
136 | |||
137 | # Compute date |
|
137 | # Compute date | |
138 | if date is None: |
|
138 | if date is None: | |
139 | date = time.time() |
|
139 | date = time.time() | |
140 | elif isinstance(date, datetime.datetime): |
|
140 | elif isinstance(date, datetime.datetime): | |
141 | date = time.mktime(date.timetuple()) |
|
141 | date = time.mktime(date.timetuple()) | |
142 |
|
142 | |||
143 | author_time = kwargs.pop('author_time', date) |
|
143 | author_time = kwargs.pop('author_time', date) | |
144 | commit.commit_time = int(date) |
|
144 | commit.commit_time = int(date) | |
145 | commit.author_time = int(author_time) |
|
145 | commit.author_time = int(author_time) | |
146 | tz = time.timezone |
|
146 | tz = time.timezone | |
147 | author_tz = kwargs.pop('author_timezone', tz) |
|
147 | author_tz = kwargs.pop('author_timezone', tz) | |
148 | commit.commit_timezone = tz |
|
148 | commit.commit_timezone = tz | |
149 | commit.author_timezone = author_tz |
|
149 | commit.author_timezone = author_tz | |
150 |
|
150 | |||
151 | object_store.add_object(commit) |
|
151 | object_store.add_object(commit) | |
152 |
|
152 | |||
153 | ref = 'refs/heads/%s' % branch |
|
153 | ref = 'refs/heads/%s' % branch | |
154 | repo.refs[ref] = commit.id |
|
154 | repo.refs[ref] = commit.id | |
155 |
|
155 | |||
156 | # Update vcs repository object & recreate dulwich repo |
|
156 | # Update vcs repository object & recreate dulwich repo | |
157 | self.repository.revisions.append(commit.id) |
|
157 | self.repository.revisions.append(commit.id) | |
158 | # invalidate parsed refs after commit |
|
158 | # invalidate parsed refs after commit | |
159 | self.repository._parsed_refs = self.repository._get_parsed_refs() |
|
159 | self.repository._parsed_refs = self.repository._get_parsed_refs() | |
160 | tip = self.repository.get_changeset() |
|
160 | tip = self.repository.get_changeset() | |
161 | self.reset() |
|
161 | self.reset() | |
162 | return tip |
|
162 | return tip | |
163 |
|
163 | |||
164 | def _get_missing_trees(self, path, root_tree): |
|
164 | def _get_missing_trees(self, path, root_tree): | |
165 | """ |
|
165 | """ | |
166 | Creates missing ``Tree`` objects for the given path. |
|
166 | Creates missing ``Tree`` objects for the given path. | |
167 |
|
167 | |||
168 | :param path: path given as a string. It may be a path to a file node |
|
168 | :param path: path given as a string. It may be a path to a file node | |
169 | (i.e. ``foo/bar/baz.txt``) or directory path - in that case it must |
|
169 | (i.e. ``foo/bar/baz.txt``) or directory path - in that case it must | |
170 | end with slash (i.e. ``foo/bar/``). |
|
170 | end with slash (i.e. ``foo/bar/``). | |
171 | :param root_tree: ``dulwich.objects.Tree`` object from which we start |
|
171 | :param root_tree: ``dulwich.objects.Tree`` object from which we start | |
172 | traversing (should be commit's root tree) |
|
172 | traversing (should be commit's root tree) | |
173 | """ |
|
173 | """ | |
174 | dirpath = posixpath.split(path)[0] |
|
174 | dirpath = posixpath.split(path)[0] | |
175 | dirs = dirpath.split('/') |
|
175 | dirs = dirpath.split('/') | |
176 | if not dirs or dirs == ['']: |
|
176 | if not dirs or dirs == ['']: | |
177 | return [] |
|
177 | return [] | |
178 |
|
178 | |||
179 | def get_tree_for_dir(tree, dirname): |
|
179 | def get_tree_for_dir(tree, dirname): | |
180 | for name, mode, id in tree.iteritems(): |
|
180 | for name, mode, id in tree.iteritems(): | |
181 | if name == dirname: |
|
181 | if name == dirname: | |
182 | obj = self.repository._repo[id] |
|
182 | obj = self.repository._repo[id] | |
183 | if isinstance(obj, objects.Tree): |
|
183 | if isinstance(obj, objects.Tree): | |
184 | return obj |
|
184 | return obj | |
185 | else: |
|
185 | else: | |
186 | raise RepositoryError("Cannot create directory %s " |
|
186 | raise RepositoryError("Cannot create directory %s " | |
187 | "at tree %s as path is occupied and is not a " |
|
187 | "at tree %s as path is occupied and is not a " | |
188 | "Tree" % (dirname, tree)) |
|
188 | "Tree" % (dirname, tree)) | |
189 | return None |
|
189 | return None | |
190 |
|
190 | |||
191 | trees = [] |
|
191 | trees = [] | |
192 | parent = root_tree |
|
192 | parent = root_tree | |
193 | for dirname in dirs: |
|
193 | for dirname in dirs: | |
194 | tree = get_tree_for_dir(parent, dirname) |
|
194 | tree = get_tree_for_dir(parent, dirname) | |
195 | if tree is None: |
|
195 | if tree is None: | |
196 | tree = objects.Tree() |
|
196 | tree = objects.Tree() | |
197 | parent.add(stat.S_IFDIR, dirname, tree.id) |
|
197 | parent.add(stat.S_IFDIR, dirname, tree.id) | |
198 | parent = tree |
|
198 | parent = tree | |
199 | # Always append tree |
|
199 | # Always append tree | |
200 | trees.append(tree) |
|
200 | trees.append(tree) | |
201 | return trees |
|
201 | return trees |
@@ -1,427 +1,427 b'' | |||||
1 | import os |
|
1 | import os | |
2 | import posixpath |
|
2 | import posixpath | |
3 |
|
3 | |||
4 | from kallithea.lib.vcs.backends.base import BaseChangeset |
|
4 | from kallithea.lib.vcs.backends.base import BaseChangeset | |
5 | from kallithea.lib.vcs.conf import settings |
|
5 | from kallithea.lib.vcs.conf import settings | |
6 | from kallithea.lib.vcs.exceptions import ChangesetDoesNotExistError, ChangesetError, ImproperArchiveTypeError, NodeDoesNotExistError, VCSError |
|
6 | from kallithea.lib.vcs.exceptions import ChangesetDoesNotExistError, ChangesetError, ImproperArchiveTypeError, NodeDoesNotExistError, VCSError | |
7 | from kallithea.lib.vcs.nodes import ( |
|
7 | from kallithea.lib.vcs.nodes import ( | |
8 | AddedFileNodesGenerator, ChangedFileNodesGenerator, DirNode, FileNode, NodeKind, RemovedFileNodesGenerator, RootNode, SubModuleNode) |
|
8 | AddedFileNodesGenerator, ChangedFileNodesGenerator, DirNode, FileNode, NodeKind, RemovedFileNodesGenerator, RootNode, SubModuleNode) | |
9 | from kallithea.lib.vcs.utils import date_fromtimestamp, safe_str, safe_unicode |
|
9 | from kallithea.lib.vcs.utils import date_fromtimestamp, safe_str, safe_unicode | |
10 | from kallithea.lib.vcs.utils.hgcompat import archival, hex, obsutil |
|
10 | from kallithea.lib.vcs.utils.hgcompat import archival, hex, obsutil | |
11 | from kallithea.lib.vcs.utils.lazy import LazyProperty |
|
11 | from kallithea.lib.vcs.utils.lazy import LazyProperty | |
12 | from kallithea.lib.vcs.utils.paths import get_dirs_for_path |
|
12 | from kallithea.lib.vcs.utils.paths import get_dirs_for_path | |
13 |
|
13 | |||
14 |
|
14 | |||
15 | class MercurialChangeset(BaseChangeset): |
|
15 | class MercurialChangeset(BaseChangeset): | |
16 | """ |
|
16 | """ | |
17 | Represents state of the repository at the single revision. |
|
17 | Represents state of the repository at the single revision. | |
18 | """ |
|
18 | """ | |
19 |
|
19 | |||
20 | def __init__(self, repository, revision): |
|
20 | def __init__(self, repository, revision): | |
21 | self.repository = repository |
|
21 | self.repository = repository | |
22 | assert isinstance(revision, basestring), repr(revision) |
|
22 | assert isinstance(revision, basestring), repr(revision) | |
23 | self.raw_id = revision |
|
23 | self.raw_id = revision | |
24 | self._ctx = repository._repo[revision] |
|
24 | self._ctx = repository._repo[revision] | |
25 | self.revision = self._ctx._rev |
|
25 | self.revision = self._ctx._rev | |
26 | self.nodes = {} |
|
26 | self.nodes = {} | |
27 |
|
27 | |||
28 | @LazyProperty |
|
28 | @LazyProperty | |
29 | def tags(self): |
|
29 | def tags(self): | |
30 |
return |
|
30 | return [safe_unicode(tag) for tag in self._ctx.tags()] | |
31 |
|
31 | |||
32 | @LazyProperty |
|
32 | @LazyProperty | |
33 | def branch(self): |
|
33 | def branch(self): | |
34 | return safe_unicode(self._ctx.branch()) |
|
34 | return safe_unicode(self._ctx.branch()) | |
35 |
|
35 | |||
36 | @LazyProperty |
|
36 | @LazyProperty | |
37 | def branches(self): |
|
37 | def branches(self): | |
38 | return [safe_unicode(self._ctx.branch())] |
|
38 | return [safe_unicode(self._ctx.branch())] | |
39 |
|
39 | |||
40 | @LazyProperty |
|
40 | @LazyProperty | |
41 | def closesbranch(self): |
|
41 | def closesbranch(self): | |
42 | return self._ctx.closesbranch() |
|
42 | return self._ctx.closesbranch() | |
43 |
|
43 | |||
44 | @LazyProperty |
|
44 | @LazyProperty | |
45 | def obsolete(self): |
|
45 | def obsolete(self): | |
46 | return self._ctx.obsolete() |
|
46 | return self._ctx.obsolete() | |
47 |
|
47 | |||
48 | @LazyProperty |
|
48 | @LazyProperty | |
49 | def bumped(self): |
|
49 | def bumped(self): | |
50 | try: |
|
50 | try: | |
51 | return self._ctx.phasedivergent() |
|
51 | return self._ctx.phasedivergent() | |
52 | except AttributeError: # renamed in Mercurial 4.6 (9fa874fb34e1) |
|
52 | except AttributeError: # renamed in Mercurial 4.6 (9fa874fb34e1) | |
53 | return self._ctx.bumped() |
|
53 | return self._ctx.bumped() | |
54 |
|
54 | |||
55 | @LazyProperty |
|
55 | @LazyProperty | |
56 | def divergent(self): |
|
56 | def divergent(self): | |
57 | try: |
|
57 | try: | |
58 | return self._ctx.contentdivergent() |
|
58 | return self._ctx.contentdivergent() | |
59 | except AttributeError: # renamed in Mercurial 4.6 (8b2d7684407b) |
|
59 | except AttributeError: # renamed in Mercurial 4.6 (8b2d7684407b) | |
60 | return self._ctx.divergent() |
|
60 | return self._ctx.divergent() | |
61 |
|
61 | |||
62 | @LazyProperty |
|
62 | @LazyProperty | |
63 | def extinct(self): |
|
63 | def extinct(self): | |
64 | return self._ctx.extinct() |
|
64 | return self._ctx.extinct() | |
65 |
|
65 | |||
66 | @LazyProperty |
|
66 | @LazyProperty | |
67 | def unstable(self): |
|
67 | def unstable(self): | |
68 | try: |
|
68 | try: | |
69 | return self._ctx.orphan() |
|
69 | return self._ctx.orphan() | |
70 | except AttributeError: # renamed in Mercurial 4.6 (03039ff3082b) |
|
70 | except AttributeError: # renamed in Mercurial 4.6 (03039ff3082b) | |
71 | return self._ctx.unstable() |
|
71 | return self._ctx.unstable() | |
72 |
|
72 | |||
73 | @LazyProperty |
|
73 | @LazyProperty | |
74 | def phase(self): |
|
74 | def phase(self): | |
75 | if(self._ctx.phase() == 1): |
|
75 | if(self._ctx.phase() == 1): | |
76 | return 'Draft' |
|
76 | return 'Draft' | |
77 | elif(self._ctx.phase() == 2): |
|
77 | elif(self._ctx.phase() == 2): | |
78 | return 'Secret' |
|
78 | return 'Secret' | |
79 | else: |
|
79 | else: | |
80 | return '' |
|
80 | return '' | |
81 |
|
81 | |||
82 | @LazyProperty |
|
82 | @LazyProperty | |
83 | def successors(self): |
|
83 | def successors(self): | |
84 | successors = obsutil.successorssets(self._ctx._repo, self._ctx.node(), closest=True) |
|
84 | successors = obsutil.successorssets(self._ctx._repo, self._ctx.node(), closest=True) | |
85 | if successors: |
|
85 | if successors: | |
86 | # flatten the list here handles both divergent (len > 1) |
|
86 | # flatten the list here handles both divergent (len > 1) | |
87 | # and the usual case (len = 1) |
|
87 | # and the usual case (len = 1) | |
88 | successors = [hex(n)[:12] for sub in successors for n in sub if n != self._ctx.node()] |
|
88 | successors = [hex(n)[:12] for sub in successors for n in sub if n != self._ctx.node()] | |
89 |
|
89 | |||
90 | return successors |
|
90 | return successors | |
91 |
|
91 | |||
92 | @LazyProperty |
|
92 | @LazyProperty | |
93 | def predecessors(self): |
|
93 | def predecessors(self): | |
94 | return [hex(n)[:12] for n in obsutil.closestpredecessors(self._ctx._repo, self._ctx.node())] |
|
94 | return [hex(n)[:12] for n in obsutil.closestpredecessors(self._ctx._repo, self._ctx.node())] | |
95 |
|
95 | |||
96 | @LazyProperty |
|
96 | @LazyProperty | |
97 | def bookmarks(self): |
|
97 | def bookmarks(self): | |
98 |
return |
|
98 | return [safe_unicode(bookmark) for bookmark in self._ctx.bookmarks()] | |
99 |
|
99 | |||
100 | @LazyProperty |
|
100 | @LazyProperty | |
101 | def message(self): |
|
101 | def message(self): | |
102 | return safe_unicode(self._ctx.description()) |
|
102 | return safe_unicode(self._ctx.description()) | |
103 |
|
103 | |||
104 | @LazyProperty |
|
104 | @LazyProperty | |
105 | def committer(self): |
|
105 | def committer(self): | |
106 | return safe_unicode(self.author) |
|
106 | return safe_unicode(self.author) | |
107 |
|
107 | |||
108 | @LazyProperty |
|
108 | @LazyProperty | |
109 | def author(self): |
|
109 | def author(self): | |
110 | return safe_unicode(self._ctx.user()) |
|
110 | return safe_unicode(self._ctx.user()) | |
111 |
|
111 | |||
112 | @LazyProperty |
|
112 | @LazyProperty | |
113 | def date(self): |
|
113 | def date(self): | |
114 | return date_fromtimestamp(*self._ctx.date()) |
|
114 | return date_fromtimestamp(*self._ctx.date()) | |
115 |
|
115 | |||
116 | @LazyProperty |
|
116 | @LazyProperty | |
117 | def _timestamp(self): |
|
117 | def _timestamp(self): | |
118 | return self._ctx.date()[0] |
|
118 | return self._ctx.date()[0] | |
119 |
|
119 | |||
120 | @LazyProperty |
|
120 | @LazyProperty | |
121 | def status(self): |
|
121 | def status(self): | |
122 | """ |
|
122 | """ | |
123 | Returns modified, added, removed, deleted files for current changeset |
|
123 | Returns modified, added, removed, deleted files for current changeset | |
124 | """ |
|
124 | """ | |
125 | return self.repository._repo.status(self._ctx.p1().node(), |
|
125 | return self.repository._repo.status(self._ctx.p1().node(), | |
126 | self._ctx.node()) |
|
126 | self._ctx.node()) | |
127 |
|
127 | |||
128 | @LazyProperty |
|
128 | @LazyProperty | |
129 | def _file_paths(self): |
|
129 | def _file_paths(self): | |
130 | return list(self._ctx) |
|
130 | return list(self._ctx) | |
131 |
|
131 | |||
132 | @LazyProperty |
|
132 | @LazyProperty | |
133 | def _dir_paths(self): |
|
133 | def _dir_paths(self): | |
134 | p = list(set(get_dirs_for_path(*self._file_paths))) |
|
134 | p = list(set(get_dirs_for_path(*self._file_paths))) | |
135 | p.insert(0, '') |
|
135 | p.insert(0, '') | |
136 | return p |
|
136 | return p | |
137 |
|
137 | |||
138 | @LazyProperty |
|
138 | @LazyProperty | |
139 | def _paths(self): |
|
139 | def _paths(self): | |
140 | return self._dir_paths + self._file_paths |
|
140 | return self._dir_paths + self._file_paths | |
141 |
|
141 | |||
142 | @LazyProperty |
|
142 | @LazyProperty | |
143 | def id(self): |
|
143 | def id(self): | |
144 | if self.last: |
|
144 | if self.last: | |
145 | return u'tip' |
|
145 | return u'tip' | |
146 | return self.short_id |
|
146 | return self.short_id | |
147 |
|
147 | |||
148 | @LazyProperty |
|
148 | @LazyProperty | |
149 | def short_id(self): |
|
149 | def short_id(self): | |
150 | return self.raw_id[:12] |
|
150 | return self.raw_id[:12] | |
151 |
|
151 | |||
152 | @LazyProperty |
|
152 | @LazyProperty | |
153 | def parents(self): |
|
153 | def parents(self): | |
154 | """ |
|
154 | """ | |
155 | Returns list of parents changesets. |
|
155 | Returns list of parents changesets. | |
156 | """ |
|
156 | """ | |
157 | return [self.repository.get_changeset(parent.rev()) |
|
157 | return [self.repository.get_changeset(parent.rev()) | |
158 | for parent in self._ctx.parents() if parent.rev() >= 0] |
|
158 | for parent in self._ctx.parents() if parent.rev() >= 0] | |
159 |
|
159 | |||
160 | @LazyProperty |
|
160 | @LazyProperty | |
161 | def children(self): |
|
161 | def children(self): | |
162 | """ |
|
162 | """ | |
163 | Returns list of children changesets. |
|
163 | Returns list of children changesets. | |
164 | """ |
|
164 | """ | |
165 | return [self.repository.get_changeset(child.rev()) |
|
165 | return [self.repository.get_changeset(child.rev()) | |
166 | for child in self._ctx.children() if child.rev() >= 0] |
|
166 | for child in self._ctx.children() if child.rev() >= 0] | |
167 |
|
167 | |||
168 | def next(self, branch=None): |
|
168 | def next(self, branch=None): | |
169 | if branch and self.branch != branch: |
|
169 | if branch and self.branch != branch: | |
170 | raise VCSError('Branch option used on changeset not belonging ' |
|
170 | raise VCSError('Branch option used on changeset not belonging ' | |
171 | 'to that branch') |
|
171 | 'to that branch') | |
172 |
|
172 | |||
173 | cs = self |
|
173 | cs = self | |
174 | while True: |
|
174 | while True: | |
175 | try: |
|
175 | try: | |
176 | next_ = cs.repository.revisions.index(cs.raw_id) + 1 |
|
176 | next_ = cs.repository.revisions.index(cs.raw_id) + 1 | |
177 | next_rev = cs.repository.revisions[next_] |
|
177 | next_rev = cs.repository.revisions[next_] | |
178 | except IndexError: |
|
178 | except IndexError: | |
179 | raise ChangesetDoesNotExistError |
|
179 | raise ChangesetDoesNotExistError | |
180 | cs = cs.repository.get_changeset(next_rev) |
|
180 | cs = cs.repository.get_changeset(next_rev) | |
181 |
|
181 | |||
182 | if not branch or branch == cs.branch: |
|
182 | if not branch or branch == cs.branch: | |
183 | return cs |
|
183 | return cs | |
184 |
|
184 | |||
185 | def prev(self, branch=None): |
|
185 | def prev(self, branch=None): | |
186 | if branch and self.branch != branch: |
|
186 | if branch and self.branch != branch: | |
187 | raise VCSError('Branch option used on changeset not belonging ' |
|
187 | raise VCSError('Branch option used on changeset not belonging ' | |
188 | 'to that branch') |
|
188 | 'to that branch') | |
189 |
|
189 | |||
190 | cs = self |
|
190 | cs = self | |
191 | while True: |
|
191 | while True: | |
192 | try: |
|
192 | try: | |
193 | prev_ = cs.repository.revisions.index(cs.raw_id) - 1 |
|
193 | prev_ = cs.repository.revisions.index(cs.raw_id) - 1 | |
194 | if prev_ < 0: |
|
194 | if prev_ < 0: | |
195 | raise IndexError |
|
195 | raise IndexError | |
196 | prev_rev = cs.repository.revisions[prev_] |
|
196 | prev_rev = cs.repository.revisions[prev_] | |
197 | except IndexError: |
|
197 | except IndexError: | |
198 | raise ChangesetDoesNotExistError |
|
198 | raise ChangesetDoesNotExistError | |
199 | cs = cs.repository.get_changeset(prev_rev) |
|
199 | cs = cs.repository.get_changeset(prev_rev) | |
200 |
|
200 | |||
201 | if not branch or branch == cs.branch: |
|
201 | if not branch or branch == cs.branch: | |
202 | return cs |
|
202 | return cs | |
203 |
|
203 | |||
204 | def diff(self): |
|
204 | def diff(self): | |
205 | # Only used for feed diffstat |
|
205 | # Only used for feed diffstat | |
206 | return ''.join(self._ctx.diff()) |
|
206 | return ''.join(self._ctx.diff()) | |
207 |
|
207 | |||
208 | def _fix_path(self, path): |
|
208 | def _fix_path(self, path): | |
209 | """ |
|
209 | """ | |
210 | Paths are stored without trailing slash so we need to get rid off it if |
|
210 | Paths are stored without trailing slash so we need to get rid off it if | |
211 | needed. Also mercurial keeps filenodes as str so we need to decode |
|
211 | needed. Also mercurial keeps filenodes as str so we need to decode | |
212 | from unicode to str |
|
212 | from unicode to str | |
213 | """ |
|
213 | """ | |
214 | if path.endswith('/'): |
|
214 | if path.endswith('/'): | |
215 | path = path.rstrip('/') |
|
215 | path = path.rstrip('/') | |
216 |
|
216 | |||
217 | return safe_str(path) |
|
217 | return safe_str(path) | |
218 |
|
218 | |||
219 | def _get_kind(self, path): |
|
219 | def _get_kind(self, path): | |
220 | path = self._fix_path(path) |
|
220 | path = self._fix_path(path) | |
221 | if path in self._file_paths: |
|
221 | if path in self._file_paths: | |
222 | return NodeKind.FILE |
|
222 | return NodeKind.FILE | |
223 | elif path in self._dir_paths: |
|
223 | elif path in self._dir_paths: | |
224 | return NodeKind.DIR |
|
224 | return NodeKind.DIR | |
225 | else: |
|
225 | else: | |
226 | raise ChangesetError("Node does not exist at the given path '%s'" |
|
226 | raise ChangesetError("Node does not exist at the given path '%s'" | |
227 | % (path)) |
|
227 | % (path)) | |
228 |
|
228 | |||
229 | def _get_filectx(self, path): |
|
229 | def _get_filectx(self, path): | |
230 | path = self._fix_path(path) |
|
230 | path = self._fix_path(path) | |
231 | if self._get_kind(path) != NodeKind.FILE: |
|
231 | if self._get_kind(path) != NodeKind.FILE: | |
232 | raise ChangesetError("File does not exist for revision %s at " |
|
232 | raise ChangesetError("File does not exist for revision %s at " | |
233 | " '%s'" % (self.raw_id, path)) |
|
233 | " '%s'" % (self.raw_id, path)) | |
234 | return self._ctx.filectx(path) |
|
234 | return self._ctx.filectx(path) | |
235 |
|
235 | |||
236 | def _extract_submodules(self): |
|
236 | def _extract_submodules(self): | |
237 | """ |
|
237 | """ | |
238 | returns a dictionary with submodule information from substate file |
|
238 | returns a dictionary with submodule information from substate file | |
239 | of hg repository |
|
239 | of hg repository | |
240 | """ |
|
240 | """ | |
241 | return self._ctx.substate |
|
241 | return self._ctx.substate | |
242 |
|
242 | |||
243 | def get_file_mode(self, path): |
|
243 | def get_file_mode(self, path): | |
244 | """ |
|
244 | """ | |
245 | Returns stat mode of the file at the given ``path``. |
|
245 | Returns stat mode of the file at the given ``path``. | |
246 | """ |
|
246 | """ | |
247 | fctx = self._get_filectx(path) |
|
247 | fctx = self._get_filectx(path) | |
248 | if 'x' in fctx.flags(): |
|
248 | if 'x' in fctx.flags(): | |
249 | return 0o100755 |
|
249 | return 0o100755 | |
250 | else: |
|
250 | else: | |
251 | return 0o100644 |
|
251 | return 0o100644 | |
252 |
|
252 | |||
253 | def get_file_content(self, path): |
|
253 | def get_file_content(self, path): | |
254 | """ |
|
254 | """ | |
255 | Returns content of the file at given ``path``. |
|
255 | Returns content of the file at given ``path``. | |
256 | """ |
|
256 | """ | |
257 | fctx = self._get_filectx(path) |
|
257 | fctx = self._get_filectx(path) | |
258 | return fctx.data() |
|
258 | return fctx.data() | |
259 |
|
259 | |||
260 | def get_file_size(self, path): |
|
260 | def get_file_size(self, path): | |
261 | """ |
|
261 | """ | |
262 | Returns size of the file at given ``path``. |
|
262 | Returns size of the file at given ``path``. | |
263 | """ |
|
263 | """ | |
264 | fctx = self._get_filectx(path) |
|
264 | fctx = self._get_filectx(path) | |
265 | return fctx.size() |
|
265 | return fctx.size() | |
266 |
|
266 | |||
267 | def get_file_changeset(self, path): |
|
267 | def get_file_changeset(self, path): | |
268 | """ |
|
268 | """ | |
269 | Returns last commit of the file at the given ``path``. |
|
269 | Returns last commit of the file at the given ``path``. | |
270 | """ |
|
270 | """ | |
271 | return self.get_file_history(path, limit=1)[0] |
|
271 | return self.get_file_history(path, limit=1)[0] | |
272 |
|
272 | |||
273 | def get_file_history(self, path, limit=None): |
|
273 | def get_file_history(self, path, limit=None): | |
274 | """ |
|
274 | """ | |
275 | Returns history of file as reversed list of ``Changeset`` objects for |
|
275 | Returns history of file as reversed list of ``Changeset`` objects for | |
276 | which file at given ``path`` has been modified. |
|
276 | which file at given ``path`` has been modified. | |
277 | """ |
|
277 | """ | |
278 | fctx = self._get_filectx(path) |
|
278 | fctx = self._get_filectx(path) | |
279 | hist = [] |
|
279 | hist = [] | |
280 | cnt = 0 |
|
280 | cnt = 0 | |
281 | for cs in reversed([x for x in fctx.filelog()]): |
|
281 | for cs in reversed([x for x in fctx.filelog()]): | |
282 | cnt += 1 |
|
282 | cnt += 1 | |
283 | hist.append(hex(fctx.filectx(cs).node())) |
|
283 | hist.append(hex(fctx.filectx(cs).node())) | |
284 | if limit is not None and cnt == limit: |
|
284 | if limit is not None and cnt == limit: | |
285 | break |
|
285 | break | |
286 |
|
286 | |||
287 | return [self.repository.get_changeset(node) for node in hist] |
|
287 | return [self.repository.get_changeset(node) for node in hist] | |
288 |
|
288 | |||
289 | def get_file_annotate(self, path): |
|
289 | def get_file_annotate(self, path): | |
290 | """ |
|
290 | """ | |
291 | Returns a generator of four element tuples with |
|
291 | Returns a generator of four element tuples with | |
292 | lineno, sha, changeset lazy loader and line |
|
292 | lineno, sha, changeset lazy loader and line | |
293 | """ |
|
293 | """ | |
294 | annotations = self._get_filectx(path).annotate() |
|
294 | annotations = self._get_filectx(path).annotate() | |
295 | try: |
|
295 | try: | |
296 | annotation_lines = [(annotateline.fctx, annotateline.text) for annotateline in annotations] |
|
296 | annotation_lines = [(annotateline.fctx, annotateline.text) for annotateline in annotations] | |
297 | except AttributeError: # annotateline was introduced in Mercurial 4.6 (b33b91ca2ec2) |
|
297 | except AttributeError: # annotateline was introduced in Mercurial 4.6 (b33b91ca2ec2) | |
298 | annotation_lines = [(aline.fctx, l) for aline, l in annotations] |
|
298 | annotation_lines = [(aline.fctx, l) for aline, l in annotations] | |
299 | for i, (fctx, l) in enumerate(annotation_lines): |
|
299 | for i, (fctx, l) in enumerate(annotation_lines): | |
300 | sha = fctx.hex() |
|
300 | sha = fctx.hex() | |
301 | yield (i + 1, sha, lambda sha=sha, l=l: self.repository.get_changeset(sha), l) |
|
301 | yield (i + 1, sha, lambda sha=sha, l=l: self.repository.get_changeset(sha), l) | |
302 |
|
302 | |||
303 | def fill_archive(self, stream=None, kind='tgz', prefix=None, |
|
303 | def fill_archive(self, stream=None, kind='tgz', prefix=None, | |
304 | subrepos=False): |
|
304 | subrepos=False): | |
305 | """ |
|
305 | """ | |
306 | Fills up given stream. |
|
306 | Fills up given stream. | |
307 |
|
307 | |||
308 | :param stream: file like object. |
|
308 | :param stream: file like object. | |
309 | :param kind: one of following: ``zip``, ``tgz`` or ``tbz2``. |
|
309 | :param kind: one of following: ``zip``, ``tgz`` or ``tbz2``. | |
310 | Default: ``tgz``. |
|
310 | Default: ``tgz``. | |
311 | :param prefix: name of root directory in archive. |
|
311 | :param prefix: name of root directory in archive. | |
312 | Default is repository name and changeset's raw_id joined with dash |
|
312 | Default is repository name and changeset's raw_id joined with dash | |
313 | (``repo-tip.<KIND>``). |
|
313 | (``repo-tip.<KIND>``). | |
314 | :param subrepos: include subrepos in this archive. |
|
314 | :param subrepos: include subrepos in this archive. | |
315 |
|
315 | |||
316 | :raise ImproperArchiveTypeError: If given kind is wrong. |
|
316 | :raise ImproperArchiveTypeError: If given kind is wrong. | |
317 | :raise VcsError: If given stream is None |
|
317 | :raise VcsError: If given stream is None | |
318 | """ |
|
318 | """ | |
319 |
|
319 | |||
320 | allowed_kinds = settings.ARCHIVE_SPECS.keys() |
|
320 | allowed_kinds = settings.ARCHIVE_SPECS.keys() | |
321 | if kind not in allowed_kinds: |
|
321 | if kind not in allowed_kinds: | |
322 | raise ImproperArchiveTypeError('Archive kind not supported use one' |
|
322 | raise ImproperArchiveTypeError('Archive kind not supported use one' | |
323 | 'of %s' % allowed_kinds) |
|
323 | 'of %s' % allowed_kinds) | |
324 |
|
324 | |||
325 | if stream is None: |
|
325 | if stream is None: | |
326 | raise VCSError('You need to pass in a valid stream for filling' |
|
326 | raise VCSError('You need to pass in a valid stream for filling' | |
327 | ' with archival data') |
|
327 | ' with archival data') | |
328 |
|
328 | |||
329 | if prefix is None: |
|
329 | if prefix is None: | |
330 | prefix = '%s-%s' % (self.repository.name, self.short_id) |
|
330 | prefix = '%s-%s' % (self.repository.name, self.short_id) | |
331 | elif prefix.startswith('/'): |
|
331 | elif prefix.startswith('/'): | |
332 | raise VCSError("Prefix cannot start with leading slash") |
|
332 | raise VCSError("Prefix cannot start with leading slash") | |
333 | elif prefix.strip() == '': |
|
333 | elif prefix.strip() == '': | |
334 | raise VCSError("Prefix cannot be empty") |
|
334 | raise VCSError("Prefix cannot be empty") | |
335 |
|
335 | |||
336 | archival.archive(self.repository._repo, stream, self.raw_id, |
|
336 | archival.archive(self.repository._repo, stream, self.raw_id, | |
337 | kind, prefix=prefix, subrepos=subrepos) |
|
337 | kind, prefix=prefix, subrepos=subrepos) | |
338 |
|
338 | |||
339 | def get_nodes(self, path): |
|
339 | def get_nodes(self, path): | |
340 | """ |
|
340 | """ | |
341 | Returns combined ``DirNode`` and ``FileNode`` objects list representing |
|
341 | Returns combined ``DirNode`` and ``FileNode`` objects list representing | |
342 | state of changeset at the given ``path``. If node at the given ``path`` |
|
342 | state of changeset at the given ``path``. If node at the given ``path`` | |
343 | is not instance of ``DirNode``, ChangesetError would be raised. |
|
343 | is not instance of ``DirNode``, ChangesetError would be raised. | |
344 | """ |
|
344 | """ | |
345 |
|
345 | |||
346 | if self._get_kind(path) != NodeKind.DIR: |
|
346 | if self._get_kind(path) != NodeKind.DIR: | |
347 | raise ChangesetError("Directory does not exist for revision %s at " |
|
347 | raise ChangesetError("Directory does not exist for revision %s at " | |
348 | " '%s'" % (self.revision, path)) |
|
348 | " '%s'" % (self.revision, path)) | |
349 | path = self._fix_path(path) |
|
349 | path = self._fix_path(path) | |
350 |
|
350 | |||
351 | filenodes = [FileNode(f, changeset=self) for f in self._file_paths |
|
351 | filenodes = [FileNode(f, changeset=self) for f in self._file_paths | |
352 | if os.path.dirname(f) == path] |
|
352 | if os.path.dirname(f) == path] | |
353 | dirs = path == '' and '' or [d for d in self._dir_paths |
|
353 | dirs = path == '' and '' or [d for d in self._dir_paths | |
354 | if d and posixpath.dirname(d) == path] |
|
354 | if d and posixpath.dirname(d) == path] | |
355 | dirnodes = [DirNode(d, changeset=self) for d in dirs |
|
355 | dirnodes = [DirNode(d, changeset=self) for d in dirs | |
356 | if os.path.dirname(d) == path] |
|
356 | if os.path.dirname(d) == path] | |
357 |
|
357 | |||
358 | als = self.repository.alias |
|
358 | als = self.repository.alias | |
359 | for k, vals in self._extract_submodules().iteritems(): |
|
359 | for k, vals in self._extract_submodules().iteritems(): | |
360 | #vals = url,rev,type |
|
360 | #vals = url,rev,type | |
361 | loc = vals[0] |
|
361 | loc = vals[0] | |
362 | cs = vals[1] |
|
362 | cs = vals[1] | |
363 | dirnodes.append(SubModuleNode(k, url=loc, changeset=cs, |
|
363 | dirnodes.append(SubModuleNode(k, url=loc, changeset=cs, | |
364 | alias=als)) |
|
364 | alias=als)) | |
365 | nodes = dirnodes + filenodes |
|
365 | nodes = dirnodes + filenodes | |
366 | # cache nodes |
|
366 | # cache nodes | |
367 | for node in nodes: |
|
367 | for node in nodes: | |
368 | self.nodes[node.path] = node |
|
368 | self.nodes[node.path] = node | |
369 | nodes.sort() |
|
369 | nodes.sort() | |
370 |
|
370 | |||
371 | return nodes |
|
371 | return nodes | |
372 |
|
372 | |||
373 | def get_node(self, path): |
|
373 | def get_node(self, path): | |
374 | """ |
|
374 | """ | |
375 | Returns ``Node`` object from the given ``path``. If there is no node at |
|
375 | Returns ``Node`` object from the given ``path``. If there is no node at | |
376 | the given ``path``, ``ChangesetError`` would be raised. |
|
376 | the given ``path``, ``ChangesetError`` would be raised. | |
377 | """ |
|
377 | """ | |
378 |
|
378 | |||
379 | path = self._fix_path(path) |
|
379 | path = self._fix_path(path) | |
380 |
|
380 | |||
381 | if path not in self.nodes: |
|
381 | if path not in self.nodes: | |
382 | if path in self._file_paths: |
|
382 | if path in self._file_paths: | |
383 | node = FileNode(path, changeset=self) |
|
383 | node = FileNode(path, changeset=self) | |
384 | elif path in self._dir_paths or path in self._dir_paths: |
|
384 | elif path in self._dir_paths or path in self._dir_paths: | |
385 | if path == '': |
|
385 | if path == '': | |
386 | node = RootNode(changeset=self) |
|
386 | node = RootNode(changeset=self) | |
387 | else: |
|
387 | else: | |
388 | node = DirNode(path, changeset=self) |
|
388 | node = DirNode(path, changeset=self) | |
389 | else: |
|
389 | else: | |
390 | raise NodeDoesNotExistError("There is no file nor directory " |
|
390 | raise NodeDoesNotExistError("There is no file nor directory " | |
391 | "at the given path: '%s' at revision %s" |
|
391 | "at the given path: '%s' at revision %s" | |
392 | % (path, self.short_id)) |
|
392 | % (path, self.short_id)) | |
393 | # cache node |
|
393 | # cache node | |
394 | self.nodes[path] = node |
|
394 | self.nodes[path] = node | |
395 | return self.nodes[path] |
|
395 | return self.nodes[path] | |
396 |
|
396 | |||
397 | @LazyProperty |
|
397 | @LazyProperty | |
398 | def affected_files(self): |
|
398 | def affected_files(self): | |
399 | """ |
|
399 | """ | |
400 | Gets a fast accessible file changes for given changeset |
|
400 | Gets a fast accessible file changes for given changeset | |
401 | """ |
|
401 | """ | |
402 | return self._ctx.files() |
|
402 | return self._ctx.files() | |
403 |
|
403 | |||
404 | @property |
|
404 | @property | |
405 | def added(self): |
|
405 | def added(self): | |
406 | """ |
|
406 | """ | |
407 | Returns list of added ``FileNode`` objects. |
|
407 | Returns list of added ``FileNode`` objects. | |
408 | """ |
|
408 | """ | |
409 | return AddedFileNodesGenerator([n for n in self.status[1]], self) |
|
409 | return AddedFileNodesGenerator([n for n in self.status[1]], self) | |
410 |
|
410 | |||
411 | @property |
|
411 | @property | |
412 | def changed(self): |
|
412 | def changed(self): | |
413 | """ |
|
413 | """ | |
414 | Returns list of modified ``FileNode`` objects. |
|
414 | Returns list of modified ``FileNode`` objects. | |
415 | """ |
|
415 | """ | |
416 | return ChangedFileNodesGenerator([n for n in self.status[0]], self) |
|
416 | return ChangedFileNodesGenerator([n for n in self.status[0]], self) | |
417 |
|
417 | |||
418 | @property |
|
418 | @property | |
419 | def removed(self): |
|
419 | def removed(self): | |
420 | """ |
|
420 | """ | |
421 | Returns list of removed ``FileNode`` objects. |
|
421 | Returns list of removed ``FileNode`` objects. | |
422 | """ |
|
422 | """ | |
423 | return RemovedFileNodesGenerator([n for n in self.status[2]], self) |
|
423 | return RemovedFileNodesGenerator([n for n in self.status[2]], self) | |
424 |
|
424 | |||
425 | @LazyProperty |
|
425 | @LazyProperty | |
426 | def extra(self): |
|
426 | def extra(self): | |
427 | return self._ctx.extra() |
|
427 | return self._ctx.extra() |
@@ -1,2548 +1,2547 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | # This program is free software: you can redistribute it and/or modify |
|
2 | # This program is free software: you can redistribute it and/or modify | |
3 | # it under the terms of the GNU General Public License as published by |
|
3 | # it under the terms of the GNU General Public License as published by | |
4 | # the Free Software Foundation, either version 3 of the License, or |
|
4 | # the Free Software Foundation, either version 3 of the License, or | |
5 | # (at your option) any later version. |
|
5 | # (at your option) any later version. | |
6 | # |
|
6 | # | |
7 | # This program is distributed in the hope that it will be useful, |
|
7 | # This program is distributed in the hope that it will be useful, | |
8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
10 | # GNU General Public License for more details. |
|
10 | # GNU General Public License for more details. | |
11 | # |
|
11 | # | |
12 | # You should have received a copy of the GNU General Public License |
|
12 | # You should have received a copy of the GNU General Public License | |
13 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
13 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
14 | """ |
|
14 | """ | |
15 | kallithea.model.db |
|
15 | kallithea.model.db | |
16 | ~~~~~~~~~~~~~~~~~~ |
|
16 | ~~~~~~~~~~~~~~~~~~ | |
17 |
|
17 | |||
18 | Database Models for Kallithea |
|
18 | Database Models for Kallithea | |
19 |
|
19 | |||
20 | This file was forked by the Kallithea project in July 2014. |
|
20 | This file was forked by the Kallithea project in July 2014. | |
21 | Original author and date, and relevant copyright and licensing information is below: |
|
21 | Original author and date, and relevant copyright and licensing information is below: | |
22 | :created_on: Apr 08, 2010 |
|
22 | :created_on: Apr 08, 2010 | |
23 | :author: marcink |
|
23 | :author: marcink | |
24 | :copyright: (c) 2013 RhodeCode GmbH, and others. |
|
24 | :copyright: (c) 2013 RhodeCode GmbH, and others. | |
25 | :license: GPLv3, see LICENSE.md for more details. |
|
25 | :license: GPLv3, see LICENSE.md for more details. | |
26 | """ |
|
26 | """ | |
27 |
|
27 | |||
28 | import collections |
|
28 | import collections | |
29 | import datetime |
|
29 | import datetime | |
30 | import functools |
|
30 | import functools | |
31 | import hashlib |
|
31 | import hashlib | |
32 | import logging |
|
32 | import logging | |
33 | import os |
|
33 | import os | |
34 | import time |
|
34 | import time | |
35 | import traceback |
|
35 | import traceback | |
36 |
|
36 | |||
37 | import ipaddr |
|
37 | import ipaddr | |
38 | import sqlalchemy |
|
38 | import sqlalchemy | |
39 | from beaker.cache import cache_region, region_invalidate |
|
39 | from beaker.cache import cache_region, region_invalidate | |
40 | from sqlalchemy import * |
|
40 | from sqlalchemy import * | |
41 | from sqlalchemy.ext.hybrid import hybrid_property |
|
41 | from sqlalchemy.ext.hybrid import hybrid_property | |
42 | from sqlalchemy.orm import class_mapper, joinedload, relationship, validates |
|
42 | from sqlalchemy.orm import class_mapper, joinedload, relationship, validates | |
43 | from tg.i18n import lazy_ugettext as _ |
|
43 | from tg.i18n import lazy_ugettext as _ | |
44 | from webob.exc import HTTPNotFound |
|
44 | from webob.exc import HTTPNotFound | |
45 |
|
45 | |||
46 | import kallithea |
|
46 | import kallithea | |
47 | from kallithea.lib.caching_query import FromCache |
|
47 | from kallithea.lib.caching_query import FromCache | |
48 | from kallithea.lib.compat import json |
|
48 | from kallithea.lib.compat import json | |
49 | from kallithea.lib.exceptions import DefaultUserException |
|
49 | from kallithea.lib.exceptions import DefaultUserException | |
50 | from kallithea.lib.utils2 import Optional, aslist, get_changeset_safe, get_clone_url, remove_prefix, safe_int, safe_str, safe_unicode, str2bool, urlreadable |
|
50 | from kallithea.lib.utils2 import Optional, aslist, get_changeset_safe, get_clone_url, remove_prefix, safe_int, safe_str, safe_unicode, str2bool, urlreadable | |
51 | from kallithea.lib.vcs import get_backend |
|
51 | from kallithea.lib.vcs import get_backend | |
52 | from kallithea.lib.vcs.backends.base import EmptyChangeset |
|
52 | from kallithea.lib.vcs.backends.base import EmptyChangeset | |
53 | from kallithea.lib.vcs.utils.helpers import get_scm |
|
53 | from kallithea.lib.vcs.utils.helpers import get_scm | |
54 | from kallithea.lib.vcs.utils.lazy import LazyProperty |
|
54 | from kallithea.lib.vcs.utils.lazy import LazyProperty | |
55 | from kallithea.model.meta import Base, Session |
|
55 | from kallithea.model.meta import Base, Session | |
56 |
|
56 | |||
57 |
|
57 | |||
58 | URL_SEP = '/' |
|
58 | URL_SEP = '/' | |
59 | log = logging.getLogger(__name__) |
|
59 | log = logging.getLogger(__name__) | |
60 |
|
60 | |||
61 | #============================================================================== |
|
61 | #============================================================================== | |
62 | # BASE CLASSES |
|
62 | # BASE CLASSES | |
63 | #============================================================================== |
|
63 | #============================================================================== | |
64 |
|
64 | |||
65 | _hash_key = lambda k: hashlib.md5(safe_str(k)).hexdigest() |
|
65 | _hash_key = lambda k: hashlib.md5(safe_str(k)).hexdigest() | |
66 |
|
66 | |||
67 |
|
67 | |||
68 | class BaseDbModel(object): |
|
68 | class BaseDbModel(object): | |
69 | """ |
|
69 | """ | |
70 | Base Model for all classes |
|
70 | Base Model for all classes | |
71 | """ |
|
71 | """ | |
72 |
|
72 | |||
73 | @classmethod |
|
73 | @classmethod | |
74 | def _get_keys(cls): |
|
74 | def _get_keys(cls): | |
75 | """return column names for this model """ |
|
75 | """return column names for this model """ | |
76 | return class_mapper(cls).c.keys() |
|
76 | return class_mapper(cls).c.keys() | |
77 |
|
77 | |||
78 | def get_dict(self): |
|
78 | def get_dict(self): | |
79 | """ |
|
79 | """ | |
80 | return dict with keys and values corresponding |
|
80 | return dict with keys and values corresponding | |
81 | to this model data """ |
|
81 | to this model data """ | |
82 |
|
82 | |||
83 | d = {} |
|
83 | d = {} | |
84 | for k in self._get_keys(): |
|
84 | for k in self._get_keys(): | |
85 | d[k] = getattr(self, k) |
|
85 | d[k] = getattr(self, k) | |
86 |
|
86 | |||
87 | # also use __json__() if present to get additional fields |
|
87 | # also use __json__() if present to get additional fields | |
88 | _json_attr = getattr(self, '__json__', None) |
|
88 | _json_attr = getattr(self, '__json__', None) | |
89 | if _json_attr: |
|
89 | if _json_attr: | |
90 | # update with attributes from __json__ |
|
90 | # update with attributes from __json__ | |
91 | if callable(_json_attr): |
|
91 | if callable(_json_attr): | |
92 | _json_attr = _json_attr() |
|
92 | _json_attr = _json_attr() | |
93 | for k, val in _json_attr.iteritems(): |
|
93 | for k, val in _json_attr.iteritems(): | |
94 | d[k] = val |
|
94 | d[k] = val | |
95 | return d |
|
95 | return d | |
96 |
|
96 | |||
97 | def get_appstruct(self): |
|
97 | def get_appstruct(self): | |
98 | """return list with keys and values tuples corresponding |
|
98 | """return list with keys and values tuples corresponding | |
99 | to this model data """ |
|
99 | to this model data """ | |
100 |
|
100 | |||
101 | return [ |
|
101 | return [ | |
102 | (k, getattr(self, k)) |
|
102 | (k, getattr(self, k)) | |
103 | for k in self._get_keys() |
|
103 | for k in self._get_keys() | |
104 | ] |
|
104 | ] | |
105 |
|
105 | |||
106 | def populate_obj(self, populate_dict): |
|
106 | def populate_obj(self, populate_dict): | |
107 | """populate model with data from given populate_dict""" |
|
107 | """populate model with data from given populate_dict""" | |
108 |
|
108 | |||
109 | for k in self._get_keys(): |
|
109 | for k in self._get_keys(): | |
110 | if k in populate_dict: |
|
110 | if k in populate_dict: | |
111 | setattr(self, k, populate_dict[k]) |
|
111 | setattr(self, k, populate_dict[k]) | |
112 |
|
112 | |||
113 | @classmethod |
|
113 | @classmethod | |
114 | def query(cls): |
|
114 | def query(cls): | |
115 | return Session().query(cls) |
|
115 | return Session().query(cls) | |
116 |
|
116 | |||
117 | @classmethod |
|
117 | @classmethod | |
118 | def get(cls, id_): |
|
118 | def get(cls, id_): | |
119 | if id_: |
|
119 | if id_: | |
120 | return cls.query().get(id_) |
|
120 | return cls.query().get(id_) | |
121 |
|
121 | |||
122 | @classmethod |
|
122 | @classmethod | |
123 | def guess_instance(cls, value, callback=None): |
|
123 | def guess_instance(cls, value, callback=None): | |
124 | """Haphazardly attempt to convert `value` to a `cls` instance. |
|
124 | """Haphazardly attempt to convert `value` to a `cls` instance. | |
125 |
|
125 | |||
126 | If `value` is None or already a `cls` instance, return it. If `value` |
|
126 | If `value` is None or already a `cls` instance, return it. If `value` | |
127 | is a number (or looks like one if you squint just right), assume it's |
|
127 | is a number (or looks like one if you squint just right), assume it's | |
128 | a database primary key and let SQLAlchemy sort things out. Otherwise, |
|
128 | a database primary key and let SQLAlchemy sort things out. Otherwise, | |
129 | fall back to resolving it using `callback` (if specified); this could |
|
129 | fall back to resolving it using `callback` (if specified); this could | |
130 | e.g. be a function that looks up instances by name (though that won't |
|
130 | e.g. be a function that looks up instances by name (though that won't | |
131 | work if the name begins with a digit). Otherwise, raise Exception. |
|
131 | work if the name begins with a digit). Otherwise, raise Exception. | |
132 | """ |
|
132 | """ | |
133 |
|
133 | |||
134 | if value is None: |
|
134 | if value is None: | |
135 | return None |
|
135 | return None | |
136 | if isinstance(value, cls): |
|
136 | if isinstance(value, cls): | |
137 | return value |
|
137 | return value | |
138 | if isinstance(value, (int, long)) or safe_str(value).isdigit(): |
|
138 | if isinstance(value, (int, long)) or safe_str(value).isdigit(): | |
139 | return cls.get(value) |
|
139 | return cls.get(value) | |
140 | if callback is not None: |
|
140 | if callback is not None: | |
141 | return callback(value) |
|
141 | return callback(value) | |
142 |
|
142 | |||
143 | raise Exception( |
|
143 | raise Exception( | |
144 | 'given object must be int, long or Instance of %s ' |
|
144 | 'given object must be int, long or Instance of %s ' | |
145 | 'got %s, no callback provided' % (cls, type(value)) |
|
145 | 'got %s, no callback provided' % (cls, type(value)) | |
146 | ) |
|
146 | ) | |
147 |
|
147 | |||
148 | @classmethod |
|
148 | @classmethod | |
149 | def get_or_404(cls, id_): |
|
149 | def get_or_404(cls, id_): | |
150 | try: |
|
150 | try: | |
151 | id_ = int(id_) |
|
151 | id_ = int(id_) | |
152 | except (TypeError, ValueError): |
|
152 | except (TypeError, ValueError): | |
153 | raise HTTPNotFound |
|
153 | raise HTTPNotFound | |
154 |
|
154 | |||
155 | res = cls.query().get(id_) |
|
155 | res = cls.query().get(id_) | |
156 | if res is None: |
|
156 | if res is None: | |
157 | raise HTTPNotFound |
|
157 | raise HTTPNotFound | |
158 | return res |
|
158 | return res | |
159 |
|
159 | |||
160 | @classmethod |
|
160 | @classmethod | |
161 | def delete(cls, id_): |
|
161 | def delete(cls, id_): | |
162 | obj = cls.query().get(id_) |
|
162 | obj = cls.query().get(id_) | |
163 | Session().delete(obj) |
|
163 | Session().delete(obj) | |
164 |
|
164 | |||
165 | def __repr__(self): |
|
165 | def __repr__(self): | |
166 | if hasattr(self, '__unicode__'): |
|
166 | if hasattr(self, '__unicode__'): | |
167 | # python repr needs to return str |
|
167 | # python repr needs to return str | |
168 | try: |
|
168 | try: | |
169 | return safe_str(self.__unicode__()) |
|
169 | return safe_str(self.__unicode__()) | |
170 | except UnicodeDecodeError: |
|
170 | except UnicodeDecodeError: | |
171 | pass |
|
171 | pass | |
172 | return '<DB:%s>' % (self.__class__.__name__) |
|
172 | return '<DB:%s>' % (self.__class__.__name__) | |
173 |
|
173 | |||
174 |
|
174 | |||
175 | _table_args_default_dict = {'extend_existing': True, |
|
175 | _table_args_default_dict = {'extend_existing': True, | |
176 | 'mysql_engine': 'InnoDB', |
|
176 | 'mysql_engine': 'InnoDB', | |
177 | 'mysql_charset': 'utf8', |
|
177 | 'mysql_charset': 'utf8', | |
178 | 'sqlite_autoincrement': True, |
|
178 | 'sqlite_autoincrement': True, | |
179 | } |
|
179 | } | |
180 |
|
180 | |||
181 | class Setting(Base, BaseDbModel): |
|
181 | class Setting(Base, BaseDbModel): | |
182 | __tablename__ = 'settings' |
|
182 | __tablename__ = 'settings' | |
183 | __table_args__ = ( |
|
183 | __table_args__ = ( | |
184 | _table_args_default_dict, |
|
184 | _table_args_default_dict, | |
185 | ) |
|
185 | ) | |
186 |
|
186 | |||
187 | SETTINGS_TYPES = { |
|
187 | SETTINGS_TYPES = { | |
188 | 'str': safe_str, |
|
188 | 'str': safe_str, | |
189 | 'int': safe_int, |
|
189 | 'int': safe_int, | |
190 | 'unicode': safe_unicode, |
|
190 | 'unicode': safe_unicode, | |
191 | 'bool': str2bool, |
|
191 | 'bool': str2bool, | |
192 | 'list': functools.partial(aslist, sep=',') |
|
192 | 'list': functools.partial(aslist, sep=',') | |
193 | } |
|
193 | } | |
194 | DEFAULT_UPDATE_URL = '' |
|
194 | DEFAULT_UPDATE_URL = '' | |
195 |
|
195 | |||
196 | app_settings_id = Column(Integer(), primary_key=True) |
|
196 | app_settings_id = Column(Integer(), primary_key=True) | |
197 | app_settings_name = Column(String(255), nullable=False, unique=True) |
|
197 | app_settings_name = Column(String(255), nullable=False, unique=True) | |
198 | _app_settings_value = Column("app_settings_value", Unicode(4096), nullable=False) |
|
198 | _app_settings_value = Column("app_settings_value", Unicode(4096), nullable=False) | |
199 | _app_settings_type = Column("app_settings_type", String(255), nullable=True) # FIXME: not nullable? |
|
199 | _app_settings_type = Column("app_settings_type", String(255), nullable=True) # FIXME: not nullable? | |
200 |
|
200 | |||
201 | def __init__(self, key='', val='', type='unicode'): |
|
201 | def __init__(self, key='', val='', type='unicode'): | |
202 | self.app_settings_name = key |
|
202 | self.app_settings_name = key | |
203 | self.app_settings_value = val |
|
203 | self.app_settings_value = val | |
204 | self.app_settings_type = type |
|
204 | self.app_settings_type = type | |
205 |
|
205 | |||
206 | @validates('_app_settings_value') |
|
206 | @validates('_app_settings_value') | |
207 | def validate_settings_value(self, key, val): |
|
207 | def validate_settings_value(self, key, val): | |
208 | assert isinstance(val, unicode) |
|
208 | assert isinstance(val, unicode) | |
209 | return val |
|
209 | return val | |
210 |
|
210 | |||
211 | @hybrid_property |
|
211 | @hybrid_property | |
212 | def app_settings_value(self): |
|
212 | def app_settings_value(self): | |
213 | v = self._app_settings_value |
|
213 | v = self._app_settings_value | |
214 | _type = self.app_settings_type |
|
214 | _type = self.app_settings_type | |
215 | converter = self.SETTINGS_TYPES.get(_type) or self.SETTINGS_TYPES['unicode'] |
|
215 | converter = self.SETTINGS_TYPES.get(_type) or self.SETTINGS_TYPES['unicode'] | |
216 | return converter(v) |
|
216 | return converter(v) | |
217 |
|
217 | |||
218 | @app_settings_value.setter |
|
218 | @app_settings_value.setter | |
219 | def app_settings_value(self, val): |
|
219 | def app_settings_value(self, val): | |
220 | """ |
|
220 | """ | |
221 | Setter that will always make sure we use unicode in app_settings_value |
|
221 | Setter that will always make sure we use unicode in app_settings_value | |
222 |
|
222 | |||
223 | :param val: |
|
223 | :param val: | |
224 | """ |
|
224 | """ | |
225 | self._app_settings_value = safe_unicode(val) |
|
225 | self._app_settings_value = safe_unicode(val) | |
226 |
|
226 | |||
227 | @hybrid_property |
|
227 | @hybrid_property | |
228 | def app_settings_type(self): |
|
228 | def app_settings_type(self): | |
229 | return self._app_settings_type |
|
229 | return self._app_settings_type | |
230 |
|
230 | |||
231 | @app_settings_type.setter |
|
231 | @app_settings_type.setter | |
232 | def app_settings_type(self, val): |
|
232 | def app_settings_type(self, val): | |
233 | if val not in self.SETTINGS_TYPES: |
|
233 | if val not in self.SETTINGS_TYPES: | |
234 | raise Exception('type must be one of %s got %s' |
|
234 | raise Exception('type must be one of %s got %s' | |
235 | % (self.SETTINGS_TYPES.keys(), val)) |
|
235 | % (self.SETTINGS_TYPES.keys(), val)) | |
236 | self._app_settings_type = val |
|
236 | self._app_settings_type = val | |
237 |
|
237 | |||
238 | def __unicode__(self): |
|
238 | def __unicode__(self): | |
239 | return u"<%s('%s:%s[%s]')>" % ( |
|
239 | return u"<%s('%s:%s[%s]')>" % ( | |
240 | self.__class__.__name__, |
|
240 | self.__class__.__name__, | |
241 | self.app_settings_name, self.app_settings_value, self.app_settings_type |
|
241 | self.app_settings_name, self.app_settings_value, self.app_settings_type | |
242 | ) |
|
242 | ) | |
243 |
|
243 | |||
244 | @classmethod |
|
244 | @classmethod | |
245 | def get_by_name(cls, key): |
|
245 | def get_by_name(cls, key): | |
246 | return cls.query() \ |
|
246 | return cls.query() \ | |
247 | .filter(cls.app_settings_name == key).scalar() |
|
247 | .filter(cls.app_settings_name == key).scalar() | |
248 |
|
248 | |||
249 | @classmethod |
|
249 | @classmethod | |
250 | def get_by_name_or_create(cls, key, val='', type='unicode'): |
|
250 | def get_by_name_or_create(cls, key, val='', type='unicode'): | |
251 | res = cls.get_by_name(key) |
|
251 | res = cls.get_by_name(key) | |
252 | if res is None: |
|
252 | if res is None: | |
253 | res = cls(key, val, type) |
|
253 | res = cls(key, val, type) | |
254 | return res |
|
254 | return res | |
255 |
|
255 | |||
256 | @classmethod |
|
256 | @classmethod | |
257 | def create_or_update(cls, key, val=Optional(''), type=Optional('unicode')): |
|
257 | def create_or_update(cls, key, val=Optional(''), type=Optional('unicode')): | |
258 | """ |
|
258 | """ | |
259 | Creates or updates Kallithea setting. If updates are triggered, it will only |
|
259 | Creates or updates Kallithea setting. If updates are triggered, it will only | |
260 | update parameters that are explicitly set. Optional instance will be skipped. |
|
260 | update parameters that are explicitly set. Optional instance will be skipped. | |
261 |
|
261 | |||
262 | :param key: |
|
262 | :param key: | |
263 | :param val: |
|
263 | :param val: | |
264 | :param type: |
|
264 | :param type: | |
265 | :return: |
|
265 | :return: | |
266 | """ |
|
266 | """ | |
267 | res = cls.get_by_name(key) |
|
267 | res = cls.get_by_name(key) | |
268 | if res is None: |
|
268 | if res is None: | |
269 | val = Optional.extract(val) |
|
269 | val = Optional.extract(val) | |
270 | type = Optional.extract(type) |
|
270 | type = Optional.extract(type) | |
271 | res = cls(key, val, type) |
|
271 | res = cls(key, val, type) | |
272 | Session().add(res) |
|
272 | Session().add(res) | |
273 | else: |
|
273 | else: | |
274 | res.app_settings_name = key |
|
274 | res.app_settings_name = key | |
275 | if not isinstance(val, Optional): |
|
275 | if not isinstance(val, Optional): | |
276 | # update if set |
|
276 | # update if set | |
277 | res.app_settings_value = val |
|
277 | res.app_settings_value = val | |
278 | if not isinstance(type, Optional): |
|
278 | if not isinstance(type, Optional): | |
279 | # update if set |
|
279 | # update if set | |
280 | res.app_settings_type = type |
|
280 | res.app_settings_type = type | |
281 | return res |
|
281 | return res | |
282 |
|
282 | |||
283 | @classmethod |
|
283 | @classmethod | |
284 | def get_app_settings(cls, cache=False): |
|
284 | def get_app_settings(cls, cache=False): | |
285 |
|
285 | |||
286 | ret = cls.query() |
|
286 | ret = cls.query() | |
287 |
|
287 | |||
288 | if cache: |
|
288 | if cache: | |
289 | ret = ret.options(FromCache("sql_cache_short", "get_hg_settings")) |
|
289 | ret = ret.options(FromCache("sql_cache_short", "get_hg_settings")) | |
290 |
|
290 | |||
291 | if ret is None: |
|
291 | if ret is None: | |
292 | raise Exception('Could not get application settings !') |
|
292 | raise Exception('Could not get application settings !') | |
293 | settings = {} |
|
293 | settings = {} | |
294 | for each in ret: |
|
294 | for each in ret: | |
295 | settings[each.app_settings_name] = \ |
|
295 | settings[each.app_settings_name] = \ | |
296 | each.app_settings_value |
|
296 | each.app_settings_value | |
297 |
|
297 | |||
298 | return settings |
|
298 | return settings | |
299 |
|
299 | |||
300 | @classmethod |
|
300 | @classmethod | |
301 | def get_auth_settings(cls, cache=False): |
|
301 | def get_auth_settings(cls, cache=False): | |
302 | ret = cls.query() \ |
|
302 | ret = cls.query() \ | |
303 | .filter(cls.app_settings_name.startswith('auth_')).all() |
|
303 | .filter(cls.app_settings_name.startswith('auth_')).all() | |
304 | fd = {} |
|
304 | fd = {} | |
305 | for row in ret: |
|
305 | for row in ret: | |
306 | fd[row.app_settings_name] = row.app_settings_value |
|
306 | fd[row.app_settings_name] = row.app_settings_value | |
307 | return fd |
|
307 | return fd | |
308 |
|
308 | |||
309 | @classmethod |
|
309 | @classmethod | |
310 | def get_default_repo_settings(cls, cache=False, strip_prefix=False): |
|
310 | def get_default_repo_settings(cls, cache=False, strip_prefix=False): | |
311 | ret = cls.query() \ |
|
311 | ret = cls.query() \ | |
312 | .filter(cls.app_settings_name.startswith('default_')).all() |
|
312 | .filter(cls.app_settings_name.startswith('default_')).all() | |
313 | fd = {} |
|
313 | fd = {} | |
314 | for row in ret: |
|
314 | for row in ret: | |
315 | key = row.app_settings_name |
|
315 | key = row.app_settings_name | |
316 | if strip_prefix: |
|
316 | if strip_prefix: | |
317 | key = remove_prefix(key, prefix='default_') |
|
317 | key = remove_prefix(key, prefix='default_') | |
318 | fd.update({key: row.app_settings_value}) |
|
318 | fd.update({key: row.app_settings_value}) | |
319 |
|
319 | |||
320 | return fd |
|
320 | return fd | |
321 |
|
321 | |||
322 | @classmethod |
|
322 | @classmethod | |
323 | def get_server_info(cls): |
|
323 | def get_server_info(cls): | |
324 | import pkg_resources |
|
324 | import pkg_resources | |
325 | import platform |
|
325 | import platform | |
326 | from kallithea.lib.utils import check_git_version |
|
326 | from kallithea.lib.utils import check_git_version | |
327 | mods = [(p.project_name, p.version) for p in pkg_resources.working_set] |
|
327 | mods = [(p.project_name, p.version) for p in pkg_resources.working_set] | |
328 | info = { |
|
328 | info = { | |
329 | 'modules': sorted(mods, key=lambda k: k[0].lower()), |
|
329 | 'modules': sorted(mods, key=lambda k: k[0].lower()), | |
330 | 'py_version': platform.python_version(), |
|
330 | 'py_version': platform.python_version(), | |
331 | 'platform': safe_unicode(platform.platform()), |
|
331 | 'platform': safe_unicode(platform.platform()), | |
332 | 'kallithea_version': kallithea.__version__, |
|
332 | 'kallithea_version': kallithea.__version__, | |
333 | 'git_version': safe_unicode(check_git_version()), |
|
333 | 'git_version': safe_unicode(check_git_version()), | |
334 | 'git_path': kallithea.CONFIG.get('git_path') |
|
334 | 'git_path': kallithea.CONFIG.get('git_path') | |
335 | } |
|
335 | } | |
336 | return info |
|
336 | return info | |
337 |
|
337 | |||
338 |
|
338 | |||
339 | class Ui(Base, BaseDbModel): |
|
339 | class Ui(Base, BaseDbModel): | |
340 | __tablename__ = 'ui' |
|
340 | __tablename__ = 'ui' | |
341 | __table_args__ = ( |
|
341 | __table_args__ = ( | |
342 | # FIXME: ui_key as key is wrong and should be removed when the corresponding |
|
342 | # FIXME: ui_key as key is wrong and should be removed when the corresponding | |
343 | # Ui.get_by_key has been replaced by the composite key |
|
343 | # Ui.get_by_key has been replaced by the composite key | |
344 | UniqueConstraint('ui_key'), |
|
344 | UniqueConstraint('ui_key'), | |
345 | UniqueConstraint('ui_section', 'ui_key'), |
|
345 | UniqueConstraint('ui_section', 'ui_key'), | |
346 | _table_args_default_dict, |
|
346 | _table_args_default_dict, | |
347 | ) |
|
347 | ) | |
348 |
|
348 | |||
349 | HOOK_UPDATE = 'changegroup.update' |
|
349 | HOOK_UPDATE = 'changegroup.update' | |
350 | HOOK_REPO_SIZE = 'changegroup.repo_size' |
|
350 | HOOK_REPO_SIZE = 'changegroup.repo_size' | |
351 |
|
351 | |||
352 | ui_id = Column(Integer(), primary_key=True) |
|
352 | ui_id = Column(Integer(), primary_key=True) | |
353 | ui_section = Column(String(255), nullable=False) |
|
353 | ui_section = Column(String(255), nullable=False) | |
354 | ui_key = Column(String(255), nullable=False) |
|
354 | ui_key = Column(String(255), nullable=False) | |
355 | ui_value = Column(String(255), nullable=True) # FIXME: not nullable? |
|
355 | ui_value = Column(String(255), nullable=True) # FIXME: not nullable? | |
356 | ui_active = Column(Boolean(), nullable=False, default=True) |
|
356 | ui_active = Column(Boolean(), nullable=False, default=True) | |
357 |
|
357 | |||
358 | @classmethod |
|
358 | @classmethod | |
359 | def get_by_key(cls, section, key): |
|
359 | def get_by_key(cls, section, key): | |
360 | """ Return specified Ui object, or None if not found. """ |
|
360 | """ Return specified Ui object, or None if not found. """ | |
361 | return cls.query().filter_by(ui_section=section, ui_key=key).scalar() |
|
361 | return cls.query().filter_by(ui_section=section, ui_key=key).scalar() | |
362 |
|
362 | |||
363 | @classmethod |
|
363 | @classmethod | |
364 | def get_or_create(cls, section, key): |
|
364 | def get_or_create(cls, section, key): | |
365 | """ Return specified Ui object, creating it if necessary. """ |
|
365 | """ Return specified Ui object, creating it if necessary. """ | |
366 | setting = cls.get_by_key(section, key) |
|
366 | setting = cls.get_by_key(section, key) | |
367 | if setting is None: |
|
367 | if setting is None: | |
368 | setting = cls(ui_section=section, ui_key=key) |
|
368 | setting = cls(ui_section=section, ui_key=key) | |
369 | Session().add(setting) |
|
369 | Session().add(setting) | |
370 | return setting |
|
370 | return setting | |
371 |
|
371 | |||
372 | @classmethod |
|
372 | @classmethod | |
373 | def get_builtin_hooks(cls): |
|
373 | def get_builtin_hooks(cls): | |
374 | q = cls.query() |
|
374 | q = cls.query() | |
375 | q = q.filter(cls.ui_key.in_([cls.HOOK_UPDATE, cls.HOOK_REPO_SIZE])) |
|
375 | q = q.filter(cls.ui_key.in_([cls.HOOK_UPDATE, cls.HOOK_REPO_SIZE])) | |
376 | q = q.filter(cls.ui_section == 'hooks') |
|
376 | q = q.filter(cls.ui_section == 'hooks') | |
377 | return q.all() |
|
377 | return q.all() | |
378 |
|
378 | |||
379 | @classmethod |
|
379 | @classmethod | |
380 | def get_custom_hooks(cls): |
|
380 | def get_custom_hooks(cls): | |
381 | q = cls.query() |
|
381 | q = cls.query() | |
382 | q = q.filter(~cls.ui_key.in_([cls.HOOK_UPDATE, cls.HOOK_REPO_SIZE])) |
|
382 | q = q.filter(~cls.ui_key.in_([cls.HOOK_UPDATE, cls.HOOK_REPO_SIZE])) | |
383 | q = q.filter(cls.ui_section == 'hooks') |
|
383 | q = q.filter(cls.ui_section == 'hooks') | |
384 | return q.all() |
|
384 | return q.all() | |
385 |
|
385 | |||
386 | @classmethod |
|
386 | @classmethod | |
387 | def get_repos_location(cls): |
|
387 | def get_repos_location(cls): | |
388 | return cls.get_by_key('paths', '/').ui_value |
|
388 | return cls.get_by_key('paths', '/').ui_value | |
389 |
|
389 | |||
390 | @classmethod |
|
390 | @classmethod | |
391 | def create_or_update_hook(cls, key, val): |
|
391 | def create_or_update_hook(cls, key, val): | |
392 | new_ui = cls.get_or_create('hooks', key) |
|
392 | new_ui = cls.get_or_create('hooks', key) | |
393 | new_ui.ui_active = True |
|
393 | new_ui.ui_active = True | |
394 | new_ui.ui_value = val |
|
394 | new_ui.ui_value = val | |
395 |
|
395 | |||
396 | def __repr__(self): |
|
396 | def __repr__(self): | |
397 | return '<%s[%s]%s=>%s]>' % (self.__class__.__name__, self.ui_section, |
|
397 | return '<%s[%s]%s=>%s]>' % (self.__class__.__name__, self.ui_section, | |
398 | self.ui_key, self.ui_value) |
|
398 | self.ui_key, self.ui_value) | |
399 |
|
399 | |||
400 |
|
400 | |||
401 | class User(Base, BaseDbModel): |
|
401 | class User(Base, BaseDbModel): | |
402 | __tablename__ = 'users' |
|
402 | __tablename__ = 'users' | |
403 | __table_args__ = ( |
|
403 | __table_args__ = ( | |
404 | Index('u_username_idx', 'username'), |
|
404 | Index('u_username_idx', 'username'), | |
405 | Index('u_email_idx', 'email'), |
|
405 | Index('u_email_idx', 'email'), | |
406 | _table_args_default_dict, |
|
406 | _table_args_default_dict, | |
407 | ) |
|
407 | ) | |
408 |
|
408 | |||
409 | DEFAULT_USER = 'default' |
|
409 | DEFAULT_USER = 'default' | |
410 | DEFAULT_GRAVATAR_URL = 'https://secure.gravatar.com/avatar/{md5email}?d=identicon&s={size}' |
|
410 | DEFAULT_GRAVATAR_URL = 'https://secure.gravatar.com/avatar/{md5email}?d=identicon&s={size}' | |
411 | # The name of the default auth type in extern_type, 'internal' lives in auth_internal.py |
|
411 | # The name of the default auth type in extern_type, 'internal' lives in auth_internal.py | |
412 | DEFAULT_AUTH_TYPE = 'internal' |
|
412 | DEFAULT_AUTH_TYPE = 'internal' | |
413 |
|
413 | |||
414 | user_id = Column(Integer(), primary_key=True) |
|
414 | user_id = Column(Integer(), primary_key=True) | |
415 | username = Column(String(255), nullable=False, unique=True) |
|
415 | username = Column(String(255), nullable=False, unique=True) | |
416 | password = Column(String(255), nullable=False) |
|
416 | password = Column(String(255), nullable=False) | |
417 | active = Column(Boolean(), nullable=False, default=True) |
|
417 | active = Column(Boolean(), nullable=False, default=True) | |
418 | admin = Column(Boolean(), nullable=False, default=False) |
|
418 | admin = Column(Boolean(), nullable=False, default=False) | |
419 | name = Column("firstname", Unicode(255), nullable=False) |
|
419 | name = Column("firstname", Unicode(255), nullable=False) | |
420 | lastname = Column(Unicode(255), nullable=False) |
|
420 | lastname = Column(Unicode(255), nullable=False) | |
421 | _email = Column("email", String(255), nullable=True, unique=True) # FIXME: not nullable? |
|
421 | _email = Column("email", String(255), nullable=True, unique=True) # FIXME: not nullable? | |
422 | last_login = Column(DateTime(timezone=False), nullable=True) |
|
422 | last_login = Column(DateTime(timezone=False), nullable=True) | |
423 | extern_type = Column(String(255), nullable=True) # FIXME: not nullable? |
|
423 | extern_type = Column(String(255), nullable=True) # FIXME: not nullable? | |
424 | extern_name = Column(String(255), nullable=True) # FIXME: not nullable? |
|
424 | extern_name = Column(String(255), nullable=True) # FIXME: not nullable? | |
425 | api_key = Column(String(255), nullable=False) |
|
425 | api_key = Column(String(255), nullable=False) | |
426 | created_on = Column(DateTime(timezone=False), nullable=False, default=datetime.datetime.now) |
|
426 | created_on = Column(DateTime(timezone=False), nullable=False, default=datetime.datetime.now) | |
427 | _user_data = Column("user_data", LargeBinary(), nullable=True) # JSON data # FIXME: not nullable? |
|
427 | _user_data = Column("user_data", LargeBinary(), nullable=True) # JSON data # FIXME: not nullable? | |
428 |
|
428 | |||
429 | user_log = relationship('UserLog') |
|
429 | user_log = relationship('UserLog') | |
430 | user_perms = relationship('UserToPerm', primaryjoin="User.user_id==UserToPerm.user_id", cascade='all') |
|
430 | user_perms = relationship('UserToPerm', primaryjoin="User.user_id==UserToPerm.user_id", cascade='all') | |
431 |
|
431 | |||
432 | repositories = relationship('Repository') |
|
432 | repositories = relationship('Repository') | |
433 | repo_groups = relationship('RepoGroup') |
|
433 | repo_groups = relationship('RepoGroup') | |
434 | user_groups = relationship('UserGroup') |
|
434 | user_groups = relationship('UserGroup') | |
435 | user_followers = relationship('UserFollowing', primaryjoin='UserFollowing.follows_user_id==User.user_id', cascade='all') |
|
435 | user_followers = relationship('UserFollowing', primaryjoin='UserFollowing.follows_user_id==User.user_id', cascade='all') | |
436 | followings = relationship('UserFollowing', primaryjoin='UserFollowing.user_id==User.user_id', cascade='all') |
|
436 | followings = relationship('UserFollowing', primaryjoin='UserFollowing.user_id==User.user_id', cascade='all') | |
437 |
|
437 | |||
438 | repo_to_perm = relationship('UserRepoToPerm', primaryjoin='UserRepoToPerm.user_id==User.user_id', cascade='all') |
|
438 | repo_to_perm = relationship('UserRepoToPerm', primaryjoin='UserRepoToPerm.user_id==User.user_id', cascade='all') | |
439 | repo_group_to_perm = relationship('UserRepoGroupToPerm', primaryjoin='UserRepoGroupToPerm.user_id==User.user_id', cascade='all') |
|
439 | repo_group_to_perm = relationship('UserRepoGroupToPerm', primaryjoin='UserRepoGroupToPerm.user_id==User.user_id', cascade='all') | |
440 |
|
440 | |||
441 | group_member = relationship('UserGroupMember', cascade='all') |
|
441 | group_member = relationship('UserGroupMember', cascade='all') | |
442 |
|
442 | |||
443 | # comments created by this user |
|
443 | # comments created by this user | |
444 | user_comments = relationship('ChangesetComment', cascade='all') |
|
444 | user_comments = relationship('ChangesetComment', cascade='all') | |
445 | # extra emails for this user |
|
445 | # extra emails for this user | |
446 | user_emails = relationship('UserEmailMap', cascade='all') |
|
446 | user_emails = relationship('UserEmailMap', cascade='all') | |
447 | # extra API keys |
|
447 | # extra API keys | |
448 | user_api_keys = relationship('UserApiKeys', cascade='all') |
|
448 | user_api_keys = relationship('UserApiKeys', cascade='all') | |
449 | ssh_keys = relationship('UserSshKeys', cascade='all') |
|
449 | ssh_keys = relationship('UserSshKeys', cascade='all') | |
450 |
|
450 | |||
451 | @hybrid_property |
|
451 | @hybrid_property | |
452 | def email(self): |
|
452 | def email(self): | |
453 | return self._email |
|
453 | return self._email | |
454 |
|
454 | |||
455 | @email.setter |
|
455 | @email.setter | |
456 | def email(self, val): |
|
456 | def email(self, val): | |
457 | self._email = val.lower() if val else None |
|
457 | self._email = val.lower() if val else None | |
458 |
|
458 | |||
459 | @property |
|
459 | @property | |
460 | def firstname(self): |
|
460 | def firstname(self): | |
461 | # alias for future |
|
461 | # alias for future | |
462 | return self.name |
|
462 | return self.name | |
463 |
|
463 | |||
464 | @property |
|
464 | @property | |
465 | def emails(self): |
|
465 | def emails(self): | |
466 | other = UserEmailMap.query().filter(UserEmailMap.user == self).all() |
|
466 | other = UserEmailMap.query().filter(UserEmailMap.user == self).all() | |
467 | return [self.email] + [x.email for x in other] |
|
467 | return [self.email] + [x.email for x in other] | |
468 |
|
468 | |||
469 | @property |
|
469 | @property | |
470 | def api_keys(self): |
|
470 | def api_keys(self): | |
471 | other = UserApiKeys.query().filter(UserApiKeys.user == self).all() |
|
471 | other = UserApiKeys.query().filter(UserApiKeys.user == self).all() | |
472 | return [self.api_key] + [x.api_key for x in other] |
|
472 | return [self.api_key] + [x.api_key for x in other] | |
473 |
|
473 | |||
474 | @property |
|
474 | @property | |
475 | def ip_addresses(self): |
|
475 | def ip_addresses(self): | |
476 | ret = UserIpMap.query().filter(UserIpMap.user == self).all() |
|
476 | ret = UserIpMap.query().filter(UserIpMap.user == self).all() | |
477 | return [x.ip_addr for x in ret] |
|
477 | return [x.ip_addr for x in ret] | |
478 |
|
478 | |||
479 | @property |
|
479 | @property | |
480 | def full_name(self): |
|
480 | def full_name(self): | |
481 | return '%s %s' % (self.firstname, self.lastname) |
|
481 | return '%s %s' % (self.firstname, self.lastname) | |
482 |
|
482 | |||
483 | @property |
|
483 | @property | |
484 | def full_name_or_username(self): |
|
484 | def full_name_or_username(self): | |
485 | """ |
|
485 | """ | |
486 | Show full name. |
|
486 | Show full name. | |
487 | If full name is not set, fall back to username. |
|
487 | If full name is not set, fall back to username. | |
488 | """ |
|
488 | """ | |
489 | return ('%s %s' % (self.firstname, self.lastname) |
|
489 | return ('%s %s' % (self.firstname, self.lastname) | |
490 | if (self.firstname and self.lastname) else self.username) |
|
490 | if (self.firstname and self.lastname) else self.username) | |
491 |
|
491 | |||
492 | @property |
|
492 | @property | |
493 | def full_name_and_username(self): |
|
493 | def full_name_and_username(self): | |
494 | """ |
|
494 | """ | |
495 | Show full name and username as 'Firstname Lastname (username)'. |
|
495 | Show full name and username as 'Firstname Lastname (username)'. | |
496 | If full name is not set, fall back to username. |
|
496 | If full name is not set, fall back to username. | |
497 | """ |
|
497 | """ | |
498 | return ('%s %s (%s)' % (self.firstname, self.lastname, self.username) |
|
498 | return ('%s %s (%s)' % (self.firstname, self.lastname, self.username) | |
499 | if (self.firstname and self.lastname) else self.username) |
|
499 | if (self.firstname and self.lastname) else self.username) | |
500 |
|
500 | |||
501 | @property |
|
501 | @property | |
502 | def full_contact(self): |
|
502 | def full_contact(self): | |
503 | return '%s %s <%s>' % (self.firstname, self.lastname, self.email) |
|
503 | return '%s %s <%s>' % (self.firstname, self.lastname, self.email) | |
504 |
|
504 | |||
505 | @property |
|
505 | @property | |
506 | def short_contact(self): |
|
506 | def short_contact(self): | |
507 | return '%s %s' % (self.firstname, self.lastname) |
|
507 | return '%s %s' % (self.firstname, self.lastname) | |
508 |
|
508 | |||
509 | @property |
|
509 | @property | |
510 | def is_admin(self): |
|
510 | def is_admin(self): | |
511 | return self.admin |
|
511 | return self.admin | |
512 |
|
512 | |||
513 | @hybrid_property |
|
513 | @hybrid_property | |
514 | def is_default_user(self): |
|
514 | def is_default_user(self): | |
515 | return self.username == User.DEFAULT_USER |
|
515 | return self.username == User.DEFAULT_USER | |
516 |
|
516 | |||
517 | @hybrid_property |
|
517 | @hybrid_property | |
518 | def user_data(self): |
|
518 | def user_data(self): | |
519 | if not self._user_data: |
|
519 | if not self._user_data: | |
520 | return {} |
|
520 | return {} | |
521 |
|
521 | |||
522 | try: |
|
522 | try: | |
523 | return json.loads(self._user_data) |
|
523 | return json.loads(self._user_data) | |
524 | except TypeError: |
|
524 | except TypeError: | |
525 | return {} |
|
525 | return {} | |
526 |
|
526 | |||
527 | @user_data.setter |
|
527 | @user_data.setter | |
528 | def user_data(self, val): |
|
528 | def user_data(self, val): | |
529 | try: |
|
529 | try: | |
530 | self._user_data = json.dumps(val) |
|
530 | self._user_data = json.dumps(val) | |
531 | except Exception: |
|
531 | except Exception: | |
532 | log.error(traceback.format_exc()) |
|
532 | log.error(traceback.format_exc()) | |
533 |
|
533 | |||
534 | def __unicode__(self): |
|
534 | def __unicode__(self): | |
535 | return u"<%s('id:%s:%s')>" % (self.__class__.__name__, |
|
535 | return u"<%s('id:%s:%s')>" % (self.__class__.__name__, | |
536 | self.user_id, self.username) |
|
536 | self.user_id, self.username) | |
537 |
|
537 | |||
538 | @classmethod |
|
538 | @classmethod | |
539 | def guess_instance(cls, value): |
|
539 | def guess_instance(cls, value): | |
540 | return super(User, cls).guess_instance(value, User.get_by_username) |
|
540 | return super(User, cls).guess_instance(value, User.get_by_username) | |
541 |
|
541 | |||
542 | @classmethod |
|
542 | @classmethod | |
543 | def get_or_404(cls, id_, allow_default=True): |
|
543 | def get_or_404(cls, id_, allow_default=True): | |
544 | ''' |
|
544 | ''' | |
545 | Overridden version of BaseDbModel.get_or_404, with an extra check on |
|
545 | Overridden version of BaseDbModel.get_or_404, with an extra check on | |
546 | the default user. |
|
546 | the default user. | |
547 | ''' |
|
547 | ''' | |
548 | user = super(User, cls).get_or_404(id_) |
|
548 | user = super(User, cls).get_or_404(id_) | |
549 | if not allow_default and user.is_default_user: |
|
549 | if not allow_default and user.is_default_user: | |
550 | raise DefaultUserException() |
|
550 | raise DefaultUserException() | |
551 | return user |
|
551 | return user | |
552 |
|
552 | |||
553 | @classmethod |
|
553 | @classmethod | |
554 | def get_by_username_or_email(cls, username_or_email, case_insensitive=False, cache=False): |
|
554 | def get_by_username_or_email(cls, username_or_email, case_insensitive=False, cache=False): | |
555 | """ |
|
555 | """ | |
556 | For anything that looks like an email address, look up by the email address (matching |
|
556 | For anything that looks like an email address, look up by the email address (matching | |
557 | case insensitively). |
|
557 | case insensitively). | |
558 | For anything else, try to look up by the user name. |
|
558 | For anything else, try to look up by the user name. | |
559 |
|
559 | |||
560 | This assumes no normal username can have '@' symbol. |
|
560 | This assumes no normal username can have '@' symbol. | |
561 | """ |
|
561 | """ | |
562 | if '@' in username_or_email: |
|
562 | if '@' in username_or_email: | |
563 | return User.get_by_email(username_or_email, cache=cache) |
|
563 | return User.get_by_email(username_or_email, cache=cache) | |
564 | else: |
|
564 | else: | |
565 | return User.get_by_username(username_or_email, case_insensitive=case_insensitive, cache=cache) |
|
565 | return User.get_by_username(username_or_email, case_insensitive=case_insensitive, cache=cache) | |
566 |
|
566 | |||
567 | @classmethod |
|
567 | @classmethod | |
568 | def get_by_username(cls, username, case_insensitive=False, cache=False): |
|
568 | def get_by_username(cls, username, case_insensitive=False, cache=False): | |
569 | if case_insensitive: |
|
569 | if case_insensitive: | |
570 | q = cls.query().filter(func.lower(cls.username) == func.lower(username)) |
|
570 | q = cls.query().filter(func.lower(cls.username) == func.lower(username)) | |
571 | else: |
|
571 | else: | |
572 | q = cls.query().filter(cls.username == username) |
|
572 | q = cls.query().filter(cls.username == username) | |
573 |
|
573 | |||
574 | if cache: |
|
574 | if cache: | |
575 | q = q.options(FromCache( |
|
575 | q = q.options(FromCache( | |
576 | "sql_cache_short", |
|
576 | "sql_cache_short", | |
577 | "get_user_%s" % _hash_key(username) |
|
577 | "get_user_%s" % _hash_key(username) | |
578 | ) |
|
578 | ) | |
579 | ) |
|
579 | ) | |
580 | return q.scalar() |
|
580 | return q.scalar() | |
581 |
|
581 | |||
582 | @classmethod |
|
582 | @classmethod | |
583 | def get_by_api_key(cls, api_key, cache=False, fallback=True): |
|
583 | def get_by_api_key(cls, api_key, cache=False, fallback=True): | |
584 | if len(api_key) != 40 or not api_key.isalnum(): |
|
584 | if len(api_key) != 40 or not api_key.isalnum(): | |
585 | return None |
|
585 | return None | |
586 |
|
586 | |||
587 | q = cls.query().filter(cls.api_key == api_key) |
|
587 | q = cls.query().filter(cls.api_key == api_key) | |
588 |
|
588 | |||
589 | if cache: |
|
589 | if cache: | |
590 | q = q.options(FromCache("sql_cache_short", |
|
590 | q = q.options(FromCache("sql_cache_short", | |
591 | "get_api_key_%s" % api_key)) |
|
591 | "get_api_key_%s" % api_key)) | |
592 | res = q.scalar() |
|
592 | res = q.scalar() | |
593 |
|
593 | |||
594 | if fallback and not res: |
|
594 | if fallback and not res: | |
595 | # fallback to additional keys |
|
595 | # fallback to additional keys | |
596 | _res = UserApiKeys.query().filter_by(api_key=api_key, is_expired=False).first() |
|
596 | _res = UserApiKeys.query().filter_by(api_key=api_key, is_expired=False).first() | |
597 | if _res: |
|
597 | if _res: | |
598 | res = _res.user |
|
598 | res = _res.user | |
599 | if res is None or not res.active or res.is_default_user: |
|
599 | if res is None or not res.active or res.is_default_user: | |
600 | return None |
|
600 | return None | |
601 | return res |
|
601 | return res | |
602 |
|
602 | |||
603 | @classmethod |
|
603 | @classmethod | |
604 | def get_by_email(cls, email, cache=False): |
|
604 | def get_by_email(cls, email, cache=False): | |
605 | q = cls.query().filter(func.lower(cls.email) == func.lower(email)) |
|
605 | q = cls.query().filter(func.lower(cls.email) == func.lower(email)) | |
606 |
|
606 | |||
607 | if cache: |
|
607 | if cache: | |
608 | q = q.options(FromCache("sql_cache_short", |
|
608 | q = q.options(FromCache("sql_cache_short", | |
609 | "get_email_key_%s" % email)) |
|
609 | "get_email_key_%s" % email)) | |
610 |
|
610 | |||
611 | ret = q.scalar() |
|
611 | ret = q.scalar() | |
612 | if ret is None: |
|
612 | if ret is None: | |
613 | q = UserEmailMap.query() |
|
613 | q = UserEmailMap.query() | |
614 | # try fetching in alternate email map |
|
614 | # try fetching in alternate email map | |
615 | q = q.filter(func.lower(UserEmailMap.email) == func.lower(email)) |
|
615 | q = q.filter(func.lower(UserEmailMap.email) == func.lower(email)) | |
616 | q = q.options(joinedload(UserEmailMap.user)) |
|
616 | q = q.options(joinedload(UserEmailMap.user)) | |
617 | if cache: |
|
617 | if cache: | |
618 | q = q.options(FromCache("sql_cache_short", |
|
618 | q = q.options(FromCache("sql_cache_short", | |
619 | "get_email_map_key_%s" % email)) |
|
619 | "get_email_map_key_%s" % email)) | |
620 | ret = getattr(q.scalar(), 'user', None) |
|
620 | ret = getattr(q.scalar(), 'user', None) | |
621 |
|
621 | |||
622 | return ret |
|
622 | return ret | |
623 |
|
623 | |||
624 | @classmethod |
|
624 | @classmethod | |
625 | def get_from_cs_author(cls, author): |
|
625 | def get_from_cs_author(cls, author): | |
626 | """ |
|
626 | """ | |
627 | Tries to get User objects out of commit author string |
|
627 | Tries to get User objects out of commit author string | |
628 |
|
628 | |||
629 | :param author: |
|
629 | :param author: | |
630 | """ |
|
630 | """ | |
631 | from kallithea.lib.helpers import email, author_name |
|
631 | from kallithea.lib.helpers import email, author_name | |
632 | # Valid email in the attribute passed, see if they're in the system |
|
632 | # Valid email in the attribute passed, see if they're in the system | |
633 | _email = email(author) |
|
633 | _email = email(author) | |
634 | if _email: |
|
634 | if _email: | |
635 | user = cls.get_by_email(_email) |
|
635 | user = cls.get_by_email(_email) | |
636 | if user is not None: |
|
636 | if user is not None: | |
637 | return user |
|
637 | return user | |
638 | # Maybe we can match by username? |
|
638 | # Maybe we can match by username? | |
639 | _author = author_name(author) |
|
639 | _author = author_name(author) | |
640 | user = cls.get_by_username(_author, case_insensitive=True) |
|
640 | user = cls.get_by_username(_author, case_insensitive=True) | |
641 | if user is not None: |
|
641 | if user is not None: | |
642 | return user |
|
642 | return user | |
643 |
|
643 | |||
644 | def update_lastlogin(self): |
|
644 | def update_lastlogin(self): | |
645 | """Update user lastlogin""" |
|
645 | """Update user lastlogin""" | |
646 | self.last_login = datetime.datetime.now() |
|
646 | self.last_login = datetime.datetime.now() | |
647 | log.debug('updated user %s lastlogin', self.username) |
|
647 | log.debug('updated user %s lastlogin', self.username) | |
648 |
|
648 | |||
649 | @classmethod |
|
649 | @classmethod | |
650 | def get_first_admin(cls): |
|
650 | def get_first_admin(cls): | |
651 | user = User.query().filter(User.admin == True).first() |
|
651 | user = User.query().filter(User.admin == True).first() | |
652 | if user is None: |
|
652 | if user is None: | |
653 | raise Exception('Missing administrative account!') |
|
653 | raise Exception('Missing administrative account!') | |
654 | return user |
|
654 | return user | |
655 |
|
655 | |||
656 | @classmethod |
|
656 | @classmethod | |
657 | def get_default_user(cls, cache=False): |
|
657 | def get_default_user(cls, cache=False): | |
658 | user = User.get_by_username(User.DEFAULT_USER, cache=cache) |
|
658 | user = User.get_by_username(User.DEFAULT_USER, cache=cache) | |
659 | if user is None: |
|
659 | if user is None: | |
660 | raise Exception('Missing default account!') |
|
660 | raise Exception('Missing default account!') | |
661 | return user |
|
661 | return user | |
662 |
|
662 | |||
663 | def get_api_data(self, details=False): |
|
663 | def get_api_data(self, details=False): | |
664 | """ |
|
664 | """ | |
665 | Common function for generating user related data for API |
|
665 | Common function for generating user related data for API | |
666 | """ |
|
666 | """ | |
667 | user = self |
|
667 | user = self | |
668 | data = dict( |
|
668 | data = dict( | |
669 | user_id=user.user_id, |
|
669 | user_id=user.user_id, | |
670 | username=user.username, |
|
670 | username=user.username, | |
671 | firstname=user.name, |
|
671 | firstname=user.name, | |
672 | lastname=user.lastname, |
|
672 | lastname=user.lastname, | |
673 | email=user.email, |
|
673 | email=user.email, | |
674 | emails=user.emails, |
|
674 | emails=user.emails, | |
675 | active=user.active, |
|
675 | active=user.active, | |
676 | admin=user.admin, |
|
676 | admin=user.admin, | |
677 | ) |
|
677 | ) | |
678 | if details: |
|
678 | if details: | |
679 | data.update(dict( |
|
679 | data.update(dict( | |
680 | extern_type=user.extern_type, |
|
680 | extern_type=user.extern_type, | |
681 | extern_name=user.extern_name, |
|
681 | extern_name=user.extern_name, | |
682 | api_key=user.api_key, |
|
682 | api_key=user.api_key, | |
683 | api_keys=user.api_keys, |
|
683 | api_keys=user.api_keys, | |
684 | last_login=user.last_login, |
|
684 | last_login=user.last_login, | |
685 | ip_addresses=user.ip_addresses |
|
685 | ip_addresses=user.ip_addresses | |
686 | )) |
|
686 | )) | |
687 | return data |
|
687 | return data | |
688 |
|
688 | |||
689 | def __json__(self): |
|
689 | def __json__(self): | |
690 | data = dict( |
|
690 | data = dict( | |
691 | full_name=self.full_name, |
|
691 | full_name=self.full_name, | |
692 | full_name_or_username=self.full_name_or_username, |
|
692 | full_name_or_username=self.full_name_or_username, | |
693 | short_contact=self.short_contact, |
|
693 | short_contact=self.short_contact, | |
694 | full_contact=self.full_contact |
|
694 | full_contact=self.full_contact | |
695 | ) |
|
695 | ) | |
696 | data.update(self.get_api_data()) |
|
696 | data.update(self.get_api_data()) | |
697 | return data |
|
697 | return data | |
698 |
|
698 | |||
699 |
|
699 | |||
700 | class UserApiKeys(Base, BaseDbModel): |
|
700 | class UserApiKeys(Base, BaseDbModel): | |
701 | __tablename__ = 'user_api_keys' |
|
701 | __tablename__ = 'user_api_keys' | |
702 | __table_args__ = ( |
|
702 | __table_args__ = ( | |
703 | Index('uak_api_key_idx', 'api_key'), |
|
703 | Index('uak_api_key_idx', 'api_key'), | |
704 | Index('uak_api_key_expires_idx', 'api_key', 'expires'), |
|
704 | Index('uak_api_key_expires_idx', 'api_key', 'expires'), | |
705 | _table_args_default_dict, |
|
705 | _table_args_default_dict, | |
706 | ) |
|
706 | ) | |
707 |
|
707 | |||
708 | user_api_key_id = Column(Integer(), primary_key=True) |
|
708 | user_api_key_id = Column(Integer(), primary_key=True) | |
709 | user_id = Column(Integer(), ForeignKey('users.user_id'), nullable=False) |
|
709 | user_id = Column(Integer(), ForeignKey('users.user_id'), nullable=False) | |
710 | api_key = Column(String(255), nullable=False, unique=True) |
|
710 | api_key = Column(String(255), nullable=False, unique=True) | |
711 | description = Column(UnicodeText(), nullable=False) |
|
711 | description = Column(UnicodeText(), nullable=False) | |
712 | expires = Column(Float(53), nullable=False) |
|
712 | expires = Column(Float(53), nullable=False) | |
713 | created_on = Column(DateTime(timezone=False), nullable=False, default=datetime.datetime.now) |
|
713 | created_on = Column(DateTime(timezone=False), nullable=False, default=datetime.datetime.now) | |
714 |
|
714 | |||
715 | user = relationship('User') |
|
715 | user = relationship('User') | |
716 |
|
716 | |||
717 | @hybrid_property |
|
717 | @hybrid_property | |
718 | def is_expired(self): |
|
718 | def is_expired(self): | |
719 | return (self.expires != -1) & (time.time() > self.expires) |
|
719 | return (self.expires != -1) & (time.time() > self.expires) | |
720 |
|
720 | |||
721 |
|
721 | |||
722 | class UserEmailMap(Base, BaseDbModel): |
|
722 | class UserEmailMap(Base, BaseDbModel): | |
723 | __tablename__ = 'user_email_map' |
|
723 | __tablename__ = 'user_email_map' | |
724 | __table_args__ = ( |
|
724 | __table_args__ = ( | |
725 | Index('uem_email_idx', 'email'), |
|
725 | Index('uem_email_idx', 'email'), | |
726 | _table_args_default_dict, |
|
726 | _table_args_default_dict, | |
727 | ) |
|
727 | ) | |
728 |
|
728 | |||
729 | email_id = Column(Integer(), primary_key=True) |
|
729 | email_id = Column(Integer(), primary_key=True) | |
730 | user_id = Column(Integer(), ForeignKey('users.user_id'), nullable=False) |
|
730 | user_id = Column(Integer(), ForeignKey('users.user_id'), nullable=False) | |
731 | _email = Column("email", String(255), nullable=False, unique=True) |
|
731 | _email = Column("email", String(255), nullable=False, unique=True) | |
732 | user = relationship('User') |
|
732 | user = relationship('User') | |
733 |
|
733 | |||
734 | @validates('_email') |
|
734 | @validates('_email') | |
735 | def validate_email(self, key, email): |
|
735 | def validate_email(self, key, email): | |
736 | # check if this email is not main one |
|
736 | # check if this email is not main one | |
737 | main_email = Session().query(User).filter(User.email == email).scalar() |
|
737 | main_email = Session().query(User).filter(User.email == email).scalar() | |
738 | if main_email is not None: |
|
738 | if main_email is not None: | |
739 | raise AttributeError('email %s is present is user table' % email) |
|
739 | raise AttributeError('email %s is present is user table' % email) | |
740 | return email |
|
740 | return email | |
741 |
|
741 | |||
742 | @hybrid_property |
|
742 | @hybrid_property | |
743 | def email(self): |
|
743 | def email(self): | |
744 | return self._email |
|
744 | return self._email | |
745 |
|
745 | |||
746 | @email.setter |
|
746 | @email.setter | |
747 | def email(self, val): |
|
747 | def email(self, val): | |
748 | self._email = val.lower() if val else None |
|
748 | self._email = val.lower() if val else None | |
749 |
|
749 | |||
750 |
|
750 | |||
751 | class UserIpMap(Base, BaseDbModel): |
|
751 | class UserIpMap(Base, BaseDbModel): | |
752 | __tablename__ = 'user_ip_map' |
|
752 | __tablename__ = 'user_ip_map' | |
753 | __table_args__ = ( |
|
753 | __table_args__ = ( | |
754 | UniqueConstraint('user_id', 'ip_addr'), |
|
754 | UniqueConstraint('user_id', 'ip_addr'), | |
755 | _table_args_default_dict, |
|
755 | _table_args_default_dict, | |
756 | ) |
|
756 | ) | |
757 |
|
757 | |||
758 | ip_id = Column(Integer(), primary_key=True) |
|
758 | ip_id = Column(Integer(), primary_key=True) | |
759 | user_id = Column(Integer(), ForeignKey('users.user_id'), nullable=False) |
|
759 | user_id = Column(Integer(), ForeignKey('users.user_id'), nullable=False) | |
760 | ip_addr = Column(String(255), nullable=False) |
|
760 | ip_addr = Column(String(255), nullable=False) | |
761 | active = Column(Boolean(), nullable=False, default=True) |
|
761 | active = Column(Boolean(), nullable=False, default=True) | |
762 | user = relationship('User') |
|
762 | user = relationship('User') | |
763 |
|
763 | |||
764 | @classmethod |
|
764 | @classmethod | |
765 | def _get_ip_range(cls, ip_addr): |
|
765 | def _get_ip_range(cls, ip_addr): | |
766 | net = ipaddr.IPNetwork(address=ip_addr) |
|
766 | net = ipaddr.IPNetwork(address=ip_addr) | |
767 | return [str(net.network), str(net.broadcast)] |
|
767 | return [str(net.network), str(net.broadcast)] | |
768 |
|
768 | |||
769 | def __json__(self): |
|
769 | def __json__(self): | |
770 | return dict( |
|
770 | return dict( | |
771 | ip_addr=self.ip_addr, |
|
771 | ip_addr=self.ip_addr, | |
772 | ip_range=self._get_ip_range(self.ip_addr) |
|
772 | ip_range=self._get_ip_range(self.ip_addr) | |
773 | ) |
|
773 | ) | |
774 |
|
774 | |||
775 | def __unicode__(self): |
|
775 | def __unicode__(self): | |
776 | return u"<%s('user_id:%s=>%s')>" % (self.__class__.__name__, |
|
776 | return u"<%s('user_id:%s=>%s')>" % (self.__class__.__name__, | |
777 | self.user_id, self.ip_addr) |
|
777 | self.user_id, self.ip_addr) | |
778 |
|
778 | |||
779 |
|
779 | |||
780 | class UserLog(Base, BaseDbModel): |
|
780 | class UserLog(Base, BaseDbModel): | |
781 | __tablename__ = 'user_logs' |
|
781 | __tablename__ = 'user_logs' | |
782 | __table_args__ = ( |
|
782 | __table_args__ = ( | |
783 | _table_args_default_dict, |
|
783 | _table_args_default_dict, | |
784 | ) |
|
784 | ) | |
785 |
|
785 | |||
786 | user_log_id = Column(Integer(), primary_key=True) |
|
786 | user_log_id = Column(Integer(), primary_key=True) | |
787 | user_id = Column(Integer(), ForeignKey('users.user_id'), nullable=True) |
|
787 | user_id = Column(Integer(), ForeignKey('users.user_id'), nullable=True) | |
788 | username = Column(String(255), nullable=False) |
|
788 | username = Column(String(255), nullable=False) | |
789 | repository_id = Column(Integer(), ForeignKey('repositories.repo_id'), nullable=True) |
|
789 | repository_id = Column(Integer(), ForeignKey('repositories.repo_id'), nullable=True) | |
790 | repository_name = Column(Unicode(255), nullable=False) |
|
790 | repository_name = Column(Unicode(255), nullable=False) | |
791 | user_ip = Column(String(255), nullable=True) |
|
791 | user_ip = Column(String(255), nullable=True) | |
792 | action = Column(UnicodeText(), nullable=False) |
|
792 | action = Column(UnicodeText(), nullable=False) | |
793 | action_date = Column(DateTime(timezone=False), nullable=False) |
|
793 | action_date = Column(DateTime(timezone=False), nullable=False) | |
794 |
|
794 | |||
795 | def __unicode__(self): |
|
795 | def __unicode__(self): | |
796 | return u"<%s('id:%s:%s')>" % (self.__class__.__name__, |
|
796 | return u"<%s('id:%s:%s')>" % (self.__class__.__name__, | |
797 | self.repository_name, |
|
797 | self.repository_name, | |
798 | self.action) |
|
798 | self.action) | |
799 |
|
799 | |||
800 | @property |
|
800 | @property | |
801 | def action_as_day(self): |
|
801 | def action_as_day(self): | |
802 | return datetime.date(*self.action_date.timetuple()[:3]) |
|
802 | return datetime.date(*self.action_date.timetuple()[:3]) | |
803 |
|
803 | |||
804 | user = relationship('User') |
|
804 | user = relationship('User') | |
805 | repository = relationship('Repository', cascade='') |
|
805 | repository = relationship('Repository', cascade='') | |
806 |
|
806 | |||
807 |
|
807 | |||
808 | class UserGroup(Base, BaseDbModel): |
|
808 | class UserGroup(Base, BaseDbModel): | |
809 | __tablename__ = 'users_groups' |
|
809 | __tablename__ = 'users_groups' | |
810 | __table_args__ = ( |
|
810 | __table_args__ = ( | |
811 | _table_args_default_dict, |
|
811 | _table_args_default_dict, | |
812 | ) |
|
812 | ) | |
813 |
|
813 | |||
814 | users_group_id = Column(Integer(), primary_key=True) |
|
814 | users_group_id = Column(Integer(), primary_key=True) | |
815 | users_group_name = Column(Unicode(255), nullable=False, unique=True) |
|
815 | users_group_name = Column(Unicode(255), nullable=False, unique=True) | |
816 | user_group_description = Column(Unicode(10000), nullable=True) # FIXME: not nullable? |
|
816 | user_group_description = Column(Unicode(10000), nullable=True) # FIXME: not nullable? | |
817 | users_group_active = Column(Boolean(), nullable=False) |
|
817 | users_group_active = Column(Boolean(), nullable=False) | |
818 | owner_id = Column('user_id', Integer(), ForeignKey('users.user_id'), nullable=False) |
|
818 | owner_id = Column('user_id', Integer(), ForeignKey('users.user_id'), nullable=False) | |
819 | created_on = Column(DateTime(timezone=False), nullable=False, default=datetime.datetime.now) |
|
819 | created_on = Column(DateTime(timezone=False), nullable=False, default=datetime.datetime.now) | |
820 | _group_data = Column("group_data", LargeBinary(), nullable=True) # JSON data # FIXME: not nullable? |
|
820 | _group_data = Column("group_data", LargeBinary(), nullable=True) # JSON data # FIXME: not nullable? | |
821 |
|
821 | |||
822 | members = relationship('UserGroupMember', cascade="all, delete-orphan") |
|
822 | members = relationship('UserGroupMember', cascade="all, delete-orphan") | |
823 | users_group_to_perm = relationship('UserGroupToPerm', cascade='all') |
|
823 | users_group_to_perm = relationship('UserGroupToPerm', cascade='all') | |
824 | users_group_repo_to_perm = relationship('UserGroupRepoToPerm', cascade='all') |
|
824 | users_group_repo_to_perm = relationship('UserGroupRepoToPerm', cascade='all') | |
825 | users_group_repo_group_to_perm = relationship('UserGroupRepoGroupToPerm', cascade='all') |
|
825 | users_group_repo_group_to_perm = relationship('UserGroupRepoGroupToPerm', cascade='all') | |
826 | user_user_group_to_perm = relationship('UserUserGroupToPerm ', cascade='all') |
|
826 | user_user_group_to_perm = relationship('UserUserGroupToPerm ', cascade='all') | |
827 | user_group_user_group_to_perm = relationship('UserGroupUserGroupToPerm ', primaryjoin="UserGroupUserGroupToPerm.target_user_group_id==UserGroup.users_group_id", cascade='all') |
|
827 | user_group_user_group_to_perm = relationship('UserGroupUserGroupToPerm ', primaryjoin="UserGroupUserGroupToPerm.target_user_group_id==UserGroup.users_group_id", cascade='all') | |
828 |
|
828 | |||
829 | owner = relationship('User') |
|
829 | owner = relationship('User') | |
830 |
|
830 | |||
831 | @hybrid_property |
|
831 | @hybrid_property | |
832 | def group_data(self): |
|
832 | def group_data(self): | |
833 | if not self._group_data: |
|
833 | if not self._group_data: | |
834 | return {} |
|
834 | return {} | |
835 |
|
835 | |||
836 | try: |
|
836 | try: | |
837 | return json.loads(self._group_data) |
|
837 | return json.loads(self._group_data) | |
838 | except TypeError: |
|
838 | except TypeError: | |
839 | return {} |
|
839 | return {} | |
840 |
|
840 | |||
841 | @group_data.setter |
|
841 | @group_data.setter | |
842 | def group_data(self, val): |
|
842 | def group_data(self, val): | |
843 | try: |
|
843 | try: | |
844 | self._group_data = json.dumps(val) |
|
844 | self._group_data = json.dumps(val) | |
845 | except Exception: |
|
845 | except Exception: | |
846 | log.error(traceback.format_exc()) |
|
846 | log.error(traceback.format_exc()) | |
847 |
|
847 | |||
848 | def __unicode__(self): |
|
848 | def __unicode__(self): | |
849 | return u"<%s('id:%s:%s')>" % (self.__class__.__name__, |
|
849 | return u"<%s('id:%s:%s')>" % (self.__class__.__name__, | |
850 | self.users_group_id, |
|
850 | self.users_group_id, | |
851 | self.users_group_name) |
|
851 | self.users_group_name) | |
852 |
|
852 | |||
853 | @classmethod |
|
853 | @classmethod | |
854 | def guess_instance(cls, value): |
|
854 | def guess_instance(cls, value): | |
855 | return super(UserGroup, cls).guess_instance(value, UserGroup.get_by_group_name) |
|
855 | return super(UserGroup, cls).guess_instance(value, UserGroup.get_by_group_name) | |
856 |
|
856 | |||
857 | @classmethod |
|
857 | @classmethod | |
858 | def get_by_group_name(cls, group_name, cache=False, |
|
858 | def get_by_group_name(cls, group_name, cache=False, | |
859 | case_insensitive=False): |
|
859 | case_insensitive=False): | |
860 | if case_insensitive: |
|
860 | if case_insensitive: | |
861 | q = cls.query().filter(func.lower(cls.users_group_name) == func.lower(group_name)) |
|
861 | q = cls.query().filter(func.lower(cls.users_group_name) == func.lower(group_name)) | |
862 | else: |
|
862 | else: | |
863 | q = cls.query().filter(cls.users_group_name == group_name) |
|
863 | q = cls.query().filter(cls.users_group_name == group_name) | |
864 | if cache: |
|
864 | if cache: | |
865 | q = q.options(FromCache( |
|
865 | q = q.options(FromCache( | |
866 | "sql_cache_short", |
|
866 | "sql_cache_short", | |
867 | "get_group_%s" % _hash_key(group_name) |
|
867 | "get_group_%s" % _hash_key(group_name) | |
868 | ) |
|
868 | ) | |
869 | ) |
|
869 | ) | |
870 | return q.scalar() |
|
870 | return q.scalar() | |
871 |
|
871 | |||
872 | @classmethod |
|
872 | @classmethod | |
873 | def get(cls, user_group_id, cache=False): |
|
873 | def get(cls, user_group_id, cache=False): | |
874 | user_group = cls.query() |
|
874 | user_group = cls.query() | |
875 | if cache: |
|
875 | if cache: | |
876 | user_group = user_group.options(FromCache("sql_cache_short", |
|
876 | user_group = user_group.options(FromCache("sql_cache_short", | |
877 | "get_users_group_%s" % user_group_id)) |
|
877 | "get_users_group_%s" % user_group_id)) | |
878 | return user_group.get(user_group_id) |
|
878 | return user_group.get(user_group_id) | |
879 |
|
879 | |||
880 | def get_api_data(self, with_members=True): |
|
880 | def get_api_data(self, with_members=True): | |
881 | user_group = self |
|
881 | user_group = self | |
882 |
|
882 | |||
883 | data = dict( |
|
883 | data = dict( | |
884 | users_group_id=user_group.users_group_id, |
|
884 | users_group_id=user_group.users_group_id, | |
885 | group_name=user_group.users_group_name, |
|
885 | group_name=user_group.users_group_name, | |
886 | group_description=user_group.user_group_description, |
|
886 | group_description=user_group.user_group_description, | |
887 | active=user_group.users_group_active, |
|
887 | active=user_group.users_group_active, | |
888 | owner=user_group.owner.username, |
|
888 | owner=user_group.owner.username, | |
889 | ) |
|
889 | ) | |
890 | if with_members: |
|
890 | if with_members: | |
891 | data['members'] = [ |
|
891 | data['members'] = [ | |
892 | ugm.user.get_api_data() |
|
892 | ugm.user.get_api_data() | |
893 | for ugm in user_group.members |
|
893 | for ugm in user_group.members | |
894 | ] |
|
894 | ] | |
895 |
|
895 | |||
896 | return data |
|
896 | return data | |
897 |
|
897 | |||
898 |
|
898 | |||
899 | class UserGroupMember(Base, BaseDbModel): |
|
899 | class UserGroupMember(Base, BaseDbModel): | |
900 | __tablename__ = 'users_groups_members' |
|
900 | __tablename__ = 'users_groups_members' | |
901 | __table_args__ = ( |
|
901 | __table_args__ = ( | |
902 | _table_args_default_dict, |
|
902 | _table_args_default_dict, | |
903 | ) |
|
903 | ) | |
904 |
|
904 | |||
905 | users_group_member_id = Column(Integer(), primary_key=True) |
|
905 | users_group_member_id = Column(Integer(), primary_key=True) | |
906 | users_group_id = Column(Integer(), ForeignKey('users_groups.users_group_id'), nullable=False) |
|
906 | users_group_id = Column(Integer(), ForeignKey('users_groups.users_group_id'), nullable=False) | |
907 | user_id = Column(Integer(), ForeignKey('users.user_id'), nullable=False) |
|
907 | user_id = Column(Integer(), ForeignKey('users.user_id'), nullable=False) | |
908 |
|
908 | |||
909 | user = relationship('User') |
|
909 | user = relationship('User') | |
910 | users_group = relationship('UserGroup') |
|
910 | users_group = relationship('UserGroup') | |
911 |
|
911 | |||
912 | def __init__(self, gr_id='', u_id=''): |
|
912 | def __init__(self, gr_id='', u_id=''): | |
913 | self.users_group_id = gr_id |
|
913 | self.users_group_id = gr_id | |
914 | self.user_id = u_id |
|
914 | self.user_id = u_id | |
915 |
|
915 | |||
916 |
|
916 | |||
917 | class RepositoryField(Base, BaseDbModel): |
|
917 | class RepositoryField(Base, BaseDbModel): | |
918 | __tablename__ = 'repositories_fields' |
|
918 | __tablename__ = 'repositories_fields' | |
919 | __table_args__ = ( |
|
919 | __table_args__ = ( | |
920 | UniqueConstraint('repository_id', 'field_key'), # no-multi field |
|
920 | UniqueConstraint('repository_id', 'field_key'), # no-multi field | |
921 | _table_args_default_dict, |
|
921 | _table_args_default_dict, | |
922 | ) |
|
922 | ) | |
923 |
|
923 | |||
924 | PREFIX = 'ex_' # prefix used in form to not conflict with already existing fields |
|
924 | PREFIX = 'ex_' # prefix used in form to not conflict with already existing fields | |
925 |
|
925 | |||
926 | repo_field_id = Column(Integer(), primary_key=True) |
|
926 | repo_field_id = Column(Integer(), primary_key=True) | |
927 | repository_id = Column(Integer(), ForeignKey('repositories.repo_id'), nullable=False) |
|
927 | repository_id = Column(Integer(), ForeignKey('repositories.repo_id'), nullable=False) | |
928 | field_key = Column(String(250), nullable=False) |
|
928 | field_key = Column(String(250), nullable=False) | |
929 | field_label = Column(String(1024), nullable=False) |
|
929 | field_label = Column(String(1024), nullable=False) | |
930 | field_value = Column(String(10000), nullable=False) |
|
930 | field_value = Column(String(10000), nullable=False) | |
931 | field_desc = Column(String(1024), nullable=False) |
|
931 | field_desc = Column(String(1024), nullable=False) | |
932 | field_type = Column(String(255), nullable=False) |
|
932 | field_type = Column(String(255), nullable=False) | |
933 | created_on = Column(DateTime(timezone=False), nullable=False, default=datetime.datetime.now) |
|
933 | created_on = Column(DateTime(timezone=False), nullable=False, default=datetime.datetime.now) | |
934 |
|
934 | |||
935 | repository = relationship('Repository') |
|
935 | repository = relationship('Repository') | |
936 |
|
936 | |||
937 | @property |
|
937 | @property | |
938 | def field_key_prefixed(self): |
|
938 | def field_key_prefixed(self): | |
939 | return 'ex_%s' % self.field_key |
|
939 | return 'ex_%s' % self.field_key | |
940 |
|
940 | |||
941 | @classmethod |
|
941 | @classmethod | |
942 | def un_prefix_key(cls, key): |
|
942 | def un_prefix_key(cls, key): | |
943 | if key.startswith(cls.PREFIX): |
|
943 | if key.startswith(cls.PREFIX): | |
944 | return key[len(cls.PREFIX):] |
|
944 | return key[len(cls.PREFIX):] | |
945 | return key |
|
945 | return key | |
946 |
|
946 | |||
947 | @classmethod |
|
947 | @classmethod | |
948 | def get_by_key_name(cls, key, repo): |
|
948 | def get_by_key_name(cls, key, repo): | |
949 | row = cls.query() \ |
|
949 | row = cls.query() \ | |
950 | .filter(cls.repository == repo) \ |
|
950 | .filter(cls.repository == repo) \ | |
951 | .filter(cls.field_key == key).scalar() |
|
951 | .filter(cls.field_key == key).scalar() | |
952 | return row |
|
952 | return row | |
953 |
|
953 | |||
954 |
|
954 | |||
955 | class Repository(Base, BaseDbModel): |
|
955 | class Repository(Base, BaseDbModel): | |
956 | __tablename__ = 'repositories' |
|
956 | __tablename__ = 'repositories' | |
957 | __table_args__ = ( |
|
957 | __table_args__ = ( | |
958 | Index('r_repo_name_idx', 'repo_name'), |
|
958 | Index('r_repo_name_idx', 'repo_name'), | |
959 | _table_args_default_dict, |
|
959 | _table_args_default_dict, | |
960 | ) |
|
960 | ) | |
961 |
|
961 | |||
962 | DEFAULT_CLONE_URI = '{scheme}://{user}@{netloc}/{repo}' |
|
962 | DEFAULT_CLONE_URI = '{scheme}://{user}@{netloc}/{repo}' | |
963 | DEFAULT_CLONE_SSH = 'ssh://{system_user}@{hostname}/{repo}' |
|
963 | DEFAULT_CLONE_SSH = 'ssh://{system_user}@{hostname}/{repo}' | |
964 |
|
964 | |||
965 | STATE_CREATED = u'repo_state_created' |
|
965 | STATE_CREATED = u'repo_state_created' | |
966 | STATE_PENDING = u'repo_state_pending' |
|
966 | STATE_PENDING = u'repo_state_pending' | |
967 | STATE_ERROR = u'repo_state_error' |
|
967 | STATE_ERROR = u'repo_state_error' | |
968 |
|
968 | |||
969 | repo_id = Column(Integer(), primary_key=True) |
|
969 | repo_id = Column(Integer(), primary_key=True) | |
970 | repo_name = Column(Unicode(255), nullable=False, unique=True) |
|
970 | repo_name = Column(Unicode(255), nullable=False, unique=True) | |
971 | repo_state = Column(String(255), nullable=False) |
|
971 | repo_state = Column(String(255), nullable=False) | |
972 |
|
972 | |||
973 | clone_uri = Column(String(255), nullable=True) # FIXME: not nullable? |
|
973 | clone_uri = Column(String(255), nullable=True) # FIXME: not nullable? | |
974 | repo_type = Column(String(255), nullable=False) # 'hg' or 'git' |
|
974 | repo_type = Column(String(255), nullable=False) # 'hg' or 'git' | |
975 | owner_id = Column('user_id', Integer(), ForeignKey('users.user_id'), nullable=False) |
|
975 | owner_id = Column('user_id', Integer(), ForeignKey('users.user_id'), nullable=False) | |
976 | private = Column(Boolean(), nullable=False) |
|
976 | private = Column(Boolean(), nullable=False) | |
977 | enable_statistics = Column("statistics", Boolean(), nullable=False, default=True) |
|
977 | enable_statistics = Column("statistics", Boolean(), nullable=False, default=True) | |
978 | enable_downloads = Column("downloads", Boolean(), nullable=False, default=True) |
|
978 | enable_downloads = Column("downloads", Boolean(), nullable=False, default=True) | |
979 | description = Column(Unicode(10000), nullable=False) |
|
979 | description = Column(Unicode(10000), nullable=False) | |
980 | created_on = Column(DateTime(timezone=False), nullable=False, default=datetime.datetime.now) |
|
980 | created_on = Column(DateTime(timezone=False), nullable=False, default=datetime.datetime.now) | |
981 | updated_on = Column(DateTime(timezone=False), nullable=False, default=datetime.datetime.now) |
|
981 | updated_on = Column(DateTime(timezone=False), nullable=False, default=datetime.datetime.now) | |
982 | _landing_revision = Column("landing_revision", String(255), nullable=False) |
|
982 | _landing_revision = Column("landing_revision", String(255), nullable=False) | |
983 | _changeset_cache = Column("changeset_cache", LargeBinary(), nullable=True) # JSON data # FIXME: not nullable? |
|
983 | _changeset_cache = Column("changeset_cache", LargeBinary(), nullable=True) # JSON data # FIXME: not nullable? | |
984 |
|
984 | |||
985 | fork_id = Column(Integer(), ForeignKey('repositories.repo_id'), nullable=True) |
|
985 | fork_id = Column(Integer(), ForeignKey('repositories.repo_id'), nullable=True) | |
986 | group_id = Column(Integer(), ForeignKey('groups.group_id'), nullable=True) |
|
986 | group_id = Column(Integer(), ForeignKey('groups.group_id'), nullable=True) | |
987 |
|
987 | |||
988 | owner = relationship('User') |
|
988 | owner = relationship('User') | |
989 | fork = relationship('Repository', remote_side=repo_id) |
|
989 | fork = relationship('Repository', remote_side=repo_id) | |
990 | group = relationship('RepoGroup') |
|
990 | group = relationship('RepoGroup') | |
991 | repo_to_perm = relationship('UserRepoToPerm', cascade='all', order_by='UserRepoToPerm.repo_to_perm_id') |
|
991 | repo_to_perm = relationship('UserRepoToPerm', cascade='all', order_by='UserRepoToPerm.repo_to_perm_id') | |
992 | users_group_to_perm = relationship('UserGroupRepoToPerm', cascade='all') |
|
992 | users_group_to_perm = relationship('UserGroupRepoToPerm', cascade='all') | |
993 | stats = relationship('Statistics', cascade='all', uselist=False) |
|
993 | stats = relationship('Statistics', cascade='all', uselist=False) | |
994 |
|
994 | |||
995 | followers = relationship('UserFollowing', |
|
995 | followers = relationship('UserFollowing', | |
996 | primaryjoin='UserFollowing.follows_repository_id==Repository.repo_id', |
|
996 | primaryjoin='UserFollowing.follows_repository_id==Repository.repo_id', | |
997 | cascade='all') |
|
997 | cascade='all') | |
998 | extra_fields = relationship('RepositoryField', |
|
998 | extra_fields = relationship('RepositoryField', | |
999 | cascade="all, delete-orphan") |
|
999 | cascade="all, delete-orphan") | |
1000 |
|
1000 | |||
1001 | logs = relationship('UserLog') |
|
1001 | logs = relationship('UserLog') | |
1002 | comments = relationship('ChangesetComment', cascade="all, delete-orphan") |
|
1002 | comments = relationship('ChangesetComment', cascade="all, delete-orphan") | |
1003 |
|
1003 | |||
1004 | pull_requests_org = relationship('PullRequest', |
|
1004 | pull_requests_org = relationship('PullRequest', | |
1005 | primaryjoin='PullRequest.org_repo_id==Repository.repo_id', |
|
1005 | primaryjoin='PullRequest.org_repo_id==Repository.repo_id', | |
1006 | cascade="all, delete-orphan") |
|
1006 | cascade="all, delete-orphan") | |
1007 |
|
1007 | |||
1008 | pull_requests_other = relationship('PullRequest', |
|
1008 | pull_requests_other = relationship('PullRequest', | |
1009 | primaryjoin='PullRequest.other_repo_id==Repository.repo_id', |
|
1009 | primaryjoin='PullRequest.other_repo_id==Repository.repo_id', | |
1010 | cascade="all, delete-orphan") |
|
1010 | cascade="all, delete-orphan") | |
1011 |
|
1011 | |||
1012 | def __unicode__(self): |
|
1012 | def __unicode__(self): | |
1013 | return u"<%s('%s:%s')>" % (self.__class__.__name__, self.repo_id, |
|
1013 | return u"<%s('%s:%s')>" % (self.__class__.__name__, self.repo_id, | |
1014 | safe_unicode(self.repo_name)) |
|
1014 | safe_unicode(self.repo_name)) | |
1015 |
|
1015 | |||
1016 | @hybrid_property |
|
1016 | @hybrid_property | |
1017 | def landing_rev(self): |
|
1017 | def landing_rev(self): | |
1018 | # always should return [rev_type, rev] |
|
1018 | # always should return [rev_type, rev] | |
1019 | if self._landing_revision: |
|
1019 | if self._landing_revision: | |
1020 | _rev_info = self._landing_revision.split(':') |
|
1020 | _rev_info = self._landing_revision.split(':') | |
1021 | if len(_rev_info) < 2: |
|
1021 | if len(_rev_info) < 2: | |
1022 | _rev_info.insert(0, 'rev') |
|
1022 | _rev_info.insert(0, 'rev') | |
1023 | return [_rev_info[0], _rev_info[1]] |
|
1023 | return [_rev_info[0], _rev_info[1]] | |
1024 | return [None, None] |
|
1024 | return [None, None] | |
1025 |
|
1025 | |||
1026 | @landing_rev.setter |
|
1026 | @landing_rev.setter | |
1027 | def landing_rev(self, val): |
|
1027 | def landing_rev(self, val): | |
1028 | if ':' not in val: |
|
1028 | if ':' not in val: | |
1029 | raise ValueError('value must be delimited with `:` and consist ' |
|
1029 | raise ValueError('value must be delimited with `:` and consist ' | |
1030 | 'of <rev_type>:<rev>, got %s instead' % val) |
|
1030 | 'of <rev_type>:<rev>, got %s instead' % val) | |
1031 | self._landing_revision = val |
|
1031 | self._landing_revision = val | |
1032 |
|
1032 | |||
1033 | @hybrid_property |
|
1033 | @hybrid_property | |
1034 | def changeset_cache(self): |
|
1034 | def changeset_cache(self): | |
1035 | try: |
|
1035 | try: | |
1036 | cs_cache = json.loads(self._changeset_cache) # might raise on bad data |
|
1036 | cs_cache = json.loads(self._changeset_cache) # might raise on bad data | |
1037 | cs_cache['raw_id'] # verify data, raise exception on error |
|
1037 | cs_cache['raw_id'] # verify data, raise exception on error | |
1038 | return cs_cache |
|
1038 | return cs_cache | |
1039 | except (TypeError, KeyError, ValueError): |
|
1039 | except (TypeError, KeyError, ValueError): | |
1040 | return EmptyChangeset().__json__() |
|
1040 | return EmptyChangeset().__json__() | |
1041 |
|
1041 | |||
1042 | @changeset_cache.setter |
|
1042 | @changeset_cache.setter | |
1043 | def changeset_cache(self, val): |
|
1043 | def changeset_cache(self, val): | |
1044 | try: |
|
1044 | try: | |
1045 | self._changeset_cache = json.dumps(val) |
|
1045 | self._changeset_cache = json.dumps(val) | |
1046 | except Exception: |
|
1046 | except Exception: | |
1047 | log.error(traceback.format_exc()) |
|
1047 | log.error(traceback.format_exc()) | |
1048 |
|
1048 | |||
1049 | @classmethod |
|
1049 | @classmethod | |
1050 | def query(cls, sorted=False): |
|
1050 | def query(cls, sorted=False): | |
1051 | """Add Repository-specific helpers for common query constructs. |
|
1051 | """Add Repository-specific helpers for common query constructs. | |
1052 |
|
1052 | |||
1053 | sorted: if True, apply the default ordering (name, case insensitive). |
|
1053 | sorted: if True, apply the default ordering (name, case insensitive). | |
1054 | """ |
|
1054 | """ | |
1055 | q = super(Repository, cls).query() |
|
1055 | q = super(Repository, cls).query() | |
1056 |
|
1056 | |||
1057 | if sorted: |
|
1057 | if sorted: | |
1058 | q = q.order_by(func.lower(Repository.repo_name)) |
|
1058 | q = q.order_by(func.lower(Repository.repo_name)) | |
1059 |
|
1059 | |||
1060 | return q |
|
1060 | return q | |
1061 |
|
1061 | |||
1062 | @classmethod |
|
1062 | @classmethod | |
1063 | def url_sep(cls): |
|
1063 | def url_sep(cls): | |
1064 | return URL_SEP |
|
1064 | return URL_SEP | |
1065 |
|
1065 | |||
1066 | @classmethod |
|
1066 | @classmethod | |
1067 | def normalize_repo_name(cls, repo_name): |
|
1067 | def normalize_repo_name(cls, repo_name): | |
1068 | """ |
|
1068 | """ | |
1069 | Normalizes os specific repo_name to the format internally stored inside |
|
1069 | Normalizes os specific repo_name to the format internally stored inside | |
1070 | database using URL_SEP |
|
1070 | database using URL_SEP | |
1071 |
|
1071 | |||
1072 | :param cls: |
|
1072 | :param cls: | |
1073 | :param repo_name: |
|
1073 | :param repo_name: | |
1074 | """ |
|
1074 | """ | |
1075 | return cls.url_sep().join(repo_name.split(os.sep)) |
|
1075 | return cls.url_sep().join(repo_name.split(os.sep)) | |
1076 |
|
1076 | |||
1077 | @classmethod |
|
1077 | @classmethod | |
1078 | def guess_instance(cls, value): |
|
1078 | def guess_instance(cls, value): | |
1079 | return super(Repository, cls).guess_instance(value, Repository.get_by_repo_name) |
|
1079 | return super(Repository, cls).guess_instance(value, Repository.get_by_repo_name) | |
1080 |
|
1080 | |||
1081 | @classmethod |
|
1081 | @classmethod | |
1082 | def get_by_repo_name(cls, repo_name, case_insensitive=False): |
|
1082 | def get_by_repo_name(cls, repo_name, case_insensitive=False): | |
1083 | """Get the repo, defaulting to database case sensitivity. |
|
1083 | """Get the repo, defaulting to database case sensitivity. | |
1084 | case_insensitive will be slower and should only be specified if necessary.""" |
|
1084 | case_insensitive will be slower and should only be specified if necessary.""" | |
1085 | if case_insensitive: |
|
1085 | if case_insensitive: | |
1086 | q = Session().query(cls).filter(func.lower(cls.repo_name) == func.lower(repo_name)) |
|
1086 | q = Session().query(cls).filter(func.lower(cls.repo_name) == func.lower(repo_name)) | |
1087 | else: |
|
1087 | else: | |
1088 | q = Session().query(cls).filter(cls.repo_name == repo_name) |
|
1088 | q = Session().query(cls).filter(cls.repo_name == repo_name) | |
1089 | q = q.options(joinedload(Repository.fork)) \ |
|
1089 | q = q.options(joinedload(Repository.fork)) \ | |
1090 | .options(joinedload(Repository.owner)) \ |
|
1090 | .options(joinedload(Repository.owner)) \ | |
1091 | .options(joinedload(Repository.group)) |
|
1091 | .options(joinedload(Repository.group)) | |
1092 | return q.scalar() |
|
1092 | return q.scalar() | |
1093 |
|
1093 | |||
1094 | @classmethod |
|
1094 | @classmethod | |
1095 | def get_by_full_path(cls, repo_full_path): |
|
1095 | def get_by_full_path(cls, repo_full_path): | |
1096 | base_full_path = os.path.realpath(cls.base_path()) |
|
1096 | base_full_path = os.path.realpath(cls.base_path()) | |
1097 | repo_full_path = os.path.realpath(repo_full_path) |
|
1097 | repo_full_path = os.path.realpath(repo_full_path) | |
1098 | assert repo_full_path.startswith(base_full_path + os.path.sep) |
|
1098 | assert repo_full_path.startswith(base_full_path + os.path.sep) | |
1099 | repo_name = repo_full_path[len(base_full_path) + 1:] |
|
1099 | repo_name = repo_full_path[len(base_full_path) + 1:] | |
1100 | repo_name = cls.normalize_repo_name(repo_name) |
|
1100 | repo_name = cls.normalize_repo_name(repo_name) | |
1101 | return cls.get_by_repo_name(repo_name.strip(URL_SEP)) |
|
1101 | return cls.get_by_repo_name(repo_name.strip(URL_SEP)) | |
1102 |
|
1102 | |||
1103 | @classmethod |
|
1103 | @classmethod | |
1104 | def get_repo_forks(cls, repo_id): |
|
1104 | def get_repo_forks(cls, repo_id): | |
1105 | return cls.query().filter(Repository.fork_id == repo_id) |
|
1105 | return cls.query().filter(Repository.fork_id == repo_id) | |
1106 |
|
1106 | |||
1107 | @classmethod |
|
1107 | @classmethod | |
1108 | def base_path(cls): |
|
1108 | def base_path(cls): | |
1109 | """ |
|
1109 | """ | |
1110 | Returns base path where all repos are stored |
|
1110 | Returns base path where all repos are stored | |
1111 |
|
1111 | |||
1112 | :param cls: |
|
1112 | :param cls: | |
1113 | """ |
|
1113 | """ | |
1114 | q = Session().query(Ui) \ |
|
1114 | q = Session().query(Ui) \ | |
1115 | .filter(Ui.ui_key == cls.url_sep()) |
|
1115 | .filter(Ui.ui_key == cls.url_sep()) | |
1116 | q = q.options(FromCache("sql_cache_short", "repository_repo_path")) |
|
1116 | q = q.options(FromCache("sql_cache_short", "repository_repo_path")) | |
1117 | return q.one().ui_value |
|
1117 | return q.one().ui_value | |
1118 |
|
1118 | |||
1119 | @property |
|
1119 | @property | |
1120 | def forks(self): |
|
1120 | def forks(self): | |
1121 | """ |
|
1121 | """ | |
1122 | Return forks of this repo |
|
1122 | Return forks of this repo | |
1123 | """ |
|
1123 | """ | |
1124 | return Repository.get_repo_forks(self.repo_id) |
|
1124 | return Repository.get_repo_forks(self.repo_id) | |
1125 |
|
1125 | |||
1126 | @property |
|
1126 | @property | |
1127 | def parent(self): |
|
1127 | def parent(self): | |
1128 | """ |
|
1128 | """ | |
1129 | Returns fork parent |
|
1129 | Returns fork parent | |
1130 | """ |
|
1130 | """ | |
1131 | return self.fork |
|
1131 | return self.fork | |
1132 |
|
1132 | |||
1133 | @property |
|
1133 | @property | |
1134 | def just_name(self): |
|
1134 | def just_name(self): | |
1135 | return self.repo_name.split(Repository.url_sep())[-1] |
|
1135 | return self.repo_name.split(Repository.url_sep())[-1] | |
1136 |
|
1136 | |||
1137 | @property |
|
1137 | @property | |
1138 | def groups_with_parents(self): |
|
1138 | def groups_with_parents(self): | |
1139 | groups = [] |
|
1139 | groups = [] | |
1140 | group = self.group |
|
1140 | group = self.group | |
1141 | while group is not None: |
|
1141 | while group is not None: | |
1142 | groups.append(group) |
|
1142 | groups.append(group) | |
1143 | group = group.parent_group |
|
1143 | group = group.parent_group | |
1144 | assert group not in groups, group # avoid recursion on bad db content |
|
1144 | assert group not in groups, group # avoid recursion on bad db content | |
1145 | groups.reverse() |
|
1145 | groups.reverse() | |
1146 | return groups |
|
1146 | return groups | |
1147 |
|
1147 | |||
1148 | @LazyProperty |
|
1148 | @LazyProperty | |
1149 | def repo_path(self): |
|
1149 | def repo_path(self): | |
1150 | """ |
|
1150 | """ | |
1151 | Returns base full path for that repository means where it actually |
|
1151 | Returns base full path for that repository means where it actually | |
1152 | exists on a filesystem |
|
1152 | exists on a filesystem | |
1153 | """ |
|
1153 | """ | |
1154 | q = Session().query(Ui).filter(Ui.ui_key == |
|
1154 | q = Session().query(Ui).filter(Ui.ui_key == | |
1155 | Repository.url_sep()) |
|
1155 | Repository.url_sep()) | |
1156 | q = q.options(FromCache("sql_cache_short", "repository_repo_path")) |
|
1156 | q = q.options(FromCache("sql_cache_short", "repository_repo_path")) | |
1157 | return q.one().ui_value |
|
1157 | return q.one().ui_value | |
1158 |
|
1158 | |||
1159 | @property |
|
1159 | @property | |
1160 | def repo_full_path(self): |
|
1160 | def repo_full_path(self): | |
1161 | p = [self.repo_path] |
|
1161 | p = [self.repo_path] | |
1162 | # we need to split the name by / since this is how we store the |
|
1162 | # we need to split the name by / since this is how we store the | |
1163 | # names in the database, but that eventually needs to be converted |
|
1163 | # names in the database, but that eventually needs to be converted | |
1164 | # into a valid system path |
|
1164 | # into a valid system path | |
1165 | p += self.repo_name.split(Repository.url_sep()) |
|
1165 | p += self.repo_name.split(Repository.url_sep()) | |
1166 |
return os.path.join(* |
|
1166 | return os.path.join(*(safe_unicode(d) for d in p)) | |
1167 |
|
1167 | |||
1168 | @property |
|
1168 | @property | |
1169 | def cache_keys(self): |
|
1169 | def cache_keys(self): | |
1170 | """ |
|
1170 | """ | |
1171 | Returns associated cache keys for that repo |
|
1171 | Returns associated cache keys for that repo | |
1172 | """ |
|
1172 | """ | |
1173 | return CacheInvalidation.query() \ |
|
1173 | return CacheInvalidation.query() \ | |
1174 | .filter(CacheInvalidation.cache_args == self.repo_name) \ |
|
1174 | .filter(CacheInvalidation.cache_args == self.repo_name) \ | |
1175 | .order_by(CacheInvalidation.cache_key) \ |
|
1175 | .order_by(CacheInvalidation.cache_key) \ | |
1176 | .all() |
|
1176 | .all() | |
1177 |
|
1177 | |||
1178 | def get_new_name(self, repo_name): |
|
1178 | def get_new_name(self, repo_name): | |
1179 | """ |
|
1179 | """ | |
1180 | returns new full repository name based on assigned group and new new |
|
1180 | returns new full repository name based on assigned group and new new | |
1181 |
|
1181 | |||
1182 | :param group_name: |
|
1182 | :param group_name: | |
1183 | """ |
|
1183 | """ | |
1184 | path_prefix = self.group.full_path_splitted if self.group else [] |
|
1184 | path_prefix = self.group.full_path_splitted if self.group else [] | |
1185 | return Repository.url_sep().join(path_prefix + [repo_name]) |
|
1185 | return Repository.url_sep().join(path_prefix + [repo_name]) | |
1186 |
|
1186 | |||
1187 | @property |
|
1187 | @property | |
1188 | def _ui(self): |
|
1188 | def _ui(self): | |
1189 | """ |
|
1189 | """ | |
1190 | Creates an db based ui object for this repository |
|
1190 | Creates an db based ui object for this repository | |
1191 | """ |
|
1191 | """ | |
1192 | from kallithea.lib.utils import make_ui |
|
1192 | from kallithea.lib.utils import make_ui | |
1193 | return make_ui() |
|
1193 | return make_ui() | |
1194 |
|
1194 | |||
1195 | @classmethod |
|
1195 | @classmethod | |
1196 | def is_valid(cls, repo_name): |
|
1196 | def is_valid(cls, repo_name): | |
1197 | """ |
|
1197 | """ | |
1198 | returns True if given repo name is a valid filesystem repository |
|
1198 | returns True if given repo name is a valid filesystem repository | |
1199 |
|
1199 | |||
1200 | :param cls: |
|
1200 | :param cls: | |
1201 | :param repo_name: |
|
1201 | :param repo_name: | |
1202 | """ |
|
1202 | """ | |
1203 | from kallithea.lib.utils import is_valid_repo |
|
1203 | from kallithea.lib.utils import is_valid_repo | |
1204 |
|
1204 | |||
1205 | return is_valid_repo(repo_name, cls.base_path()) |
|
1205 | return is_valid_repo(repo_name, cls.base_path()) | |
1206 |
|
1206 | |||
1207 | def get_api_data(self, with_revision_names=False, |
|
1207 | def get_api_data(self, with_revision_names=False, | |
1208 | with_pullrequests=False): |
|
1208 | with_pullrequests=False): | |
1209 | """ |
|
1209 | """ | |
1210 | Common function for generating repo api data. |
|
1210 | Common function for generating repo api data. | |
1211 | Optionally, also return tags, branches, bookmarks and PRs. |
|
1211 | Optionally, also return tags, branches, bookmarks and PRs. | |
1212 | """ |
|
1212 | """ | |
1213 | repo = self |
|
1213 | repo = self | |
1214 | data = dict( |
|
1214 | data = dict( | |
1215 | repo_id=repo.repo_id, |
|
1215 | repo_id=repo.repo_id, | |
1216 | repo_name=repo.repo_name, |
|
1216 | repo_name=repo.repo_name, | |
1217 | repo_type=repo.repo_type, |
|
1217 | repo_type=repo.repo_type, | |
1218 | clone_uri=repo.clone_uri, |
|
1218 | clone_uri=repo.clone_uri, | |
1219 | private=repo.private, |
|
1219 | private=repo.private, | |
1220 | created_on=repo.created_on, |
|
1220 | created_on=repo.created_on, | |
1221 | description=repo.description, |
|
1221 | description=repo.description, | |
1222 | landing_rev=repo.landing_rev, |
|
1222 | landing_rev=repo.landing_rev, | |
1223 | owner=repo.owner.username, |
|
1223 | owner=repo.owner.username, | |
1224 | fork_of=repo.fork.repo_name if repo.fork else None, |
|
1224 | fork_of=repo.fork.repo_name if repo.fork else None, | |
1225 | enable_statistics=repo.enable_statistics, |
|
1225 | enable_statistics=repo.enable_statistics, | |
1226 | enable_downloads=repo.enable_downloads, |
|
1226 | enable_downloads=repo.enable_downloads, | |
1227 | last_changeset=repo.changeset_cache, |
|
1227 | last_changeset=repo.changeset_cache, | |
1228 | ) |
|
1228 | ) | |
1229 | if with_revision_names: |
|
1229 | if with_revision_names: | |
1230 | scm_repo = repo.scm_instance_no_cache() |
|
1230 | scm_repo = repo.scm_instance_no_cache() | |
1231 | data.update(dict( |
|
1231 | data.update(dict( | |
1232 | tags=scm_repo.tags, |
|
1232 | tags=scm_repo.tags, | |
1233 | branches=scm_repo.branches, |
|
1233 | branches=scm_repo.branches, | |
1234 | bookmarks=scm_repo.bookmarks, |
|
1234 | bookmarks=scm_repo.bookmarks, | |
1235 | )) |
|
1235 | )) | |
1236 | if with_pullrequests: |
|
1236 | if with_pullrequests: | |
1237 | data['pull_requests'] = repo.pull_requests_other |
|
1237 | data['pull_requests'] = repo.pull_requests_other | |
1238 | rc_config = Setting.get_app_settings() |
|
1238 | rc_config = Setting.get_app_settings() | |
1239 | repository_fields = str2bool(rc_config.get('repository_fields')) |
|
1239 | repository_fields = str2bool(rc_config.get('repository_fields')) | |
1240 | if repository_fields: |
|
1240 | if repository_fields: | |
1241 | for f in self.extra_fields: |
|
1241 | for f in self.extra_fields: | |
1242 | data[f.field_key_prefixed] = f.field_value |
|
1242 | data[f.field_key_prefixed] = f.field_value | |
1243 |
|
1243 | |||
1244 | return data |
|
1244 | return data | |
1245 |
|
1245 | |||
1246 | @property |
|
1246 | @property | |
1247 | def last_db_change(self): |
|
1247 | def last_db_change(self): | |
1248 | return self.updated_on |
|
1248 | return self.updated_on | |
1249 |
|
1249 | |||
1250 | @property |
|
1250 | @property | |
1251 | def clone_uri_hidden(self): |
|
1251 | def clone_uri_hidden(self): | |
1252 | clone_uri = self.clone_uri |
|
1252 | clone_uri = self.clone_uri | |
1253 | if clone_uri: |
|
1253 | if clone_uri: | |
1254 | import urlobject |
|
1254 | import urlobject | |
1255 | url_obj = urlobject.URLObject(self.clone_uri) |
|
1255 | url_obj = urlobject.URLObject(self.clone_uri) | |
1256 | if url_obj.password: |
|
1256 | if url_obj.password: | |
1257 | clone_uri = url_obj.with_password('*****') |
|
1257 | clone_uri = url_obj.with_password('*****') | |
1258 | return clone_uri |
|
1258 | return clone_uri | |
1259 |
|
1259 | |||
1260 | def clone_url(self, clone_uri_tmpl, with_id=False, username=None): |
|
1260 | def clone_url(self, clone_uri_tmpl, with_id=False, username=None): | |
1261 | if '{repo}' not in clone_uri_tmpl and '_{repoid}' not in clone_uri_tmpl: |
|
1261 | if '{repo}' not in clone_uri_tmpl and '_{repoid}' not in clone_uri_tmpl: | |
1262 | log.error("Configured clone_uri_tmpl %r has no '{repo}' or '_{repoid}' and cannot toggle to use repo id URLs", clone_uri_tmpl) |
|
1262 | log.error("Configured clone_uri_tmpl %r has no '{repo}' or '_{repoid}' and cannot toggle to use repo id URLs", clone_uri_tmpl) | |
1263 | elif with_id: |
|
1263 | elif with_id: | |
1264 | clone_uri_tmpl = clone_uri_tmpl.replace('{repo}', '_{repoid}') |
|
1264 | clone_uri_tmpl = clone_uri_tmpl.replace('{repo}', '_{repoid}') | |
1265 | else: |
|
1265 | else: | |
1266 | clone_uri_tmpl = clone_uri_tmpl.replace('_{repoid}', '{repo}') |
|
1266 | clone_uri_tmpl = clone_uri_tmpl.replace('_{repoid}', '{repo}') | |
1267 |
|
1267 | |||
1268 | import kallithea.lib.helpers as h |
|
1268 | import kallithea.lib.helpers as h | |
1269 | prefix_url = h.canonical_url('home') |
|
1269 | prefix_url = h.canonical_url('home') | |
1270 |
|
1270 | |||
1271 | return get_clone_url(clone_uri_tmpl=clone_uri_tmpl, |
|
1271 | return get_clone_url(clone_uri_tmpl=clone_uri_tmpl, | |
1272 | prefix_url=prefix_url, |
|
1272 | prefix_url=prefix_url, | |
1273 | repo_name=self.repo_name, |
|
1273 | repo_name=self.repo_name, | |
1274 | repo_id=self.repo_id, |
|
1274 | repo_id=self.repo_id, | |
1275 | username=username) |
|
1275 | username=username) | |
1276 |
|
1276 | |||
1277 | def set_state(self, state): |
|
1277 | def set_state(self, state): | |
1278 | self.repo_state = state |
|
1278 | self.repo_state = state | |
1279 |
|
1279 | |||
1280 | #========================================================================== |
|
1280 | #========================================================================== | |
1281 | # SCM PROPERTIES |
|
1281 | # SCM PROPERTIES | |
1282 | #========================================================================== |
|
1282 | #========================================================================== | |
1283 |
|
1283 | |||
1284 | def get_changeset(self, rev=None): |
|
1284 | def get_changeset(self, rev=None): | |
1285 | return get_changeset_safe(self.scm_instance, rev) |
|
1285 | return get_changeset_safe(self.scm_instance, rev) | |
1286 |
|
1286 | |||
1287 | def get_landing_changeset(self): |
|
1287 | def get_landing_changeset(self): | |
1288 | """ |
|
1288 | """ | |
1289 | Returns landing changeset, or if that doesn't exist returns the tip |
|
1289 | Returns landing changeset, or if that doesn't exist returns the tip | |
1290 | """ |
|
1290 | """ | |
1291 | _rev_type, _rev = self.landing_rev |
|
1291 | _rev_type, _rev = self.landing_rev | |
1292 | cs = self.get_changeset(_rev) |
|
1292 | cs = self.get_changeset(_rev) | |
1293 | if isinstance(cs, EmptyChangeset): |
|
1293 | if isinstance(cs, EmptyChangeset): | |
1294 | return self.get_changeset() |
|
1294 | return self.get_changeset() | |
1295 | return cs |
|
1295 | return cs | |
1296 |
|
1296 | |||
1297 | def update_changeset_cache(self, cs_cache=None): |
|
1297 | def update_changeset_cache(self, cs_cache=None): | |
1298 | """ |
|
1298 | """ | |
1299 | Update cache of last changeset for repository, keys should be:: |
|
1299 | Update cache of last changeset for repository, keys should be:: | |
1300 |
|
1300 | |||
1301 | short_id |
|
1301 | short_id | |
1302 | raw_id |
|
1302 | raw_id | |
1303 | revision |
|
1303 | revision | |
1304 | message |
|
1304 | message | |
1305 | date |
|
1305 | date | |
1306 | author |
|
1306 | author | |
1307 |
|
1307 | |||
1308 | :param cs_cache: |
|
1308 | :param cs_cache: | |
1309 | """ |
|
1309 | """ | |
1310 | from kallithea.lib.vcs.backends.base import BaseChangeset |
|
1310 | from kallithea.lib.vcs.backends.base import BaseChangeset | |
1311 | if cs_cache is None: |
|
1311 | if cs_cache is None: | |
1312 | cs_cache = EmptyChangeset() |
|
1312 | cs_cache = EmptyChangeset() | |
1313 | # use no-cache version here |
|
1313 | # use no-cache version here | |
1314 | scm_repo = self.scm_instance_no_cache() |
|
1314 | scm_repo = self.scm_instance_no_cache() | |
1315 | if scm_repo: |
|
1315 | if scm_repo: | |
1316 | cs_cache = scm_repo.get_changeset() |
|
1316 | cs_cache = scm_repo.get_changeset() | |
1317 |
|
1317 | |||
1318 | if isinstance(cs_cache, BaseChangeset): |
|
1318 | if isinstance(cs_cache, BaseChangeset): | |
1319 | cs_cache = cs_cache.__json__() |
|
1319 | cs_cache = cs_cache.__json__() | |
1320 |
|
1320 | |||
1321 | if (not self.changeset_cache or cs_cache['raw_id'] != self.changeset_cache['raw_id']): |
|
1321 | if (not self.changeset_cache or cs_cache['raw_id'] != self.changeset_cache['raw_id']): | |
1322 | _default = datetime.datetime.fromtimestamp(0) |
|
1322 | _default = datetime.datetime.fromtimestamp(0) | |
1323 | last_change = cs_cache.get('date') or _default |
|
1323 | last_change = cs_cache.get('date') or _default | |
1324 | log.debug('updated repo %s with new cs cache %s', |
|
1324 | log.debug('updated repo %s with new cs cache %s', | |
1325 | self.repo_name, cs_cache) |
|
1325 | self.repo_name, cs_cache) | |
1326 | self.updated_on = last_change |
|
1326 | self.updated_on = last_change | |
1327 | self.changeset_cache = cs_cache |
|
1327 | self.changeset_cache = cs_cache | |
1328 | Session().commit() |
|
1328 | Session().commit() | |
1329 | else: |
|
1329 | else: | |
1330 | log.debug('changeset_cache for %s already up to date with %s', |
|
1330 | log.debug('changeset_cache for %s already up to date with %s', | |
1331 | self.repo_name, cs_cache['raw_id']) |
|
1331 | self.repo_name, cs_cache['raw_id']) | |
1332 |
|
1332 | |||
1333 | @property |
|
1333 | @property | |
1334 | def tip(self): |
|
1334 | def tip(self): | |
1335 | return self.get_changeset('tip') |
|
1335 | return self.get_changeset('tip') | |
1336 |
|
1336 | |||
1337 | @property |
|
1337 | @property | |
1338 | def author(self): |
|
1338 | def author(self): | |
1339 | return self.tip.author |
|
1339 | return self.tip.author | |
1340 |
|
1340 | |||
1341 | @property |
|
1341 | @property | |
1342 | def last_change(self): |
|
1342 | def last_change(self): | |
1343 | return self.scm_instance.last_change |
|
1343 | return self.scm_instance.last_change | |
1344 |
|
1344 | |||
1345 | def get_comments(self, revisions=None): |
|
1345 | def get_comments(self, revisions=None): | |
1346 | """ |
|
1346 | """ | |
1347 | Returns comments for this repository grouped by revisions |
|
1347 | Returns comments for this repository grouped by revisions | |
1348 |
|
1348 | |||
1349 | :param revisions: filter query by revisions only |
|
1349 | :param revisions: filter query by revisions only | |
1350 | """ |
|
1350 | """ | |
1351 | cmts = ChangesetComment.query() \ |
|
1351 | cmts = ChangesetComment.query() \ | |
1352 | .filter(ChangesetComment.repo == self) |
|
1352 | .filter(ChangesetComment.repo == self) | |
1353 | if revisions is not None: |
|
1353 | if revisions is not None: | |
1354 | if not revisions: |
|
1354 | if not revisions: | |
1355 | return {} # don't use sql 'in' on empty set |
|
1355 | return {} # don't use sql 'in' on empty set | |
1356 | cmts = cmts.filter(ChangesetComment.revision.in_(revisions)) |
|
1356 | cmts = cmts.filter(ChangesetComment.revision.in_(revisions)) | |
1357 | grouped = collections.defaultdict(list) |
|
1357 | grouped = collections.defaultdict(list) | |
1358 | for cmt in cmts.all(): |
|
1358 | for cmt in cmts.all(): | |
1359 | grouped[cmt.revision].append(cmt) |
|
1359 | grouped[cmt.revision].append(cmt) | |
1360 | return grouped |
|
1360 | return grouped | |
1361 |
|
1361 | |||
1362 | def statuses(self, revisions): |
|
1362 | def statuses(self, revisions): | |
1363 | """ |
|
1363 | """ | |
1364 | Returns statuses for this repository. |
|
1364 | Returns statuses for this repository. | |
1365 | PRs without any votes do _not_ show up as unreviewed. |
|
1365 | PRs without any votes do _not_ show up as unreviewed. | |
1366 |
|
1366 | |||
1367 | :param revisions: list of revisions to get statuses for |
|
1367 | :param revisions: list of revisions to get statuses for | |
1368 | """ |
|
1368 | """ | |
1369 | if not revisions: |
|
1369 | if not revisions: | |
1370 | return {} |
|
1370 | return {} | |
1371 |
|
1371 | |||
1372 | statuses = ChangesetStatus.query() \ |
|
1372 | statuses = ChangesetStatus.query() \ | |
1373 | .filter(ChangesetStatus.repo == self) \ |
|
1373 | .filter(ChangesetStatus.repo == self) \ | |
1374 | .filter(ChangesetStatus.version == 0) \ |
|
1374 | .filter(ChangesetStatus.version == 0) \ | |
1375 | .filter(ChangesetStatus.revision.in_(revisions)) |
|
1375 | .filter(ChangesetStatus.revision.in_(revisions)) | |
1376 |
|
1376 | |||
1377 | grouped = {} |
|
1377 | grouped = {} | |
1378 | for stat in statuses.all(): |
|
1378 | for stat in statuses.all(): | |
1379 | pr_id = pr_nice_id = pr_repo = None |
|
1379 | pr_id = pr_nice_id = pr_repo = None | |
1380 | if stat.pull_request: |
|
1380 | if stat.pull_request: | |
1381 | pr_id = stat.pull_request.pull_request_id |
|
1381 | pr_id = stat.pull_request.pull_request_id | |
1382 | pr_nice_id = PullRequest.make_nice_id(pr_id) |
|
1382 | pr_nice_id = PullRequest.make_nice_id(pr_id) | |
1383 | pr_repo = stat.pull_request.other_repo.repo_name |
|
1383 | pr_repo = stat.pull_request.other_repo.repo_name | |
1384 | grouped[stat.revision] = [str(stat.status), stat.status_lbl, |
|
1384 | grouped[stat.revision] = [str(stat.status), stat.status_lbl, | |
1385 | pr_id, pr_repo, pr_nice_id, |
|
1385 | pr_id, pr_repo, pr_nice_id, | |
1386 | stat.author] |
|
1386 | stat.author] | |
1387 | return grouped |
|
1387 | return grouped | |
1388 |
|
1388 | |||
1389 | def _repo_size(self): |
|
1389 | def _repo_size(self): | |
1390 | from kallithea.lib import helpers as h |
|
1390 | from kallithea.lib import helpers as h | |
1391 | log.debug('calculating repository size...') |
|
1391 | log.debug('calculating repository size...') | |
1392 | return h.format_byte_size(self.scm_instance.size) |
|
1392 | return h.format_byte_size(self.scm_instance.size) | |
1393 |
|
1393 | |||
1394 | #========================================================================== |
|
1394 | #========================================================================== | |
1395 | # SCM CACHE INSTANCE |
|
1395 | # SCM CACHE INSTANCE | |
1396 | #========================================================================== |
|
1396 | #========================================================================== | |
1397 |
|
1397 | |||
1398 | def set_invalidate(self): |
|
1398 | def set_invalidate(self): | |
1399 | """ |
|
1399 | """ | |
1400 | Mark caches of this repo as invalid. |
|
1400 | Mark caches of this repo as invalid. | |
1401 | """ |
|
1401 | """ | |
1402 | CacheInvalidation.set_invalidate(self.repo_name) |
|
1402 | CacheInvalidation.set_invalidate(self.repo_name) | |
1403 |
|
1403 | |||
1404 | _scm_instance = None |
|
1404 | _scm_instance = None | |
1405 |
|
1405 | |||
1406 | @property |
|
1406 | @property | |
1407 | def scm_instance(self): |
|
1407 | def scm_instance(self): | |
1408 | if self._scm_instance is None: |
|
1408 | if self._scm_instance is None: | |
1409 | self._scm_instance = self.scm_instance_cached() |
|
1409 | self._scm_instance = self.scm_instance_cached() | |
1410 | return self._scm_instance |
|
1410 | return self._scm_instance | |
1411 |
|
1411 | |||
1412 | def scm_instance_cached(self, valid_cache_keys=None): |
|
1412 | def scm_instance_cached(self, valid_cache_keys=None): | |
1413 | @cache_region('long_term', 'scm_instance_cached') |
|
1413 | @cache_region('long_term', 'scm_instance_cached') | |
1414 | def _c(repo_name): # repo_name is just for the cache key |
|
1414 | def _c(repo_name): # repo_name is just for the cache key | |
1415 | log.debug('Creating new %s scm_instance and populating cache', repo_name) |
|
1415 | log.debug('Creating new %s scm_instance and populating cache', repo_name) | |
1416 | return self.scm_instance_no_cache() |
|
1416 | return self.scm_instance_no_cache() | |
1417 | rn = self.repo_name |
|
1417 | rn = self.repo_name | |
1418 |
|
1418 | |||
1419 | valid = CacheInvalidation.test_and_set_valid(rn, None, valid_cache_keys=valid_cache_keys) |
|
1419 | valid = CacheInvalidation.test_and_set_valid(rn, None, valid_cache_keys=valid_cache_keys) | |
1420 | if not valid: |
|
1420 | if not valid: | |
1421 | log.debug('Cache for %s invalidated, getting new object', rn) |
|
1421 | log.debug('Cache for %s invalidated, getting new object', rn) | |
1422 | region_invalidate(_c, None, 'scm_instance_cached', rn) |
|
1422 | region_invalidate(_c, None, 'scm_instance_cached', rn) | |
1423 | else: |
|
1423 | else: | |
1424 | log.debug('Trying to get scm_instance of %s from cache', rn) |
|
1424 | log.debug('Trying to get scm_instance of %s from cache', rn) | |
1425 | return _c(rn) |
|
1425 | return _c(rn) | |
1426 |
|
1426 | |||
1427 | def scm_instance_no_cache(self): |
|
1427 | def scm_instance_no_cache(self): | |
1428 | repo_full_path = safe_str(self.repo_full_path) |
|
1428 | repo_full_path = safe_str(self.repo_full_path) | |
1429 | alias = get_scm(repo_full_path)[0] |
|
1429 | alias = get_scm(repo_full_path)[0] | |
1430 | log.debug('Creating instance of %s repository from %s', |
|
1430 | log.debug('Creating instance of %s repository from %s', | |
1431 | alias, self.repo_full_path) |
|
1431 | alias, self.repo_full_path) | |
1432 | backend = get_backend(alias) |
|
1432 | backend = get_backend(alias) | |
1433 |
|
1433 | |||
1434 | if alias == 'hg': |
|
1434 | if alias == 'hg': | |
1435 | repo = backend(repo_full_path, create=False, |
|
1435 | repo = backend(repo_full_path, create=False, | |
1436 | baseui=self._ui) |
|
1436 | baseui=self._ui) | |
1437 | else: |
|
1437 | else: | |
1438 | repo = backend(repo_full_path, create=False) |
|
1438 | repo = backend(repo_full_path, create=False) | |
1439 |
|
1439 | |||
1440 | return repo |
|
1440 | return repo | |
1441 |
|
1441 | |||
1442 | def __json__(self): |
|
1442 | def __json__(self): | |
1443 | return dict( |
|
1443 | return dict( | |
1444 | repo_id=self.repo_id, |
|
1444 | repo_id=self.repo_id, | |
1445 | repo_name=self.repo_name, |
|
1445 | repo_name=self.repo_name, | |
1446 | landing_rev=self.landing_rev, |
|
1446 | landing_rev=self.landing_rev, | |
1447 | ) |
|
1447 | ) | |
1448 |
|
1448 | |||
1449 |
|
1449 | |||
1450 | class RepoGroup(Base, BaseDbModel): |
|
1450 | class RepoGroup(Base, BaseDbModel): | |
1451 | __tablename__ = 'groups' |
|
1451 | __tablename__ = 'groups' | |
1452 | __table_args__ = ( |
|
1452 | __table_args__ = ( | |
1453 | _table_args_default_dict, |
|
1453 | _table_args_default_dict, | |
1454 | ) |
|
1454 | ) | |
1455 |
|
1455 | |||
1456 | SEP = ' » ' |
|
1456 | SEP = ' » ' | |
1457 |
|
1457 | |||
1458 | group_id = Column(Integer(), primary_key=True) |
|
1458 | group_id = Column(Integer(), primary_key=True) | |
1459 | group_name = Column(Unicode(255), nullable=False, unique=True) # full path |
|
1459 | group_name = Column(Unicode(255), nullable=False, unique=True) # full path | |
1460 | parent_group_id = Column('group_parent_id', Integer(), ForeignKey('groups.group_id'), nullable=True) |
|
1460 | parent_group_id = Column('group_parent_id', Integer(), ForeignKey('groups.group_id'), nullable=True) | |
1461 | group_description = Column(Unicode(10000), nullable=False) |
|
1461 | group_description = Column(Unicode(10000), nullable=False) | |
1462 | owner_id = Column('user_id', Integer(), ForeignKey('users.user_id'), nullable=False) |
|
1462 | owner_id = Column('user_id', Integer(), ForeignKey('users.user_id'), nullable=False) | |
1463 | created_on = Column(DateTime(timezone=False), nullable=False, default=datetime.datetime.now) |
|
1463 | created_on = Column(DateTime(timezone=False), nullable=False, default=datetime.datetime.now) | |
1464 |
|
1464 | |||
1465 | repo_group_to_perm = relationship('UserRepoGroupToPerm', cascade='all', order_by='UserRepoGroupToPerm.group_to_perm_id') |
|
1465 | repo_group_to_perm = relationship('UserRepoGroupToPerm', cascade='all', order_by='UserRepoGroupToPerm.group_to_perm_id') | |
1466 | users_group_to_perm = relationship('UserGroupRepoGroupToPerm', cascade='all') |
|
1466 | users_group_to_perm = relationship('UserGroupRepoGroupToPerm', cascade='all') | |
1467 | parent_group = relationship('RepoGroup', remote_side=group_id) |
|
1467 | parent_group = relationship('RepoGroup', remote_side=group_id) | |
1468 | owner = relationship('User') |
|
1468 | owner = relationship('User') | |
1469 |
|
1469 | |||
1470 | @classmethod |
|
1470 | @classmethod | |
1471 | def query(cls, sorted=False): |
|
1471 | def query(cls, sorted=False): | |
1472 | """Add RepoGroup-specific helpers for common query constructs. |
|
1472 | """Add RepoGroup-specific helpers for common query constructs. | |
1473 |
|
1473 | |||
1474 | sorted: if True, apply the default ordering (name, case insensitive). |
|
1474 | sorted: if True, apply the default ordering (name, case insensitive). | |
1475 | """ |
|
1475 | """ | |
1476 | q = super(RepoGroup, cls).query() |
|
1476 | q = super(RepoGroup, cls).query() | |
1477 |
|
1477 | |||
1478 | if sorted: |
|
1478 | if sorted: | |
1479 | q = q.order_by(func.lower(RepoGroup.group_name)) |
|
1479 | q = q.order_by(func.lower(RepoGroup.group_name)) | |
1480 |
|
1480 | |||
1481 | return q |
|
1481 | return q | |
1482 |
|
1482 | |||
1483 | def __init__(self, group_name='', parent_group=None): |
|
1483 | def __init__(self, group_name='', parent_group=None): | |
1484 | self.group_name = group_name |
|
1484 | self.group_name = group_name | |
1485 | self.parent_group = parent_group |
|
1485 | self.parent_group = parent_group | |
1486 |
|
1486 | |||
1487 | def __unicode__(self): |
|
1487 | def __unicode__(self): | |
1488 | return u"<%s('id:%s:%s')>" % (self.__class__.__name__, self.group_id, |
|
1488 | return u"<%s('id:%s:%s')>" % (self.__class__.__name__, self.group_id, | |
1489 | self.group_name) |
|
1489 | self.group_name) | |
1490 |
|
1490 | |||
1491 | @classmethod |
|
1491 | @classmethod | |
1492 | def _generate_choice(cls, repo_group): |
|
1492 | def _generate_choice(cls, repo_group): | |
1493 | """Return tuple with group_id and name as html literal""" |
|
1493 | """Return tuple with group_id and name as html literal""" | |
1494 | from webhelpers2.html import literal |
|
1494 | from webhelpers2.html import literal | |
1495 | if repo_group is None: |
|
1495 | if repo_group is None: | |
1496 | return (-1, u'-- %s --' % _('top level')) |
|
1496 | return (-1, u'-- %s --' % _('top level')) | |
1497 | return repo_group.group_id, literal(cls.SEP.join(repo_group.full_path_splitted)) |
|
1497 | return repo_group.group_id, literal(cls.SEP.join(repo_group.full_path_splitted)) | |
1498 |
|
1498 | |||
1499 | @classmethod |
|
1499 | @classmethod | |
1500 | def groups_choices(cls, groups): |
|
1500 | def groups_choices(cls, groups): | |
1501 | """Return tuples with group_id and name as html literal.""" |
|
1501 | """Return tuples with group_id and name as html literal.""" | |
1502 | return sorted((cls._generate_choice(g) for g in groups), |
|
1502 | return sorted((cls._generate_choice(g) for g in groups), | |
1503 | key=lambda c: c[1].split(cls.SEP)) |
|
1503 | key=lambda c: c[1].split(cls.SEP)) | |
1504 |
|
1504 | |||
1505 | @classmethod |
|
1505 | @classmethod | |
1506 | def url_sep(cls): |
|
1506 | def url_sep(cls): | |
1507 | return URL_SEP |
|
1507 | return URL_SEP | |
1508 |
|
1508 | |||
1509 | @classmethod |
|
1509 | @classmethod | |
1510 | def guess_instance(cls, value): |
|
1510 | def guess_instance(cls, value): | |
1511 | return super(RepoGroup, cls).guess_instance(value, RepoGroup.get_by_group_name) |
|
1511 | return super(RepoGroup, cls).guess_instance(value, RepoGroup.get_by_group_name) | |
1512 |
|
1512 | |||
1513 | @classmethod |
|
1513 | @classmethod | |
1514 | def get_by_group_name(cls, group_name, cache=False, case_insensitive=False): |
|
1514 | def get_by_group_name(cls, group_name, cache=False, case_insensitive=False): | |
1515 | group_name = group_name.rstrip('/') |
|
1515 | group_name = group_name.rstrip('/') | |
1516 | if case_insensitive: |
|
1516 | if case_insensitive: | |
1517 | gr = cls.query() \ |
|
1517 | gr = cls.query() \ | |
1518 | .filter(func.lower(cls.group_name) == func.lower(group_name)) |
|
1518 | .filter(func.lower(cls.group_name) == func.lower(group_name)) | |
1519 | else: |
|
1519 | else: | |
1520 | gr = cls.query() \ |
|
1520 | gr = cls.query() \ | |
1521 | .filter(cls.group_name == group_name) |
|
1521 | .filter(cls.group_name == group_name) | |
1522 | if cache: |
|
1522 | if cache: | |
1523 | gr = gr.options(FromCache( |
|
1523 | gr = gr.options(FromCache( | |
1524 | "sql_cache_short", |
|
1524 | "sql_cache_short", | |
1525 | "get_group_%s" % _hash_key(group_name) |
|
1525 | "get_group_%s" % _hash_key(group_name) | |
1526 | ) |
|
1526 | ) | |
1527 | ) |
|
1527 | ) | |
1528 | return gr.scalar() |
|
1528 | return gr.scalar() | |
1529 |
|
1529 | |||
1530 | @property |
|
1530 | @property | |
1531 | def parents(self): |
|
1531 | def parents(self): | |
1532 | groups = [] |
|
1532 | groups = [] | |
1533 | group = self.parent_group |
|
1533 | group = self.parent_group | |
1534 | while group is not None: |
|
1534 | while group is not None: | |
1535 | groups.append(group) |
|
1535 | groups.append(group) | |
1536 | group = group.parent_group |
|
1536 | group = group.parent_group | |
1537 | assert group not in groups, group # avoid recursion on bad db content |
|
1537 | assert group not in groups, group # avoid recursion on bad db content | |
1538 | groups.reverse() |
|
1538 | groups.reverse() | |
1539 | return groups |
|
1539 | return groups | |
1540 |
|
1540 | |||
1541 | @property |
|
1541 | @property | |
1542 | def children(self): |
|
1542 | def children(self): | |
1543 | return RepoGroup.query().filter(RepoGroup.parent_group == self) |
|
1543 | return RepoGroup.query().filter(RepoGroup.parent_group == self) | |
1544 |
|
1544 | |||
1545 | @property |
|
1545 | @property | |
1546 | def name(self): |
|
1546 | def name(self): | |
1547 | return self.group_name.split(RepoGroup.url_sep())[-1] |
|
1547 | return self.group_name.split(RepoGroup.url_sep())[-1] | |
1548 |
|
1548 | |||
1549 | @property |
|
1549 | @property | |
1550 | def full_path(self): |
|
1550 | def full_path(self): | |
1551 | return self.group_name |
|
1551 | return self.group_name | |
1552 |
|
1552 | |||
1553 | @property |
|
1553 | @property | |
1554 | def full_path_splitted(self): |
|
1554 | def full_path_splitted(self): | |
1555 | return self.group_name.split(RepoGroup.url_sep()) |
|
1555 | return self.group_name.split(RepoGroup.url_sep()) | |
1556 |
|
1556 | |||
1557 | @property |
|
1557 | @property | |
1558 | def repositories(self): |
|
1558 | def repositories(self): | |
1559 | return Repository.query(sorted=True).filter_by(group=self) |
|
1559 | return Repository.query(sorted=True).filter_by(group=self) | |
1560 |
|
1560 | |||
1561 | @property |
|
1561 | @property | |
1562 | def repositories_recursive_count(self): |
|
1562 | def repositories_recursive_count(self): | |
1563 | cnt = self.repositories.count() |
|
1563 | cnt = self.repositories.count() | |
1564 |
|
1564 | |||
1565 | def children_count(group): |
|
1565 | def children_count(group): | |
1566 | cnt = 0 |
|
1566 | cnt = 0 | |
1567 | for child in group.children: |
|
1567 | for child in group.children: | |
1568 | cnt += child.repositories.count() |
|
1568 | cnt += child.repositories.count() | |
1569 | cnt += children_count(child) |
|
1569 | cnt += children_count(child) | |
1570 | return cnt |
|
1570 | return cnt | |
1571 |
|
1571 | |||
1572 | return cnt + children_count(self) |
|
1572 | return cnt + children_count(self) | |
1573 |
|
1573 | |||
1574 | def _recursive_objects(self, include_repos=True): |
|
1574 | def _recursive_objects(self, include_repos=True): | |
1575 | all_ = [] |
|
1575 | all_ = [] | |
1576 |
|
1576 | |||
1577 | def _get_members(root_gr): |
|
1577 | def _get_members(root_gr): | |
1578 | if include_repos: |
|
1578 | if include_repos: | |
1579 | for r in root_gr.repositories: |
|
1579 | for r in root_gr.repositories: | |
1580 | all_.append(r) |
|
1580 | all_.append(r) | |
1581 | childs = root_gr.children.all() |
|
1581 | childs = root_gr.children.all() | |
1582 | if childs: |
|
1582 | if childs: | |
1583 | for gr in childs: |
|
1583 | for gr in childs: | |
1584 | all_.append(gr) |
|
1584 | all_.append(gr) | |
1585 | _get_members(gr) |
|
1585 | _get_members(gr) | |
1586 |
|
1586 | |||
1587 | _get_members(self) |
|
1587 | _get_members(self) | |
1588 | return [self] + all_ |
|
1588 | return [self] + all_ | |
1589 |
|
1589 | |||
1590 | def recursive_groups_and_repos(self): |
|
1590 | def recursive_groups_and_repos(self): | |
1591 | """ |
|
1591 | """ | |
1592 | Recursive return all groups, with repositories in those groups |
|
1592 | Recursive return all groups, with repositories in those groups | |
1593 | """ |
|
1593 | """ | |
1594 | return self._recursive_objects() |
|
1594 | return self._recursive_objects() | |
1595 |
|
1595 | |||
1596 | def recursive_groups(self): |
|
1596 | def recursive_groups(self): | |
1597 | """ |
|
1597 | """ | |
1598 | Returns all children groups for this group including children of children |
|
1598 | Returns all children groups for this group including children of children | |
1599 | """ |
|
1599 | """ | |
1600 | return self._recursive_objects(include_repos=False) |
|
1600 | return self._recursive_objects(include_repos=False) | |
1601 |
|
1601 | |||
1602 | def get_new_name(self, group_name): |
|
1602 | def get_new_name(self, group_name): | |
1603 | """ |
|
1603 | """ | |
1604 | returns new full group name based on parent and new name |
|
1604 | returns new full group name based on parent and new name | |
1605 |
|
1605 | |||
1606 | :param group_name: |
|
1606 | :param group_name: | |
1607 | """ |
|
1607 | """ | |
1608 | path_prefix = (self.parent_group.full_path_splitted if |
|
1608 | path_prefix = (self.parent_group.full_path_splitted if | |
1609 | self.parent_group else []) |
|
1609 | self.parent_group else []) | |
1610 | return RepoGroup.url_sep().join(path_prefix + [group_name]) |
|
1610 | return RepoGroup.url_sep().join(path_prefix + [group_name]) | |
1611 |
|
1611 | |||
1612 | def get_api_data(self): |
|
1612 | def get_api_data(self): | |
1613 | """ |
|
1613 | """ | |
1614 | Common function for generating api data |
|
1614 | Common function for generating api data | |
1615 |
|
1615 | |||
1616 | """ |
|
1616 | """ | |
1617 | group = self |
|
1617 | group = self | |
1618 | data = dict( |
|
1618 | data = dict( | |
1619 | group_id=group.group_id, |
|
1619 | group_id=group.group_id, | |
1620 | group_name=group.group_name, |
|
1620 | group_name=group.group_name, | |
1621 | group_description=group.group_description, |
|
1621 | group_description=group.group_description, | |
1622 | parent_group=group.parent_group.group_name if group.parent_group else None, |
|
1622 | parent_group=group.parent_group.group_name if group.parent_group else None, | |
1623 | repositories=[x.repo_name for x in group.repositories], |
|
1623 | repositories=[x.repo_name for x in group.repositories], | |
1624 | owner=group.owner.username |
|
1624 | owner=group.owner.username | |
1625 | ) |
|
1625 | ) | |
1626 | return data |
|
1626 | return data | |
1627 |
|
1627 | |||
1628 |
|
1628 | |||
1629 | class Permission(Base, BaseDbModel): |
|
1629 | class Permission(Base, BaseDbModel): | |
1630 | __tablename__ = 'permissions' |
|
1630 | __tablename__ = 'permissions' | |
1631 | __table_args__ = ( |
|
1631 | __table_args__ = ( | |
1632 | Index('p_perm_name_idx', 'permission_name'), |
|
1632 | Index('p_perm_name_idx', 'permission_name'), | |
1633 | _table_args_default_dict, |
|
1633 | _table_args_default_dict, | |
1634 | ) |
|
1634 | ) | |
1635 |
|
1635 | |||
1636 | PERMS = ( |
|
1636 | PERMS = ( | |
1637 | ('hg.admin', _('Kallithea Administrator')), |
|
1637 | ('hg.admin', _('Kallithea Administrator')), | |
1638 |
|
1638 | |||
1639 | ('repository.none', _('Default user has no access to new repositories')), |
|
1639 | ('repository.none', _('Default user has no access to new repositories')), | |
1640 | ('repository.read', _('Default user has read access to new repositories')), |
|
1640 | ('repository.read', _('Default user has read access to new repositories')), | |
1641 | ('repository.write', _('Default user has write access to new repositories')), |
|
1641 | ('repository.write', _('Default user has write access to new repositories')), | |
1642 | ('repository.admin', _('Default user has admin access to new repositories')), |
|
1642 | ('repository.admin', _('Default user has admin access to new repositories')), | |
1643 |
|
1643 | |||
1644 | ('group.none', _('Default user has no access to new repository groups')), |
|
1644 | ('group.none', _('Default user has no access to new repository groups')), | |
1645 | ('group.read', _('Default user has read access to new repository groups')), |
|
1645 | ('group.read', _('Default user has read access to new repository groups')), | |
1646 | ('group.write', _('Default user has write access to new repository groups')), |
|
1646 | ('group.write', _('Default user has write access to new repository groups')), | |
1647 | ('group.admin', _('Default user has admin access to new repository groups')), |
|
1647 | ('group.admin', _('Default user has admin access to new repository groups')), | |
1648 |
|
1648 | |||
1649 | ('usergroup.none', _('Default user has no access to new user groups')), |
|
1649 | ('usergroup.none', _('Default user has no access to new user groups')), | |
1650 | ('usergroup.read', _('Default user has read access to new user groups')), |
|
1650 | ('usergroup.read', _('Default user has read access to new user groups')), | |
1651 | ('usergroup.write', _('Default user has write access to new user groups')), |
|
1651 | ('usergroup.write', _('Default user has write access to new user groups')), | |
1652 | ('usergroup.admin', _('Default user has admin access to new user groups')), |
|
1652 | ('usergroup.admin', _('Default user has admin access to new user groups')), | |
1653 |
|
1653 | |||
1654 | ('hg.repogroup.create.false', _('Only admins can create repository groups')), |
|
1654 | ('hg.repogroup.create.false', _('Only admins can create repository groups')), | |
1655 | ('hg.repogroup.create.true', _('Non-admins can create repository groups')), |
|
1655 | ('hg.repogroup.create.true', _('Non-admins can create repository groups')), | |
1656 |
|
1656 | |||
1657 | ('hg.usergroup.create.false', _('Only admins can create user groups')), |
|
1657 | ('hg.usergroup.create.false', _('Only admins can create user groups')), | |
1658 | ('hg.usergroup.create.true', _('Non-admins can create user groups')), |
|
1658 | ('hg.usergroup.create.true', _('Non-admins can create user groups')), | |
1659 |
|
1659 | |||
1660 | ('hg.create.none', _('Only admins can create top level repositories')), |
|
1660 | ('hg.create.none', _('Only admins can create top level repositories')), | |
1661 | ('hg.create.repository', _('Non-admins can create top level repositories')), |
|
1661 | ('hg.create.repository', _('Non-admins can create top level repositories')), | |
1662 |
|
1662 | |||
1663 | ('hg.create.write_on_repogroup.true', _('Repository creation enabled with write permission to a repository group')), |
|
1663 | ('hg.create.write_on_repogroup.true', _('Repository creation enabled with write permission to a repository group')), | |
1664 | ('hg.create.write_on_repogroup.false', _('Repository creation disabled with write permission to a repository group')), |
|
1664 | ('hg.create.write_on_repogroup.false', _('Repository creation disabled with write permission to a repository group')), | |
1665 |
|
1665 | |||
1666 | ('hg.fork.none', _('Only admins can fork repositories')), |
|
1666 | ('hg.fork.none', _('Only admins can fork repositories')), | |
1667 | ('hg.fork.repository', _('Non-admins can fork repositories')), |
|
1667 | ('hg.fork.repository', _('Non-admins can fork repositories')), | |
1668 |
|
1668 | |||
1669 | ('hg.register.none', _('Registration disabled')), |
|
1669 | ('hg.register.none', _('Registration disabled')), | |
1670 | ('hg.register.manual_activate', _('User registration with manual account activation')), |
|
1670 | ('hg.register.manual_activate', _('User registration with manual account activation')), | |
1671 | ('hg.register.auto_activate', _('User registration with automatic account activation')), |
|
1671 | ('hg.register.auto_activate', _('User registration with automatic account activation')), | |
1672 |
|
1672 | |||
1673 | ('hg.extern_activate.manual', _('Manual activation of external account')), |
|
1673 | ('hg.extern_activate.manual', _('Manual activation of external account')), | |
1674 | ('hg.extern_activate.auto', _('Automatic activation of external account')), |
|
1674 | ('hg.extern_activate.auto', _('Automatic activation of external account')), | |
1675 | ) |
|
1675 | ) | |
1676 |
|
1676 | |||
1677 | # definition of system default permissions for DEFAULT user |
|
1677 | # definition of system default permissions for DEFAULT user | |
1678 | DEFAULT_USER_PERMISSIONS = ( |
|
1678 | DEFAULT_USER_PERMISSIONS = ( | |
1679 | 'repository.read', |
|
1679 | 'repository.read', | |
1680 | 'group.read', |
|
1680 | 'group.read', | |
1681 | 'usergroup.read', |
|
1681 | 'usergroup.read', | |
1682 | 'hg.create.repository', |
|
1682 | 'hg.create.repository', | |
1683 | 'hg.create.write_on_repogroup.true', |
|
1683 | 'hg.create.write_on_repogroup.true', | |
1684 | 'hg.fork.repository', |
|
1684 | 'hg.fork.repository', | |
1685 | 'hg.register.manual_activate', |
|
1685 | 'hg.register.manual_activate', | |
1686 | 'hg.extern_activate.auto', |
|
1686 | 'hg.extern_activate.auto', | |
1687 | ) |
|
1687 | ) | |
1688 |
|
1688 | |||
1689 | # defines which permissions are more important higher the more important |
|
1689 | # defines which permissions are more important higher the more important | |
1690 | # Weight defines which permissions are more important. |
|
1690 | # Weight defines which permissions are more important. | |
1691 | # The higher number the more important. |
|
1691 | # The higher number the more important. | |
1692 | PERM_WEIGHTS = { |
|
1692 | PERM_WEIGHTS = { | |
1693 | 'repository.none': 0, |
|
1693 | 'repository.none': 0, | |
1694 | 'repository.read': 1, |
|
1694 | 'repository.read': 1, | |
1695 | 'repository.write': 3, |
|
1695 | 'repository.write': 3, | |
1696 | 'repository.admin': 4, |
|
1696 | 'repository.admin': 4, | |
1697 |
|
1697 | |||
1698 | 'group.none': 0, |
|
1698 | 'group.none': 0, | |
1699 | 'group.read': 1, |
|
1699 | 'group.read': 1, | |
1700 | 'group.write': 3, |
|
1700 | 'group.write': 3, | |
1701 | 'group.admin': 4, |
|
1701 | 'group.admin': 4, | |
1702 |
|
1702 | |||
1703 | 'usergroup.none': 0, |
|
1703 | 'usergroup.none': 0, | |
1704 | 'usergroup.read': 1, |
|
1704 | 'usergroup.read': 1, | |
1705 | 'usergroup.write': 3, |
|
1705 | 'usergroup.write': 3, | |
1706 | 'usergroup.admin': 4, |
|
1706 | 'usergroup.admin': 4, | |
1707 |
|
1707 | |||
1708 | 'hg.repogroup.create.false': 0, |
|
1708 | 'hg.repogroup.create.false': 0, | |
1709 | 'hg.repogroup.create.true': 1, |
|
1709 | 'hg.repogroup.create.true': 1, | |
1710 |
|
1710 | |||
1711 | 'hg.usergroup.create.false': 0, |
|
1711 | 'hg.usergroup.create.false': 0, | |
1712 | 'hg.usergroup.create.true': 1, |
|
1712 | 'hg.usergroup.create.true': 1, | |
1713 |
|
1713 | |||
1714 | 'hg.fork.none': 0, |
|
1714 | 'hg.fork.none': 0, | |
1715 | 'hg.fork.repository': 1, |
|
1715 | 'hg.fork.repository': 1, | |
1716 |
|
1716 | |||
1717 | 'hg.create.none': 0, |
|
1717 | 'hg.create.none': 0, | |
1718 | 'hg.create.repository': 1, |
|
1718 | 'hg.create.repository': 1, | |
1719 |
|
1719 | |||
1720 | 'hg.create.write_on_repogroup.false': 0, |
|
1720 | 'hg.create.write_on_repogroup.false': 0, | |
1721 | 'hg.create.write_on_repogroup.true': 1, |
|
1721 | 'hg.create.write_on_repogroup.true': 1, | |
1722 |
|
1722 | |||
1723 | 'hg.register.none': 0, |
|
1723 | 'hg.register.none': 0, | |
1724 | 'hg.register.manual_activate': 1, |
|
1724 | 'hg.register.manual_activate': 1, | |
1725 | 'hg.register.auto_activate': 2, |
|
1725 | 'hg.register.auto_activate': 2, | |
1726 |
|
1726 | |||
1727 | 'hg.extern_activate.manual': 0, |
|
1727 | 'hg.extern_activate.manual': 0, | |
1728 | 'hg.extern_activate.auto': 1, |
|
1728 | 'hg.extern_activate.auto': 1, | |
1729 | } |
|
1729 | } | |
1730 |
|
1730 | |||
1731 | permission_id = Column(Integer(), primary_key=True) |
|
1731 | permission_id = Column(Integer(), primary_key=True) | |
1732 | permission_name = Column(String(255), nullable=False) |
|
1732 | permission_name = Column(String(255), nullable=False) | |
1733 |
|
1733 | |||
1734 | def __unicode__(self): |
|
1734 | def __unicode__(self): | |
1735 | return u"<%s('%s:%s')>" % ( |
|
1735 | return u"<%s('%s:%s')>" % ( | |
1736 | self.__class__.__name__, self.permission_id, self.permission_name |
|
1736 | self.__class__.__name__, self.permission_id, self.permission_name | |
1737 | ) |
|
1737 | ) | |
1738 |
|
1738 | |||
1739 | @classmethod |
|
1739 | @classmethod | |
1740 | def guess_instance(cls, value): |
|
1740 | def guess_instance(cls, value): | |
1741 | return super(Permission, cls).guess_instance(value, Permission.get_by_key) |
|
1741 | return super(Permission, cls).guess_instance(value, Permission.get_by_key) | |
1742 |
|
1742 | |||
1743 | @classmethod |
|
1743 | @classmethod | |
1744 | def get_by_key(cls, key): |
|
1744 | def get_by_key(cls, key): | |
1745 | return cls.query().filter(cls.permission_name == key).scalar() |
|
1745 | return cls.query().filter(cls.permission_name == key).scalar() | |
1746 |
|
1746 | |||
1747 | @classmethod |
|
1747 | @classmethod | |
1748 | def get_default_perms(cls, default_user_id): |
|
1748 | def get_default_perms(cls, default_user_id): | |
1749 | q = Session().query(UserRepoToPerm, Repository, cls) \ |
|
1749 | q = Session().query(UserRepoToPerm, Repository, cls) \ | |
1750 | .join((Repository, UserRepoToPerm.repository_id == Repository.repo_id)) \ |
|
1750 | .join((Repository, UserRepoToPerm.repository_id == Repository.repo_id)) \ | |
1751 | .join((cls, UserRepoToPerm.permission_id == cls.permission_id)) \ |
|
1751 | .join((cls, UserRepoToPerm.permission_id == cls.permission_id)) \ | |
1752 | .filter(UserRepoToPerm.user_id == default_user_id) |
|
1752 | .filter(UserRepoToPerm.user_id == default_user_id) | |
1753 |
|
1753 | |||
1754 | return q.all() |
|
1754 | return q.all() | |
1755 |
|
1755 | |||
1756 | @classmethod |
|
1756 | @classmethod | |
1757 | def get_default_group_perms(cls, default_user_id): |
|
1757 | def get_default_group_perms(cls, default_user_id): | |
1758 | q = Session().query(UserRepoGroupToPerm, RepoGroup, cls) \ |
|
1758 | q = Session().query(UserRepoGroupToPerm, RepoGroup, cls) \ | |
1759 | .join((RepoGroup, UserRepoGroupToPerm.group_id == RepoGroup.group_id)) \ |
|
1759 | .join((RepoGroup, UserRepoGroupToPerm.group_id == RepoGroup.group_id)) \ | |
1760 | .join((cls, UserRepoGroupToPerm.permission_id == cls.permission_id)) \ |
|
1760 | .join((cls, UserRepoGroupToPerm.permission_id == cls.permission_id)) \ | |
1761 | .filter(UserRepoGroupToPerm.user_id == default_user_id) |
|
1761 | .filter(UserRepoGroupToPerm.user_id == default_user_id) | |
1762 |
|
1762 | |||
1763 | return q.all() |
|
1763 | return q.all() | |
1764 |
|
1764 | |||
1765 | @classmethod |
|
1765 | @classmethod | |
1766 | def get_default_user_group_perms(cls, default_user_id): |
|
1766 | def get_default_user_group_perms(cls, default_user_id): | |
1767 | q = Session().query(UserUserGroupToPerm, UserGroup, cls) \ |
|
1767 | q = Session().query(UserUserGroupToPerm, UserGroup, cls) \ | |
1768 | .join((UserGroup, UserUserGroupToPerm.user_group_id == UserGroup.users_group_id)) \ |
|
1768 | .join((UserGroup, UserUserGroupToPerm.user_group_id == UserGroup.users_group_id)) \ | |
1769 | .join((cls, UserUserGroupToPerm.permission_id == cls.permission_id)) \ |
|
1769 | .join((cls, UserUserGroupToPerm.permission_id == cls.permission_id)) \ | |
1770 | .filter(UserUserGroupToPerm.user_id == default_user_id) |
|
1770 | .filter(UserUserGroupToPerm.user_id == default_user_id) | |
1771 |
|
1771 | |||
1772 | return q.all() |
|
1772 | return q.all() | |
1773 |
|
1773 | |||
1774 |
|
1774 | |||
1775 | class UserRepoToPerm(Base, BaseDbModel): |
|
1775 | class UserRepoToPerm(Base, BaseDbModel): | |
1776 | __tablename__ = 'repo_to_perm' |
|
1776 | __tablename__ = 'repo_to_perm' | |
1777 | __table_args__ = ( |
|
1777 | __table_args__ = ( | |
1778 | UniqueConstraint('user_id', 'repository_id', 'permission_id'), |
|
1778 | UniqueConstraint('user_id', 'repository_id', 'permission_id'), | |
1779 | _table_args_default_dict, |
|
1779 | _table_args_default_dict, | |
1780 | ) |
|
1780 | ) | |
1781 |
|
1781 | |||
1782 | repo_to_perm_id = Column(Integer(), primary_key=True) |
|
1782 | repo_to_perm_id = Column(Integer(), primary_key=True) | |
1783 | user_id = Column(Integer(), ForeignKey('users.user_id'), nullable=False) |
|
1783 | user_id = Column(Integer(), ForeignKey('users.user_id'), nullable=False) | |
1784 | permission_id = Column(Integer(), ForeignKey('permissions.permission_id'), nullable=False) |
|
1784 | permission_id = Column(Integer(), ForeignKey('permissions.permission_id'), nullable=False) | |
1785 | repository_id = Column(Integer(), ForeignKey('repositories.repo_id'), nullable=False) |
|
1785 | repository_id = Column(Integer(), ForeignKey('repositories.repo_id'), nullable=False) | |
1786 |
|
1786 | |||
1787 | user = relationship('User') |
|
1787 | user = relationship('User') | |
1788 | repository = relationship('Repository') |
|
1788 | repository = relationship('Repository') | |
1789 | permission = relationship('Permission') |
|
1789 | permission = relationship('Permission') | |
1790 |
|
1790 | |||
1791 | @classmethod |
|
1791 | @classmethod | |
1792 | def create(cls, user, repository, permission): |
|
1792 | def create(cls, user, repository, permission): | |
1793 | n = cls() |
|
1793 | n = cls() | |
1794 | n.user = user |
|
1794 | n.user = user | |
1795 | n.repository = repository |
|
1795 | n.repository = repository | |
1796 | n.permission = permission |
|
1796 | n.permission = permission | |
1797 | Session().add(n) |
|
1797 | Session().add(n) | |
1798 | return n |
|
1798 | return n | |
1799 |
|
1799 | |||
1800 | def __unicode__(self): |
|
1800 | def __unicode__(self): | |
1801 | return u'<%s => %s >' % (self.user, self.repository) |
|
1801 | return u'<%s => %s >' % (self.user, self.repository) | |
1802 |
|
1802 | |||
1803 |
|
1803 | |||
1804 | class UserUserGroupToPerm(Base, BaseDbModel): |
|
1804 | class UserUserGroupToPerm(Base, BaseDbModel): | |
1805 | __tablename__ = 'user_user_group_to_perm' |
|
1805 | __tablename__ = 'user_user_group_to_perm' | |
1806 | __table_args__ = ( |
|
1806 | __table_args__ = ( | |
1807 | UniqueConstraint('user_id', 'user_group_id', 'permission_id'), |
|
1807 | UniqueConstraint('user_id', 'user_group_id', 'permission_id'), | |
1808 | _table_args_default_dict, |
|
1808 | _table_args_default_dict, | |
1809 | ) |
|
1809 | ) | |
1810 |
|
1810 | |||
1811 | user_user_group_to_perm_id = Column(Integer(), primary_key=True) |
|
1811 | user_user_group_to_perm_id = Column(Integer(), primary_key=True) | |
1812 | user_id = Column(Integer(), ForeignKey('users.user_id'), nullable=False) |
|
1812 | user_id = Column(Integer(), ForeignKey('users.user_id'), nullable=False) | |
1813 | permission_id = Column(Integer(), ForeignKey('permissions.permission_id'), nullable=False) |
|
1813 | permission_id = Column(Integer(), ForeignKey('permissions.permission_id'), nullable=False) | |
1814 | user_group_id = Column(Integer(), ForeignKey('users_groups.users_group_id'), nullable=False) |
|
1814 | user_group_id = Column(Integer(), ForeignKey('users_groups.users_group_id'), nullable=False) | |
1815 |
|
1815 | |||
1816 | user = relationship('User') |
|
1816 | user = relationship('User') | |
1817 | user_group = relationship('UserGroup') |
|
1817 | user_group = relationship('UserGroup') | |
1818 | permission = relationship('Permission') |
|
1818 | permission = relationship('Permission') | |
1819 |
|
1819 | |||
1820 | @classmethod |
|
1820 | @classmethod | |
1821 | def create(cls, user, user_group, permission): |
|
1821 | def create(cls, user, user_group, permission): | |
1822 | n = cls() |
|
1822 | n = cls() | |
1823 | n.user = user |
|
1823 | n.user = user | |
1824 | n.user_group = user_group |
|
1824 | n.user_group = user_group | |
1825 | n.permission = permission |
|
1825 | n.permission = permission | |
1826 | Session().add(n) |
|
1826 | Session().add(n) | |
1827 | return n |
|
1827 | return n | |
1828 |
|
1828 | |||
1829 | def __unicode__(self): |
|
1829 | def __unicode__(self): | |
1830 | return u'<%s => %s >' % (self.user, self.user_group) |
|
1830 | return u'<%s => %s >' % (self.user, self.user_group) | |
1831 |
|
1831 | |||
1832 |
|
1832 | |||
1833 | class UserToPerm(Base, BaseDbModel): |
|
1833 | class UserToPerm(Base, BaseDbModel): | |
1834 | __tablename__ = 'user_to_perm' |
|
1834 | __tablename__ = 'user_to_perm' | |
1835 | __table_args__ = ( |
|
1835 | __table_args__ = ( | |
1836 | UniqueConstraint('user_id', 'permission_id'), |
|
1836 | UniqueConstraint('user_id', 'permission_id'), | |
1837 | _table_args_default_dict, |
|
1837 | _table_args_default_dict, | |
1838 | ) |
|
1838 | ) | |
1839 |
|
1839 | |||
1840 | user_to_perm_id = Column(Integer(), primary_key=True) |
|
1840 | user_to_perm_id = Column(Integer(), primary_key=True) | |
1841 | user_id = Column(Integer(), ForeignKey('users.user_id'), nullable=False) |
|
1841 | user_id = Column(Integer(), ForeignKey('users.user_id'), nullable=False) | |
1842 | permission_id = Column(Integer(), ForeignKey('permissions.permission_id'), nullable=False) |
|
1842 | permission_id = Column(Integer(), ForeignKey('permissions.permission_id'), nullable=False) | |
1843 |
|
1843 | |||
1844 | user = relationship('User') |
|
1844 | user = relationship('User') | |
1845 | permission = relationship('Permission') |
|
1845 | permission = relationship('Permission') | |
1846 |
|
1846 | |||
1847 | def __unicode__(self): |
|
1847 | def __unicode__(self): | |
1848 | return u'<%s => %s >' % (self.user, self.permission) |
|
1848 | return u'<%s => %s >' % (self.user, self.permission) | |
1849 |
|
1849 | |||
1850 |
|
1850 | |||
1851 | class UserGroupRepoToPerm(Base, BaseDbModel): |
|
1851 | class UserGroupRepoToPerm(Base, BaseDbModel): | |
1852 | __tablename__ = 'users_group_repo_to_perm' |
|
1852 | __tablename__ = 'users_group_repo_to_perm' | |
1853 | __table_args__ = ( |
|
1853 | __table_args__ = ( | |
1854 | UniqueConstraint('repository_id', 'users_group_id', 'permission_id'), |
|
1854 | UniqueConstraint('repository_id', 'users_group_id', 'permission_id'), | |
1855 | _table_args_default_dict, |
|
1855 | _table_args_default_dict, | |
1856 | ) |
|
1856 | ) | |
1857 |
|
1857 | |||
1858 | users_group_to_perm_id = Column(Integer(), primary_key=True) |
|
1858 | users_group_to_perm_id = Column(Integer(), primary_key=True) | |
1859 | users_group_id = Column(Integer(), ForeignKey('users_groups.users_group_id'), nullable=False) |
|
1859 | users_group_id = Column(Integer(), ForeignKey('users_groups.users_group_id'), nullable=False) | |
1860 | permission_id = Column(Integer(), ForeignKey('permissions.permission_id'), nullable=False) |
|
1860 | permission_id = Column(Integer(), ForeignKey('permissions.permission_id'), nullable=False) | |
1861 | repository_id = Column(Integer(), ForeignKey('repositories.repo_id'), nullable=False) |
|
1861 | repository_id = Column(Integer(), ForeignKey('repositories.repo_id'), nullable=False) | |
1862 |
|
1862 | |||
1863 | users_group = relationship('UserGroup') |
|
1863 | users_group = relationship('UserGroup') | |
1864 | permission = relationship('Permission') |
|
1864 | permission = relationship('Permission') | |
1865 | repository = relationship('Repository') |
|
1865 | repository = relationship('Repository') | |
1866 |
|
1866 | |||
1867 | @classmethod |
|
1867 | @classmethod | |
1868 | def create(cls, users_group, repository, permission): |
|
1868 | def create(cls, users_group, repository, permission): | |
1869 | n = cls() |
|
1869 | n = cls() | |
1870 | n.users_group = users_group |
|
1870 | n.users_group = users_group | |
1871 | n.repository = repository |
|
1871 | n.repository = repository | |
1872 | n.permission = permission |
|
1872 | n.permission = permission | |
1873 | Session().add(n) |
|
1873 | Session().add(n) | |
1874 | return n |
|
1874 | return n | |
1875 |
|
1875 | |||
1876 | def __unicode__(self): |
|
1876 | def __unicode__(self): | |
1877 | return u'<UserGroupRepoToPerm:%s => %s >' % (self.users_group, self.repository) |
|
1877 | return u'<UserGroupRepoToPerm:%s => %s >' % (self.users_group, self.repository) | |
1878 |
|
1878 | |||
1879 |
|
1879 | |||
1880 | class UserGroupUserGroupToPerm(Base, BaseDbModel): |
|
1880 | class UserGroupUserGroupToPerm(Base, BaseDbModel): | |
1881 | __tablename__ = 'user_group_user_group_to_perm' |
|
1881 | __tablename__ = 'user_group_user_group_to_perm' | |
1882 | __table_args__ = ( |
|
1882 | __table_args__ = ( | |
1883 | UniqueConstraint('target_user_group_id', 'user_group_id', 'permission_id'), |
|
1883 | UniqueConstraint('target_user_group_id', 'user_group_id', 'permission_id'), | |
1884 | _table_args_default_dict, |
|
1884 | _table_args_default_dict, | |
1885 | ) |
|
1885 | ) | |
1886 |
|
1886 | |||
1887 | user_group_user_group_to_perm_id = Column(Integer(), primary_key=True) |
|
1887 | user_group_user_group_to_perm_id = Column(Integer(), primary_key=True) | |
1888 | target_user_group_id = Column(Integer(), ForeignKey('users_groups.users_group_id'), nullable=False) |
|
1888 | target_user_group_id = Column(Integer(), ForeignKey('users_groups.users_group_id'), nullable=False) | |
1889 | permission_id = Column(Integer(), ForeignKey('permissions.permission_id'), nullable=False) |
|
1889 | permission_id = Column(Integer(), ForeignKey('permissions.permission_id'), nullable=False) | |
1890 | user_group_id = Column(Integer(), ForeignKey('users_groups.users_group_id'), nullable=False) |
|
1890 | user_group_id = Column(Integer(), ForeignKey('users_groups.users_group_id'), nullable=False) | |
1891 |
|
1891 | |||
1892 | target_user_group = relationship('UserGroup', primaryjoin='UserGroupUserGroupToPerm.target_user_group_id==UserGroup.users_group_id') |
|
1892 | target_user_group = relationship('UserGroup', primaryjoin='UserGroupUserGroupToPerm.target_user_group_id==UserGroup.users_group_id') | |
1893 | user_group = relationship('UserGroup', primaryjoin='UserGroupUserGroupToPerm.user_group_id==UserGroup.users_group_id') |
|
1893 | user_group = relationship('UserGroup', primaryjoin='UserGroupUserGroupToPerm.user_group_id==UserGroup.users_group_id') | |
1894 | permission = relationship('Permission') |
|
1894 | permission = relationship('Permission') | |
1895 |
|
1895 | |||
1896 | @classmethod |
|
1896 | @classmethod | |
1897 | def create(cls, target_user_group, user_group, permission): |
|
1897 | def create(cls, target_user_group, user_group, permission): | |
1898 | n = cls() |
|
1898 | n = cls() | |
1899 | n.target_user_group = target_user_group |
|
1899 | n.target_user_group = target_user_group | |
1900 | n.user_group = user_group |
|
1900 | n.user_group = user_group | |
1901 | n.permission = permission |
|
1901 | n.permission = permission | |
1902 | Session().add(n) |
|
1902 | Session().add(n) | |
1903 | return n |
|
1903 | return n | |
1904 |
|
1904 | |||
1905 | def __unicode__(self): |
|
1905 | def __unicode__(self): | |
1906 | return u'<UserGroupUserGroup:%s => %s >' % (self.target_user_group, self.user_group) |
|
1906 | return u'<UserGroupUserGroup:%s => %s >' % (self.target_user_group, self.user_group) | |
1907 |
|
1907 | |||
1908 |
|
1908 | |||
1909 | class UserGroupToPerm(Base, BaseDbModel): |
|
1909 | class UserGroupToPerm(Base, BaseDbModel): | |
1910 | __tablename__ = 'users_group_to_perm' |
|
1910 | __tablename__ = 'users_group_to_perm' | |
1911 | __table_args__ = ( |
|
1911 | __table_args__ = ( | |
1912 | UniqueConstraint('users_group_id', 'permission_id',), |
|
1912 | UniqueConstraint('users_group_id', 'permission_id',), | |
1913 | _table_args_default_dict, |
|
1913 | _table_args_default_dict, | |
1914 | ) |
|
1914 | ) | |
1915 |
|
1915 | |||
1916 | users_group_to_perm_id = Column(Integer(), primary_key=True) |
|
1916 | users_group_to_perm_id = Column(Integer(), primary_key=True) | |
1917 | users_group_id = Column(Integer(), ForeignKey('users_groups.users_group_id'), nullable=False) |
|
1917 | users_group_id = Column(Integer(), ForeignKey('users_groups.users_group_id'), nullable=False) | |
1918 | permission_id = Column(Integer(), ForeignKey('permissions.permission_id'), nullable=False) |
|
1918 | permission_id = Column(Integer(), ForeignKey('permissions.permission_id'), nullable=False) | |
1919 |
|
1919 | |||
1920 | users_group = relationship('UserGroup') |
|
1920 | users_group = relationship('UserGroup') | |
1921 | permission = relationship('Permission') |
|
1921 | permission = relationship('Permission') | |
1922 |
|
1922 | |||
1923 |
|
1923 | |||
1924 | class UserRepoGroupToPerm(Base, BaseDbModel): |
|
1924 | class UserRepoGroupToPerm(Base, BaseDbModel): | |
1925 | __tablename__ = 'user_repo_group_to_perm' |
|
1925 | __tablename__ = 'user_repo_group_to_perm' | |
1926 | __table_args__ = ( |
|
1926 | __table_args__ = ( | |
1927 | UniqueConstraint('user_id', 'group_id', 'permission_id'), |
|
1927 | UniqueConstraint('user_id', 'group_id', 'permission_id'), | |
1928 | _table_args_default_dict, |
|
1928 | _table_args_default_dict, | |
1929 | ) |
|
1929 | ) | |
1930 |
|
1930 | |||
1931 | group_to_perm_id = Column(Integer(), primary_key=True) |
|
1931 | group_to_perm_id = Column(Integer(), primary_key=True) | |
1932 | user_id = Column(Integer(), ForeignKey('users.user_id'), nullable=False) |
|
1932 | user_id = Column(Integer(), ForeignKey('users.user_id'), nullable=False) | |
1933 | group_id = Column(Integer(), ForeignKey('groups.group_id'), nullable=False) |
|
1933 | group_id = Column(Integer(), ForeignKey('groups.group_id'), nullable=False) | |
1934 | permission_id = Column(Integer(), ForeignKey('permissions.permission_id'), nullable=False) |
|
1934 | permission_id = Column(Integer(), ForeignKey('permissions.permission_id'), nullable=False) | |
1935 |
|
1935 | |||
1936 | user = relationship('User') |
|
1936 | user = relationship('User') | |
1937 | group = relationship('RepoGroup') |
|
1937 | group = relationship('RepoGroup') | |
1938 | permission = relationship('Permission') |
|
1938 | permission = relationship('Permission') | |
1939 |
|
1939 | |||
1940 | @classmethod |
|
1940 | @classmethod | |
1941 | def create(cls, user, repository_group, permission): |
|
1941 | def create(cls, user, repository_group, permission): | |
1942 | n = cls() |
|
1942 | n = cls() | |
1943 | n.user = user |
|
1943 | n.user = user | |
1944 | n.group = repository_group |
|
1944 | n.group = repository_group | |
1945 | n.permission = permission |
|
1945 | n.permission = permission | |
1946 | Session().add(n) |
|
1946 | Session().add(n) | |
1947 | return n |
|
1947 | return n | |
1948 |
|
1948 | |||
1949 |
|
1949 | |||
1950 | class UserGroupRepoGroupToPerm(Base, BaseDbModel): |
|
1950 | class UserGroupRepoGroupToPerm(Base, BaseDbModel): | |
1951 | __tablename__ = 'users_group_repo_group_to_perm' |
|
1951 | __tablename__ = 'users_group_repo_group_to_perm' | |
1952 | __table_args__ = ( |
|
1952 | __table_args__ = ( | |
1953 | UniqueConstraint('users_group_id', 'group_id'), |
|
1953 | UniqueConstraint('users_group_id', 'group_id'), | |
1954 | _table_args_default_dict, |
|
1954 | _table_args_default_dict, | |
1955 | ) |
|
1955 | ) | |
1956 |
|
1956 | |||
1957 | users_group_repo_group_to_perm_id = Column(Integer(), primary_key=True) |
|
1957 | users_group_repo_group_to_perm_id = Column(Integer(), primary_key=True) | |
1958 | users_group_id = Column(Integer(), ForeignKey('users_groups.users_group_id'), nullable=False) |
|
1958 | users_group_id = Column(Integer(), ForeignKey('users_groups.users_group_id'), nullable=False) | |
1959 | group_id = Column(Integer(), ForeignKey('groups.group_id'), nullable=False) |
|
1959 | group_id = Column(Integer(), ForeignKey('groups.group_id'), nullable=False) | |
1960 | permission_id = Column(Integer(), ForeignKey('permissions.permission_id'), nullable=False) |
|
1960 | permission_id = Column(Integer(), ForeignKey('permissions.permission_id'), nullable=False) | |
1961 |
|
1961 | |||
1962 | users_group = relationship('UserGroup') |
|
1962 | users_group = relationship('UserGroup') | |
1963 | permission = relationship('Permission') |
|
1963 | permission = relationship('Permission') | |
1964 | group = relationship('RepoGroup') |
|
1964 | group = relationship('RepoGroup') | |
1965 |
|
1965 | |||
1966 | @classmethod |
|
1966 | @classmethod | |
1967 | def create(cls, user_group, repository_group, permission): |
|
1967 | def create(cls, user_group, repository_group, permission): | |
1968 | n = cls() |
|
1968 | n = cls() | |
1969 | n.users_group = user_group |
|
1969 | n.users_group = user_group | |
1970 | n.group = repository_group |
|
1970 | n.group = repository_group | |
1971 | n.permission = permission |
|
1971 | n.permission = permission | |
1972 | Session().add(n) |
|
1972 | Session().add(n) | |
1973 | return n |
|
1973 | return n | |
1974 |
|
1974 | |||
1975 |
|
1975 | |||
1976 | class Statistics(Base, BaseDbModel): |
|
1976 | class Statistics(Base, BaseDbModel): | |
1977 | __tablename__ = 'statistics' |
|
1977 | __tablename__ = 'statistics' | |
1978 | __table_args__ = ( |
|
1978 | __table_args__ = ( | |
1979 | _table_args_default_dict, |
|
1979 | _table_args_default_dict, | |
1980 | ) |
|
1980 | ) | |
1981 |
|
1981 | |||
1982 | stat_id = Column(Integer(), primary_key=True) |
|
1982 | stat_id = Column(Integer(), primary_key=True) | |
1983 | repository_id = Column(Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=True) |
|
1983 | repository_id = Column(Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=True) | |
1984 | stat_on_revision = Column(Integer(), nullable=False) |
|
1984 | stat_on_revision = Column(Integer(), nullable=False) | |
1985 | commit_activity = Column(LargeBinary(1000000), nullable=False) # JSON data |
|
1985 | commit_activity = Column(LargeBinary(1000000), nullable=False) # JSON data | |
1986 | commit_activity_combined = Column(LargeBinary(), nullable=False) # JSON data |
|
1986 | commit_activity_combined = Column(LargeBinary(), nullable=False) # JSON data | |
1987 | languages = Column(LargeBinary(1000000), nullable=False) # JSON data |
|
1987 | languages = Column(LargeBinary(1000000), nullable=False) # JSON data | |
1988 |
|
1988 | |||
1989 | repository = relationship('Repository', single_parent=True) |
|
1989 | repository = relationship('Repository', single_parent=True) | |
1990 |
|
1990 | |||
1991 |
|
1991 | |||
1992 | class UserFollowing(Base, BaseDbModel): |
|
1992 | class UserFollowing(Base, BaseDbModel): | |
1993 | __tablename__ = 'user_followings' |
|
1993 | __tablename__ = 'user_followings' | |
1994 | __table_args__ = ( |
|
1994 | __table_args__ = ( | |
1995 | UniqueConstraint('user_id', 'follows_repository_id', name='uq_user_followings_user_repo'), |
|
1995 | UniqueConstraint('user_id', 'follows_repository_id', name='uq_user_followings_user_repo'), | |
1996 | UniqueConstraint('user_id', 'follows_user_id', name='uq_user_followings_user_user'), |
|
1996 | UniqueConstraint('user_id', 'follows_user_id', name='uq_user_followings_user_user'), | |
1997 | _table_args_default_dict, |
|
1997 | _table_args_default_dict, | |
1998 | ) |
|
1998 | ) | |
1999 |
|
1999 | |||
2000 | user_following_id = Column(Integer(), primary_key=True) |
|
2000 | user_following_id = Column(Integer(), primary_key=True) | |
2001 | user_id = Column(Integer(), ForeignKey('users.user_id'), nullable=False) |
|
2001 | user_id = Column(Integer(), ForeignKey('users.user_id'), nullable=False) | |
2002 | follows_repository_id = Column(Integer(), ForeignKey('repositories.repo_id'), nullable=True) |
|
2002 | follows_repository_id = Column(Integer(), ForeignKey('repositories.repo_id'), nullable=True) | |
2003 | follows_user_id = Column(Integer(), ForeignKey('users.user_id'), nullable=True) |
|
2003 | follows_user_id = Column(Integer(), ForeignKey('users.user_id'), nullable=True) | |
2004 | follows_from = Column(DateTime(timezone=False), nullable=False, default=datetime.datetime.now) |
|
2004 | follows_from = Column(DateTime(timezone=False), nullable=False, default=datetime.datetime.now) | |
2005 |
|
2005 | |||
2006 | user = relationship('User', primaryjoin='User.user_id==UserFollowing.user_id') |
|
2006 | user = relationship('User', primaryjoin='User.user_id==UserFollowing.user_id') | |
2007 |
|
2007 | |||
2008 | follows_user = relationship('User', primaryjoin='User.user_id==UserFollowing.follows_user_id') |
|
2008 | follows_user = relationship('User', primaryjoin='User.user_id==UserFollowing.follows_user_id') | |
2009 | follows_repository = relationship('Repository', order_by=lambda: func.lower(Repository.repo_name)) |
|
2009 | follows_repository = relationship('Repository', order_by=lambda: func.lower(Repository.repo_name)) | |
2010 |
|
2010 | |||
2011 | @classmethod |
|
2011 | @classmethod | |
2012 | def get_repo_followers(cls, repo_id): |
|
2012 | def get_repo_followers(cls, repo_id): | |
2013 | return cls.query().filter(cls.follows_repository_id == repo_id) |
|
2013 | return cls.query().filter(cls.follows_repository_id == repo_id) | |
2014 |
|
2014 | |||
2015 |
|
2015 | |||
2016 | class CacheInvalidation(Base, BaseDbModel): |
|
2016 | class CacheInvalidation(Base, BaseDbModel): | |
2017 | __tablename__ = 'cache_invalidation' |
|
2017 | __tablename__ = 'cache_invalidation' | |
2018 | __table_args__ = ( |
|
2018 | __table_args__ = ( | |
2019 | Index('key_idx', 'cache_key'), |
|
2019 | Index('key_idx', 'cache_key'), | |
2020 | _table_args_default_dict, |
|
2020 | _table_args_default_dict, | |
2021 | ) |
|
2021 | ) | |
2022 |
|
2022 | |||
2023 | # cache_id, not used |
|
2023 | # cache_id, not used | |
2024 | cache_id = Column(Integer(), primary_key=True) |
|
2024 | cache_id = Column(Integer(), primary_key=True) | |
2025 | # cache_key as created by _get_cache_key |
|
2025 | # cache_key as created by _get_cache_key | |
2026 | cache_key = Column(Unicode(255), nullable=False, unique=True) |
|
2026 | cache_key = Column(Unicode(255), nullable=False, unique=True) | |
2027 | # cache_args is a repo_name |
|
2027 | # cache_args is a repo_name | |
2028 | cache_args = Column(Unicode(255), nullable=False) |
|
2028 | cache_args = Column(Unicode(255), nullable=False) | |
2029 | # instance sets cache_active True when it is caching, other instances set |
|
2029 | # instance sets cache_active True when it is caching, other instances set | |
2030 | # cache_active to False to indicate that this cache is invalid |
|
2030 | # cache_active to False to indicate that this cache is invalid | |
2031 | cache_active = Column(Boolean(), nullable=False, default=False) |
|
2031 | cache_active = Column(Boolean(), nullable=False, default=False) | |
2032 |
|
2032 | |||
2033 | def __init__(self, cache_key, repo_name=''): |
|
2033 | def __init__(self, cache_key, repo_name=''): | |
2034 | self.cache_key = cache_key |
|
2034 | self.cache_key = cache_key | |
2035 | self.cache_args = repo_name |
|
2035 | self.cache_args = repo_name | |
2036 | self.cache_active = False |
|
2036 | self.cache_active = False | |
2037 |
|
2037 | |||
2038 | def __unicode__(self): |
|
2038 | def __unicode__(self): | |
2039 | return u"<%s('%s:%s[%s]')>" % ( |
|
2039 | return u"<%s('%s:%s[%s]')>" % ( | |
2040 | self.__class__.__name__, |
|
2040 | self.__class__.__name__, | |
2041 | self.cache_id, self.cache_key, self.cache_active) |
|
2041 | self.cache_id, self.cache_key, self.cache_active) | |
2042 |
|
2042 | |||
2043 | def _cache_key_partition(self): |
|
2043 | def _cache_key_partition(self): | |
2044 | prefix, repo_name, suffix = self.cache_key.partition(self.cache_args) |
|
2044 | prefix, repo_name, suffix = self.cache_key.partition(self.cache_args) | |
2045 | return prefix, repo_name, suffix |
|
2045 | return prefix, repo_name, suffix | |
2046 |
|
2046 | |||
2047 | def get_prefix(self): |
|
2047 | def get_prefix(self): | |
2048 | """ |
|
2048 | """ | |
2049 | get prefix that might have been used in _get_cache_key to |
|
2049 | get prefix that might have been used in _get_cache_key to | |
2050 | generate self.cache_key. Only used for informational purposes |
|
2050 | generate self.cache_key. Only used for informational purposes | |
2051 | in repo_edit.html. |
|
2051 | in repo_edit.html. | |
2052 | """ |
|
2052 | """ | |
2053 | # prefix, repo_name, suffix |
|
2053 | # prefix, repo_name, suffix | |
2054 | return self._cache_key_partition()[0] |
|
2054 | return self._cache_key_partition()[0] | |
2055 |
|
2055 | |||
2056 | def get_suffix(self): |
|
2056 | def get_suffix(self): | |
2057 | """ |
|
2057 | """ | |
2058 | get suffix that might have been used in _get_cache_key to |
|
2058 | get suffix that might have been used in _get_cache_key to | |
2059 | generate self.cache_key. Only used for informational purposes |
|
2059 | generate self.cache_key. Only used for informational purposes | |
2060 | in repo_edit.html. |
|
2060 | in repo_edit.html. | |
2061 | """ |
|
2061 | """ | |
2062 | # prefix, repo_name, suffix |
|
2062 | # prefix, repo_name, suffix | |
2063 | return self._cache_key_partition()[2] |
|
2063 | return self._cache_key_partition()[2] | |
2064 |
|
2064 | |||
2065 | @classmethod |
|
2065 | @classmethod | |
2066 | def clear_cache(cls): |
|
2066 | def clear_cache(cls): | |
2067 | """ |
|
2067 | """ | |
2068 | Delete all cache keys from database. |
|
2068 | Delete all cache keys from database. | |
2069 | Should only be run when all instances are down and all entries thus stale. |
|
2069 | Should only be run when all instances are down and all entries thus stale. | |
2070 | """ |
|
2070 | """ | |
2071 | cls.query().delete() |
|
2071 | cls.query().delete() | |
2072 | Session().commit() |
|
2072 | Session().commit() | |
2073 |
|
2073 | |||
2074 | @classmethod |
|
2074 | @classmethod | |
2075 | def _get_cache_key(cls, key): |
|
2075 | def _get_cache_key(cls, key): | |
2076 | """ |
|
2076 | """ | |
2077 | Wrapper for generating a unique cache key for this instance and "key". |
|
2077 | Wrapper for generating a unique cache key for this instance and "key". | |
2078 | key must / will start with a repo_name which will be stored in .cache_args . |
|
2078 | key must / will start with a repo_name which will be stored in .cache_args . | |
2079 | """ |
|
2079 | """ | |
2080 | prefix = kallithea.CONFIG.get('instance_id', '') |
|
2080 | prefix = kallithea.CONFIG.get('instance_id', '') | |
2081 | return "%s%s" % (prefix, key) |
|
2081 | return "%s%s" % (prefix, key) | |
2082 |
|
2082 | |||
2083 | @classmethod |
|
2083 | @classmethod | |
2084 | def set_invalidate(cls, repo_name): |
|
2084 | def set_invalidate(cls, repo_name): | |
2085 | """ |
|
2085 | """ | |
2086 | Mark all caches of a repo as invalid in the database. |
|
2086 | Mark all caches of a repo as invalid in the database. | |
2087 | """ |
|
2087 | """ | |
2088 | inv_objs = Session().query(cls).filter(cls.cache_args == repo_name).all() |
|
2088 | inv_objs = Session().query(cls).filter(cls.cache_args == repo_name).all() | |
2089 | log.debug('for repo %s got %s invalidation objects', |
|
2089 | log.debug('for repo %s got %s invalidation objects', | |
2090 | safe_str(repo_name), inv_objs) |
|
2090 | safe_str(repo_name), inv_objs) | |
2091 |
|
2091 | |||
2092 | for inv_obj in inv_objs: |
|
2092 | for inv_obj in inv_objs: | |
2093 | log.debug('marking %s key for invalidation based on repo_name=%s', |
|
2093 | log.debug('marking %s key for invalidation based on repo_name=%s', | |
2094 | inv_obj, safe_str(repo_name)) |
|
2094 | inv_obj, safe_str(repo_name)) | |
2095 | Session().delete(inv_obj) |
|
2095 | Session().delete(inv_obj) | |
2096 | Session().commit() |
|
2096 | Session().commit() | |
2097 |
|
2097 | |||
2098 | @classmethod |
|
2098 | @classmethod | |
2099 | def test_and_set_valid(cls, repo_name, kind, valid_cache_keys=None): |
|
2099 | def test_and_set_valid(cls, repo_name, kind, valid_cache_keys=None): | |
2100 | """ |
|
2100 | """ | |
2101 | Mark this cache key as active and currently cached. |
|
2101 | Mark this cache key as active and currently cached. | |
2102 | Return True if the existing cache registration still was valid. |
|
2102 | Return True if the existing cache registration still was valid. | |
2103 | Return False to indicate that it had been invalidated and caches should be refreshed. |
|
2103 | Return False to indicate that it had been invalidated and caches should be refreshed. | |
2104 | """ |
|
2104 | """ | |
2105 |
|
2105 | |||
2106 | key = (repo_name + '_' + kind) if kind else repo_name |
|
2106 | key = (repo_name + '_' + kind) if kind else repo_name | |
2107 | cache_key = cls._get_cache_key(key) |
|
2107 | cache_key = cls._get_cache_key(key) | |
2108 |
|
2108 | |||
2109 | if valid_cache_keys and cache_key in valid_cache_keys: |
|
2109 | if valid_cache_keys and cache_key in valid_cache_keys: | |
2110 | return True |
|
2110 | return True | |
2111 |
|
2111 | |||
2112 | inv_obj = cls.query().filter(cls.cache_key == cache_key).scalar() |
|
2112 | inv_obj = cls.query().filter(cls.cache_key == cache_key).scalar() | |
2113 | if inv_obj is None: |
|
2113 | if inv_obj is None: | |
2114 | inv_obj = cls(cache_key, repo_name) |
|
2114 | inv_obj = cls(cache_key, repo_name) | |
2115 | Session().add(inv_obj) |
|
2115 | Session().add(inv_obj) | |
2116 | elif inv_obj.cache_active: |
|
2116 | elif inv_obj.cache_active: | |
2117 | return True |
|
2117 | return True | |
2118 | inv_obj.cache_active = True |
|
2118 | inv_obj.cache_active = True | |
2119 | try: |
|
2119 | try: | |
2120 | Session().commit() |
|
2120 | Session().commit() | |
2121 | except sqlalchemy.exc.IntegrityError: |
|
2121 | except sqlalchemy.exc.IntegrityError: | |
2122 | log.error('commit of CacheInvalidation failed - retrying') |
|
2122 | log.error('commit of CacheInvalidation failed - retrying') | |
2123 | Session().rollback() |
|
2123 | Session().rollback() | |
2124 | inv_obj = cls.query().filter(cls.cache_key == cache_key).scalar() |
|
2124 | inv_obj = cls.query().filter(cls.cache_key == cache_key).scalar() | |
2125 | if inv_obj is None: |
|
2125 | if inv_obj is None: | |
2126 | log.error('failed to create CacheInvalidation entry') |
|
2126 | log.error('failed to create CacheInvalidation entry') | |
2127 | # TODO: fail badly? |
|
2127 | # TODO: fail badly? | |
2128 | # else: TOCTOU - another thread added the key at the same time; no further action required |
|
2128 | # else: TOCTOU - another thread added the key at the same time; no further action required | |
2129 | return False |
|
2129 | return False | |
2130 |
|
2130 | |||
2131 | @classmethod |
|
2131 | @classmethod | |
2132 | def get_valid_cache_keys(cls): |
|
2132 | def get_valid_cache_keys(cls): | |
2133 | """ |
|
2133 | """ | |
2134 | Return opaque object with information of which caches still are valid |
|
2134 | Return opaque object with information of which caches still are valid | |
2135 | and can be used without checking for invalidation. |
|
2135 | and can be used without checking for invalidation. | |
2136 | """ |
|
2136 | """ | |
2137 | return set(inv_obj.cache_key for inv_obj in cls.query().filter(cls.cache_active).all()) |
|
2137 | return set(inv_obj.cache_key for inv_obj in cls.query().filter(cls.cache_active).all()) | |
2138 |
|
2138 | |||
2139 |
|
2139 | |||
2140 | class ChangesetComment(Base, BaseDbModel): |
|
2140 | class ChangesetComment(Base, BaseDbModel): | |
2141 | __tablename__ = 'changeset_comments' |
|
2141 | __tablename__ = 'changeset_comments' | |
2142 | __table_args__ = ( |
|
2142 | __table_args__ = ( | |
2143 | Index('cc_revision_idx', 'revision'), |
|
2143 | Index('cc_revision_idx', 'revision'), | |
2144 | Index('cc_pull_request_id_idx', 'pull_request_id'), |
|
2144 | Index('cc_pull_request_id_idx', 'pull_request_id'), | |
2145 | _table_args_default_dict, |
|
2145 | _table_args_default_dict, | |
2146 | ) |
|
2146 | ) | |
2147 |
|
2147 | |||
2148 | comment_id = Column(Integer(), primary_key=True) |
|
2148 | comment_id = Column(Integer(), primary_key=True) | |
2149 | repo_id = Column(Integer(), ForeignKey('repositories.repo_id'), nullable=False) |
|
2149 | repo_id = Column(Integer(), ForeignKey('repositories.repo_id'), nullable=False) | |
2150 | revision = Column(String(40), nullable=True) |
|
2150 | revision = Column(String(40), nullable=True) | |
2151 | pull_request_id = Column(Integer(), ForeignKey('pull_requests.pull_request_id'), nullable=True) |
|
2151 | pull_request_id = Column(Integer(), ForeignKey('pull_requests.pull_request_id'), nullable=True) | |
2152 | line_no = Column(Unicode(10), nullable=True) |
|
2152 | line_no = Column(Unicode(10), nullable=True) | |
2153 | f_path = Column(Unicode(1000), nullable=True) |
|
2153 | f_path = Column(Unicode(1000), nullable=True) | |
2154 | author_id = Column('user_id', Integer(), ForeignKey('users.user_id'), nullable=False) |
|
2154 | author_id = Column('user_id', Integer(), ForeignKey('users.user_id'), nullable=False) | |
2155 | text = Column(UnicodeText(), nullable=False) |
|
2155 | text = Column(UnicodeText(), nullable=False) | |
2156 | created_on = Column(DateTime(timezone=False), nullable=False, default=datetime.datetime.now) |
|
2156 | created_on = Column(DateTime(timezone=False), nullable=False, default=datetime.datetime.now) | |
2157 | modified_at = Column(DateTime(timezone=False), nullable=False, default=datetime.datetime.now) |
|
2157 | modified_at = Column(DateTime(timezone=False), nullable=False, default=datetime.datetime.now) | |
2158 |
|
2158 | |||
2159 | author = relationship('User') |
|
2159 | author = relationship('User') | |
2160 | repo = relationship('Repository') |
|
2160 | repo = relationship('Repository') | |
2161 | # status_change is frequently used directly in templates - make it a lazy |
|
2161 | # status_change is frequently used directly in templates - make it a lazy | |
2162 | # join to avoid fetching each related ChangesetStatus on demand. |
|
2162 | # join to avoid fetching each related ChangesetStatus on demand. | |
2163 | # There will only be one ChangesetStatus referencing each comment so the join will not explode. |
|
2163 | # There will only be one ChangesetStatus referencing each comment so the join will not explode. | |
2164 | status_change = relationship('ChangesetStatus', |
|
2164 | status_change = relationship('ChangesetStatus', | |
2165 | cascade="all, delete-orphan", lazy='joined') |
|
2165 | cascade="all, delete-orphan", lazy='joined') | |
2166 | pull_request = relationship('PullRequest') |
|
2166 | pull_request = relationship('PullRequest') | |
2167 |
|
2167 | |||
2168 | def url(self): |
|
2168 | def url(self): | |
2169 | anchor = "comment-%s" % self.comment_id |
|
2169 | anchor = "comment-%s" % self.comment_id | |
2170 | import kallithea.lib.helpers as h |
|
2170 | import kallithea.lib.helpers as h | |
2171 | if self.revision: |
|
2171 | if self.revision: | |
2172 | return h.url('changeset_home', repo_name=self.repo.repo_name, revision=self.revision, anchor=anchor) |
|
2172 | return h.url('changeset_home', repo_name=self.repo.repo_name, revision=self.revision, anchor=anchor) | |
2173 | elif self.pull_request_id is not None: |
|
2173 | elif self.pull_request_id is not None: | |
2174 | return self.pull_request.url(anchor=anchor) |
|
2174 | return self.pull_request.url(anchor=anchor) | |
2175 |
|
2175 | |||
2176 | def __json__(self): |
|
2176 | def __json__(self): | |
2177 | return dict( |
|
2177 | return dict( | |
2178 | comment_id=self.comment_id, |
|
2178 | comment_id=self.comment_id, | |
2179 | username=self.author.username, |
|
2179 | username=self.author.username, | |
2180 | text=self.text, |
|
2180 | text=self.text, | |
2181 | ) |
|
2181 | ) | |
2182 |
|
2182 | |||
2183 | def deletable(self): |
|
2183 | def deletable(self): | |
2184 | return self.created_on > datetime.datetime.now() - datetime.timedelta(minutes=5) |
|
2184 | return self.created_on > datetime.datetime.now() - datetime.timedelta(minutes=5) | |
2185 |
|
2185 | |||
2186 |
|
2186 | |||
2187 | class ChangesetStatus(Base, BaseDbModel): |
|
2187 | class ChangesetStatus(Base, BaseDbModel): | |
2188 | __tablename__ = 'changeset_statuses' |
|
2188 | __tablename__ = 'changeset_statuses' | |
2189 | __table_args__ = ( |
|
2189 | __table_args__ = ( | |
2190 | Index('cs_revision_idx', 'revision'), |
|
2190 | Index('cs_revision_idx', 'revision'), | |
2191 | Index('cs_version_idx', 'version'), |
|
2191 | Index('cs_version_idx', 'version'), | |
2192 | Index('cs_pull_request_id_idx', 'pull_request_id'), |
|
2192 | Index('cs_pull_request_id_idx', 'pull_request_id'), | |
2193 | Index('cs_changeset_comment_id_idx', 'changeset_comment_id'), |
|
2193 | Index('cs_changeset_comment_id_idx', 'changeset_comment_id'), | |
2194 | Index('cs_pull_request_id_user_id_version_idx', 'pull_request_id', 'user_id', 'version'), |
|
2194 | Index('cs_pull_request_id_user_id_version_idx', 'pull_request_id', 'user_id', 'version'), | |
2195 | Index('cs_repo_id_pull_request_id_idx', 'repo_id', 'pull_request_id'), |
|
2195 | Index('cs_repo_id_pull_request_id_idx', 'repo_id', 'pull_request_id'), | |
2196 | UniqueConstraint('repo_id', 'revision', 'version'), |
|
2196 | UniqueConstraint('repo_id', 'revision', 'version'), | |
2197 | _table_args_default_dict, |
|
2197 | _table_args_default_dict, | |
2198 | ) |
|
2198 | ) | |
2199 |
|
2199 | |||
2200 | STATUS_NOT_REVIEWED = DEFAULT = 'not_reviewed' |
|
2200 | STATUS_NOT_REVIEWED = DEFAULT = 'not_reviewed' | |
2201 | STATUS_APPROVED = 'approved' |
|
2201 | STATUS_APPROVED = 'approved' | |
2202 | STATUS_REJECTED = 'rejected' # is shown as "Not approved" - TODO: change database content / scheme |
|
2202 | STATUS_REJECTED = 'rejected' # is shown as "Not approved" - TODO: change database content / scheme | |
2203 | STATUS_UNDER_REVIEW = 'under_review' |
|
2203 | STATUS_UNDER_REVIEW = 'under_review' | |
2204 |
|
2204 | |||
2205 | STATUSES = [ |
|
2205 | STATUSES = [ | |
2206 | (STATUS_NOT_REVIEWED, _("Not reviewed")), # (no icon) and default |
|
2206 | (STATUS_NOT_REVIEWED, _("Not reviewed")), # (no icon) and default | |
2207 | (STATUS_UNDER_REVIEW, _("Under review")), |
|
2207 | (STATUS_UNDER_REVIEW, _("Under review")), | |
2208 | (STATUS_REJECTED, _("Not approved")), |
|
2208 | (STATUS_REJECTED, _("Not approved")), | |
2209 | (STATUS_APPROVED, _("Approved")), |
|
2209 | (STATUS_APPROVED, _("Approved")), | |
2210 | ] |
|
2210 | ] | |
2211 | STATUSES_DICT = dict(STATUSES) |
|
2211 | STATUSES_DICT = dict(STATUSES) | |
2212 |
|
2212 | |||
2213 | changeset_status_id = Column(Integer(), primary_key=True) |
|
2213 | changeset_status_id = Column(Integer(), primary_key=True) | |
2214 | repo_id = Column(Integer(), ForeignKey('repositories.repo_id'), nullable=False) |
|
2214 | repo_id = Column(Integer(), ForeignKey('repositories.repo_id'), nullable=False) | |
2215 | user_id = Column(Integer(), ForeignKey('users.user_id'), nullable=False) |
|
2215 | user_id = Column(Integer(), ForeignKey('users.user_id'), nullable=False) | |
2216 | revision = Column(String(40), nullable=True) |
|
2216 | revision = Column(String(40), nullable=True) | |
2217 | status = Column(String(128), nullable=False, default=DEFAULT) |
|
2217 | status = Column(String(128), nullable=False, default=DEFAULT) | |
2218 | comment_id = Column('changeset_comment_id', Integer(), ForeignKey('changeset_comments.comment_id'), nullable=False) |
|
2218 | comment_id = Column('changeset_comment_id', Integer(), ForeignKey('changeset_comments.comment_id'), nullable=False) | |
2219 | modified_at = Column(DateTime(), nullable=False, default=datetime.datetime.now) |
|
2219 | modified_at = Column(DateTime(), nullable=False, default=datetime.datetime.now) | |
2220 | version = Column(Integer(), nullable=False, default=0) |
|
2220 | version = Column(Integer(), nullable=False, default=0) | |
2221 | pull_request_id = Column(Integer(), ForeignKey('pull_requests.pull_request_id'), nullable=True) |
|
2221 | pull_request_id = Column(Integer(), ForeignKey('pull_requests.pull_request_id'), nullable=True) | |
2222 |
|
2222 | |||
2223 | author = relationship('User') |
|
2223 | author = relationship('User') | |
2224 | repo = relationship('Repository') |
|
2224 | repo = relationship('Repository') | |
2225 | comment = relationship('ChangesetComment') |
|
2225 | comment = relationship('ChangesetComment') | |
2226 | pull_request = relationship('PullRequest') |
|
2226 | pull_request = relationship('PullRequest') | |
2227 |
|
2227 | |||
2228 | def __unicode__(self): |
|
2228 | def __unicode__(self): | |
2229 | return u"<%s('%s:%s')>" % ( |
|
2229 | return u"<%s('%s:%s')>" % ( | |
2230 | self.__class__.__name__, |
|
2230 | self.__class__.__name__, | |
2231 | self.status, self.author |
|
2231 | self.status, self.author | |
2232 | ) |
|
2232 | ) | |
2233 |
|
2233 | |||
2234 | @classmethod |
|
2234 | @classmethod | |
2235 | def get_status_lbl(cls, value): |
|
2235 | def get_status_lbl(cls, value): | |
2236 | return cls.STATUSES_DICT.get(value) |
|
2236 | return cls.STATUSES_DICT.get(value) | |
2237 |
|
2237 | |||
2238 | @property |
|
2238 | @property | |
2239 | def status_lbl(self): |
|
2239 | def status_lbl(self): | |
2240 | return ChangesetStatus.get_status_lbl(self.status) |
|
2240 | return ChangesetStatus.get_status_lbl(self.status) | |
2241 |
|
2241 | |||
2242 | def __json__(self): |
|
2242 | def __json__(self): | |
2243 | return dict( |
|
2243 | return dict( | |
2244 | status=self.status, |
|
2244 | status=self.status, | |
2245 | modified_at=self.modified_at.replace(microsecond=0), |
|
2245 | modified_at=self.modified_at.replace(microsecond=0), | |
2246 | reviewer=self.author.username, |
|
2246 | reviewer=self.author.username, | |
2247 | ) |
|
2247 | ) | |
2248 |
|
2248 | |||
2249 |
|
2249 | |||
2250 | class PullRequest(Base, BaseDbModel): |
|
2250 | class PullRequest(Base, BaseDbModel): | |
2251 | __tablename__ = 'pull_requests' |
|
2251 | __tablename__ = 'pull_requests' | |
2252 | __table_args__ = ( |
|
2252 | __table_args__ = ( | |
2253 | Index('pr_org_repo_id_idx', 'org_repo_id'), |
|
2253 | Index('pr_org_repo_id_idx', 'org_repo_id'), | |
2254 | Index('pr_other_repo_id_idx', 'other_repo_id'), |
|
2254 | Index('pr_other_repo_id_idx', 'other_repo_id'), | |
2255 | _table_args_default_dict, |
|
2255 | _table_args_default_dict, | |
2256 | ) |
|
2256 | ) | |
2257 |
|
2257 | |||
2258 | # values for .status |
|
2258 | # values for .status | |
2259 | STATUS_NEW = u'new' |
|
2259 | STATUS_NEW = u'new' | |
2260 | STATUS_CLOSED = u'closed' |
|
2260 | STATUS_CLOSED = u'closed' | |
2261 |
|
2261 | |||
2262 | pull_request_id = Column(Integer(), primary_key=True) |
|
2262 | pull_request_id = Column(Integer(), primary_key=True) | |
2263 | title = Column(Unicode(255), nullable=False) |
|
2263 | title = Column(Unicode(255), nullable=False) | |
2264 | description = Column(UnicodeText(), nullable=False) |
|
2264 | description = Column(UnicodeText(), nullable=False) | |
2265 | status = Column(Unicode(255), nullable=False, default=STATUS_NEW) # only for closedness, not approve/reject/etc |
|
2265 | status = Column(Unicode(255), nullable=False, default=STATUS_NEW) # only for closedness, not approve/reject/etc | |
2266 | created_on = Column(DateTime(timezone=False), nullable=False, default=datetime.datetime.now) |
|
2266 | created_on = Column(DateTime(timezone=False), nullable=False, default=datetime.datetime.now) | |
2267 | updated_on = Column(DateTime(timezone=False), nullable=False, default=datetime.datetime.now) |
|
2267 | updated_on = Column(DateTime(timezone=False), nullable=False, default=datetime.datetime.now) | |
2268 | owner_id = Column('user_id', Integer(), ForeignKey('users.user_id'), nullable=False) |
|
2268 | owner_id = Column('user_id', Integer(), ForeignKey('users.user_id'), nullable=False) | |
2269 | _revisions = Column('revisions', UnicodeText(), nullable=False) |
|
2269 | _revisions = Column('revisions', UnicodeText(), nullable=False) | |
2270 | org_repo_id = Column(Integer(), ForeignKey('repositories.repo_id'), nullable=False) |
|
2270 | org_repo_id = Column(Integer(), ForeignKey('repositories.repo_id'), nullable=False) | |
2271 | org_ref = Column(Unicode(255), nullable=False) |
|
2271 | org_ref = Column(Unicode(255), nullable=False) | |
2272 | other_repo_id = Column(Integer(), ForeignKey('repositories.repo_id'), nullable=False) |
|
2272 | other_repo_id = Column(Integer(), ForeignKey('repositories.repo_id'), nullable=False) | |
2273 | other_ref = Column(Unicode(255), nullable=False) |
|
2273 | other_ref = Column(Unicode(255), nullable=False) | |
2274 |
|
2274 | |||
2275 | @hybrid_property |
|
2275 | @hybrid_property | |
2276 | def revisions(self): |
|
2276 | def revisions(self): | |
2277 | return self._revisions.split(':') |
|
2277 | return self._revisions.split(':') | |
2278 |
|
2278 | |||
2279 | @revisions.setter |
|
2279 | @revisions.setter | |
2280 | def revisions(self, val): |
|
2280 | def revisions(self, val): | |
2281 | self._revisions = safe_unicode(':'.join(val)) |
|
2281 | self._revisions = safe_unicode(':'.join(val)) | |
2282 |
|
2282 | |||
2283 | @property |
|
2283 | @property | |
2284 | def org_ref_parts(self): |
|
2284 | def org_ref_parts(self): | |
2285 | return self.org_ref.split(':') |
|
2285 | return self.org_ref.split(':') | |
2286 |
|
2286 | |||
2287 | @property |
|
2287 | @property | |
2288 | def other_ref_parts(self): |
|
2288 | def other_ref_parts(self): | |
2289 | return self.other_ref.split(':') |
|
2289 | return self.other_ref.split(':') | |
2290 |
|
2290 | |||
2291 | owner = relationship('User') |
|
2291 | owner = relationship('User') | |
2292 | reviewers = relationship('PullRequestReviewer', |
|
2292 | reviewers = relationship('PullRequestReviewer', | |
2293 | cascade="all, delete-orphan") |
|
2293 | cascade="all, delete-orphan") | |
2294 | org_repo = relationship('Repository', primaryjoin='PullRequest.org_repo_id==Repository.repo_id') |
|
2294 | org_repo = relationship('Repository', primaryjoin='PullRequest.org_repo_id==Repository.repo_id') | |
2295 | other_repo = relationship('Repository', primaryjoin='PullRequest.other_repo_id==Repository.repo_id') |
|
2295 | other_repo = relationship('Repository', primaryjoin='PullRequest.other_repo_id==Repository.repo_id') | |
2296 | statuses = relationship('ChangesetStatus', order_by='ChangesetStatus.changeset_status_id') |
|
2296 | statuses = relationship('ChangesetStatus', order_by='ChangesetStatus.changeset_status_id') | |
2297 | comments = relationship('ChangesetComment', order_by='ChangesetComment.comment_id', |
|
2297 | comments = relationship('ChangesetComment', order_by='ChangesetComment.comment_id', | |
2298 | cascade="all, delete-orphan") |
|
2298 | cascade="all, delete-orphan") | |
2299 |
|
2299 | |||
2300 | @classmethod |
|
2300 | @classmethod | |
2301 | def query(cls, reviewer_id=None, include_closed=True, sorted=False): |
|
2301 | def query(cls, reviewer_id=None, include_closed=True, sorted=False): | |
2302 | """Add PullRequest-specific helpers for common query constructs. |
|
2302 | """Add PullRequest-specific helpers for common query constructs. | |
2303 |
|
2303 | |||
2304 | reviewer_id: only PRs with the specified user added as reviewer. |
|
2304 | reviewer_id: only PRs with the specified user added as reviewer. | |
2305 |
|
2305 | |||
2306 | include_closed: if False, do not include closed PRs. |
|
2306 | include_closed: if False, do not include closed PRs. | |
2307 |
|
2307 | |||
2308 | sorted: if True, apply the default ordering (newest first). |
|
2308 | sorted: if True, apply the default ordering (newest first). | |
2309 | """ |
|
2309 | """ | |
2310 | q = super(PullRequest, cls).query() |
|
2310 | q = super(PullRequest, cls).query() | |
2311 |
|
2311 | |||
2312 | if reviewer_id is not None: |
|
2312 | if reviewer_id is not None: | |
2313 | q = q.join(PullRequestReviewer).filter(PullRequestReviewer.user_id == reviewer_id) |
|
2313 | q = q.join(PullRequestReviewer).filter(PullRequestReviewer.user_id == reviewer_id) | |
2314 |
|
2314 | |||
2315 | if not include_closed: |
|
2315 | if not include_closed: | |
2316 | q = q.filter(PullRequest.status != PullRequest.STATUS_CLOSED) |
|
2316 | q = q.filter(PullRequest.status != PullRequest.STATUS_CLOSED) | |
2317 |
|
2317 | |||
2318 | if sorted: |
|
2318 | if sorted: | |
2319 | q = q.order_by(PullRequest.created_on.desc()) |
|
2319 | q = q.order_by(PullRequest.created_on.desc()) | |
2320 |
|
2320 | |||
2321 | return q |
|
2321 | return q | |
2322 |
|
2322 | |||
2323 | def get_reviewer_users(self): |
|
2323 | def get_reviewer_users(self): | |
2324 | """Like .reviewers, but actually returning the users""" |
|
2324 | """Like .reviewers, but actually returning the users""" | |
2325 | return User.query() \ |
|
2325 | return User.query() \ | |
2326 | .join(PullRequestReviewer) \ |
|
2326 | .join(PullRequestReviewer) \ | |
2327 | .filter(PullRequestReviewer.pull_request == self) \ |
|
2327 | .filter(PullRequestReviewer.pull_request == self) \ | |
2328 | .order_by(PullRequestReviewer.pull_request_reviewers_id) \ |
|
2328 | .order_by(PullRequestReviewer.pull_request_reviewers_id) \ | |
2329 | .all() |
|
2329 | .all() | |
2330 |
|
2330 | |||
2331 | def is_closed(self): |
|
2331 | def is_closed(self): | |
2332 | return self.status == self.STATUS_CLOSED |
|
2332 | return self.status == self.STATUS_CLOSED | |
2333 |
|
2333 | |||
2334 | def user_review_status(self, user_id): |
|
2334 | def user_review_status(self, user_id): | |
2335 | """Return the user's latest status votes on PR""" |
|
2335 | """Return the user's latest status votes on PR""" | |
2336 | # note: no filtering on repo - that would be redundant |
|
2336 | # note: no filtering on repo - that would be redundant | |
2337 | status = ChangesetStatus.query() \ |
|
2337 | status = ChangesetStatus.query() \ | |
2338 | .filter(ChangesetStatus.pull_request == self) \ |
|
2338 | .filter(ChangesetStatus.pull_request == self) \ | |
2339 | .filter(ChangesetStatus.user_id == user_id) \ |
|
2339 | .filter(ChangesetStatus.user_id == user_id) \ | |
2340 | .order_by(ChangesetStatus.version) \ |
|
2340 | .order_by(ChangesetStatus.version) \ | |
2341 | .first() |
|
2341 | .first() | |
2342 | return str(status.status) if status else '' |
|
2342 | return str(status.status) if status else '' | |
2343 |
|
2343 | |||
2344 | @classmethod |
|
2344 | @classmethod | |
2345 | def make_nice_id(cls, pull_request_id): |
|
2345 | def make_nice_id(cls, pull_request_id): | |
2346 | '''Return pull request id nicely formatted for displaying''' |
|
2346 | '''Return pull request id nicely formatted for displaying''' | |
2347 | return '#%s' % pull_request_id |
|
2347 | return '#%s' % pull_request_id | |
2348 |
|
2348 | |||
2349 | def nice_id(self): |
|
2349 | def nice_id(self): | |
2350 | '''Return the id of this pull request, nicely formatted for displaying''' |
|
2350 | '''Return the id of this pull request, nicely formatted for displaying''' | |
2351 | return self.make_nice_id(self.pull_request_id) |
|
2351 | return self.make_nice_id(self.pull_request_id) | |
2352 |
|
2352 | |||
2353 | def get_api_data(self): |
|
2353 | def get_api_data(self): | |
2354 | return self.__json__() |
|
2354 | return self.__json__() | |
2355 |
|
2355 | |||
2356 | def __json__(self): |
|
2356 | def __json__(self): | |
2357 | clone_uri_tmpl = kallithea.CONFIG.get('clone_uri_tmpl') or Repository.DEFAULT_CLONE_URI |
|
2357 | clone_uri_tmpl = kallithea.CONFIG.get('clone_uri_tmpl') or Repository.DEFAULT_CLONE_URI | |
2358 | return dict( |
|
2358 | return dict( | |
2359 | pull_request_id=self.pull_request_id, |
|
2359 | pull_request_id=self.pull_request_id, | |
2360 | url=self.url(), |
|
2360 | url=self.url(), | |
2361 | reviewers=self.reviewers, |
|
2361 | reviewers=self.reviewers, | |
2362 | revisions=self.revisions, |
|
2362 | revisions=self.revisions, | |
2363 | owner=self.owner.username, |
|
2363 | owner=self.owner.username, | |
2364 | title=self.title, |
|
2364 | title=self.title, | |
2365 | description=self.description, |
|
2365 | description=self.description, | |
2366 | org_repo_url=self.org_repo.clone_url(clone_uri_tmpl=clone_uri_tmpl), |
|
2366 | org_repo_url=self.org_repo.clone_url(clone_uri_tmpl=clone_uri_tmpl), | |
2367 | org_ref_parts=self.org_ref_parts, |
|
2367 | org_ref_parts=self.org_ref_parts, | |
2368 | other_ref_parts=self.other_ref_parts, |
|
2368 | other_ref_parts=self.other_ref_parts, | |
2369 | status=self.status, |
|
2369 | status=self.status, | |
2370 | comments=self.comments, |
|
2370 | comments=self.comments, | |
2371 | statuses=self.statuses, |
|
2371 | statuses=self.statuses, | |
2372 | ) |
|
2372 | ) | |
2373 |
|
2373 | |||
2374 | def url(self, **kwargs): |
|
2374 | def url(self, **kwargs): | |
2375 | canonical = kwargs.pop('canonical', None) |
|
2375 | canonical = kwargs.pop('canonical', None) | |
2376 | import kallithea.lib.helpers as h |
|
2376 | import kallithea.lib.helpers as h | |
2377 | b = self.org_ref_parts[1] |
|
2377 | b = self.org_ref_parts[1] | |
2378 | if b != self.other_ref_parts[1]: |
|
2378 | if b != self.other_ref_parts[1]: | |
2379 | s = '/_/' + b |
|
2379 | s = '/_/' + b | |
2380 | else: |
|
2380 | else: | |
2381 | s = '/_/' + self.title |
|
2381 | s = '/_/' + self.title | |
2382 | kwargs['extra'] = urlreadable(s) |
|
2382 | kwargs['extra'] = urlreadable(s) | |
2383 | if canonical: |
|
2383 | if canonical: | |
2384 | return h.canonical_url('pullrequest_show', repo_name=self.other_repo.repo_name, |
|
2384 | return h.canonical_url('pullrequest_show', repo_name=self.other_repo.repo_name, | |
2385 | pull_request_id=self.pull_request_id, **kwargs) |
|
2385 | pull_request_id=self.pull_request_id, **kwargs) | |
2386 | return h.url('pullrequest_show', repo_name=self.other_repo.repo_name, |
|
2386 | return h.url('pullrequest_show', repo_name=self.other_repo.repo_name, | |
2387 | pull_request_id=self.pull_request_id, **kwargs) |
|
2387 | pull_request_id=self.pull_request_id, **kwargs) | |
2388 |
|
2388 | |||
2389 |
|
2389 | |||
2390 | class PullRequestReviewer(Base, BaseDbModel): |
|
2390 | class PullRequestReviewer(Base, BaseDbModel): | |
2391 | __tablename__ = 'pull_request_reviewers' |
|
2391 | __tablename__ = 'pull_request_reviewers' | |
2392 | __table_args__ = ( |
|
2392 | __table_args__ = ( | |
2393 | Index('pull_request_reviewers_user_id_idx', 'user_id'), |
|
2393 | Index('pull_request_reviewers_user_id_idx', 'user_id'), | |
2394 | _table_args_default_dict, |
|
2394 | _table_args_default_dict, | |
2395 | ) |
|
2395 | ) | |
2396 |
|
2396 | |||
2397 | def __init__(self, user=None, pull_request=None): |
|
2397 | def __init__(self, user=None, pull_request=None): | |
2398 | self.user = user |
|
2398 | self.user = user | |
2399 | self.pull_request = pull_request |
|
2399 | self.pull_request = pull_request | |
2400 |
|
2400 | |||
2401 | pull_request_reviewers_id = Column('pull_requests_reviewers_id', Integer(), primary_key=True) |
|
2401 | pull_request_reviewers_id = Column('pull_requests_reviewers_id', Integer(), primary_key=True) | |
2402 | pull_request_id = Column(Integer(), ForeignKey('pull_requests.pull_request_id'), nullable=False) |
|
2402 | pull_request_id = Column(Integer(), ForeignKey('pull_requests.pull_request_id'), nullable=False) | |
2403 | user_id = Column(Integer(), ForeignKey('users.user_id'), nullable=False) |
|
2403 | user_id = Column(Integer(), ForeignKey('users.user_id'), nullable=False) | |
2404 |
|
2404 | |||
2405 | user = relationship('User') |
|
2405 | user = relationship('User') | |
2406 | pull_request = relationship('PullRequest') |
|
2406 | pull_request = relationship('PullRequest') | |
2407 |
|
2407 | |||
2408 | def __json__(self): |
|
2408 | def __json__(self): | |
2409 | return dict( |
|
2409 | return dict( | |
2410 | username=self.user.username if self.user else None, |
|
2410 | username=self.user.username if self.user else None, | |
2411 | ) |
|
2411 | ) | |
2412 |
|
2412 | |||
2413 |
|
2413 | |||
2414 | class Notification(object): |
|
2414 | class Notification(object): | |
2415 | __tablename__ = 'notifications' |
|
2415 | __tablename__ = 'notifications' | |
2416 |
|
2416 | |||
2417 | class UserNotification(object): |
|
2417 | class UserNotification(object): | |
2418 | __tablename__ = 'user_to_notification' |
|
2418 | __tablename__ = 'user_to_notification' | |
2419 |
|
2419 | |||
2420 |
|
2420 | |||
2421 | class Gist(Base, BaseDbModel): |
|
2421 | class Gist(Base, BaseDbModel): | |
2422 | __tablename__ = 'gists' |
|
2422 | __tablename__ = 'gists' | |
2423 | __table_args__ = ( |
|
2423 | __table_args__ = ( | |
2424 | Index('g_gist_access_id_idx', 'gist_access_id'), |
|
2424 | Index('g_gist_access_id_idx', 'gist_access_id'), | |
2425 | Index('g_created_on_idx', 'created_on'), |
|
2425 | Index('g_created_on_idx', 'created_on'), | |
2426 | _table_args_default_dict, |
|
2426 | _table_args_default_dict, | |
2427 | ) |
|
2427 | ) | |
2428 |
|
2428 | |||
2429 | GIST_PUBLIC = u'public' |
|
2429 | GIST_PUBLIC = u'public' | |
2430 | GIST_PRIVATE = u'private' |
|
2430 | GIST_PRIVATE = u'private' | |
2431 | DEFAULT_FILENAME = u'gistfile1.txt' |
|
2431 | DEFAULT_FILENAME = u'gistfile1.txt' | |
2432 |
|
2432 | |||
2433 | gist_id = Column(Integer(), primary_key=True) |
|
2433 | gist_id = Column(Integer(), primary_key=True) | |
2434 | gist_access_id = Column(Unicode(250), nullable=False) |
|
2434 | gist_access_id = Column(Unicode(250), nullable=False) | |
2435 | gist_description = Column(UnicodeText(), nullable=False) |
|
2435 | gist_description = Column(UnicodeText(), nullable=False) | |
2436 | owner_id = Column('user_id', Integer(), ForeignKey('users.user_id'), nullable=False) |
|
2436 | owner_id = Column('user_id', Integer(), ForeignKey('users.user_id'), nullable=False) | |
2437 | gist_expires = Column(Float(53), nullable=False) |
|
2437 | gist_expires = Column(Float(53), nullable=False) | |
2438 | gist_type = Column(Unicode(128), nullable=False) |
|
2438 | gist_type = Column(Unicode(128), nullable=False) | |
2439 | created_on = Column(DateTime(timezone=False), nullable=False, default=datetime.datetime.now) |
|
2439 | created_on = Column(DateTime(timezone=False), nullable=False, default=datetime.datetime.now) | |
2440 | modified_at = Column(DateTime(timezone=False), nullable=False, default=datetime.datetime.now) |
|
2440 | modified_at = Column(DateTime(timezone=False), nullable=False, default=datetime.datetime.now) | |
2441 |
|
2441 | |||
2442 | owner = relationship('User') |
|
2442 | owner = relationship('User') | |
2443 |
|
2443 | |||
2444 | @hybrid_property |
|
2444 | @hybrid_property | |
2445 | def is_expired(self): |
|
2445 | def is_expired(self): | |
2446 | return (self.gist_expires != -1) & (time.time() > self.gist_expires) |
|
2446 | return (self.gist_expires != -1) & (time.time() > self.gist_expires) | |
2447 |
|
2447 | |||
2448 | def __repr__(self): |
|
2448 | def __repr__(self): | |
2449 | return '<Gist:[%s]%s>' % (self.gist_type, self.gist_access_id) |
|
2449 | return '<Gist:[%s]%s>' % (self.gist_type, self.gist_access_id) | |
2450 |
|
2450 | |||
2451 | @classmethod |
|
2451 | @classmethod | |
2452 | def guess_instance(cls, value): |
|
2452 | def guess_instance(cls, value): | |
2453 | return super(Gist, cls).guess_instance(value, Gist.get_by_access_id) |
|
2453 | return super(Gist, cls).guess_instance(value, Gist.get_by_access_id) | |
2454 |
|
2454 | |||
2455 | @classmethod |
|
2455 | @classmethod | |
2456 | def get_or_404(cls, id_): |
|
2456 | def get_or_404(cls, id_): | |
2457 | res = cls.query().filter(cls.gist_access_id == id_).scalar() |
|
2457 | res = cls.query().filter(cls.gist_access_id == id_).scalar() | |
2458 | if res is None: |
|
2458 | if res is None: | |
2459 | raise HTTPNotFound |
|
2459 | raise HTTPNotFound | |
2460 | return res |
|
2460 | return res | |
2461 |
|
2461 | |||
2462 | @classmethod |
|
2462 | @classmethod | |
2463 | def get_by_access_id(cls, gist_access_id): |
|
2463 | def get_by_access_id(cls, gist_access_id): | |
2464 | return cls.query().filter(cls.gist_access_id == gist_access_id).scalar() |
|
2464 | return cls.query().filter(cls.gist_access_id == gist_access_id).scalar() | |
2465 |
|
2465 | |||
2466 | def gist_url(self): |
|
2466 | def gist_url(self): | |
2467 | alias_url = kallithea.CONFIG.get('gist_alias_url') |
|
2467 | alias_url = kallithea.CONFIG.get('gist_alias_url') | |
2468 | if alias_url: |
|
2468 | if alias_url: | |
2469 | return alias_url.replace('{gistid}', self.gist_access_id) |
|
2469 | return alias_url.replace('{gistid}', self.gist_access_id) | |
2470 |
|
2470 | |||
2471 | import kallithea.lib.helpers as h |
|
2471 | import kallithea.lib.helpers as h | |
2472 | return h.canonical_url('gist', gist_id=self.gist_access_id) |
|
2472 | return h.canonical_url('gist', gist_id=self.gist_access_id) | |
2473 |
|
2473 | |||
2474 | @classmethod |
|
2474 | @classmethod | |
2475 | def base_path(cls): |
|
2475 | def base_path(cls): | |
2476 | """ |
|
2476 | """ | |
2477 | Returns base path where all gists are stored |
|
2477 | Returns base path where all gists are stored | |
2478 |
|
2478 | |||
2479 | :param cls: |
|
2479 | :param cls: | |
2480 | """ |
|
2480 | """ | |
2481 | from kallithea.model.gist import GIST_STORE_LOC |
|
2481 | from kallithea.model.gist import GIST_STORE_LOC | |
2482 | q = Session().query(Ui) \ |
|
2482 | q = Session().query(Ui) \ | |
2483 | .filter(Ui.ui_key == URL_SEP) |
|
2483 | .filter(Ui.ui_key == URL_SEP) | |
2484 | q = q.options(FromCache("sql_cache_short", "repository_repo_path")) |
|
2484 | q = q.options(FromCache("sql_cache_short", "repository_repo_path")) | |
2485 | return os.path.join(q.one().ui_value, GIST_STORE_LOC) |
|
2485 | return os.path.join(q.one().ui_value, GIST_STORE_LOC) | |
2486 |
|
2486 | |||
2487 | def get_api_data(self): |
|
2487 | def get_api_data(self): | |
2488 | """ |
|
2488 | """ | |
2489 | Common function for generating gist related data for API |
|
2489 | Common function for generating gist related data for API | |
2490 | """ |
|
2490 | """ | |
2491 | gist = self |
|
2491 | gist = self | |
2492 | data = dict( |
|
2492 | data = dict( | |
2493 | gist_id=gist.gist_id, |
|
2493 | gist_id=gist.gist_id, | |
2494 | type=gist.gist_type, |
|
2494 | type=gist.gist_type, | |
2495 | access_id=gist.gist_access_id, |
|
2495 | access_id=gist.gist_access_id, | |
2496 | description=gist.gist_description, |
|
2496 | description=gist.gist_description, | |
2497 | url=gist.gist_url(), |
|
2497 | url=gist.gist_url(), | |
2498 | expires=gist.gist_expires, |
|
2498 | expires=gist.gist_expires, | |
2499 | created_on=gist.created_on, |
|
2499 | created_on=gist.created_on, | |
2500 | ) |
|
2500 | ) | |
2501 | return data |
|
2501 | return data | |
2502 |
|
2502 | |||
2503 | def __json__(self): |
|
2503 | def __json__(self): | |
2504 | data = dict( |
|
2504 | data = dict( | |
2505 | ) |
|
2505 | ) | |
2506 | data.update(self.get_api_data()) |
|
2506 | data.update(self.get_api_data()) | |
2507 | return data |
|
2507 | return data | |
2508 | ## SCM functions |
|
2508 | ## SCM functions | |
2509 |
|
2509 | |||
2510 | @property |
|
2510 | @property | |
2511 | def scm_instance(self): |
|
2511 | def scm_instance(self): | |
2512 | from kallithea.lib.vcs import get_repo |
|
2512 | from kallithea.lib.vcs import get_repo | |
2513 | base_path = self.base_path() |
|
2513 | base_path = self.base_path() | |
2514 |
return get_repo(os.path.join( |
|
2514 | return get_repo(os.path.join(safe_str(base_path), safe_str(self.gist_access_id))) | |
2515 | [base_path, self.gist_access_id]))) |
|
|||
2516 |
|
2515 | |||
2517 |
|
2516 | |||
2518 | class UserSshKeys(Base, BaseDbModel): |
|
2517 | class UserSshKeys(Base, BaseDbModel): | |
2519 | __tablename__ = 'user_ssh_keys' |
|
2518 | __tablename__ = 'user_ssh_keys' | |
2520 | __table_args__ = ( |
|
2519 | __table_args__ = ( | |
2521 | Index('usk_public_key_idx', 'public_key'), |
|
2520 | Index('usk_public_key_idx', 'public_key'), | |
2522 | Index('usk_fingerprint_idx', 'fingerprint'), |
|
2521 | Index('usk_fingerprint_idx', 'fingerprint'), | |
2523 | UniqueConstraint('fingerprint'), |
|
2522 | UniqueConstraint('fingerprint'), | |
2524 | _table_args_default_dict |
|
2523 | _table_args_default_dict | |
2525 | ) |
|
2524 | ) | |
2526 | __mapper_args__ = {} |
|
2525 | __mapper_args__ = {} | |
2527 |
|
2526 | |||
2528 | user_ssh_key_id = Column(Integer(), primary_key=True) |
|
2527 | user_ssh_key_id = Column(Integer(), primary_key=True) | |
2529 | user_id = Column(Integer(), ForeignKey('users.user_id'), nullable=False) |
|
2528 | user_id = Column(Integer(), ForeignKey('users.user_id'), nullable=False) | |
2530 | _public_key = Column('public_key', UnicodeText(), nullable=False) |
|
2529 | _public_key = Column('public_key', UnicodeText(), nullable=False) | |
2531 | description = Column(UnicodeText(), nullable=False) |
|
2530 | description = Column(UnicodeText(), nullable=False) | |
2532 | fingerprint = Column(String(255), nullable=False, unique=True) |
|
2531 | fingerprint = Column(String(255), nullable=False, unique=True) | |
2533 | created_on = Column(DateTime(timezone=False), nullable=False, default=datetime.datetime.now) |
|
2532 | created_on = Column(DateTime(timezone=False), nullable=False, default=datetime.datetime.now) | |
2534 | last_seen = Column(DateTime(timezone=False), nullable=True) |
|
2533 | last_seen = Column(DateTime(timezone=False), nullable=True) | |
2535 |
|
2534 | |||
2536 | user = relationship('User') |
|
2535 | user = relationship('User') | |
2537 |
|
2536 | |||
2538 | @property |
|
2537 | @property | |
2539 | def public_key(self): |
|
2538 | def public_key(self): | |
2540 | return self._public_key |
|
2539 | return self._public_key | |
2541 |
|
2540 | |||
2542 | @public_key.setter |
|
2541 | @public_key.setter | |
2543 | def public_key(self, full_key): |
|
2542 | def public_key(self, full_key): | |
2544 | # the full public key is too long to be suitable as database key - instead, |
|
2543 | # the full public key is too long to be suitable as database key - instead, | |
2545 | # use fingerprints similar to 'ssh-keygen -E sha256 -lf ~/.ssh/id_rsa.pub' |
|
2544 | # use fingerprints similar to 'ssh-keygen -E sha256 -lf ~/.ssh/id_rsa.pub' | |
2546 | self._public_key = full_key |
|
2545 | self._public_key = full_key | |
2547 | enc_key = full_key.split(" ")[1] |
|
2546 | enc_key = full_key.split(" ")[1] | |
2548 | self.fingerprint = hashlib.sha256(enc_key.decode('base64')).digest().encode('base64').replace('\n', '').rstrip('=') |
|
2547 | self.fingerprint = hashlib.sha256(enc_key.decode('base64')).digest().encode('base64').replace('\n', '').rstrip('=') |
@@ -1,168 +1,167 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | # This program is free software: you can redistribute it and/or modify |
|
2 | # This program is free software: you can redistribute it and/or modify | |
3 | # it under the terms of the GNU General Public License as published by |
|
3 | # it under the terms of the GNU General Public License as published by | |
4 | # the Free Software Foundation, either version 3 of the License, or |
|
4 | # the Free Software Foundation, either version 3 of the License, or | |
5 | # (at your option) any later version. |
|
5 | # (at your option) any later version. | |
6 | # |
|
6 | # | |
7 | # This program is distributed in the hope that it will be useful, |
|
7 | # This program is distributed in the hope that it will be useful, | |
8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
10 | # GNU General Public License for more details. |
|
10 | # GNU General Public License for more details. | |
11 | # |
|
11 | # | |
12 | # You should have received a copy of the GNU General Public License |
|
12 | # You should have received a copy of the GNU General Public License | |
13 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
13 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
14 | """ |
|
14 | """ | |
15 | kallithea.model.permission |
|
15 | kallithea.model.permission | |
16 | ~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
16 | ~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
17 |
|
17 | |||
18 | permissions model for Kallithea |
|
18 | permissions model for Kallithea | |
19 |
|
19 | |||
20 | This file was forked by the Kallithea project in July 2014. |
|
20 | This file was forked by the Kallithea project in July 2014. | |
21 | Original author and date, and relevant copyright and licensing information is below: |
|
21 | Original author and date, and relevant copyright and licensing information is below: | |
22 | :created_on: Aug 20, 2010 |
|
22 | :created_on: Aug 20, 2010 | |
23 | :author: marcink |
|
23 | :author: marcink | |
24 | :copyright: (c) 2013 RhodeCode GmbH, and others. |
|
24 | :copyright: (c) 2013 RhodeCode GmbH, and others. | |
25 | :license: GPLv3, see LICENSE.md for more details. |
|
25 | :license: GPLv3, see LICENSE.md for more details. | |
26 | """ |
|
26 | """ | |
27 |
|
27 | |||
28 |
|
28 | |||
29 | import logging |
|
29 | import logging | |
30 | import traceback |
|
30 | import traceback | |
31 |
|
31 | |||
32 | from sqlalchemy.exc import DatabaseError |
|
32 | from sqlalchemy.exc import DatabaseError | |
33 |
|
33 | |||
34 | from kallithea.lib.utils2 import str2bool |
|
34 | from kallithea.lib.utils2 import str2bool | |
35 | from kallithea.model.db import Permission, Session, User, UserRepoGroupToPerm, UserRepoToPerm, UserToPerm, UserUserGroupToPerm |
|
35 | from kallithea.model.db import Permission, Session, User, UserRepoGroupToPerm, UserRepoToPerm, UserToPerm, UserUserGroupToPerm | |
36 |
|
36 | |||
37 |
|
37 | |||
38 | log = logging.getLogger(__name__) |
|
38 | log = logging.getLogger(__name__) | |
39 |
|
39 | |||
40 |
|
40 | |||
41 | class PermissionModel(object): |
|
41 | class PermissionModel(object): | |
42 | """ |
|
42 | """ | |
43 | Permissions model for Kallithea |
|
43 | Permissions model for Kallithea | |
44 | """ |
|
44 | """ | |
45 |
|
45 | |||
46 | def create_permissions(self): |
|
46 | def create_permissions(self): | |
47 | """ |
|
47 | """ | |
48 | Create permissions for whole system |
|
48 | Create permissions for whole system | |
49 | """ |
|
49 | """ | |
50 | for p in Permission.PERMS: |
|
50 | for p in Permission.PERMS: | |
51 | if not Permission.get_by_key(p[0]): |
|
51 | if not Permission.get_by_key(p[0]): | |
52 | new_perm = Permission() |
|
52 | new_perm = Permission() | |
53 | new_perm.permission_name = p[0] |
|
53 | new_perm.permission_name = p[0] | |
54 | Session().add(new_perm) |
|
54 | Session().add(new_perm) | |
55 |
|
55 | |||
56 | def create_default_permissions(self, user, force=False): |
|
56 | def create_default_permissions(self, user, force=False): | |
57 | """ |
|
57 | """ | |
58 | Create missing default permissions for user. If force is set, the default |
|
58 | Create missing default permissions for user. If force is set, the default | |
59 | permissions for the user are reset, otherwise only missing permissions are |
|
59 | permissions for the user are reset, otherwise only missing permissions are | |
60 | created. |
|
60 | created. | |
61 |
|
61 | |||
62 | :param user: |
|
62 | :param user: | |
63 | """ |
|
63 | """ | |
64 | user = User.guess_instance(user) |
|
64 | user = User.guess_instance(user) | |
65 |
|
65 | |||
66 | def _make_perm(perm): |
|
66 | def _make_perm(perm): | |
67 | new_perm = UserToPerm() |
|
67 | new_perm = UserToPerm() | |
68 | new_perm.user = user |
|
68 | new_perm.user = user | |
69 | new_perm.permission = Permission.get_by_key(perm) |
|
69 | new_perm.permission = Permission.get_by_key(perm) | |
70 | return new_perm |
|
70 | return new_perm | |
71 |
|
71 | |||
72 | def _get_group(perm_name): |
|
72 | def _get_group(perm_name): | |
73 | return '.'.join(perm_name.split('.')[:1]) |
|
73 | return '.'.join(perm_name.split('.')[:1]) | |
74 |
|
74 | |||
75 | perms = UserToPerm.query().filter(UserToPerm.user == user).all() |
|
75 | perms = UserToPerm.query().filter(UserToPerm.user == user).all() | |
76 |
defined_perms_groups = |
|
76 | defined_perms_groups = set(_get_group(x.permission.permission_name) for x in perms) | |
77 | (x.permission.permission_name for x in perms)) |
|
|||
78 | log.debug('GOT ALREADY DEFINED:%s', perms) |
|
77 | log.debug('GOT ALREADY DEFINED:%s', perms) | |
79 | DEFAULT_PERMS = Permission.DEFAULT_USER_PERMISSIONS |
|
78 | DEFAULT_PERMS = Permission.DEFAULT_USER_PERMISSIONS | |
80 |
|
79 | |||
81 | if force: |
|
80 | if force: | |
82 | for perm in perms: |
|
81 | for perm in perms: | |
83 | Session().delete(perm) |
|
82 | Session().delete(perm) | |
84 | Session().commit() |
|
83 | Session().commit() | |
85 | defined_perms_groups = [] |
|
84 | defined_perms_groups = [] | |
86 | # For every default permission that needs to be created, we check if |
|
85 | # For every default permission that needs to be created, we check if | |
87 | # its group is already defined. If it's not, we create default permission. |
|
86 | # its group is already defined. If it's not, we create default permission. | |
88 | for perm_name in DEFAULT_PERMS: |
|
87 | for perm_name in DEFAULT_PERMS: | |
89 | gr = _get_group(perm_name) |
|
88 | gr = _get_group(perm_name) | |
90 | if gr not in defined_perms_groups: |
|
89 | if gr not in defined_perms_groups: | |
91 | log.debug('GR:%s not found, creating permission %s', |
|
90 | log.debug('GR:%s not found, creating permission %s', | |
92 | gr, perm_name) |
|
91 | gr, perm_name) | |
93 | new_perm = _make_perm(perm_name) |
|
92 | new_perm = _make_perm(perm_name) | |
94 | Session().add(new_perm) |
|
93 | Session().add(new_perm) | |
95 |
|
94 | |||
96 | def update(self, form_result): |
|
95 | def update(self, form_result): | |
97 | perm_user = User.get_by_username(username=form_result['perm_user_name']) |
|
96 | perm_user = User.get_by_username(username=form_result['perm_user_name']) | |
98 |
|
97 | |||
99 | try: |
|
98 | try: | |
100 | # stage 1 set anonymous access |
|
99 | # stage 1 set anonymous access | |
101 | if perm_user.is_default_user: |
|
100 | if perm_user.is_default_user: | |
102 | perm_user.active = str2bool(form_result['anonymous']) |
|
101 | perm_user.active = str2bool(form_result['anonymous']) | |
103 |
|
102 | |||
104 | # stage 2 reset defaults and set them from form data |
|
103 | # stage 2 reset defaults and set them from form data | |
105 | def _make_new(usr, perm_name): |
|
104 | def _make_new(usr, perm_name): | |
106 | log.debug('Creating new permission:%s', perm_name) |
|
105 | log.debug('Creating new permission:%s', perm_name) | |
107 | new = UserToPerm() |
|
106 | new = UserToPerm() | |
108 | new.user = usr |
|
107 | new.user = usr | |
109 | new.permission = Permission.get_by_key(perm_name) |
|
108 | new.permission = Permission.get_by_key(perm_name) | |
110 | return new |
|
109 | return new | |
111 | # clear current entries, to make this function idempotent |
|
110 | # clear current entries, to make this function idempotent | |
112 | # it will fix even if we define more permissions or permissions |
|
111 | # it will fix even if we define more permissions or permissions | |
113 | # are somehow missing |
|
112 | # are somehow missing | |
114 | u2p = UserToPerm.query() \ |
|
113 | u2p = UserToPerm.query() \ | |
115 | .filter(UserToPerm.user == perm_user) \ |
|
114 | .filter(UserToPerm.user == perm_user) \ | |
116 | .all() |
|
115 | .all() | |
117 | for p in u2p: |
|
116 | for p in u2p: | |
118 | Session().delete(p) |
|
117 | Session().delete(p) | |
119 | # create fresh set of permissions |
|
118 | # create fresh set of permissions | |
120 | for def_perm_key in ['default_repo_perm', |
|
119 | for def_perm_key in ['default_repo_perm', | |
121 | 'default_group_perm', |
|
120 | 'default_group_perm', | |
122 | 'default_user_group_perm', |
|
121 | 'default_user_group_perm', | |
123 | 'default_repo_create', |
|
122 | 'default_repo_create', | |
124 | 'create_on_write', # special case for create repos on write access to group |
|
123 | 'create_on_write', # special case for create repos on write access to group | |
125 | #'default_repo_group_create', # not implemented yet |
|
124 | #'default_repo_group_create', # not implemented yet | |
126 | 'default_user_group_create', |
|
125 | 'default_user_group_create', | |
127 | 'default_fork', |
|
126 | 'default_fork', | |
128 | 'default_register', |
|
127 | 'default_register', | |
129 | 'default_extern_activate']: |
|
128 | 'default_extern_activate']: | |
130 | p = _make_new(perm_user, form_result[def_perm_key]) |
|
129 | p = _make_new(perm_user, form_result[def_perm_key]) | |
131 | Session().add(p) |
|
130 | Session().add(p) | |
132 |
|
131 | |||
133 | # stage 3 update all default permissions for repos if checked |
|
132 | # stage 3 update all default permissions for repos if checked | |
134 | if form_result['overwrite_default_repo']: |
|
133 | if form_result['overwrite_default_repo']: | |
135 | _def_name = form_result['default_repo_perm'].split('repository.')[-1] |
|
134 | _def_name = form_result['default_repo_perm'].split('repository.')[-1] | |
136 | _def = Permission.get_by_key('repository.' + _def_name) |
|
135 | _def = Permission.get_by_key('repository.' + _def_name) | |
137 | # repos |
|
136 | # repos | |
138 | for r2p in UserRepoToPerm.query() \ |
|
137 | for r2p in UserRepoToPerm.query() \ | |
139 | .filter(UserRepoToPerm.user == perm_user) \ |
|
138 | .filter(UserRepoToPerm.user == perm_user) \ | |
140 | .all(): |
|
139 | .all(): | |
141 |
|
140 | |||
142 | # don't reset PRIVATE repositories |
|
141 | # don't reset PRIVATE repositories | |
143 | if not r2p.repository.private: |
|
142 | if not r2p.repository.private: | |
144 | r2p.permission = _def |
|
143 | r2p.permission = _def | |
145 |
|
144 | |||
146 | if form_result['overwrite_default_group']: |
|
145 | if form_result['overwrite_default_group']: | |
147 | _def_name = form_result['default_group_perm'].split('group.')[-1] |
|
146 | _def_name = form_result['default_group_perm'].split('group.')[-1] | |
148 | # groups |
|
147 | # groups | |
149 | _def = Permission.get_by_key('group.' + _def_name) |
|
148 | _def = Permission.get_by_key('group.' + _def_name) | |
150 | for g2p in UserRepoGroupToPerm.query() \ |
|
149 | for g2p in UserRepoGroupToPerm.query() \ | |
151 | .filter(UserRepoGroupToPerm.user == perm_user) \ |
|
150 | .filter(UserRepoGroupToPerm.user == perm_user) \ | |
152 | .all(): |
|
151 | .all(): | |
153 | g2p.permission = _def |
|
152 | g2p.permission = _def | |
154 |
|
153 | |||
155 | if form_result['overwrite_default_user_group']: |
|
154 | if form_result['overwrite_default_user_group']: | |
156 | _def_name = form_result['default_user_group_perm'].split('usergroup.')[-1] |
|
155 | _def_name = form_result['default_user_group_perm'].split('usergroup.')[-1] | |
157 | # groups |
|
156 | # groups | |
158 | _def = Permission.get_by_key('usergroup.' + _def_name) |
|
157 | _def = Permission.get_by_key('usergroup.' + _def_name) | |
159 | for g2p in UserUserGroupToPerm.query() \ |
|
158 | for g2p in UserUserGroupToPerm.query() \ | |
160 | .filter(UserUserGroupToPerm.user == perm_user) \ |
|
159 | .filter(UserUserGroupToPerm.user == perm_user) \ | |
161 | .all(): |
|
160 | .all(): | |
162 | g2p.permission = _def |
|
161 | g2p.permission = _def | |
163 |
|
162 | |||
164 | Session().commit() |
|
163 | Session().commit() | |
165 | except (DatabaseError,): |
|
164 | except (DatabaseError,): | |
166 | log.error(traceback.format_exc()) |
|
165 | log.error(traceback.format_exc()) | |
167 | Session().rollback() |
|
166 | Session().rollback() | |
168 | raise |
|
167 | raise |
@@ -1,720 +1,720 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | # This program is free software: you can redistribute it and/or modify |
|
2 | # This program is free software: you can redistribute it and/or modify | |
3 | # it under the terms of the GNU General Public License as published by |
|
3 | # it under the terms of the GNU General Public License as published by | |
4 | # the Free Software Foundation, either version 3 of the License, or |
|
4 | # the Free Software Foundation, either version 3 of the License, or | |
5 | # (at your option) any later version. |
|
5 | # (at your option) any later version. | |
6 | # |
|
6 | # | |
7 | # This program is distributed in the hope that it will be useful, |
|
7 | # This program is distributed in the hope that it will be useful, | |
8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
10 | # GNU General Public License for more details. |
|
10 | # GNU General Public License for more details. | |
11 | # |
|
11 | # | |
12 | # You should have received a copy of the GNU General Public License |
|
12 | # You should have received a copy of the GNU General Public License | |
13 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
13 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
14 | """ |
|
14 | """ | |
15 | kallithea.model.repo |
|
15 | kallithea.model.repo | |
16 | ~~~~~~~~~~~~~~~~~~~~ |
|
16 | ~~~~~~~~~~~~~~~~~~~~ | |
17 |
|
17 | |||
18 | Repository model for kallithea |
|
18 | Repository model for kallithea | |
19 |
|
19 | |||
20 | This file was forked by the Kallithea project in July 2014. |
|
20 | This file was forked by the Kallithea project in July 2014. | |
21 | Original author and date, and relevant copyright and licensing information is below: |
|
21 | Original author and date, and relevant copyright and licensing information is below: | |
22 | :created_on: Jun 5, 2010 |
|
22 | :created_on: Jun 5, 2010 | |
23 | :author: marcink |
|
23 | :author: marcink | |
24 | :copyright: (c) 2013 RhodeCode GmbH, and others. |
|
24 | :copyright: (c) 2013 RhodeCode GmbH, and others. | |
25 | :license: GPLv3, see LICENSE.md for more details. |
|
25 | :license: GPLv3, see LICENSE.md for more details. | |
26 |
|
26 | |||
27 | """ |
|
27 | """ | |
28 |
|
28 | |||
29 | import logging |
|
29 | import logging | |
30 | import os |
|
30 | import os | |
31 | import shutil |
|
31 | import shutil | |
32 | import traceback |
|
32 | import traceback | |
33 | from datetime import datetime |
|
33 | from datetime import datetime | |
34 |
|
34 | |||
35 | import kallithea.lib.utils2 |
|
35 | import kallithea.lib.utils2 | |
36 | from kallithea.lib import helpers as h |
|
36 | from kallithea.lib import helpers as h | |
37 | from kallithea.lib.auth import HasRepoPermissionLevel, HasUserGroupPermissionLevel |
|
37 | from kallithea.lib.auth import HasRepoPermissionLevel, HasUserGroupPermissionLevel | |
38 | from kallithea.lib.caching_query import FromCache |
|
38 | from kallithea.lib.caching_query import FromCache | |
39 | from kallithea.lib.exceptions import AttachedForksError |
|
39 | from kallithea.lib.exceptions import AttachedForksError | |
40 | from kallithea.lib.hooks import log_delete_repository |
|
40 | from kallithea.lib.hooks import log_delete_repository | |
41 | from kallithea.lib.utils import is_valid_repo_uri, make_ui |
|
41 | from kallithea.lib.utils import is_valid_repo_uri, make_ui | |
42 | from kallithea.lib.utils2 import LazyProperty, get_current_authuser, obfuscate_url_pw, remove_prefix, safe_str, safe_unicode |
|
42 | from kallithea.lib.utils2 import LazyProperty, get_current_authuser, obfuscate_url_pw, remove_prefix, safe_str, safe_unicode | |
43 | from kallithea.lib.vcs.backends import get_backend |
|
43 | from kallithea.lib.vcs.backends import get_backend | |
44 | from kallithea.model.db import ( |
|
44 | from kallithea.model.db import ( | |
45 | Permission, RepoGroup, Repository, RepositoryField, Session, Statistics, Ui, User, UserGroup, UserGroupRepoGroupToPerm, UserGroupRepoToPerm, UserRepoGroupToPerm, UserRepoToPerm) |
|
45 | Permission, RepoGroup, Repository, RepositoryField, Session, Statistics, Ui, User, UserGroup, UserGroupRepoGroupToPerm, UserGroupRepoToPerm, UserRepoGroupToPerm, UserRepoToPerm) | |
46 |
|
46 | |||
47 |
|
47 | |||
48 | log = logging.getLogger(__name__) |
|
48 | log = logging.getLogger(__name__) | |
49 |
|
49 | |||
50 |
|
50 | |||
51 | class RepoModel(object): |
|
51 | class RepoModel(object): | |
52 |
|
52 | |||
53 | URL_SEPARATOR = Repository.url_sep() |
|
53 | URL_SEPARATOR = Repository.url_sep() | |
54 |
|
54 | |||
55 | def _create_default_perms(self, repository, private): |
|
55 | def _create_default_perms(self, repository, private): | |
56 | # create default permission |
|
56 | # create default permission | |
57 | default = 'repository.read' |
|
57 | default = 'repository.read' | |
58 | def_user = User.get_default_user() |
|
58 | def_user = User.get_default_user() | |
59 | for p in def_user.user_perms: |
|
59 | for p in def_user.user_perms: | |
60 | if p.permission.permission_name.startswith('repository.'): |
|
60 | if p.permission.permission_name.startswith('repository.'): | |
61 | default = p.permission.permission_name |
|
61 | default = p.permission.permission_name | |
62 | break |
|
62 | break | |
63 |
|
63 | |||
64 | default_perm = 'repository.none' if private else default |
|
64 | default_perm = 'repository.none' if private else default | |
65 |
|
65 | |||
66 | repo_to_perm = UserRepoToPerm() |
|
66 | repo_to_perm = UserRepoToPerm() | |
67 | repo_to_perm.permission = Permission.get_by_key(default_perm) |
|
67 | repo_to_perm.permission = Permission.get_by_key(default_perm) | |
68 |
|
68 | |||
69 | repo_to_perm.repository = repository |
|
69 | repo_to_perm.repository = repository | |
70 | repo_to_perm.user_id = def_user.user_id |
|
70 | repo_to_perm.user_id = def_user.user_id | |
71 | Session().add(repo_to_perm) |
|
71 | Session().add(repo_to_perm) | |
72 |
|
72 | |||
73 | return repo_to_perm |
|
73 | return repo_to_perm | |
74 |
|
74 | |||
75 | @LazyProperty |
|
75 | @LazyProperty | |
76 | def repos_path(self): |
|
76 | def repos_path(self): | |
77 | """ |
|
77 | """ | |
78 | Gets the repositories root path from database |
|
78 | Gets the repositories root path from database | |
79 | """ |
|
79 | """ | |
80 |
|
80 | |||
81 | q = Ui.query().filter(Ui.ui_key == '/').one() |
|
81 | q = Ui.query().filter(Ui.ui_key == '/').one() | |
82 | return q.ui_value |
|
82 | return q.ui_value | |
83 |
|
83 | |||
84 | def get(self, repo_id, cache=False): |
|
84 | def get(self, repo_id, cache=False): | |
85 | repo = Repository.query() \ |
|
85 | repo = Repository.query() \ | |
86 | .filter(Repository.repo_id == repo_id) |
|
86 | .filter(Repository.repo_id == repo_id) | |
87 |
|
87 | |||
88 | if cache: |
|
88 | if cache: | |
89 | repo = repo.options(FromCache("sql_cache_short", |
|
89 | repo = repo.options(FromCache("sql_cache_short", | |
90 | "get_repo_%s" % repo_id)) |
|
90 | "get_repo_%s" % repo_id)) | |
91 | return repo.scalar() |
|
91 | return repo.scalar() | |
92 |
|
92 | |||
93 | def get_repo(self, repository): |
|
93 | def get_repo(self, repository): | |
94 | return Repository.guess_instance(repository) |
|
94 | return Repository.guess_instance(repository) | |
95 |
|
95 | |||
96 | def get_by_repo_name(self, repo_name, cache=False): |
|
96 | def get_by_repo_name(self, repo_name, cache=False): | |
97 | repo = Repository.query() \ |
|
97 | repo = Repository.query() \ | |
98 | .filter(Repository.repo_name == repo_name) |
|
98 | .filter(Repository.repo_name == repo_name) | |
99 |
|
99 | |||
100 | if cache: |
|
100 | if cache: | |
101 | repo = repo.options(FromCache("sql_cache_short", |
|
101 | repo = repo.options(FromCache("sql_cache_short", | |
102 | "get_repo_%s" % repo_name)) |
|
102 | "get_repo_%s" % repo_name)) | |
103 | return repo.scalar() |
|
103 | return repo.scalar() | |
104 |
|
104 | |||
105 | def get_all_user_repos(self, user): |
|
105 | def get_all_user_repos(self, user): | |
106 | """ |
|
106 | """ | |
107 | Gets all repositories that user have at least read access |
|
107 | Gets all repositories that user have at least read access | |
108 |
|
108 | |||
109 | :param user: |
|
109 | :param user: | |
110 | """ |
|
110 | """ | |
111 | from kallithea.lib.auth import AuthUser |
|
111 | from kallithea.lib.auth import AuthUser | |
112 | user = User.guess_instance(user) |
|
112 | user = User.guess_instance(user) | |
113 | repos = AuthUser(dbuser=user).permissions['repositories'] |
|
113 | repos = AuthUser(dbuser=user).permissions['repositories'] | |
114 | access_check = lambda r: r[1] in ['repository.read', |
|
114 | access_check = lambda r: r[1] in ['repository.read', | |
115 | 'repository.write', |
|
115 | 'repository.write', | |
116 | 'repository.admin'] |
|
116 | 'repository.admin'] | |
117 | repos = [x[0] for x in filter(access_check, repos.items())] |
|
117 | repos = [x[0] for x in filter(access_check, repos.items())] | |
118 | return Repository.query().filter(Repository.repo_name.in_(repos)) |
|
118 | return Repository.query().filter(Repository.repo_name.in_(repos)) | |
119 |
|
119 | |||
120 | @classmethod |
|
120 | @classmethod | |
121 | def _render_datatable(cls, tmpl, *args, **kwargs): |
|
121 | def _render_datatable(cls, tmpl, *args, **kwargs): | |
122 | import kallithea |
|
122 | import kallithea | |
123 | from tg import tmpl_context as c, request, app_globals |
|
123 | from tg import tmpl_context as c, request, app_globals | |
124 | from tg.i18n import ugettext as _ |
|
124 | from tg.i18n import ugettext as _ | |
125 |
|
125 | |||
126 | _tmpl_lookup = app_globals.mako_lookup |
|
126 | _tmpl_lookup = app_globals.mako_lookup | |
127 | template = _tmpl_lookup.get_template('data_table/_dt_elements.html') |
|
127 | template = _tmpl_lookup.get_template('data_table/_dt_elements.html') | |
128 |
|
128 | |||
129 | tmpl = template.get_def(tmpl) |
|
129 | tmpl = template.get_def(tmpl) | |
130 | kwargs.update(dict(_=_, h=h, c=c, request=request)) |
|
130 | kwargs.update(dict(_=_, h=h, c=c, request=request)) | |
131 | return tmpl.render_unicode(*args, **kwargs) |
|
131 | return tmpl.render_unicode(*args, **kwargs) | |
132 |
|
132 | |||
133 | def get_repos_as_dict(self, repos_list, repo_groups_list=None, |
|
133 | def get_repos_as_dict(self, repos_list, repo_groups_list=None, | |
134 | admin=False, |
|
134 | admin=False, | |
135 | short_name=False): |
|
135 | short_name=False): | |
136 | """Return repository list for use by DataTable. |
|
136 | """Return repository list for use by DataTable. | |
137 | repos_list: list of repositories - but will be filtered for read permission. |
|
137 | repos_list: list of repositories - but will be filtered for read permission. | |
138 | repo_groups_list: added at top of list without permission check. |
|
138 | repo_groups_list: added at top of list without permission check. | |
139 | admin: return data for action column. |
|
139 | admin: return data for action column. | |
140 | """ |
|
140 | """ | |
141 | _render = self._render_datatable |
|
141 | _render = self._render_datatable | |
142 | from tg import tmpl_context as c |
|
142 | from tg import tmpl_context as c | |
143 |
|
143 | |||
144 | def repo_lnk(name, rtype, rstate, private, fork_of): |
|
144 | def repo_lnk(name, rtype, rstate, private, fork_of): | |
145 | return _render('repo_name', name, rtype, rstate, private, fork_of, |
|
145 | return _render('repo_name', name, rtype, rstate, private, fork_of, | |
146 | short_name=short_name) |
|
146 | short_name=short_name) | |
147 |
|
147 | |||
148 | def last_change(last_change): |
|
148 | def last_change(last_change): | |
149 | return _render("last_change", last_change) |
|
149 | return _render("last_change", last_change) | |
150 |
|
150 | |||
151 | def rss_lnk(repo_name): |
|
151 | def rss_lnk(repo_name): | |
152 | return _render("rss", repo_name) |
|
152 | return _render("rss", repo_name) | |
153 |
|
153 | |||
154 | def atom_lnk(repo_name): |
|
154 | def atom_lnk(repo_name): | |
155 | return _render("atom", repo_name) |
|
155 | return _render("atom", repo_name) | |
156 |
|
156 | |||
157 | def last_rev(repo_name, cs_cache): |
|
157 | def last_rev(repo_name, cs_cache): | |
158 | return _render('revision', repo_name, cs_cache.get('revision'), |
|
158 | return _render('revision', repo_name, cs_cache.get('revision'), | |
159 | cs_cache.get('raw_id'), cs_cache.get('author'), |
|
159 | cs_cache.get('raw_id'), cs_cache.get('author'), | |
160 | cs_cache.get('message')) |
|
160 | cs_cache.get('message')) | |
161 |
|
161 | |||
162 | def desc(desc): |
|
162 | def desc(desc): | |
163 | return h.urlify_text(desc, truncate=80, stylize=c.visual.stylify_metalabels) |
|
163 | return h.urlify_text(desc, truncate=80, stylize=c.visual.stylify_metalabels) | |
164 |
|
164 | |||
165 | def state(repo_state): |
|
165 | def state(repo_state): | |
166 | return _render("repo_state", repo_state) |
|
166 | return _render("repo_state", repo_state) | |
167 |
|
167 | |||
168 | def repo_actions(repo_name): |
|
168 | def repo_actions(repo_name): | |
169 | return _render('repo_actions', repo_name) |
|
169 | return _render('repo_actions', repo_name) | |
170 |
|
170 | |||
171 | def owner_actions(owner_id, username): |
|
171 | def owner_actions(owner_id, username): | |
172 | return _render('user_name', owner_id, username) |
|
172 | return _render('user_name', owner_id, username) | |
173 |
|
173 | |||
174 | repos_data = [] |
|
174 | repos_data = [] | |
175 |
|
175 | |||
176 | for gr in repo_groups_list or []: |
|
176 | for gr in repo_groups_list or []: | |
177 | repos_data.append(dict( |
|
177 | repos_data.append(dict( | |
178 | raw_name='\0' + gr.name, # sort before repositories |
|
178 | raw_name='\0' + gr.name, # sort before repositories | |
179 | just_name=gr.name, |
|
179 | just_name=gr.name, | |
180 | name=_render('group_name_html', group_name=gr.group_name, name=gr.name), |
|
180 | name=_render('group_name_html', group_name=gr.group_name, name=gr.name), | |
181 | desc=gr.group_description)) |
|
181 | desc=gr.group_description)) | |
182 |
|
182 | |||
183 | for repo in repos_list: |
|
183 | for repo in repos_list: | |
184 | if not HasRepoPermissionLevel('read')(repo.repo_name, 'get_repos_as_dict check'): |
|
184 | if not HasRepoPermissionLevel('read')(repo.repo_name, 'get_repos_as_dict check'): | |
185 | continue |
|
185 | continue | |
186 | cs_cache = repo.changeset_cache |
|
186 | cs_cache = repo.changeset_cache | |
187 | row = { |
|
187 | row = { | |
188 | "raw_name": repo.repo_name, |
|
188 | "raw_name": repo.repo_name, | |
189 | "just_name": repo.just_name, |
|
189 | "just_name": repo.just_name, | |
190 | "name": repo_lnk(repo.repo_name, repo.repo_type, |
|
190 | "name": repo_lnk(repo.repo_name, repo.repo_type, | |
191 | repo.repo_state, repo.private, repo.fork), |
|
191 | repo.repo_state, repo.private, repo.fork), | |
192 | "last_change_iso": repo.last_db_change.isoformat(), |
|
192 | "last_change_iso": repo.last_db_change.isoformat(), | |
193 | "last_change": last_change(repo.last_db_change), |
|
193 | "last_change": last_change(repo.last_db_change), | |
194 | "last_changeset": last_rev(repo.repo_name, cs_cache), |
|
194 | "last_changeset": last_rev(repo.repo_name, cs_cache), | |
195 | "last_rev_raw": cs_cache.get('revision'), |
|
195 | "last_rev_raw": cs_cache.get('revision'), | |
196 | "desc": desc(repo.description), |
|
196 | "desc": desc(repo.description), | |
197 | "owner": h.person(repo.owner), |
|
197 | "owner": h.person(repo.owner), | |
198 | "state": state(repo.repo_state), |
|
198 | "state": state(repo.repo_state), | |
199 | "rss": rss_lnk(repo.repo_name), |
|
199 | "rss": rss_lnk(repo.repo_name), | |
200 | "atom": atom_lnk(repo.repo_name), |
|
200 | "atom": atom_lnk(repo.repo_name), | |
201 | } |
|
201 | } | |
202 | if admin: |
|
202 | if admin: | |
203 | row.update({ |
|
203 | row.update({ | |
204 | "action": repo_actions(repo.repo_name), |
|
204 | "action": repo_actions(repo.repo_name), | |
205 | "owner": owner_actions(repo.owner_id, |
|
205 | "owner": owner_actions(repo.owner_id, | |
206 | h.person(repo.owner)) |
|
206 | h.person(repo.owner)) | |
207 | }) |
|
207 | }) | |
208 | repos_data.append(row) |
|
208 | repos_data.append(row) | |
209 |
|
209 | |||
210 | return { |
|
210 | return { | |
211 | "sort": "name", |
|
211 | "sort": "name", | |
212 | "dir": "asc", |
|
212 | "dir": "asc", | |
213 | "records": repos_data |
|
213 | "records": repos_data | |
214 | } |
|
214 | } | |
215 |
|
215 | |||
216 | def _get_defaults(self, repo_name): |
|
216 | def _get_defaults(self, repo_name): | |
217 | """ |
|
217 | """ | |
218 | Gets information about repository, and returns a dict for |
|
218 | Gets information about repository, and returns a dict for | |
219 | usage in forms |
|
219 | usage in forms | |
220 |
|
220 | |||
221 | :param repo_name: |
|
221 | :param repo_name: | |
222 | """ |
|
222 | """ | |
223 |
|
223 | |||
224 | repo_info = Repository.get_by_repo_name(repo_name) |
|
224 | repo_info = Repository.get_by_repo_name(repo_name) | |
225 |
|
225 | |||
226 | if repo_info is None: |
|
226 | if repo_info is None: | |
227 | return None |
|
227 | return None | |
228 |
|
228 | |||
229 | defaults = repo_info.get_dict() |
|
229 | defaults = repo_info.get_dict() | |
230 | defaults['repo_name'] = repo_info.just_name |
|
230 | defaults['repo_name'] = repo_info.just_name | |
231 | defaults['repo_group'] = repo_info.group_id |
|
231 | defaults['repo_group'] = repo_info.group_id | |
232 |
|
232 | |||
233 | for strip, k in [(0, 'repo_type'), (1, 'repo_enable_downloads'), |
|
233 | for strip, k in [(0, 'repo_type'), (1, 'repo_enable_downloads'), | |
234 | (1, 'repo_description'), |
|
234 | (1, 'repo_description'), | |
235 | (1, 'repo_landing_rev'), (0, 'clone_uri'), |
|
235 | (1, 'repo_landing_rev'), (0, 'clone_uri'), | |
236 | (1, 'repo_private'), (1, 'repo_enable_statistics')]: |
|
236 | (1, 'repo_private'), (1, 'repo_enable_statistics')]: | |
237 | attr = k |
|
237 | attr = k | |
238 | if strip: |
|
238 | if strip: | |
239 | attr = remove_prefix(k, 'repo_') |
|
239 | attr = remove_prefix(k, 'repo_') | |
240 |
|
240 | |||
241 | val = defaults[attr] |
|
241 | val = defaults[attr] | |
242 | if k == 'repo_landing_rev': |
|
242 | if k == 'repo_landing_rev': | |
243 | val = ':'.join(defaults[attr]) |
|
243 | val = ':'.join(defaults[attr]) | |
244 | defaults[k] = val |
|
244 | defaults[k] = val | |
245 | if k == 'clone_uri': |
|
245 | if k == 'clone_uri': | |
246 | defaults['clone_uri_hidden'] = repo_info.clone_uri_hidden |
|
246 | defaults['clone_uri_hidden'] = repo_info.clone_uri_hidden | |
247 |
|
247 | |||
248 | # fill owner |
|
248 | # fill owner | |
249 | if repo_info.owner: |
|
249 | if repo_info.owner: | |
250 | defaults.update({'owner': repo_info.owner.username}) |
|
250 | defaults.update({'owner': repo_info.owner.username}) | |
251 | else: |
|
251 | else: | |
252 | replacement_user = User.query().filter(User.admin == |
|
252 | replacement_user = User.query().filter(User.admin == | |
253 | True).first().username |
|
253 | True).first().username | |
254 | defaults.update({'owner': replacement_user}) |
|
254 | defaults.update({'owner': replacement_user}) | |
255 |
|
255 | |||
256 | # fill repository users |
|
256 | # fill repository users | |
257 | for p in repo_info.repo_to_perm: |
|
257 | for p in repo_info.repo_to_perm: | |
258 | defaults.update({'u_perm_%s' % p.user.username: |
|
258 | defaults.update({'u_perm_%s' % p.user.username: | |
259 | p.permission.permission_name}) |
|
259 | p.permission.permission_name}) | |
260 |
|
260 | |||
261 | # fill repository groups |
|
261 | # fill repository groups | |
262 | for p in repo_info.users_group_to_perm: |
|
262 | for p in repo_info.users_group_to_perm: | |
263 | defaults.update({'g_perm_%s' % p.users_group.users_group_name: |
|
263 | defaults.update({'g_perm_%s' % p.users_group.users_group_name: | |
264 | p.permission.permission_name}) |
|
264 | p.permission.permission_name}) | |
265 |
|
265 | |||
266 | return defaults |
|
266 | return defaults | |
267 |
|
267 | |||
268 | def update(self, repo, **kwargs): |
|
268 | def update(self, repo, **kwargs): | |
269 | try: |
|
269 | try: | |
270 | cur_repo = Repository.guess_instance(repo) |
|
270 | cur_repo = Repository.guess_instance(repo) | |
271 | org_repo_name = cur_repo.repo_name |
|
271 | org_repo_name = cur_repo.repo_name | |
272 | if 'owner' in kwargs: |
|
272 | if 'owner' in kwargs: | |
273 | cur_repo.owner = User.get_by_username(kwargs['owner']) |
|
273 | cur_repo.owner = User.get_by_username(kwargs['owner']) | |
274 |
|
274 | |||
275 | if 'repo_group' in kwargs: |
|
275 | if 'repo_group' in kwargs: | |
276 | assert kwargs['repo_group'] != u'-1', kwargs # RepoForm should have converted to None |
|
276 | assert kwargs['repo_group'] != u'-1', kwargs # RepoForm should have converted to None | |
277 | cur_repo.group = RepoGroup.get(kwargs['repo_group']) |
|
277 | cur_repo.group = RepoGroup.get(kwargs['repo_group']) | |
278 | cur_repo.repo_name = cur_repo.get_new_name(cur_repo.just_name) |
|
278 | cur_repo.repo_name = cur_repo.get_new_name(cur_repo.just_name) | |
279 | log.debug('Updating repo %s with params:%s', cur_repo, kwargs) |
|
279 | log.debug('Updating repo %s with params:%s', cur_repo, kwargs) | |
280 | for k in ['repo_enable_downloads', |
|
280 | for k in ['repo_enable_downloads', | |
281 | 'repo_description', |
|
281 | 'repo_description', | |
282 | 'repo_landing_rev', |
|
282 | 'repo_landing_rev', | |
283 | 'repo_private', |
|
283 | 'repo_private', | |
284 | 'repo_enable_statistics', |
|
284 | 'repo_enable_statistics', | |
285 | ]: |
|
285 | ]: | |
286 | if k in kwargs: |
|
286 | if k in kwargs: | |
287 | setattr(cur_repo, remove_prefix(k, 'repo_'), kwargs[k]) |
|
287 | setattr(cur_repo, remove_prefix(k, 'repo_'), kwargs[k]) | |
288 | clone_uri = kwargs.get('clone_uri') |
|
288 | clone_uri = kwargs.get('clone_uri') | |
289 | if clone_uri is not None and clone_uri != cur_repo.clone_uri_hidden: |
|
289 | if clone_uri is not None and clone_uri != cur_repo.clone_uri_hidden: | |
290 | # clone_uri is modified - if given a value, check it is valid |
|
290 | # clone_uri is modified - if given a value, check it is valid | |
291 | if clone_uri != '': |
|
291 | if clone_uri != '': | |
292 | # will raise exception on error |
|
292 | # will raise exception on error | |
293 | is_valid_repo_uri(cur_repo.repo_type, clone_uri, make_ui()) |
|
293 | is_valid_repo_uri(cur_repo.repo_type, clone_uri, make_ui()) | |
294 | cur_repo.clone_uri = clone_uri |
|
294 | cur_repo.clone_uri = clone_uri | |
295 |
|
295 | |||
296 | if 'repo_name' in kwargs: |
|
296 | if 'repo_name' in kwargs: | |
297 | repo_name = kwargs['repo_name'] |
|
297 | repo_name = kwargs['repo_name'] | |
298 | if kallithea.lib.utils2.repo_name_slug(repo_name) != repo_name: |
|
298 | if kallithea.lib.utils2.repo_name_slug(repo_name) != repo_name: | |
299 | raise Exception('invalid repo name %s' % repo_name) |
|
299 | raise Exception('invalid repo name %s' % repo_name) | |
300 | cur_repo.repo_name = cur_repo.get_new_name(repo_name) |
|
300 | cur_repo.repo_name = cur_repo.get_new_name(repo_name) | |
301 |
|
301 | |||
302 | # if private flag is set, reset default permission to NONE |
|
302 | # if private flag is set, reset default permission to NONE | |
303 | if kwargs.get('repo_private'): |
|
303 | if kwargs.get('repo_private'): | |
304 | EMPTY_PERM = 'repository.none' |
|
304 | EMPTY_PERM = 'repository.none' | |
305 | RepoModel().grant_user_permission( |
|
305 | RepoModel().grant_user_permission( | |
306 | repo=cur_repo, user='default', perm=EMPTY_PERM |
|
306 | repo=cur_repo, user='default', perm=EMPTY_PERM | |
307 | ) |
|
307 | ) | |
308 | # handle extra fields |
|
308 | # handle extra fields | |
309 | for field in filter(lambda k: k.startswith(RepositoryField.PREFIX), |
|
309 | for field in filter(lambda k: k.startswith(RepositoryField.PREFIX), | |
310 | kwargs): |
|
310 | kwargs): | |
311 | k = RepositoryField.un_prefix_key(field) |
|
311 | k = RepositoryField.un_prefix_key(field) | |
312 | ex_field = RepositoryField.get_by_key_name(key=k, repo=cur_repo) |
|
312 | ex_field = RepositoryField.get_by_key_name(key=k, repo=cur_repo) | |
313 | if ex_field: |
|
313 | if ex_field: | |
314 | ex_field.field_value = kwargs[field] |
|
314 | ex_field.field_value = kwargs[field] | |
315 |
|
315 | |||
316 | if org_repo_name != cur_repo.repo_name: |
|
316 | if org_repo_name != cur_repo.repo_name: | |
317 | # rename repository |
|
317 | # rename repository | |
318 | self._rename_filesystem_repo(old=org_repo_name, new=cur_repo.repo_name) |
|
318 | self._rename_filesystem_repo(old=org_repo_name, new=cur_repo.repo_name) | |
319 |
|
319 | |||
320 | return cur_repo |
|
320 | return cur_repo | |
321 | except Exception: |
|
321 | except Exception: | |
322 | log.error(traceback.format_exc()) |
|
322 | log.error(traceback.format_exc()) | |
323 | raise |
|
323 | raise | |
324 |
|
324 | |||
325 | def _create_repo(self, repo_name, repo_type, description, owner, |
|
325 | def _create_repo(self, repo_name, repo_type, description, owner, | |
326 | private=False, clone_uri=None, repo_group=None, |
|
326 | private=False, clone_uri=None, repo_group=None, | |
327 | landing_rev='rev:tip', fork_of=None, |
|
327 | landing_rev='rev:tip', fork_of=None, | |
328 | copy_fork_permissions=False, enable_statistics=False, |
|
328 | copy_fork_permissions=False, enable_statistics=False, | |
329 | enable_downloads=False, |
|
329 | enable_downloads=False, | |
330 | copy_group_permissions=False, state=Repository.STATE_PENDING): |
|
330 | copy_group_permissions=False, state=Repository.STATE_PENDING): | |
331 | """ |
|
331 | """ | |
332 | Create repository inside database with PENDING state. This should only be |
|
332 | Create repository inside database with PENDING state. This should only be | |
333 | executed by create() repo, with exception of importing existing repos. |
|
333 | executed by create() repo, with exception of importing existing repos. | |
334 |
|
334 | |||
335 | """ |
|
335 | """ | |
336 | from kallithea.model.scm import ScmModel |
|
336 | from kallithea.model.scm import ScmModel | |
337 |
|
337 | |||
338 | owner = User.guess_instance(owner) |
|
338 | owner = User.guess_instance(owner) | |
339 | fork_of = Repository.guess_instance(fork_of) |
|
339 | fork_of = Repository.guess_instance(fork_of) | |
340 | repo_group = RepoGroup.guess_instance(repo_group) |
|
340 | repo_group = RepoGroup.guess_instance(repo_group) | |
341 | try: |
|
341 | try: | |
342 | repo_name = safe_unicode(repo_name) |
|
342 | repo_name = safe_unicode(repo_name) | |
343 | description = safe_unicode(description) |
|
343 | description = safe_unicode(description) | |
344 | # repo name is just a name of repository |
|
344 | # repo name is just a name of repository | |
345 | # while repo_name_full is a full qualified name that is combined |
|
345 | # while repo_name_full is a full qualified name that is combined | |
346 | # with name and path of group |
|
346 | # with name and path of group | |
347 | repo_name_full = repo_name |
|
347 | repo_name_full = repo_name | |
348 | repo_name = repo_name.split(self.URL_SEPARATOR)[-1] |
|
348 | repo_name = repo_name.split(self.URL_SEPARATOR)[-1] | |
349 | if kallithea.lib.utils2.repo_name_slug(repo_name) != repo_name: |
|
349 | if kallithea.lib.utils2.repo_name_slug(repo_name) != repo_name: | |
350 | raise Exception('invalid repo name %s' % repo_name) |
|
350 | raise Exception('invalid repo name %s' % repo_name) | |
351 |
|
351 | |||
352 | new_repo = Repository() |
|
352 | new_repo = Repository() | |
353 | new_repo.repo_state = state |
|
353 | new_repo.repo_state = state | |
354 | new_repo.enable_statistics = False |
|
354 | new_repo.enable_statistics = False | |
355 | new_repo.repo_name = repo_name_full |
|
355 | new_repo.repo_name = repo_name_full | |
356 | new_repo.repo_type = repo_type |
|
356 | new_repo.repo_type = repo_type | |
357 | new_repo.owner = owner |
|
357 | new_repo.owner = owner | |
358 | new_repo.group = repo_group |
|
358 | new_repo.group = repo_group | |
359 | new_repo.description = description or repo_name |
|
359 | new_repo.description = description or repo_name | |
360 | new_repo.private = private |
|
360 | new_repo.private = private | |
361 | if clone_uri: |
|
361 | if clone_uri: | |
362 | # will raise exception on error |
|
362 | # will raise exception on error | |
363 | is_valid_repo_uri(repo_type, clone_uri, make_ui()) |
|
363 | is_valid_repo_uri(repo_type, clone_uri, make_ui()) | |
364 | new_repo.clone_uri = clone_uri |
|
364 | new_repo.clone_uri = clone_uri | |
365 | new_repo.landing_rev = landing_rev |
|
365 | new_repo.landing_rev = landing_rev | |
366 |
|
366 | |||
367 | new_repo.enable_statistics = enable_statistics |
|
367 | new_repo.enable_statistics = enable_statistics | |
368 | new_repo.enable_downloads = enable_downloads |
|
368 | new_repo.enable_downloads = enable_downloads | |
369 |
|
369 | |||
370 | if fork_of: |
|
370 | if fork_of: | |
371 | parent_repo = fork_of |
|
371 | parent_repo = fork_of | |
372 | new_repo.fork = parent_repo |
|
372 | new_repo.fork = parent_repo | |
373 |
|
373 | |||
374 | Session().add(new_repo) |
|
374 | Session().add(new_repo) | |
375 |
|
375 | |||
376 | if fork_of and copy_fork_permissions: |
|
376 | if fork_of and copy_fork_permissions: | |
377 | repo = fork_of |
|
377 | repo = fork_of | |
378 | user_perms = UserRepoToPerm.query() \ |
|
378 | user_perms = UserRepoToPerm.query() \ | |
379 | .filter(UserRepoToPerm.repository == repo).all() |
|
379 | .filter(UserRepoToPerm.repository == repo).all() | |
380 | group_perms = UserGroupRepoToPerm.query() \ |
|
380 | group_perms = UserGroupRepoToPerm.query() \ | |
381 | .filter(UserGroupRepoToPerm.repository == repo).all() |
|
381 | .filter(UserGroupRepoToPerm.repository == repo).all() | |
382 |
|
382 | |||
383 | for perm in user_perms: |
|
383 | for perm in user_perms: | |
384 | UserRepoToPerm.create(perm.user, new_repo, perm.permission) |
|
384 | UserRepoToPerm.create(perm.user, new_repo, perm.permission) | |
385 |
|
385 | |||
386 | for perm in group_perms: |
|
386 | for perm in group_perms: | |
387 | UserGroupRepoToPerm.create(perm.users_group, new_repo, |
|
387 | UserGroupRepoToPerm.create(perm.users_group, new_repo, | |
388 | perm.permission) |
|
388 | perm.permission) | |
389 |
|
389 | |||
390 | elif repo_group and copy_group_permissions: |
|
390 | elif repo_group and copy_group_permissions: | |
391 |
|
391 | |||
392 | user_perms = UserRepoGroupToPerm.query() \ |
|
392 | user_perms = UserRepoGroupToPerm.query() \ | |
393 | .filter(UserRepoGroupToPerm.group == repo_group).all() |
|
393 | .filter(UserRepoGroupToPerm.group == repo_group).all() | |
394 |
|
394 | |||
395 | group_perms = UserGroupRepoGroupToPerm.query() \ |
|
395 | group_perms = UserGroupRepoGroupToPerm.query() \ | |
396 | .filter(UserGroupRepoGroupToPerm.group == repo_group).all() |
|
396 | .filter(UserGroupRepoGroupToPerm.group == repo_group).all() | |
397 |
|
397 | |||
398 | for perm in user_perms: |
|
398 | for perm in user_perms: | |
399 | perm_name = perm.permission.permission_name.replace('group.', 'repository.') |
|
399 | perm_name = perm.permission.permission_name.replace('group.', 'repository.') | |
400 | perm_obj = Permission.get_by_key(perm_name) |
|
400 | perm_obj = Permission.get_by_key(perm_name) | |
401 | UserRepoToPerm.create(perm.user, new_repo, perm_obj) |
|
401 | UserRepoToPerm.create(perm.user, new_repo, perm_obj) | |
402 |
|
402 | |||
403 | for perm in group_perms: |
|
403 | for perm in group_perms: | |
404 | perm_name = perm.permission.permission_name.replace('group.', 'repository.') |
|
404 | perm_name = perm.permission.permission_name.replace('group.', 'repository.') | |
405 | perm_obj = Permission.get_by_key(perm_name) |
|
405 | perm_obj = Permission.get_by_key(perm_name) | |
406 | UserGroupRepoToPerm.create(perm.users_group, new_repo, perm_obj) |
|
406 | UserGroupRepoToPerm.create(perm.users_group, new_repo, perm_obj) | |
407 |
|
407 | |||
408 | else: |
|
408 | else: | |
409 | self._create_default_perms(new_repo, private) |
|
409 | self._create_default_perms(new_repo, private) | |
410 |
|
410 | |||
411 | # now automatically start following this repository as owner |
|
411 | # now automatically start following this repository as owner | |
412 | ScmModel().toggle_following_repo(new_repo.repo_id, owner.user_id) |
|
412 | ScmModel().toggle_following_repo(new_repo.repo_id, owner.user_id) | |
413 | # we need to flush here, in order to check if database won't |
|
413 | # we need to flush here, in order to check if database won't | |
414 | # throw any exceptions, create filesystem dirs at the very end |
|
414 | # throw any exceptions, create filesystem dirs at the very end | |
415 | Session().flush() |
|
415 | Session().flush() | |
416 | return new_repo |
|
416 | return new_repo | |
417 | except Exception: |
|
417 | except Exception: | |
418 | log.error(traceback.format_exc()) |
|
418 | log.error(traceback.format_exc()) | |
419 | raise |
|
419 | raise | |
420 |
|
420 | |||
421 | def create(self, form_data, cur_user): |
|
421 | def create(self, form_data, cur_user): | |
422 | """ |
|
422 | """ | |
423 | Create repository using celery tasks |
|
423 | Create repository using celery tasks | |
424 |
|
424 | |||
425 | :param form_data: |
|
425 | :param form_data: | |
426 | :param cur_user: |
|
426 | :param cur_user: | |
427 | """ |
|
427 | """ | |
428 | from kallithea.lib.celerylib import tasks |
|
428 | from kallithea.lib.celerylib import tasks | |
429 | return tasks.create_repo(form_data, cur_user) |
|
429 | return tasks.create_repo(form_data, cur_user) | |
430 |
|
430 | |||
431 | def _update_permissions(self, repo, perms_new=None, perms_updates=None, |
|
431 | def _update_permissions(self, repo, perms_new=None, perms_updates=None, | |
432 | check_perms=True): |
|
432 | check_perms=True): | |
433 | if not perms_new: |
|
433 | if not perms_new: | |
434 | perms_new = [] |
|
434 | perms_new = [] | |
435 | if not perms_updates: |
|
435 | if not perms_updates: | |
436 | perms_updates = [] |
|
436 | perms_updates = [] | |
437 |
|
437 | |||
438 | # update permissions |
|
438 | # update permissions | |
439 | for member, perm, member_type in perms_updates: |
|
439 | for member, perm, member_type in perms_updates: | |
440 | if member_type == 'user': |
|
440 | if member_type == 'user': | |
441 | # this updates existing one |
|
441 | # this updates existing one | |
442 | self.grant_user_permission( |
|
442 | self.grant_user_permission( | |
443 | repo=repo, user=member, perm=perm |
|
443 | repo=repo, user=member, perm=perm | |
444 | ) |
|
444 | ) | |
445 | else: |
|
445 | else: | |
446 | # check if we have permissions to alter this usergroup's access |
|
446 | # check if we have permissions to alter this usergroup's access | |
447 | if not check_perms or HasUserGroupPermissionLevel('read')(member): |
|
447 | if not check_perms or HasUserGroupPermissionLevel('read')(member): | |
448 | self.grant_user_group_permission( |
|
448 | self.grant_user_group_permission( | |
449 | repo=repo, group_name=member, perm=perm |
|
449 | repo=repo, group_name=member, perm=perm | |
450 | ) |
|
450 | ) | |
451 | # set new permissions |
|
451 | # set new permissions | |
452 | for member, perm, member_type in perms_new: |
|
452 | for member, perm, member_type in perms_new: | |
453 | if member_type == 'user': |
|
453 | if member_type == 'user': | |
454 | self.grant_user_permission( |
|
454 | self.grant_user_permission( | |
455 | repo=repo, user=member, perm=perm |
|
455 | repo=repo, user=member, perm=perm | |
456 | ) |
|
456 | ) | |
457 | else: |
|
457 | else: | |
458 | # check if we have permissions to alter this usergroup's access |
|
458 | # check if we have permissions to alter this usergroup's access | |
459 | if not check_perms or HasUserGroupPermissionLevel('read')(member): |
|
459 | if not check_perms or HasUserGroupPermissionLevel('read')(member): | |
460 | self.grant_user_group_permission( |
|
460 | self.grant_user_group_permission( | |
461 | repo=repo, group_name=member, perm=perm |
|
461 | repo=repo, group_name=member, perm=perm | |
462 | ) |
|
462 | ) | |
463 |
|
463 | |||
464 | def create_fork(self, form_data, cur_user): |
|
464 | def create_fork(self, form_data, cur_user): | |
465 | """ |
|
465 | """ | |
466 | Simple wrapper into executing celery task for fork creation |
|
466 | Simple wrapper into executing celery task for fork creation | |
467 |
|
467 | |||
468 | :param form_data: |
|
468 | :param form_data: | |
469 | :param cur_user: |
|
469 | :param cur_user: | |
470 | """ |
|
470 | """ | |
471 | from kallithea.lib.celerylib import tasks |
|
471 | from kallithea.lib.celerylib import tasks | |
472 | return tasks.create_repo_fork(form_data, cur_user) |
|
472 | return tasks.create_repo_fork(form_data, cur_user) | |
473 |
|
473 | |||
474 | def delete(self, repo, forks=None, fs_remove=True, cur_user=None): |
|
474 | def delete(self, repo, forks=None, fs_remove=True, cur_user=None): | |
475 | """ |
|
475 | """ | |
476 | Delete given repository, forks parameter defines what do do with |
|
476 | Delete given repository, forks parameter defines what do do with | |
477 | attached forks. Throws AttachedForksError if deleted repo has attached |
|
477 | attached forks. Throws AttachedForksError if deleted repo has attached | |
478 | forks |
|
478 | forks | |
479 |
|
479 | |||
480 | :param repo: |
|
480 | :param repo: | |
481 | :param forks: str 'delete' or 'detach' |
|
481 | :param forks: str 'delete' or 'detach' | |
482 | :param fs_remove: remove(archive) repo from filesystem |
|
482 | :param fs_remove: remove(archive) repo from filesystem | |
483 | """ |
|
483 | """ | |
484 | if not cur_user: |
|
484 | if not cur_user: | |
485 | cur_user = getattr(get_current_authuser(), 'username', None) |
|
485 | cur_user = getattr(get_current_authuser(), 'username', None) | |
486 | repo = Repository.guess_instance(repo) |
|
486 | repo = Repository.guess_instance(repo) | |
487 | if repo is not None: |
|
487 | if repo is not None: | |
488 | if forks == 'detach': |
|
488 | if forks == 'detach': | |
489 | for r in repo.forks: |
|
489 | for r in repo.forks: | |
490 | r.fork = None |
|
490 | r.fork = None | |
491 | elif forks == 'delete': |
|
491 | elif forks == 'delete': | |
492 | for r in repo.forks: |
|
492 | for r in repo.forks: | |
493 | self.delete(r, forks='delete') |
|
493 | self.delete(r, forks='delete') | |
494 | elif [f for f in repo.forks]: |
|
494 | elif [f for f in repo.forks]: | |
495 | raise AttachedForksError() |
|
495 | raise AttachedForksError() | |
496 |
|
496 | |||
497 | old_repo_dict = repo.get_dict() |
|
497 | old_repo_dict = repo.get_dict() | |
498 | try: |
|
498 | try: | |
499 | Session().delete(repo) |
|
499 | Session().delete(repo) | |
500 | if fs_remove: |
|
500 | if fs_remove: | |
501 | self._delete_filesystem_repo(repo) |
|
501 | self._delete_filesystem_repo(repo) | |
502 | else: |
|
502 | else: | |
503 | log.debug('skipping removal from filesystem') |
|
503 | log.debug('skipping removal from filesystem') | |
504 | log_delete_repository(old_repo_dict, |
|
504 | log_delete_repository(old_repo_dict, | |
505 | deleted_by=cur_user) |
|
505 | deleted_by=cur_user) | |
506 | except Exception: |
|
506 | except Exception: | |
507 | log.error(traceback.format_exc()) |
|
507 | log.error(traceback.format_exc()) | |
508 | raise |
|
508 | raise | |
509 |
|
509 | |||
510 | def grant_user_permission(self, repo, user, perm): |
|
510 | def grant_user_permission(self, repo, user, perm): | |
511 | """ |
|
511 | """ | |
512 | Grant permission for user on given repository, or update existing one |
|
512 | Grant permission for user on given repository, or update existing one | |
513 | if found |
|
513 | if found | |
514 |
|
514 | |||
515 | :param repo: Instance of Repository, repository_id, or repository name |
|
515 | :param repo: Instance of Repository, repository_id, or repository name | |
516 | :param user: Instance of User, user_id or username |
|
516 | :param user: Instance of User, user_id or username | |
517 | :param perm: Instance of Permission, or permission_name |
|
517 | :param perm: Instance of Permission, or permission_name | |
518 | """ |
|
518 | """ | |
519 | user = User.guess_instance(user) |
|
519 | user = User.guess_instance(user) | |
520 | repo = Repository.guess_instance(repo) |
|
520 | repo = Repository.guess_instance(repo) | |
521 | permission = Permission.guess_instance(perm) |
|
521 | permission = Permission.guess_instance(perm) | |
522 |
|
522 | |||
523 | # check if we have that permission already |
|
523 | # check if we have that permission already | |
524 | obj = UserRepoToPerm.query() \ |
|
524 | obj = UserRepoToPerm.query() \ | |
525 | .filter(UserRepoToPerm.user == user) \ |
|
525 | .filter(UserRepoToPerm.user == user) \ | |
526 | .filter(UserRepoToPerm.repository == repo) \ |
|
526 | .filter(UserRepoToPerm.repository == repo) \ | |
527 | .scalar() |
|
527 | .scalar() | |
528 | if obj is None: |
|
528 | if obj is None: | |
529 | # create new ! |
|
529 | # create new ! | |
530 | obj = UserRepoToPerm() |
|
530 | obj = UserRepoToPerm() | |
531 | Session().add(obj) |
|
531 | Session().add(obj) | |
532 | obj.repository = repo |
|
532 | obj.repository = repo | |
533 | obj.user = user |
|
533 | obj.user = user | |
534 | obj.permission = permission |
|
534 | obj.permission = permission | |
535 | log.debug('Granted perm %s to %s on %s', perm, user, repo) |
|
535 | log.debug('Granted perm %s to %s on %s', perm, user, repo) | |
536 | return obj |
|
536 | return obj | |
537 |
|
537 | |||
538 | def revoke_user_permission(self, repo, user): |
|
538 | def revoke_user_permission(self, repo, user): | |
539 | """ |
|
539 | """ | |
540 | Revoke permission for user on given repository |
|
540 | Revoke permission for user on given repository | |
541 |
|
541 | |||
542 | :param repo: Instance of Repository, repository_id, or repository name |
|
542 | :param repo: Instance of Repository, repository_id, or repository name | |
543 | :param user: Instance of User, user_id or username |
|
543 | :param user: Instance of User, user_id or username | |
544 | """ |
|
544 | """ | |
545 |
|
545 | |||
546 | user = User.guess_instance(user) |
|
546 | user = User.guess_instance(user) | |
547 | repo = Repository.guess_instance(repo) |
|
547 | repo = Repository.guess_instance(repo) | |
548 |
|
548 | |||
549 | obj = UserRepoToPerm.query() \ |
|
549 | obj = UserRepoToPerm.query() \ | |
550 | .filter(UserRepoToPerm.repository == repo) \ |
|
550 | .filter(UserRepoToPerm.repository == repo) \ | |
551 | .filter(UserRepoToPerm.user == user) \ |
|
551 | .filter(UserRepoToPerm.user == user) \ | |
552 | .scalar() |
|
552 | .scalar() | |
553 | if obj is not None: |
|
553 | if obj is not None: | |
554 | Session().delete(obj) |
|
554 | Session().delete(obj) | |
555 | log.debug('Revoked perm on %s on %s', repo, user) |
|
555 | log.debug('Revoked perm on %s on %s', repo, user) | |
556 |
|
556 | |||
557 | def grant_user_group_permission(self, repo, group_name, perm): |
|
557 | def grant_user_group_permission(self, repo, group_name, perm): | |
558 | """ |
|
558 | """ | |
559 | Grant permission for user group on given repository, or update |
|
559 | Grant permission for user group on given repository, or update | |
560 | existing one if found |
|
560 | existing one if found | |
561 |
|
561 | |||
562 | :param repo: Instance of Repository, repository_id, or repository name |
|
562 | :param repo: Instance of Repository, repository_id, or repository name | |
563 | :param group_name: Instance of UserGroup, users_group_id, |
|
563 | :param group_name: Instance of UserGroup, users_group_id, | |
564 | or user group name |
|
564 | or user group name | |
565 | :param perm: Instance of Permission, or permission_name |
|
565 | :param perm: Instance of Permission, or permission_name | |
566 | """ |
|
566 | """ | |
567 | repo = Repository.guess_instance(repo) |
|
567 | repo = Repository.guess_instance(repo) | |
568 | group_name = UserGroup.guess_instance(group_name) |
|
568 | group_name = UserGroup.guess_instance(group_name) | |
569 | permission = Permission.guess_instance(perm) |
|
569 | permission = Permission.guess_instance(perm) | |
570 |
|
570 | |||
571 | # check if we have that permission already |
|
571 | # check if we have that permission already | |
572 | obj = UserGroupRepoToPerm.query() \ |
|
572 | obj = UserGroupRepoToPerm.query() \ | |
573 | .filter(UserGroupRepoToPerm.users_group == group_name) \ |
|
573 | .filter(UserGroupRepoToPerm.users_group == group_name) \ | |
574 | .filter(UserGroupRepoToPerm.repository == repo) \ |
|
574 | .filter(UserGroupRepoToPerm.repository == repo) \ | |
575 | .scalar() |
|
575 | .scalar() | |
576 |
|
576 | |||
577 | if obj is None: |
|
577 | if obj is None: | |
578 | # create new |
|
578 | # create new | |
579 | obj = UserGroupRepoToPerm() |
|
579 | obj = UserGroupRepoToPerm() | |
580 | Session().add(obj) |
|
580 | Session().add(obj) | |
581 |
|
581 | |||
582 | obj.repository = repo |
|
582 | obj.repository = repo | |
583 | obj.users_group = group_name |
|
583 | obj.users_group = group_name | |
584 | obj.permission = permission |
|
584 | obj.permission = permission | |
585 | log.debug('Granted perm %s to %s on %s', perm, group_name, repo) |
|
585 | log.debug('Granted perm %s to %s on %s', perm, group_name, repo) | |
586 | return obj |
|
586 | return obj | |
587 |
|
587 | |||
588 | def revoke_user_group_permission(self, repo, group_name): |
|
588 | def revoke_user_group_permission(self, repo, group_name): | |
589 | """ |
|
589 | """ | |
590 | Revoke permission for user group on given repository |
|
590 | Revoke permission for user group on given repository | |
591 |
|
591 | |||
592 | :param repo: Instance of Repository, repository_id, or repository name |
|
592 | :param repo: Instance of Repository, repository_id, or repository name | |
593 | :param group_name: Instance of UserGroup, users_group_id, |
|
593 | :param group_name: Instance of UserGroup, users_group_id, | |
594 | or user group name |
|
594 | or user group name | |
595 | """ |
|
595 | """ | |
596 | repo = Repository.guess_instance(repo) |
|
596 | repo = Repository.guess_instance(repo) | |
597 | group_name = UserGroup.guess_instance(group_name) |
|
597 | group_name = UserGroup.guess_instance(group_name) | |
598 |
|
598 | |||
599 | obj = UserGroupRepoToPerm.query() \ |
|
599 | obj = UserGroupRepoToPerm.query() \ | |
600 | .filter(UserGroupRepoToPerm.repository == repo) \ |
|
600 | .filter(UserGroupRepoToPerm.repository == repo) \ | |
601 | .filter(UserGroupRepoToPerm.users_group == group_name) \ |
|
601 | .filter(UserGroupRepoToPerm.users_group == group_name) \ | |
602 | .scalar() |
|
602 | .scalar() | |
603 | if obj is not None: |
|
603 | if obj is not None: | |
604 | Session().delete(obj) |
|
604 | Session().delete(obj) | |
605 | log.debug('Revoked perm to %s on %s', repo, group_name) |
|
605 | log.debug('Revoked perm to %s on %s', repo, group_name) | |
606 |
|
606 | |||
607 | def delete_stats(self, repo_name): |
|
607 | def delete_stats(self, repo_name): | |
608 | """ |
|
608 | """ | |
609 | removes stats for given repo |
|
609 | removes stats for given repo | |
610 |
|
610 | |||
611 | :param repo_name: |
|
611 | :param repo_name: | |
612 | """ |
|
612 | """ | |
613 | repo = Repository.guess_instance(repo_name) |
|
613 | repo = Repository.guess_instance(repo_name) | |
614 | try: |
|
614 | try: | |
615 | obj = Statistics.query() \ |
|
615 | obj = Statistics.query() \ | |
616 | .filter(Statistics.repository == repo).scalar() |
|
616 | .filter(Statistics.repository == repo).scalar() | |
617 | if obj is not None: |
|
617 | if obj is not None: | |
618 | Session().delete(obj) |
|
618 | Session().delete(obj) | |
619 | except Exception: |
|
619 | except Exception: | |
620 | log.error(traceback.format_exc()) |
|
620 | log.error(traceback.format_exc()) | |
621 | raise |
|
621 | raise | |
622 |
|
622 | |||
623 | def _create_filesystem_repo(self, repo_name, repo_type, repo_group, |
|
623 | def _create_filesystem_repo(self, repo_name, repo_type, repo_group, | |
624 | clone_uri=None, repo_store_location=None): |
|
624 | clone_uri=None, repo_store_location=None): | |
625 | """ |
|
625 | """ | |
626 | Makes repository on filesystem. Operation is group aware, meaning that it will create |
|
626 | Makes repository on filesystem. Operation is group aware, meaning that it will create | |
627 | a repository within a group, and alter the paths accordingly to the group location. |
|
627 | a repository within a group, and alter the paths accordingly to the group location. | |
628 |
|
628 | |||
629 | Note: clone_uri is low level and not validated - it might be a file system path used for validated cloning |
|
629 | Note: clone_uri is low level and not validated - it might be a file system path used for validated cloning | |
630 | """ |
|
630 | """ | |
631 | from kallithea.lib.utils import is_valid_repo, is_valid_repo_group |
|
631 | from kallithea.lib.utils import is_valid_repo, is_valid_repo_group | |
632 | from kallithea.model.scm import ScmModel |
|
632 | from kallithea.model.scm import ScmModel | |
633 |
|
633 | |||
634 | if '/' in repo_name: |
|
634 | if '/' in repo_name: | |
635 | raise ValueError('repo_name must not contain groups got `%s`' % repo_name) |
|
635 | raise ValueError('repo_name must not contain groups got `%s`' % repo_name) | |
636 |
|
636 | |||
637 | if isinstance(repo_group, RepoGroup): |
|
637 | if isinstance(repo_group, RepoGroup): | |
638 | new_parent_path = os.sep.join(repo_group.full_path_splitted) |
|
638 | new_parent_path = os.sep.join(repo_group.full_path_splitted) | |
639 | else: |
|
639 | else: | |
640 | new_parent_path = repo_group or '' |
|
640 | new_parent_path = repo_group or '' | |
641 |
|
641 | |||
642 | if repo_store_location: |
|
642 | if repo_store_location: | |
643 | _paths = [repo_store_location] |
|
643 | _paths = [repo_store_location] | |
644 | else: |
|
644 | else: | |
645 | _paths = [self.repos_path, new_parent_path, repo_name] |
|
645 | _paths = [self.repos_path, new_parent_path, repo_name] | |
646 | # we need to make it str for mercurial |
|
646 | # we need to make it str for mercurial | |
647 |
repo_path = os.path.join(* |
|
647 | repo_path = os.path.join(*(safe_str(x) for x in _paths)) | |
648 |
|
648 | |||
649 | # check if this path is not a repository |
|
649 | # check if this path is not a repository | |
650 | if is_valid_repo(repo_path, self.repos_path): |
|
650 | if is_valid_repo(repo_path, self.repos_path): | |
651 | raise Exception('This path %s is a valid repository' % repo_path) |
|
651 | raise Exception('This path %s is a valid repository' % repo_path) | |
652 |
|
652 | |||
653 | # check if this path is a group |
|
653 | # check if this path is a group | |
654 | if is_valid_repo_group(repo_path, self.repos_path): |
|
654 | if is_valid_repo_group(repo_path, self.repos_path): | |
655 | raise Exception('This path %s is a valid group' % repo_path) |
|
655 | raise Exception('This path %s is a valid group' % repo_path) | |
656 |
|
656 | |||
657 | log.info('creating repo %s in %s from url: `%s`', |
|
657 | log.info('creating repo %s in %s from url: `%s`', | |
658 | repo_name, safe_unicode(repo_path), |
|
658 | repo_name, safe_unicode(repo_path), | |
659 | obfuscate_url_pw(clone_uri)) |
|
659 | obfuscate_url_pw(clone_uri)) | |
660 |
|
660 | |||
661 | backend = get_backend(repo_type) |
|
661 | backend = get_backend(repo_type) | |
662 |
|
662 | |||
663 | if repo_type == 'hg': |
|
663 | if repo_type == 'hg': | |
664 | baseui = make_ui() |
|
664 | baseui = make_ui() | |
665 | # patch and reset hooks section of UI config to not run any |
|
665 | # patch and reset hooks section of UI config to not run any | |
666 | # hooks on creating remote repo |
|
666 | # hooks on creating remote repo | |
667 | for k, v in baseui.configitems('hooks'): |
|
667 | for k, v in baseui.configitems('hooks'): | |
668 | baseui.setconfig('hooks', k, None) |
|
668 | baseui.setconfig('hooks', k, None) | |
669 |
|
669 | |||
670 | repo = backend(repo_path, create=True, src_url=clone_uri, baseui=baseui) |
|
670 | repo = backend(repo_path, create=True, src_url=clone_uri, baseui=baseui) | |
671 | elif repo_type == 'git': |
|
671 | elif repo_type == 'git': | |
672 | repo = backend(repo_path, create=True, src_url=clone_uri, bare=True) |
|
672 | repo = backend(repo_path, create=True, src_url=clone_uri, bare=True) | |
673 | # add kallithea hook into this repo |
|
673 | # add kallithea hook into this repo | |
674 | ScmModel().install_git_hooks(repo=repo) |
|
674 | ScmModel().install_git_hooks(repo=repo) | |
675 | else: |
|
675 | else: | |
676 | raise Exception('Not supported repo_type %s expected hg/git' % repo_type) |
|
676 | raise Exception('Not supported repo_type %s expected hg/git' % repo_type) | |
677 |
|
677 | |||
678 | log.debug('Created repo %s with %s backend', |
|
678 | log.debug('Created repo %s with %s backend', | |
679 | safe_unicode(repo_name), safe_unicode(repo_type)) |
|
679 | safe_unicode(repo_name), safe_unicode(repo_type)) | |
680 | return repo |
|
680 | return repo | |
681 |
|
681 | |||
682 | def _rename_filesystem_repo(self, old, new): |
|
682 | def _rename_filesystem_repo(self, old, new): | |
683 | """ |
|
683 | """ | |
684 | renames repository on filesystem |
|
684 | renames repository on filesystem | |
685 |
|
685 | |||
686 | :param old: old name |
|
686 | :param old: old name | |
687 | :param new: new name |
|
687 | :param new: new name | |
688 | """ |
|
688 | """ | |
689 | log.info('renaming repo from %s to %s', old, new) |
|
689 | log.info('renaming repo from %s to %s', old, new) | |
690 |
|
690 | |||
691 | old_path = safe_str(os.path.join(self.repos_path, old)) |
|
691 | old_path = safe_str(os.path.join(self.repos_path, old)) | |
692 | new_path = safe_str(os.path.join(self.repos_path, new)) |
|
692 | new_path = safe_str(os.path.join(self.repos_path, new)) | |
693 | if os.path.isdir(new_path): |
|
693 | if os.path.isdir(new_path): | |
694 | raise Exception( |
|
694 | raise Exception( | |
695 | 'Was trying to rename to already existing dir %s' % new_path |
|
695 | 'Was trying to rename to already existing dir %s' % new_path | |
696 | ) |
|
696 | ) | |
697 | shutil.move(old_path, new_path) |
|
697 | shutil.move(old_path, new_path) | |
698 |
|
698 | |||
699 | def _delete_filesystem_repo(self, repo): |
|
699 | def _delete_filesystem_repo(self, repo): | |
700 | """ |
|
700 | """ | |
701 | removes repo from filesystem, the removal is actually done by |
|
701 | removes repo from filesystem, the removal is actually done by | |
702 | renaming dir to a 'rm__*' prefix which Kallithea will skip. |
|
702 | renaming dir to a 'rm__*' prefix which Kallithea will skip. | |
703 | It can be undeleted later by reverting the rename. |
|
703 | It can be undeleted later by reverting the rename. | |
704 |
|
704 | |||
705 | :param repo: repo object |
|
705 | :param repo: repo object | |
706 | """ |
|
706 | """ | |
707 | rm_path = safe_str(os.path.join(self.repos_path, repo.repo_name)) |
|
707 | rm_path = safe_str(os.path.join(self.repos_path, repo.repo_name)) | |
708 | log.info("Removing %s", rm_path) |
|
708 | log.info("Removing %s", rm_path) | |
709 |
|
709 | |||
710 | _now = datetime.now() |
|
710 | _now = datetime.now() | |
711 | _ms = str(_now.microsecond).rjust(6, '0') |
|
711 | _ms = str(_now.microsecond).rjust(6, '0') | |
712 | _d = 'rm__%s__%s' % (_now.strftime('%Y%m%d_%H%M%S_' + _ms), |
|
712 | _d = 'rm__%s__%s' % (_now.strftime('%Y%m%d_%H%M%S_' + _ms), | |
713 | repo.just_name) |
|
713 | repo.just_name) | |
714 | if repo.group: |
|
714 | if repo.group: | |
715 | args = repo.group.full_path_splitted + [_d] |
|
715 | args = repo.group.full_path_splitted + [_d] | |
716 | _d = os.path.join(*args) |
|
716 | _d = os.path.join(*args) | |
717 | if os.path.exists(rm_path): |
|
717 | if os.path.exists(rm_path): | |
718 | shutil.move(rm_path, safe_str(os.path.join(self.repos_path, _d))) |
|
718 | shutil.move(rm_path, safe_str(os.path.join(self.repos_path, _d))) | |
719 | else: |
|
719 | else: | |
720 | log.error("Can't find repo to delete in %r", rm_path) |
|
720 | log.error("Can't find repo to delete in %r", rm_path) |
General Comments 0
You need to be logged in to leave comments.
Login now