Mercurial > kallithea
comparison rhodecode/controllers/admin/repos_groups.py @ 1345:3bce31f026b8 beta
#47 implemented Adding of new repo_groups+forms+validators. Fixed sorting of repo groups by main names in multiple locations. Removed some unneeded calls to self.sa for exchange to .query() methods.
Added new db unique key for Group
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Mon, 23 May 2011 02:22:00 +0200 |
parents | a04fe5986109 |
children | cf78d302d441 |
comparison
equal
deleted
inserted
replaced
1344:eef7a1b953e8 | 1345:3bce31f026b8 |
---|---|
1 import logging | 1 import logging |
2 import traceback | |
3 import formencode | |
4 | |
5 from formencode import htmlfill | |
2 from operator import itemgetter | 6 from operator import itemgetter |
3 | 7 |
4 from pylons import request, response, session, tmpl_context as c, url | 8 from pylons import request, response, session, tmpl_context as c, url |
5 from pylons.controllers.util import abort, redirect | 9 from pylons.controllers.util import abort, redirect |
10 from pylons.i18n.translation import _ | |
6 | 11 |
12 from rhodecode.lib import helpers as h | |
13 from rhodecode.lib.auth import LoginRequired, HasPermissionAllDecorator, \ | |
14 HasPermissionAnyDecorator | |
7 from rhodecode.lib.base import BaseController, render | 15 from rhodecode.lib.base import BaseController, render |
8 from rhodecode.model.db import Group | 16 from rhodecode.model.db import Group |
17 from rhodecode.model.repos_group import ReposGroupModel | |
18 from rhodecode.model.forms import ReposGroupForm | |
9 | 19 |
10 log = logging.getLogger(__name__) | 20 log = logging.getLogger(__name__) |
11 | 21 |
12 | 22 |
13 class ReposGroupsController(BaseController): | 23 class ReposGroupsController(BaseController): |
14 """REST Controller styled on the Atom Publishing Protocol""" | 24 """REST Controller styled on the Atom Publishing Protocol""" |
15 # To properly map this controller, ensure your config/routing.py | 25 # To properly map this controller, ensure your config/routing.py |
16 # file has a resource setup: | 26 # file has a resource setup: |
17 # map.resource('repos_group', 'repos_groups') | 27 # map.resource('repos_group', 'repos_groups') |
18 | 28 |
29 def __load_defaults(self): | |
30 | |
31 c.repo_groups = [('', '')] | |
32 parents_link = lambda k: h.literal('»'.join( | |
33 map(lambda k: k.group_name, | |
34 k.parents + [k]) | |
35 ) | |
36 ) | |
37 | |
38 c.repo_groups.extend([(x.group_id, parents_link(x)) for \ | |
39 x in self.sa.query(Group).all()]) | |
40 | |
41 c.repo_groups = sorted(c.repo_groups, | |
42 key=lambda t: t[1].split('»')[0]) | |
43 c.repo_groups_choices = map(lambda k: unicode(k[0]), c.repo_groups) | |
44 | |
45 @LoginRequired() | |
46 def __before__(self): | |
47 super(ReposGroupsController, self).__before__() | |
48 | |
49 @HasPermissionAnyDecorator('hg.admin') | |
19 def index(self, format='html'): | 50 def index(self, format='html'): |
20 """GET /repos_groups: All items in the collection""" | 51 """GET /repos_groups: All items in the collection""" |
21 # url('repos_groups') | 52 # url('repos_groups') |
22 | 53 |
54 sk = lambda g:g.parents[0].group_name if g.parents else g.group_name | |
55 c.groups = sorted(Group.query().all(), key=sk) | |
56 return render('admin/repos_groups/repos_groups_show.html') | |
57 | |
58 @HasPermissionAnyDecorator('hg.admin') | |
23 def create(self): | 59 def create(self): |
24 """POST /repos_groups: Create a new item""" | 60 """POST /repos_groups: Create a new item""" |
25 # url('repos_groups') | 61 # url('repos_groups') |
62 self.__load_defaults() | |
63 repos_group_model = ReposGroupModel() | |
64 repos_group_form = ReposGroupForm(available_groups= | |
65 c.repo_groups_choices)() | |
66 try: | |
67 form_result = repos_group_form.to_python(dict(request.POST)) | |
68 repos_group_model.create(form_result) | |
69 h.flash(_('created repos group %s') \ | |
70 % form_result['repos_group_name'], category='success') | |
71 #TODO: in futureaction_logger(, '', '', '', self.sa) | |
72 except formencode.Invalid, errors: | |
26 | 73 |
74 return htmlfill.render( | |
75 render('admin/repos_groups/repos_groups_add.html'), | |
76 defaults=errors.value, | |
77 errors=errors.error_dict or {}, | |
78 prefix_error=False, | |
79 encoding="UTF-8") | |
80 except Exception: | |
81 log.error(traceback.format_exc()) | |
82 h.flash(_('error occurred during creation of repos group %s') \ | |
83 % request.POST.get('repos_group_name'), category='error') | |
84 | |
85 return redirect(url('repos_groups')) | |
86 | |
87 | |
88 @HasPermissionAnyDecorator('hg.admin') | |
27 def new(self, format='html'): | 89 def new(self, format='html'): |
28 """GET /repos_groups/new: Form to create a new item""" | 90 """GET /repos_groups/new: Form to create a new item""" |
29 # url('new_repos_group') | 91 # url('new_repos_group') |
92 self.__load_defaults() | |
93 return render('admin/repos_groups/repos_groups_add.html') | |
30 | 94 |
95 @HasPermissionAnyDecorator('hg.admin') | |
31 def update(self, id): | 96 def update(self, id): |
32 """PUT /repos_groups/id: Update an existing item""" | 97 """PUT /repos_groups/id: Update an existing item""" |
33 # Forms posted to this method should contain a hidden field: | 98 # Forms posted to this method should contain a hidden field: |
34 # <input type="hidden" name="_method" value="PUT" /> | 99 # <input type="hidden" name="_method" value="PUT" /> |
35 # Or using helpers: | 100 # Or using helpers: |
36 # h.form(url('repos_group', id=ID), | 101 # h.form(url('repos_group', id=ID), |
37 # method='put') | 102 # method='put') |
38 # url('repos_group', id=ID) | 103 # url('repos_group', id=ID) |
39 | 104 |
105 @HasPermissionAnyDecorator('hg.admin') | |
40 def delete(self, id): | 106 def delete(self, id): |
41 """DELETE /repos_groups/id: Delete an existing item""" | 107 """DELETE /repos_groups/id: Delete an existing item""" |
42 # Forms posted to this method should contain a hidden field: | 108 # Forms posted to this method should contain a hidden field: |
43 # <input type="hidden" name="_method" value="DELETE" /> | 109 # <input type="hidden" name="_method" value="DELETE" /> |
44 # Or using helpers: | 110 # Or using helpers: |
86 c.groups = self.sa.query(Group).order_by(Group.group_name)\ | 152 c.groups = self.sa.query(Group).order_by(Group.group_name)\ |
87 .filter(Group.group_parent_id == id).all() | 153 .filter(Group.group_parent_id == id).all() |
88 | 154 |
89 return render('admin/repos_groups/repos_groups.html') | 155 return render('admin/repos_groups/repos_groups.html') |
90 | 156 |
157 @HasPermissionAnyDecorator('hg.admin') | |
91 def edit(self, id, format='html'): | 158 def edit(self, id, format='html'): |
92 """GET /repos_groups/id/edit: Form to edit an existing item""" | 159 """GET /repos_groups/id/edit: Form to edit an existing item""" |
93 # url('edit_repos_group', id=ID) | 160 # url('edit_repos_group', id=ID) |