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