annotate pylons_app/controllers/users.py @ 235:fcab58c43ea1

Fixed access to repos and users.
author Marcin Kuzminski <marcin@python-works.com>
date Sun, 30 May 2010 00:44:45 +0200
parents a0116e944da1
children a55c17874486
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
235
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
1 from formencode import htmlfill
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
2 from pylons import request, response, session, tmpl_context as c, url, \
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
3 app_globals as g
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
4 from pylons.controllers.util import abort, redirect
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
5 from pylons_app.lib.auth import LoginRequired
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
6 from pylons_app.lib.base import BaseController, render
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
7 from pylons_app.model.db import User, UserLog
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
8 from pylons_app.model.forms import UserForm
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
9 from pylons_app.model.user_model import UserModel
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
10 import formencode
47
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
11 import logging
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
12
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
13
52
25e516447a33 implemented autentication
marcink
parents: 50
diff changeset
14
47
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
15 log = logging.getLogger(__name__)
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
16
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
17 class UsersController(BaseController):
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
18 """REST Controller styled on the Atom Publishing Protocol"""
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
19 # To properly map this controller, ensure your config/routing.py
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
20 # file has a resource setup:
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
21 # map.resource('user', 'users')
235
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
22 @LoginRequired()
47
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
23 def __before__(self):
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
24 c.admin_user = session.get('admin_user')
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
25 c.admin_username = session.get('admin_username')
191
b68b2246e5a6 Authenticated controller with LoginRequired decorator, and cleaned __before__ (used in baseController now). fixed User for clone url with logged in session user.
Marcin Kuzminski <marcin@python-works.com>
parents: 151
diff changeset
26 super(UsersController, self).__before__()
47
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
27
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
28 def index(self, format='html'):
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
29 """GET /users: All items in the collection"""
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
30 # url('users')
48
8e250e86a670 Css fixes, implemented removal of users, and display draft
Marcin Kuzminski <marcin@python-blog.com>
parents: 47
diff changeset
31
234
a0116e944da1 changed naming convention for db modules.
Marcin Kuzminski <marcin@python-works.com>
parents: 191
diff changeset
32 c.users_list = self.sa.query(User).all()
127
20dc7a5eb748 Html changes and cleanups, made folders for html templates, implemented tags and branches pages
Marcin Kuzminski <marcin@python-works.com>
parents: 101
diff changeset
33 return render('admin/users/users.html')
47
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
34
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
35 def create(self):
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
36 """POST /users: Create a new item"""
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
37 # url('users')
235
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
38
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
39 user_model = UserModel()
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
40 login_form = UserForm()
50
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
41 try:
235
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
42 form_result = login_form.to_python(dict(request.POST))
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
43 user_model.create(form_result)
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
44 return redirect(url('users'))
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
45
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
46 except formencode.Invalid as errors:
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
47 c.form_errors = errors.error_dict
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
48 return htmlfill.render(
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
49 render('admin/users/user_add.html'),
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
50 defaults=errors.value,
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
51 encoding="UTF-8")
50
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
52
47
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
53 def new(self, format='html'):
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
54 """GET /users/new: Form to create a new item"""
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
55 # url('new_user')
127
20dc7a5eb748 Html changes and cleanups, made folders for html templates, implemented tags and branches pages
Marcin Kuzminski <marcin@python-works.com>
parents: 101
diff changeset
56 return render('admin/users/user_add.html')
47
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
57
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
58 def update(self, id):
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
59 """PUT /users/id: Update an existing item"""
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
60 # Forms posted to this method should contain a hidden field:
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
61 # <input type="hidden" name="_method" value="PUT" />
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
62 # Or using helpers:
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
63 # h.form(url('user', id=ID),
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
64 # method='put')
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
65 # url('user', id=ID)
235
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
66 user_model = UserModel()
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
67 login_form = UserForm()
50
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
68 try:
235
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
69 form_result = login_form.to_python(dict(request.POST))
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
70 user_model.update(id, form_result)
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
71 return redirect(url('users'))
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
72
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
73 except formencode.Invalid as errors:
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
74 errors.value
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
75 c.user = user_model.get_user(id)
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
76 c.form_errors = errors.error_dict
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
77 return htmlfill.render(
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
78 render('admin/users/user_edit.html'),
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
79 defaults=errors.value,
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
80 encoding="UTF-8")
50
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
81
47
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
82 def delete(self, id):
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
83 """DELETE /users/id: Delete an existing item"""
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
84 # Forms posted to this method should contain a hidden field:
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
85 # <input type="hidden" name="_method" value="DELETE" />
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
86 # Or using helpers:
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
87 # h.form(url('user', id=ID),
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
88 # method='delete')
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
89 # url('user', id=ID)
48
8e250e86a670 Css fixes, implemented removal of users, and display draft
Marcin Kuzminski <marcin@python-blog.com>
parents: 47
diff changeset
90 try:
234
a0116e944da1 changed naming convention for db modules.
Marcin Kuzminski <marcin@python-works.com>
parents: 191
diff changeset
91 self.sa.delete(self.sa.query(User).get(id))
49
3ada2f409c1c Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents: 48
diff changeset
92 self.sa.commit()
48
8e250e86a670 Css fixes, implemented removal of users, and display draft
Marcin Kuzminski <marcin@python-blog.com>
parents: 47
diff changeset
93 except:
49
3ada2f409c1c Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents: 48
diff changeset
94 self.sa.rollback()
48
8e250e86a670 Css fixes, implemented removal of users, and display draft
Marcin Kuzminski <marcin@python-blog.com>
parents: 47
diff changeset
95 raise
8e250e86a670 Css fixes, implemented removal of users, and display draft
Marcin Kuzminski <marcin@python-blog.com>
parents: 47
diff changeset
96 return redirect(url('users'))
8e250e86a670 Css fixes, implemented removal of users, and display draft
Marcin Kuzminski <marcin@python-blog.com>
parents: 47
diff changeset
97
47
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
98 def show(self, id, format='html'):
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
99 """GET /users/id: Show a specific item"""
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
100 # url('user', id=ID)
50
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
101
48
8e250e86a670 Css fixes, implemented removal of users, and display draft
Marcin Kuzminski <marcin@python-blog.com>
parents: 47
diff changeset
102
47
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
103 def edit(self, id, format='html'):
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
104 """GET /users/id/edit: Form to edit an existing item"""
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
105 # url('edit_user', id=ID)
234
a0116e944da1 changed naming convention for db modules.
Marcin Kuzminski <marcin@python-works.com>
parents: 191
diff changeset
106 c.user = self.sa.query(User).get(id)
70
9a2affee4a45 Updated defaults bug of htmlfill + changed routing
Marcin Kuzminski <marcin@python-blog.com>
parents: 65
diff changeset
107 defaults = c.user.__dict__
50
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
108 return htmlfill.render(
127
20dc7a5eb748 Html changes and cleanups, made folders for html templates, implemented tags and branches pages
Marcin Kuzminski <marcin@python-works.com>
parents: 101
diff changeset
109 render('admin/users/user_edit.html'),
70
9a2affee4a45 Updated defaults bug of htmlfill + changed routing
Marcin Kuzminski <marcin@python-blog.com>
parents: 65
diff changeset
110 defaults=defaults,
50
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
111 encoding="UTF-8",
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
112 force_defaults=False
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
113 )