annotate pylons_app/controllers/users.py @ 50:73f413946c14

user managment implementation continued update/delete/create works + templating changes
author Marcin Kuzminski <marcin@python-blog.com>
date Thu, 08 Apr 2010 03:22:32 +0200
parents 3ada2f409c1c
children 25e516447a33
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
47
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
1 import logging
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
2
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
3 from pylons import request, response, session, tmpl_context as c, url, app_globals as g
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
4 from pylons.controllers.util import abort, redirect
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
5
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
6 from pylons_app.lib.base import BaseController, render
49
3ada2f409c1c Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents: 48
diff changeset
7 from formencode import htmlfill
3ada2f409c1c Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents: 48
diff changeset
8 from pylons_app.model import meta
3ada2f409c1c Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents: 48
diff changeset
9 from pylons_app.model.db import Users, UserLogs
50
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
10 import crypt
47
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
11 log = logging.getLogger(__name__)
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 class UsersController(BaseController):
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
14 """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
15 # 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
16 # file has a resource setup:
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
17 # map.resource('user', 'users')
50
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
18
47
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
19 def __before__(self):
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
20 c.staticurl = g.statics
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
21 c.admin_user = session.get('admin_user')
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
22 c.admin_username = session.get('admin_username')
49
3ada2f409c1c Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents: 48
diff changeset
23 self.sa = meta.Session
47
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
24
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
25 def index(self, format='html'):
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
26 """GET /users: All items in the collection"""
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
27 # url('users')
48
8e250e86a670 Css fixes, implemented removal of users, and display draft
Marcin Kuzminski <marcin@python-blog.com>
parents: 47
diff changeset
28
49
3ada2f409c1c Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents: 48
diff changeset
29 c.users_list = self.sa.query(Users).all()
48
8e250e86a670 Css fixes, implemented removal of users, and display draft
Marcin Kuzminski <marcin@python-blog.com>
parents: 47
diff changeset
30 return render('/users.html')
47
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
31
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
32 def create(self):
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
33 """POST /users: Create a new item"""
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
34 # url('users')
50
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
35 params = dict(request.params)
47
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
36
50
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
37 try:
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
38 new_user = Users()
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
39 new_user.active = params.get('active', False)
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
40 new_user.username = params.get('username')
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
41 new_user.password = crypt.crypt(params.get('password'), '6a')
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
42 new_user.admin = False
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
43 self.sa.add(new_user)
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
44 self.sa.commit()
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
45 except:
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
46 self.sa.rollback()
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
47 raise
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
48
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
49 return redirect(url('users'))
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
50
47
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
51 def new(self, format='html'):
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
52 """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
53 # url('new_user')
50
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
54 return render('/user_add.html')
47
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
55
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
56 def update(self, id):
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
57 """PUT /users/id: Update an existing item"""
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
58 # 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
59 # <input type="hidden" name="_method" value="PUT" />
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
60 # Or using helpers:
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
61 # h.form(url('user', id=ID),
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
62 # method='put')
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
63 # url('user', id=ID)
50
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
64 params = dict(request.params)
47
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
65
50
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
66 try:
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
67 new_user = self.sa.query(Users).get(id)
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
68 new_user.active = params.get('active')
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
69 new_user.username = params.get('username')
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
70 print params
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
71 if params.get('new_password'):
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
72 new_user.password = crypt.crypt(params.get('new_password'), '6a')
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
73 self.sa.add(new_user)
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
74 self.sa.commit()
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
75 except:
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
76 self.sa.rollback()
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
77 raise
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
78
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
79 return redirect(url('users'))
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
80
47
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
81 def delete(self, id):
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
82 """DELETE /users/id: Delete an existing item"""
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
83 # 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
84 # <input type="hidden" name="_method" value="DELETE" />
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
85 # Or using helpers:
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
86 # h.form(url('user', id=ID),
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
87 # method='delete')
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
88 # 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
89 try:
49
3ada2f409c1c Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents: 48
diff changeset
90 self.sa.delete(self.sa.query(Users).get(id))
3ada2f409c1c Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents: 48
diff changeset
91 self.sa.commit()
48
8e250e86a670 Css fixes, implemented removal of users, and display draft
Marcin Kuzminski <marcin@python-blog.com>
parents: 47
diff changeset
92 except:
49
3ada2f409c1c Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents: 48
diff changeset
93 self.sa.rollback()
48
8e250e86a670 Css fixes, implemented removal of users, and display draft
Marcin Kuzminski <marcin@python-blog.com>
parents: 47
diff changeset
94 raise
8e250e86a670 Css fixes, implemented removal of users, and display draft
Marcin Kuzminski <marcin@python-blog.com>
parents: 47
diff changeset
95 return redirect(url('users'))
8e250e86a670 Css fixes, implemented removal of users, and display draft
Marcin Kuzminski <marcin@python-blog.com>
parents: 47
diff changeset
96
47
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
97 def show(self, id, format='html'):
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
98 """GET /users/id: Show a specific item"""
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
99 # url('user', id=ID)
50
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
100
48
8e250e86a670 Css fixes, implemented removal of users, and display draft
Marcin Kuzminski <marcin@python-blog.com>
parents: 47
diff changeset
101
47
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
102 def edit(self, id, format='html'):
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
103 """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
104 # url('edit_user', id=ID)
50
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
105 c.user = self.sa.query(Users).get(id)
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
106
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
107 return htmlfill.render(
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
108 render('/user_edit.html'),
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
109 defaults=c.user.__dict__,
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
110 encoding="UTF-8",
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
111 force_defaults=False
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
112 )