##// END OF EJS Templates
protected admin controllers
marcink -
r305:61be6dcd default
parent child Browse files
Show More
@@ -1,54 +1,52 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 # encoding: utf-8
2 # encoding: utf-8
3 # admin controller for pylons
3 # admin controller for pylons
4 # Copyright (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com>
4 # Copyright (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com>
5
5
6 # This program is free software; you can redistribute it and/or
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; version 2
8 # as published by the Free Software Foundation; version 2
9 # of the License or (at your opinion) any later version of the license.
9 # of the License or (at your opinion) any later version of the license.
10 #
10 #
11 # This program is distributed in the hope that it will be useful,
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
14 # GNU General Public License for more details.
15 #
15 #
16 # You should have received a copy of the GNU General Public License
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 # MA 02110-1301, USA.
19 # MA 02110-1301, USA.
20 """
20 """
21 Created on April 7, 2010
21 Created on April 7, 2010
22 admin controller for pylons
22 admin controller for pylons
23 @author: marcink
23 @author: marcink
24 """
24 """
25 import logging
25 import logging
26 from pylons import request, response, session, tmpl_context as c
26 from pylons import request, response, session, tmpl_context as c
27 from pylons_app.lib.base import BaseController, render
27 from pylons_app.lib.base import BaseController, render
28 from pylons_app.model import meta
28 from pylons_app.model import meta
29 from pylons_app.model.db import UserLog
29 from pylons_app.model.db import UserLog
30 from webhelpers.paginate import Page
30 from webhelpers.paginate import Page
31 from pylons_app.lib.auth import LoginRequired
31 from pylons_app.lib.auth import LoginRequired, HasPermissionAllDecorator
32
32
33 log = logging.getLogger(__name__)
33 log = logging.getLogger(__name__)
34
34
35 class AdminController(BaseController):
35 class AdminController(BaseController):
36
36
37 @LoginRequired()
37 @LoginRequired()
38 def __before__(self):
38 def __before__(self):
39 user = session['hg_app_user']
40 c.admin_user = user.is_admin
41 c.admin_username = user.username
42 super(AdminController, self).__before__()
39 super(AdminController, self).__before__()
43
40
41 @HasPermissionAllDecorator('hg.admin')
44 def index(self):
42 def index(self):
45 sa = meta.Session
43 sa = meta.Session
46
44
47 users_log = sa.query(UserLog).order_by(UserLog.action_date.desc())
45 users_log = sa.query(UserLog).order_by(UserLog.action_date.desc())
48 p = int(request.params.get('page', 1))
46 p = int(request.params.get('page', 1))
49 c.users_log = Page(users_log, page=p, items_per_page=10)
47 c.users_log = Page(users_log, page=p, items_per_page=10)
50 c.log_data = render('admin/admin_log.html')
48 c.log_data = render('admin/admin_log.html')
51 if request.params.get('partial'):
49 if request.params.get('partial'):
52 return c.log_data
50 return c.log_data
53 return render('admin/admin.html')
51 return render('admin/admin.html')
54
52
@@ -1,77 +1,90 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 # encoding: utf-8
2 # encoding: utf-8
3 # permissions controller for pylons
3 # permissions controller for pylons
4 # Copyright (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com>
4 # Copyright (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com>
5
5
6 # This program is free software; you can redistribute it and/or
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; version 2
8 # as published by the Free Software Foundation; version 2
9 # of the License or (at your opinion) any later version of the license.
9 # of the License or (at your opinion) any later version of the license.
10 #
10 #
11 # This program is distributed in the hope that it will be useful,
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
14 # GNU General Public License for more details.
15 #
15 #
16 # You should have received a copy of the GNU General Public License
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 # MA 02110-1301, USA.
19 # MA 02110-1301, USA.
20 """
20 """
21 Created on April 27, 2010
21 Created on April 27, 2010
22 permissions controller for pylons
22 permissions controller for pylons
23 @author: marcink
23 @author: marcink
24 """
24 """
25 from formencode import htmlfill
26 from pylons import request, 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.model.db import User, UserLog
33 from pylons_app.model.forms import UserForm
34 from pylons_app.model.user_model import UserModel
35 import formencode
25 import logging
36 import logging
26
37
27 from pylons import request, response, session, tmpl_context as c, url
28 from pylons.controllers.util import abort, redirect
29
30 from pylons_app.lib.base import BaseController, render
31
32 log = logging.getLogger(__name__)
38 log = logging.getLogger(__name__)
33
39
34 class PermissionsController(BaseController):
40 class PermissionsController(BaseController):
35 """REST Controller styled on the Atom Publishing Protocol"""
41 """REST Controller styled on the Atom Publishing Protocol"""
36 # To properly map this controller, ensure your config/routing.py
42 # To properly map this controller, ensure your config/routing.py
37 # file has a resource setup:
43 # file has a resource setup:
38 # map.resource('permission', 'permissions')
44 # map.resource('permission', 'permissions')
39
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(PermissionsController, self).__before__()
52
40 def index(self, format='html'):
53 def index(self, format='html'):
41 """GET /permissions: All items in the collection"""
54 """GET /permissions: All items in the collection"""
42 # url('permissions')
55 # url('permissions')
43 return render('admin/permissions/permissions.html')
56 return render('admin/permissions/permissions.html')
44
57
45 def create(self):
58 def create(self):
46 """POST /permissions: Create a new item"""
59 """POST /permissions: Create a new item"""
47 # url('permissions')
60 # url('permissions')
48
61
49 def new(self, format='html'):
62 def new(self, format='html'):
50 """GET /permissions/new: Form to create a new item"""
63 """GET /permissions/new: Form to create a new item"""
51 # url('new_permission')
64 # url('new_permission')
52
65
53 def update(self, id):
66 def update(self, id):
54 """PUT /permissions/id: Update an existing item"""
67 """PUT /permissions/id: Update an existing item"""
55 # Forms posted to this method should contain a hidden field:
68 # Forms posted to this method should contain a hidden field:
56 # <input type="hidden" name="_method" value="PUT" />
69 # <input type="hidden" name="_method" value="PUT" />
57 # Or using helpers:
70 # Or using helpers:
58 # h.form(url('permission', id=ID),
71 # h.form(url('permission', id=ID),
59 # method='put')
72 # method='put')
60 # url('permission', id=ID)
73 # url('permission', id=ID)
61
74
62 def delete(self, id):
75 def delete(self, id):
63 """DELETE /permissions/id: Delete an existing item"""
76 """DELETE /permissions/id: Delete an existing item"""
64 # Forms posted to this method should contain a hidden field:
77 # Forms posted to this method should contain a hidden field:
65 # <input type="hidden" name="_method" value="DELETE" />
78 # <input type="hidden" name="_method" value="DELETE" />
66 # Or using helpers:
79 # Or using helpers:
67 # h.form(url('permission', id=ID),
80 # h.form(url('permission', id=ID),
68 # method='delete')
81 # method='delete')
69 # url('permission', id=ID)
82 # url('permission', id=ID)
70
83
71 def show(self, id, format='html'):
84 def show(self, id, format='html'):
72 """GET /permissions/id: Show a specific item"""
85 """GET /permissions/id: Show a specific item"""
73 # url('permission', id=ID)
86 # url('permission', id=ID)
74
87
75 def edit(self, id, format='html'):
88 def edit(self, id, format='html'):
76 """GET /permissions/id/edit: Form to edit an existing item"""
89 """GET /permissions/id/edit: Form to edit an existing item"""
77 # url('edit_permission', id=ID)
90 # url('edit_permission', id=ID)
@@ -1,147 +1,149 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 # encoding: utf-8
2 # encoding: utf-8
3 # users controller for pylons
3 # users controller for pylons
4 # Copyright (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com>
4 # Copyright (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com>
5
5
6 # This program is free software; you can redistribute it and/or
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; version 2
8 # as published by the Free Software Foundation; version 2
9 # of the License or (at your opinion) any later version of the license.
9 # of the License or (at your opinion) any later version of the license.
10 #
10 #
11 # This program is distributed in the hope that it will be useful,
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
14 # GNU General Public License for more details.
15 #
15 #
16 # You should have received a copy of the GNU General Public License
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 # MA 02110-1301, USA.
19 # MA 02110-1301, USA.
20 """
20 """
21 Created on April 4, 2010
21 Created on April 4, 2010
22 users controller for pylons
22 users controller for pylons
23 @author: marcink
23 @author: marcink
24 """
24 """
25 import logging
25 from formencode import htmlfill
26 from pylons import request, session, tmpl_context as c, url
26 from pylons import request, session, tmpl_context as c, url
27 from pylons.controllers.util import abort, redirect
27 from pylons.controllers.util import abort, redirect
28 from pylons.i18n.translation import _
28 from pylons.i18n.translation import _
29 from pylons_app.lib import helpers as h
29 from pylons_app.lib import helpers as h
30 from pylons_app.lib.auth import LoginRequired
30 from pylons_app.lib.auth import LoginRequired, HasPermissionAllDecorator
31 from pylons_app.lib.base import BaseController, render
31 from pylons_app.lib.base import BaseController, render
32 from pylons_app.model.db import User, UserLog
32 from pylons_app.model.db import User, UserLog
33 from pylons_app.model.forms import UserForm
33 from pylons_app.model.forms import UserForm
34 from pylons_app.model.user_model import UserModel
34 from pylons_app.model.user_model import UserModel
35 import formencode
35 import formencode
36 from formencode import htmlfill
36 import logging
37
37
38 log = logging.getLogger(__name__)
38 log = logging.getLogger(__name__)
39
39
40 class UsersController(BaseController):
40 class UsersController(BaseController):
41 """REST Controller styled on the Atom Publishing Protocol"""
41 """REST Controller styled on the Atom Publishing Protocol"""
42 # To properly map this controller, ensure your config/routing.py
42 # To properly map this controller, ensure your config/routing.py
43 # file has a resource setup:
43 # file has a resource setup:
44 # map.resource('user', 'users')
44 # map.resource('user', 'users')
45
45 @LoginRequired()
46 @LoginRequired()
47 @HasPermissionAllDecorator('hg.admin')
46 def __before__(self):
48 def __before__(self):
47 c.admin_user = session.get('admin_user')
49 c.admin_user = session.get('admin_user')
48 c.admin_username = session.get('admin_username')
50 c.admin_username = session.get('admin_username')
49 super(UsersController, self).__before__()
51 super(UsersController, self).__before__()
50
52
51
53
52 def index(self, format='html'):
54 def index(self, format='html'):
53 """GET /users: All items in the collection"""
55 """GET /users: All items in the collection"""
54 # url('users')
56 # url('users')
55
57
56 c.users_list = self.sa.query(User).all()
58 c.users_list = self.sa.query(User).all()
57 return render('admin/users/users.html')
59 return render('admin/users/users.html')
58
60
59 def create(self):
61 def create(self):
60 """POST /users: Create a new item"""
62 """POST /users: Create a new item"""
61 # url('users')
63 # url('users')
62
64
63 user_model = UserModel()
65 user_model = UserModel()
64 login_form = UserForm()()
66 login_form = UserForm()()
65 try:
67 try:
66 form_result = login_form.to_python(dict(request.POST))
68 form_result = login_form.to_python(dict(request.POST))
67 user_model.create(form_result)
69 user_model.create(form_result)
68 h.flash(_('created user %s') % form_result['username'],
70 h.flash(_('created user %s') % form_result['username'],
69 category='success')
71 category='success')
70 except formencode.Invalid as errors:
72 except formencode.Invalid as errors:
71 c.form_errors = errors.error_dict
73 c.form_errors = errors.error_dict
72 return htmlfill.render(
74 return htmlfill.render(
73 render('admin/users/user_add.html'),
75 render('admin/users/user_add.html'),
74 defaults=errors.value,
76 defaults=errors.value,
75 encoding="UTF-8")
77 encoding="UTF-8")
76 except Exception:
78 except Exception:
77 h.flash(_('error occured during creation of user %s') \
79 h.flash(_('error occured during creation of user %s') \
78 % form_result['username'], category='error')
80 % form_result['username'], category='error')
79 return redirect(url('users'))
81 return redirect(url('users'))
80
82
81 def new(self, format='html'):
83 def new(self, format='html'):
82 """GET /users/new: Form to create a new item"""
84 """GET /users/new: Form to create a new item"""
83 # url('new_user')
85 # url('new_user')
84 return render('admin/users/user_add.html')
86 return render('admin/users/user_add.html')
85
87
86 def update(self, id):
88 def update(self, id):
87 """PUT /users/id: Update an existing item"""
89 """PUT /users/id: Update an existing item"""
88 # Forms posted to this method should contain a hidden field:
90 # Forms posted to this method should contain a hidden field:
89 # <input type="hidden" name="_method" value="PUT" />
91 # <input type="hidden" name="_method" value="PUT" />
90 # Or using helpers:
92 # Or using helpers:
91 # h.form(url('user', id=ID),
93 # h.form(url('user', id=ID),
92 # method='put')
94 # method='put')
93 # url('user', id=ID)
95 # url('user', id=ID)
94 user_model = UserModel()
96 user_model = UserModel()
95 _form = UserForm(edit=True)()
97 _form = UserForm(edit=True)()
96 try:
98 try:
97 form_result = _form.to_python(dict(request.POST))
99 form_result = _form.to_python(dict(request.POST))
98 user_model.update(id, form_result)
100 user_model.update(id, form_result)
99 h.flash(_('User updated succesfully'), category='success')
101 h.flash(_('User updated succesfully'), category='success')
100
102
101 except formencode.Invalid as errors:
103 except formencode.Invalid as errors:
102 c.user = user_model.get_user(id)
104 c.user = user_model.get_user(id)
103 c.form_errors = errors.error_dict
105 c.form_errors = errors.error_dict
104 return htmlfill.render(
106 return htmlfill.render(
105 render('admin/users/user_edit.html'),
107 render('admin/users/user_edit.html'),
106 defaults=errors.value,
108 defaults=errors.value,
107 encoding="UTF-8")
109 encoding="UTF-8")
108 except Exception:
110 except Exception:
109 h.flash(_('error occured during update of user %s') \
111 h.flash(_('error occured during update of user %s') \
110 % form_result['username'], category='error')
112 % form_result['username'], category='error')
111
113
112 return redirect(url('users'))
114 return redirect(url('users'))
113
115
114 def delete(self, id):
116 def delete(self, id):
115 """DELETE /users/id: Delete an existing item"""
117 """DELETE /users/id: Delete an existing item"""
116 # Forms posted to this method should contain a hidden field:
118 # Forms posted to this method should contain a hidden field:
117 # <input type="hidden" name="_method" value="DELETE" />
119 # <input type="hidden" name="_method" value="DELETE" />
118 # Or using helpers:
120 # Or using helpers:
119 # h.form(url('user', id=ID),
121 # h.form(url('user', id=ID),
120 # method='delete')
122 # method='delete')
121 # url('user', id=ID)
123 # url('user', id=ID)
122 user_model = UserModel()
124 user_model = UserModel()
123 try:
125 try:
124 user_model.delete(id)
126 user_model.delete(id)
125 h.flash(_('sucessfully deleted user'), category='success')
127 h.flash(_('sucessfully deleted user'), category='success')
126 except Exception:
128 except Exception:
127 h.flash(_('An error occured during deletion of user'),
129 h.flash(_('An error occured during deletion of user'),
128 category='error')
130 category='error')
129
131
130 return redirect(url('users'))
132 return redirect(url('users'))
131
133
132 def show(self, id, format='html'):
134 def show(self, id, format='html'):
133 """GET /users/id: Show a specific item"""
135 """GET /users/id: Show a specific item"""
134 # url('user', id=ID)
136 # url('user', id=ID)
135
137
136
138
137 def edit(self, id, format='html'):
139 def edit(self, id, format='html'):
138 """GET /users/id/edit: Form to edit an existing item"""
140 """GET /users/id/edit: Form to edit an existing item"""
139 # url('edit_user', id=ID)
141 # url('edit_user', id=ID)
140 c.user = self.sa.query(User).get(id)
142 c.user = self.sa.query(User).get(id)
141 defaults = c.user.__dict__
143 defaults = c.user.__dict__
142 return htmlfill.render(
144 return htmlfill.render(
143 render('admin/users/user_edit.html'),
145 render('admin/users/user_edit.html'),
144 defaults=defaults,
146 defaults=defaults,
145 encoding="UTF-8",
147 encoding="UTF-8",
146 force_defaults=False
148 force_defaults=False
147 )
149 )
General Comments 0
You need to be logged in to leave comments. Login now