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