comparison pylons_app/controllers/admin/repos.py @ 323:8026872a10ee

Moved admin controllers to separate module
author Marcin Kuzminski <marcin@python-works.com>
date Wed, 30 Jun 2010 17:14:47 +0200
parents pylons_app/controllers/repos.py@fdf9f6ee5217
children b0715a788432
comparison
equal deleted inserted replaced
322:46b7d108ea7a 323:8026872a10ee
1 #!/usr/bin/env python
2 # encoding: utf-8
3 # repos controller for pylons
4 # Copyright (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com>
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; version 2
8 # of the License or (at your opinion) any later version of the license.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 # MA 02110-1301, USA.
19 """
20 Created on April 7, 2010
21 admin controller for pylons
22 @author: marcink
23 """
24 from formencode import htmlfill
25 from operator import itemgetter
26 from pylons import request, response, session, tmpl_context as c, url
27 from pylons.controllers.util import abort, redirect
28 from pylons.i18n.translation import _
29 from pylons_app.lib import helpers as h
30 from pylons_app.lib.auth import LoginRequired, HasPermissionAllDecorator
31 from pylons_app.lib.base import BaseController, render
32 from pylons_app.lib.utils import invalidate_cache
33 from pylons_app.model.forms import RepoForm
34 from pylons_app.model.hg_model import HgModel
35 from pylons_app.model.repo_model import RepoModel
36 import formencode
37 import logging
38 log = logging.getLogger(__name__)
39
40 class ReposController(BaseController):
41 """REST Controller styled on the Atom Publishing Protocol"""
42 # To properly map this controller, ensure your config/routing.py
43 # file has a resource setup:
44 # map.resource('repo', 'repos')
45
46 @LoginRequired()
47 @HasPermissionAllDecorator('hg.admin')
48 def __before__(self):
49 c.admin_user = session.get('admin_user')
50 c.admin_username = session.get('admin_username')
51 super(ReposController, self).__before__()
52
53 def index(self, format='html'):
54 """GET /repos: All items in the collection"""
55 # url('repos')
56 cached_repo_list = HgModel().get_repos()
57 c.repos_list = sorted(cached_repo_list, key=itemgetter('name_sort'))
58 return render('admin/repos/repos.html')
59
60 def create(self):
61 """POST /repos: Create a new item"""
62 # url('repos')
63 repo_model = RepoModel()
64 _form = RepoForm()()
65 try:
66 form_result = _form.to_python(dict(request.POST))
67 repo_model.create(form_result, c.hg_app_user)
68 invalidate_cache('cached_repo_list')
69 h.flash(_('created repository %s') % form_result['repo_name'],
70 category='success')
71
72 except formencode.Invalid as errors:
73 c.form_errors = errors.error_dict
74 c.new_repo = errors.value['repo_name']
75 return htmlfill.render(
76 render('admin/repos/repo_add.html'),
77 defaults=errors.value,
78 encoding="UTF-8")
79
80 except Exception:
81 h.flash(_('error occured during creation of repository %s') \
82 % form_result['repo_name'], category='error')
83
84 return redirect('repos')
85
86 def new(self, format='html'):
87 """GET /repos/new: Form to create a new item"""
88 new_repo = request.GET.get('repo', '')
89 c.new_repo = h.repo_name_slug(new_repo)
90
91 return render('admin/repos/repo_add.html')
92
93 def update(self, repo_name):
94 """PUT /repos/repo_name: Update an existing item"""
95 # Forms posted to this method should contain a hidden field:
96 # <input type="hidden" name="_method" value="PUT" />
97 # Or using helpers:
98 # h.form(url('repo', repo_name=ID),
99 # method='put')
100 # url('repo', repo_name=ID)
101 repo_model = RepoModel()
102 _form = RepoForm(edit=True)()
103 try:
104 form_result = _form.to_python(dict(request.POST))
105 repo_model.update(repo_name, form_result)
106 invalidate_cache('cached_repo_list')
107 h.flash(_('Repository %s updated succesfully' % repo_name),
108 category='success')
109
110 except formencode.Invalid as errors:
111 c.repo_info = repo_model.get(repo_name)
112 c.users_array = repo_model.get_users_js()
113 errors.value.update({'user':c.repo_info.user.username})
114 c.form_errors = errors.error_dict
115 return htmlfill.render(
116 render('admin/repos/repo_edit.html'),
117 defaults=errors.value,
118 encoding="UTF-8")
119 except Exception:
120 h.flash(_('error occured during update of repository %s') \
121 % form_result['repo_name'], category='error')
122 return redirect(url('repos'))
123
124 def delete(self, repo_name):
125 """DELETE /repos/repo_name: Delete an existing item"""
126 # Forms posted to this method should contain a hidden field:
127 # <input type="hidden" name="_method" value="DELETE" />
128 # Or using helpers:
129 # h.form(url('repo', repo_name=ID),
130 # method='delete')
131 # url('repo', repo_name=ID)
132
133 repo_model = RepoModel()
134 repo = repo_model.get(repo_name)
135 if not repo:
136 h.flash(_('%s repository is not mapped to db perhaps'
137 ' it was moved or renamed from the filesystem'
138 ' please run the application again'
139 ' in order to rescan repositories') % repo_name,
140 category='error')
141
142 return redirect(url('repos'))
143 try:
144 repo_model.delete(repo)
145 invalidate_cache('cached_repo_list')
146 h.flash(_('deleted repository %s') % repo_name, category='success')
147 except Exception:
148 h.flash(_('An error occured during deletion of %s') % repo_name,
149 category='error')
150
151 return redirect(url('repos'))
152
153 def delete_perm_user(self, repo_name):
154 """
155 DELETE an existing repository permission user
156 @param repo_name:
157 """
158
159 try:
160 repo_model = RepoModel()
161 repo_model.delete_perm_user(request.POST, repo_name)
162 except Exception as e:
163 h.flash(_('An error occured during deletion of repository user'),
164 category='error')
165
166
167 def show(self, repo_name, format='html'):
168 """GET /repos/repo_name: Show a specific item"""
169 # url('repo', repo_name=ID)
170
171 def edit(self, repo_name, format='html'):
172 """GET /repos/repo_name/edit: Form to edit an existing item"""
173 # url('edit_repo', repo_name=ID)
174 repo_model = RepoModel()
175 c.repo_info = repo = repo_model.get(repo_name)
176 if not repo:
177 h.flash(_('%s repository is not mapped to db perhaps'
178 ' it was created or renamed from the filesystem'
179 ' please run the application again'
180 ' in order to rescan repositories') % repo_name,
181 category='error')
182
183 return redirect(url('repos'))
184 defaults = c.repo_info.__dict__
185 defaults.update({'user':c.repo_info.user.username})
186 c.users_array = repo_model.get_users_js()
187
188 for p in c.repo_info.repo2perm:
189 defaults.update({'perm_%s' % p.user.username:
190 p.permission.permission_name})
191
192 return htmlfill.render(
193 render('admin/repos/repo_edit.html'),
194 defaults=defaults,
195 encoding="UTF-8",
196 force_defaults=False
197 )