Mercurial > kallithea
comparison rhodecode/controllers/admin/repos_groups.py @ 1512:bf263968da47
merge beta in stable branch
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Fri, 07 Oct 2011 01:08:50 +0200 |
parents | a39c0e5fca89 |
children | 27be8f94c207 |
comparison
equal
deleted
inserted
replaced
1329:e058df3ff2b4 | 1512:bf263968da47 |
---|---|
1 import logging | |
2 import traceback | |
3 import formencode | |
4 | |
5 from formencode import htmlfill | |
6 from operator import itemgetter | |
7 | |
8 from pylons import request, response, session, tmpl_context as c, url | |
9 from pylons.controllers.util import abort, redirect | |
10 from pylons.i18n.translation import _ | |
11 | |
12 from rhodecode.lib import helpers as h | |
13 from rhodecode.lib.auth import LoginRequired, HasPermissionAllDecorator, \ | |
14 HasPermissionAnyDecorator | |
15 from rhodecode.lib.base import BaseController, render | |
16 from rhodecode.model.db import Group | |
17 from rhodecode.model.repos_group import ReposGroupModel | |
18 from rhodecode.model.forms import ReposGroupForm | |
19 | |
20 log = logging.getLogger(__name__) | |
21 | |
22 | |
23 class ReposGroupsController(BaseController): | |
24 """REST Controller styled on the Atom Publishing Protocol""" | |
25 # To properly map this controller, ensure your config/routing.py | |
26 # file has a resource setup: | |
27 # map.resource('repos_group', 'repos_groups') | |
28 | |
29 @LoginRequired() | |
30 def __before__(self): | |
31 super(ReposGroupsController, self).__before__() | |
32 | |
33 def __load_defaults(self): | |
34 | |
35 c.repo_groups = [('', '')] | |
36 parents_link = lambda k: h.literal('»'.join( | |
37 map(lambda k: k.group_name, | |
38 k.parents + [k]) | |
39 ) | |
40 ) | |
41 | |
42 c.repo_groups.extend([(x.group_id, parents_link(x)) for \ | |
43 x in self.sa.query(Group).all()]) | |
44 | |
45 c.repo_groups = sorted(c.repo_groups, | |
46 key=lambda t: t[1].split('»')[0]) | |
47 c.repo_groups_choices = map(lambda k: unicode(k[0]), c.repo_groups) | |
48 | |
49 def __load_data(self, group_id): | |
50 """ | |
51 Load defaults settings for edit, and update | |
52 | |
53 :param group_id: | |
54 """ | |
55 self.__load_defaults() | |
56 | |
57 repo_group = Group.get(group_id) | |
58 | |
59 data = repo_group.get_dict() | |
60 | |
61 return data | |
62 | |
63 @HasPermissionAnyDecorator('hg.admin') | |
64 def index(self, format='html'): | |
65 """GET /repos_groups: All items in the collection""" | |
66 # url('repos_groups') | |
67 | |
68 sk = lambda g:g.parents[0].group_name if g.parents else g.group_name | |
69 c.groups = sorted(Group.query().all(), key=sk) | |
70 return render('admin/repos_groups/repos_groups_show.html') | |
71 | |
72 @HasPermissionAnyDecorator('hg.admin') | |
73 def create(self): | |
74 """POST /repos_groups: Create a new item""" | |
75 # url('repos_groups') | |
76 self.__load_defaults() | |
77 repos_group_model = ReposGroupModel() | |
78 repos_group_form = ReposGroupForm(available_groups= | |
79 c.repo_groups_choices)() | |
80 try: | |
81 form_result = repos_group_form.to_python(dict(request.POST)) | |
82 repos_group_model.create(form_result) | |
83 h.flash(_('created repos group %s') \ | |
84 % form_result['group_name'], category='success') | |
85 #TODO: in futureaction_logger(, '', '', '', self.sa) | |
86 except formencode.Invalid, errors: | |
87 | |
88 return htmlfill.render( | |
89 render('admin/repos_groups/repos_groups_add.html'), | |
90 defaults=errors.value, | |
91 errors=errors.error_dict or {}, | |
92 prefix_error=False, | |
93 encoding="UTF-8") | |
94 except Exception: | |
95 log.error(traceback.format_exc()) | |
96 h.flash(_('error occurred during creation of repos group %s') \ | |
97 % request.POST.get('group_name'), category='error') | |
98 | |
99 return redirect(url('repos_groups')) | |
100 | |
101 | |
102 @HasPermissionAnyDecorator('hg.admin') | |
103 def new(self, format='html'): | |
104 """GET /repos_groups/new: Form to create a new item""" | |
105 # url('new_repos_group') | |
106 self.__load_defaults() | |
107 return render('admin/repos_groups/repos_groups_add.html') | |
108 | |
109 @HasPermissionAnyDecorator('hg.admin') | |
110 def update(self, id): | |
111 """PUT /repos_groups/id: Update an existing item""" | |
112 # Forms posted to this method should contain a hidden field: | |
113 # <input type="hidden" name="_method" value="PUT" /> | |
114 # Or using helpers: | |
115 # h.form(url('repos_group', id=ID), | |
116 # method='put') | |
117 # url('repos_group', id=ID) | |
118 | |
119 self.__load_defaults() | |
120 c.repos_group = Group.get(id) | |
121 | |
122 repos_group_model = ReposGroupModel() | |
123 repos_group_form = ReposGroupForm(edit=True, | |
124 old_data=c.repos_group.get_dict(), | |
125 available_groups= | |
126 c.repo_groups_choices)() | |
127 try: | |
128 form_result = repos_group_form.to_python(dict(request.POST)) | |
129 repos_group_model.update(id, form_result) | |
130 h.flash(_('updated repos group %s') \ | |
131 % form_result['group_name'], category='success') | |
132 #TODO: in futureaction_logger(, '', '', '', self.sa) | |
133 except formencode.Invalid, errors: | |
134 | |
135 return htmlfill.render( | |
136 render('admin/repos_groups/repos_groups_edit.html'), | |
137 defaults=errors.value, | |
138 errors=errors.error_dict or {}, | |
139 prefix_error=False, | |
140 encoding="UTF-8") | |
141 except Exception: | |
142 log.error(traceback.format_exc()) | |
143 h.flash(_('error occurred during update of repos group %s') \ | |
144 % request.POST.get('group_name'), category='error') | |
145 | |
146 return redirect(url('repos_groups')) | |
147 | |
148 | |
149 @HasPermissionAnyDecorator('hg.admin') | |
150 def delete(self, id): | |
151 """DELETE /repos_groups/id: Delete an existing item""" | |
152 # Forms posted to this method should contain a hidden field: | |
153 # <input type="hidden" name="_method" value="DELETE" /> | |
154 # Or using helpers: | |
155 # h.form(url('repos_group', id=ID), | |
156 # method='delete') | |
157 # url('repos_group', id=ID) | |
158 | |
159 repos_group_model = ReposGroupModel() | |
160 gr = Group.get(id) | |
161 repos = gr.repositories.all() | |
162 if repos: | |
163 h.flash(_('This group contains %s repositores and cannot be ' | |
164 'deleted' % len(repos)), | |
165 category='error') | |
166 return redirect(url('repos_groups')) | |
167 | |
168 try: | |
169 repos_group_model.delete(id) | |
170 h.flash(_('removed repos group %s' % gr.group_name), category='success') | |
171 #TODO: in future action_logger(, '', '', '', self.sa) | |
172 except Exception: | |
173 log.error(traceback.format_exc()) | |
174 h.flash(_('error occurred during deletion of repos group %s' % gr.group_name), | |
175 category='error') | |
176 | |
177 return redirect(url('repos_groups')) | |
178 | |
179 def show(self, id, format='html'): | |
180 """GET /repos_groups/id: Show a specific item""" | |
181 # url('repos_group', id=ID) | |
182 | |
183 c.group = Group.get(id) | |
184 | |
185 if c.group: | |
186 c.group_repos = c.group.repositories.all() | |
187 else: | |
188 return redirect(url('home')) | |
189 | |
190 #overwrite our cached list with current filter | |
191 gr_filter = c.group_repos | |
192 c.cached_repo_list = self.scm_model.get_repos(all_repos=gr_filter) | |
193 | |
194 c.repos_list = c.cached_repo_list | |
195 | |
196 c.repo_cnt = 0 | |
197 | |
198 c.groups = self.sa.query(Group).order_by(Group.group_name)\ | |
199 .filter(Group.group_parent_id == id).all() | |
200 | |
201 return render('admin/repos_groups/repos_groups.html') | |
202 | |
203 @HasPermissionAnyDecorator('hg.admin') | |
204 def edit(self, id, format='html'): | |
205 """GET /repos_groups/id/edit: Form to edit an existing item""" | |
206 # url('edit_repos_group', id=ID) | |
207 | |
208 id_ = int(id) | |
209 | |
210 c.repos_group = Group.get(id_) | |
211 defaults = self.__load_data(id_) | |
212 | |
213 # we need to exclude this group from the group list for editing | |
214 c.repo_groups = filter(lambda x:x[0] != id_, c.repo_groups) | |
215 | |
216 return htmlfill.render( | |
217 render('admin/repos_groups/repos_groups_edit.html'), | |
218 defaults=defaults, | |
219 encoding="UTF-8", | |
220 force_defaults=False | |
221 ) | |
222 | |
223 |