comparison pylons_app/controllers/users.py @ 49:3ada2f409c1c

Added sqlalchemy support made models for database changed views to handle sqlalchemy
author Marcin Kuzminski <marcin@python-blog.com>
date Thu, 08 Apr 2010 01:50:46 +0200
parents 8e250e86a670
children 73f413946c14
comparison
equal deleted inserted replaced
48:8e250e86a670 49:3ada2f409c1c
2 2
3 from pylons import request, response, session, tmpl_context as c, url, app_globals as g 3 from pylons import request, response, session, tmpl_context as c, url, app_globals as g
4 from pylons.controllers.util import abort, redirect 4 from pylons.controllers.util import abort, redirect
5 5
6 from pylons_app.lib.base import BaseController, render 6 from pylons_app.lib.base import BaseController, render
7 from pylons_app.lib import auth 7 from formencode import htmlfill
8 from pylons_app.model import meta
9 from pylons_app.model.db import Users, UserLogs
8 log = logging.getLogger(__name__) 10 log = logging.getLogger(__name__)
9 11
10 class UsersController(BaseController): 12 class UsersController(BaseController):
11 """REST Controller styled on the Atom Publishing Protocol""" 13 """REST Controller styled on the Atom Publishing Protocol"""
12 # To properly map this controller, ensure your config/routing.py 14 # To properly map this controller, ensure your config/routing.py
14 # map.resource('user', 'users') 16 # map.resource('user', 'users')
15 def __before__(self): 17 def __before__(self):
16 c.staticurl = g.statics 18 c.staticurl = g.statics
17 c.admin_user = session.get('admin_user') 19 c.admin_user = session.get('admin_user')
18 c.admin_username = session.get('admin_username') 20 c.admin_username = session.get('admin_username')
19 self.conn, self.cur = auth.get_sqlite_conn_cur() 21 self.sa = meta.Session
20 22
21 def index(self, format='html'): 23 def index(self, format='html'):
22 """GET /users: All items in the collection""" 24 """GET /users: All items in the collection"""
23 # url('users') 25 # url('users')
24 26
25 self.cur.execute('SELECT * FROM users') 27 c.users_list = self.sa.query(Users).all()
26 c.users_list = self.cur.fetchall()
27 return render('/users.html') 28 return render('/users.html')
28 29
29 def create(self): 30 def create(self):
30 """POST /users: Create a new item""" 31 """POST /users: Create a new item"""
31 # url('users') 32 # url('users')
50 # Or using helpers: 51 # Or using helpers:
51 # h.form(url('user', id=ID), 52 # h.form(url('user', id=ID),
52 # method='delete') 53 # method='delete')
53 # url('user', id=ID) 54 # url('user', id=ID)
54 try: 55 try:
55 self.cur.execute("DELETE FROM users WHERE user_id=?", (id,)) 56 self.sa.delete(self.sa.query(Users).get(id))
56 self.conn.commit() 57 self.sa.commit()
57 except: 58 except:
58 self.conn.rollback() 59 self.sa.rollback()
59 raise 60 raise
60 return redirect(url('users')) 61 return redirect(url('users'))
61 62
62 def show(self, id, format='html'): 63 def show(self, id, format='html'):
63 """GET /users/id: Show a specific item""" 64 """GET /users/id: Show a specific item"""
64 # url('user', id=ID) 65 # url('user', id=ID)
65 self.cur.execute("SELECT * FROM users WHERE user_id=?", (id,)) 66 c.user = self.sa.query(Users).get(id)
66 ret = self.cur.fetchone() 67
67 c.user_name = ret[1] 68 return htmlfill.render(
68 return render('/users_show.html') 69 render('/users_show.html'),
70 defaults=c.user.__dict__,
71 encoding="UTF-8",
72 force_defaults=False
73 )
69 74
70 def edit(self, id, format='html'): 75 def edit(self, id, format='html'):
71 """GET /users/id/edit: Form to edit an existing item""" 76 """GET /users/id/edit: Form to edit an existing item"""
72 # url('edit_user', id=ID) 77 # url('edit_user', id=ID)