annotate pylons_app/controllers/users.py @ 48:8e250e86a670

Css fixes, implemented removal of users, and display draft
author Marcin Kuzminski <marcin@python-blog.com>
date Wed, 07 Apr 2010 21:10:43 +0200
parents f6ac79182600
children 3ada2f409c1c
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
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
7 from pylons_app.lib import auth
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
8 log = logging.getLogger(__name__)
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
9
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
10 class UsersController(BaseController):
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
11 """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
12 # 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
13 # file has a resource setup:
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
14 # map.resource('user', 'users')
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
15 def __before__(self):
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
16 c.staticurl = g.statics
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
17 c.admin_user = session.get('admin_user')
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
18 c.admin_username = session.get('admin_username')
48
8e250e86a670 Css fixes, implemented removal of users, and display draft
Marcin Kuzminski <marcin@python-blog.com>
parents: 47
diff changeset
19 self.conn, self.cur = auth.get_sqlite_conn_cur()
47
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
20
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
21 def index(self, format='html'):
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
22 """GET /users: All items in the collection"""
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
23 # url('users')
48
8e250e86a670 Css fixes, implemented removal of users, and display draft
Marcin Kuzminski <marcin@python-blog.com>
parents: 47
diff changeset
24
8e250e86a670 Css fixes, implemented removal of users, and display draft
Marcin Kuzminski <marcin@python-blog.com>
parents: 47
diff changeset
25 self.cur.execute('SELECT * FROM users')
8e250e86a670 Css fixes, implemented removal of users, and display draft
Marcin Kuzminski <marcin@python-blog.com>
parents: 47
diff changeset
26 c.users_list = self.cur.fetchall()
8e250e86a670 Css fixes, implemented removal of users, and display draft
Marcin Kuzminski <marcin@python-blog.com>
parents: 47
diff changeset
27 return render('/users.html')
47
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
28
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
29 def create(self):
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
30 """POST /users: Create a new item"""
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
31 # url('users')
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
32
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
33 def new(self, format='html'):
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
34 """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
35 # url('new_user')
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
36
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
37 def update(self, id):
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
38 """PUT /users/id: Update an existing item"""
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
39 # 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
40 # <input type="hidden" name="_method" value="PUT" />
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
41 # Or using helpers:
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
42 # h.form(url('user', id=ID),
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
43 # method='put')
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
44 # url('user', id=ID)
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
45
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
46 def delete(self, id):
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
47 """DELETE /users/id: Delete an existing item"""
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
48 # 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
49 # <input type="hidden" name="_method" value="DELETE" />
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
50 # Or using helpers:
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
51 # h.form(url('user', id=ID),
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
52 # method='delete')
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
53 # 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
54 try:
8e250e86a670 Css fixes, implemented removal of users, and display draft
Marcin Kuzminski <marcin@python-blog.com>
parents: 47
diff changeset
55 self.cur.execute("DELETE FROM users WHERE user_id=?", (id,))
8e250e86a670 Css fixes, implemented removal of users, and display draft
Marcin Kuzminski <marcin@python-blog.com>
parents: 47
diff changeset
56 self.conn.commit()
8e250e86a670 Css fixes, implemented removal of users, and display draft
Marcin Kuzminski <marcin@python-blog.com>
parents: 47
diff changeset
57 except:
8e250e86a670 Css fixes, implemented removal of users, and display draft
Marcin Kuzminski <marcin@python-blog.com>
parents: 47
diff changeset
58 self.conn.rollback()
8e250e86a670 Css fixes, implemented removal of users, and display draft
Marcin Kuzminski <marcin@python-blog.com>
parents: 47
diff changeset
59 raise
8e250e86a670 Css fixes, implemented removal of users, and display draft
Marcin Kuzminski <marcin@python-blog.com>
parents: 47
diff changeset
60 return redirect(url('users'))
8e250e86a670 Css fixes, implemented removal of users, and display draft
Marcin Kuzminski <marcin@python-blog.com>
parents: 47
diff changeset
61
47
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
62 def show(self, id, format='html'):
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
63 """GET /users/id: Show a specific item"""
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
64 # 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
65 self.cur.execute("SELECT * FROM users WHERE user_id=?", (id,))
8e250e86a670 Css fixes, implemented removal of users, and display draft
Marcin Kuzminski <marcin@python-blog.com>
parents: 47
diff changeset
66 ret = self.cur.fetchone()
8e250e86a670 Css fixes, implemented removal of users, and display draft
Marcin Kuzminski <marcin@python-blog.com>
parents: 47
diff changeset
67 c.user_name = ret[1]
8e250e86a670 Css fixes, implemented removal of users, and display draft
Marcin Kuzminski <marcin@python-blog.com>
parents: 47
diff changeset
68 return render('/users_show.html')
8e250e86a670 Css fixes, implemented removal of users, and display draft
Marcin Kuzminski <marcin@python-blog.com>
parents: 47
diff changeset
69
47
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
70 def edit(self, id, format='html'):
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
71 """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
72 # url('edit_user', id=ID)