Show More
@@ -1,192 +1,191 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """ |
|
3 | 3 | rhodecode.controllers.forks |
|
4 | 4 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
5 | 5 | |
|
6 | 6 | forks controller for rhodecode |
|
7 | 7 | |
|
8 | 8 | :created_on: Apr 23, 2011 |
|
9 | 9 | :author: marcink |
|
10 | 10 | :copyright: (C) 2011-2012 Marcin Kuzminski <marcin@python-works.com> |
|
11 | 11 | :license: GPLv3, see COPYING for more details. |
|
12 | 12 | """ |
|
13 | 13 | # This program is free software: you can redistribute it and/or modify |
|
14 | 14 | # it under the terms of the GNU General Public License as published by |
|
15 | 15 | # the Free Software Foundation, either version 3 of the License, or |
|
16 | 16 | # (at your option) any later version. |
|
17 | 17 | # |
|
18 | 18 | # This program is distributed in the hope that it will be useful, |
|
19 | 19 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
20 | 20 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
21 | 21 | # GNU General Public License for more details. |
|
22 | 22 | # |
|
23 | 23 | # You should have received a copy of the GNU General Public License |
|
24 | 24 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
25 | 25 | import logging |
|
26 | 26 | import formencode |
|
27 | 27 | import traceback |
|
28 | 28 | from formencode import htmlfill |
|
29 | 29 | |
|
30 | 30 | from pylons import tmpl_context as c, request, url |
|
31 | 31 | from pylons.controllers.util import redirect |
|
32 | 32 | from pylons.i18n.translation import _ |
|
33 | 33 | |
|
34 | 34 | import rhodecode.lib.helpers as h |
|
35 | 35 | |
|
36 | 36 | from rhodecode.lib.helpers import Page |
|
37 | 37 | from rhodecode.lib.auth import LoginRequired, HasRepoPermissionAnyDecorator, \ |
|
38 | 38 | NotAnonymous, HasRepoPermissionAny, HasPermissionAllDecorator,\ |
|
39 | 39 | HasPermissionAnyDecorator |
|
40 | 40 | from rhodecode.lib.base import BaseRepoController, render |
|
41 | 41 | from rhodecode.model.db import Repository, RepoGroup, UserFollowing, User,\ |
|
42 | 42 | RhodeCodeUi |
|
43 | 43 | from rhodecode.model.repo import RepoModel |
|
44 | 44 | from rhodecode.model.forms import RepoForkForm |
|
45 | 45 | from rhodecode.model.scm import ScmModel, GroupList |
|
46 | 46 | from rhodecode.lib.utils2 import safe_int |
|
47 | 47 | |
|
48 | 48 | log = logging.getLogger(__name__) |
|
49 | 49 | |
|
50 | 50 | |
|
51 | 51 | class ForksController(BaseRepoController): |
|
52 | 52 | |
|
53 | 53 | @LoginRequired() |
|
54 | 54 | def __before__(self): |
|
55 | 55 | super(ForksController, self).__before__() |
|
56 | 56 | |
|
57 | 57 | def __load_defaults(self): |
|
58 | 58 | acl_groups = GroupList(RepoGroup.query().all(), |
|
59 | 59 | perm_set=['group.write', 'group.admin']) |
|
60 | 60 | c.repo_groups = RepoGroup.groups_choices(groups=acl_groups) |
|
61 | 61 | c.repo_groups_choices = map(lambda k: unicode(k[0]), c.repo_groups) |
|
62 | 62 | choices, c.landing_revs = ScmModel().get_repo_landing_revs() |
|
63 | 63 | c.landing_revs_choices = choices |
|
64 | c.can_update = RhodeCodeUi.get_by_key(RhodeCodeUi.HOOK_UPDATE).ui_active | |
|
64 | 65 | |
|
65 | 66 | def __load_data(self, repo_name=None): |
|
66 | 67 | """ |
|
67 | 68 | Load defaults settings for edit, and update |
|
68 | 69 | |
|
69 | 70 | :param repo_name: |
|
70 | 71 | """ |
|
71 | 72 | self.__load_defaults() |
|
72 | 73 | |
|
73 | 74 | c.repo_info = db_repo = Repository.get_by_repo_name(repo_name) |
|
74 | 75 | repo = db_repo.scm_instance |
|
75 | 76 | |
|
76 | 77 | if c.repo_info is None: |
|
77 | 78 | h.not_mapped_error(repo_name) |
|
78 | 79 | return redirect(url('repos')) |
|
79 | 80 | |
|
80 | 81 | c.default_user_id = User.get_by_username('default').user_id |
|
81 | 82 | c.in_public_journal = UserFollowing.query()\ |
|
82 | 83 | .filter(UserFollowing.user_id == c.default_user_id)\ |
|
83 | 84 | .filter(UserFollowing.follows_repository == c.repo_info).scalar() |
|
84 | 85 | |
|
85 | 86 | if c.repo_info.stats: |
|
86 | 87 | last_rev = c.repo_info.stats.stat_on_revision+1 |
|
87 | 88 | else: |
|
88 | 89 | last_rev = 0 |
|
89 | 90 | c.stats_revision = last_rev |
|
90 | 91 | |
|
91 | 92 | c.repo_last_rev = repo.count() if repo.revisions else 0 |
|
92 | 93 | |
|
93 | 94 | if last_rev == 0 or c.repo_last_rev == 0: |
|
94 | 95 | c.stats_percentage = 0 |
|
95 | 96 | else: |
|
96 | 97 | c.stats_percentage = '%.2f' % ((float((last_rev)) / |
|
97 | 98 | c.repo_last_rev) * 100) |
|
98 | 99 | |
|
99 | c.can_update = RhodeCodeUi.get_by_key(RhodeCodeUi.HOOK_UPDATE).ui_active | |
|
100 | ||
|
101 | 100 | defaults = RepoModel()._get_defaults(repo_name) |
|
102 | 101 | # alter the description to indicate a fork |
|
103 | 102 | defaults['description'] = ('fork of repository: %s \n%s' |
|
104 | 103 | % (defaults['repo_name'], |
|
105 | 104 | defaults['description'])) |
|
106 | 105 | # add suffix to fork |
|
107 | 106 | defaults['repo_name'] = '%s-fork' % defaults['repo_name'] |
|
108 | 107 | |
|
109 | 108 | return defaults |
|
110 | 109 | |
|
111 | 110 | @HasRepoPermissionAnyDecorator('repository.read', 'repository.write', |
|
112 | 111 | 'repository.admin') |
|
113 | 112 | def forks(self, repo_name): |
|
114 | 113 | p = safe_int(request.params.get('page', 1), 1) |
|
115 | 114 | repo_id = c.rhodecode_db_repo.repo_id |
|
116 | 115 | d = [] |
|
117 | 116 | for r in Repository.get_repo_forks(repo_id): |
|
118 | 117 | if not HasRepoPermissionAny( |
|
119 | 118 | 'repository.read', 'repository.write', 'repository.admin' |
|
120 | 119 | )(r.repo_name, 'get forks check'): |
|
121 | 120 | continue |
|
122 | 121 | d.append(r) |
|
123 | 122 | c.forks_pager = Page(d, page=p, items_per_page=20) |
|
124 | 123 | |
|
125 | 124 | c.forks_data = render('/forks/forks_data.html') |
|
126 | 125 | |
|
127 | 126 | if request.environ.get('HTTP_X_PARTIAL_XHR'): |
|
128 | 127 | return c.forks_data |
|
129 | 128 | |
|
130 | 129 | return render('/forks/forks.html') |
|
131 | 130 | |
|
132 | 131 | @NotAnonymous() |
|
133 | 132 | @HasPermissionAnyDecorator('hg.admin', 'hg.fork.repository') |
|
134 | 133 | @HasRepoPermissionAnyDecorator('repository.read', 'repository.write', |
|
135 | 134 | 'repository.admin') |
|
136 | 135 | def fork(self, repo_name): |
|
137 | 136 | c.repo_info = Repository.get_by_repo_name(repo_name) |
|
138 | 137 | if not c.repo_info: |
|
139 | 138 | h.not_mapped_error(repo_name) |
|
140 | 139 | return redirect(url('home')) |
|
141 | 140 | |
|
142 | 141 | defaults = self.__load_data(repo_name) |
|
143 | 142 | |
|
144 | 143 | return htmlfill.render( |
|
145 | 144 | render('forks/fork.html'), |
|
146 | 145 | defaults=defaults, |
|
147 | 146 | encoding="UTF-8", |
|
148 | 147 | force_defaults=False |
|
149 | 148 | ) |
|
150 | 149 | |
|
151 | 150 | @NotAnonymous() |
|
152 | 151 | @HasPermissionAnyDecorator('hg.admin', 'hg.fork.repository') |
|
153 | 152 | @HasRepoPermissionAnyDecorator('repository.read', 'repository.write', |
|
154 | 153 | 'repository.admin') |
|
155 | 154 | def fork_create(self, repo_name): |
|
156 | 155 | self.__load_defaults() |
|
157 | 156 | c.repo_info = Repository.get_by_repo_name(repo_name) |
|
158 | 157 | _form = RepoForkForm(old_data={'repo_type': c.repo_info.repo_type}, |
|
159 | 158 | repo_groups=c.repo_groups_choices, |
|
160 | 159 | landing_revs=c.landing_revs_choices)() |
|
161 | 160 | form_result = {} |
|
162 | 161 | try: |
|
163 | 162 | form_result = _form.to_python(dict(request.POST)) |
|
164 | 163 | |
|
165 | 164 | # an approximation that is better than nothing |
|
166 | 165 | if not RhodeCodeUi.get_by_key(RhodeCodeUi.HOOK_UPDATE).ui_active: |
|
167 | 166 | form_result['update_after_clone'] = False |
|
168 | 167 | |
|
169 | 168 | # create fork is done sometimes async on celery, db transaction |
|
170 | 169 | # management is handled there. |
|
171 | 170 | RepoModel().create_fork(form_result, self.rhodecode_user.user_id) |
|
172 | 171 | fork_url = h.link_to(form_result['repo_name_full'], |
|
173 | 172 | h.url('summary_home', repo_name=form_result['repo_name_full'])) |
|
174 | 173 | |
|
175 | 174 | h.flash(h.literal(_('Forked repository %s as %s') \ |
|
176 | 175 | % (repo_name, fork_url)), |
|
177 | 176 | category='success') |
|
178 | 177 | except formencode.Invalid, errors: |
|
179 | 178 | c.new_repo = errors.value['repo_name'] |
|
180 | 179 | |
|
181 | 180 | return htmlfill.render( |
|
182 | 181 | render('forks/fork.html'), |
|
183 | 182 | defaults=errors.value, |
|
184 | 183 | errors=errors.error_dict or {}, |
|
185 | 184 | prefix_error=False, |
|
186 | 185 | encoding="UTF-8") |
|
187 | 186 | except Exception: |
|
188 | 187 | log.error(traceback.format_exc()) |
|
189 | 188 | h.flash(_('An error occurred during repository forking %s') % |
|
190 | 189 | repo_name, category='error') |
|
191 | 190 | |
|
192 | 191 | return redirect(h.url('summary_home', repo_name=repo_name)) |
General Comments 0
You need to be logged in to leave comments.
Login now