Mercurial > public > src > rhodecode
annotate 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 |
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 |
47
f6ac79182600
Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
10 log = logging.getLogger(__name__) |
f6ac79182600
Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
11 |
f6ac79182600
Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
12 class UsersController(BaseController): |
f6ac79182600
Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
13 """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
|
14 # 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
|
15 # file has a resource setup: |
f6ac79182600
Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
16 # map.resource('user', 'users') |
f6ac79182600
Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
17 def __before__(self): |
f6ac79182600
Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
18 c.staticurl = g.statics |
f6ac79182600
Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
19 c.admin_user = session.get('admin_user') |
f6ac79182600
Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
20 c.admin_username = session.get('admin_username') |
49
3ada2f409c1c
Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
48
diff
changeset
|
21 self.sa = meta.Session |
47
f6ac79182600
Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
22 |
f6ac79182600
Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
23 def index(self, format='html'): |
f6ac79182600
Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
24 """GET /users: All items in the collection""" |
f6ac79182600
Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
25 # url('users') |
48
8e250e86a670
Css fixes, implemented removal of users, and display draft
Marcin Kuzminski <marcin@python-blog.com>
parents:
47
diff
changeset
|
26 |
49
3ada2f409c1c
Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
48
diff
changeset
|
27 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
|
28 return render('/users.html') |
47
f6ac79182600
Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
29 |
f6ac79182600
Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
30 def create(self): |
f6ac79182600
Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
31 """POST /users: Create a new item""" |
f6ac79182600
Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
32 # url('users') |
f6ac79182600
Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
33 |
f6ac79182600
Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
34 def new(self, format='html'): |
f6ac79182600
Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
35 """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
|
36 # url('new_user') |
f6ac79182600
Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
37 |
f6ac79182600
Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
38 def update(self, id): |
f6ac79182600
Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
39 """PUT /users/id: Update an existing item""" |
f6ac79182600
Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
40 # 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
|
41 # <input type="hidden" name="_method" value="PUT" /> |
f6ac79182600
Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
42 # Or using helpers: |
f6ac79182600
Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
43 # h.form(url('user', id=ID), |
f6ac79182600
Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
44 # method='put') |
f6ac79182600
Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
45 # url('user', id=ID) |
f6ac79182600
Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
46 |
f6ac79182600
Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
47 def delete(self, id): |
f6ac79182600
Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
48 """DELETE /users/id: Delete an existing item""" |
f6ac79182600
Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
49 # 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
|
50 # <input type="hidden" name="_method" value="DELETE" /> |
f6ac79182600
Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
51 # Or using helpers: |
f6ac79182600
Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
52 # h.form(url('user', id=ID), |
f6ac79182600
Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
53 # method='delete') |
f6ac79182600
Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
54 # 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
|
55 try: |
49
3ada2f409c1c
Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
48
diff
changeset
|
56 self.sa.delete(self.sa.query(Users).get(id)) |
3ada2f409c1c
Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
48
diff
changeset
|
57 self.sa.commit() |
48
8e250e86a670
Css fixes, implemented removal of users, and display draft
Marcin Kuzminski <marcin@python-blog.com>
parents:
47
diff
changeset
|
58 except: |
49
3ada2f409c1c
Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
48
diff
changeset
|
59 self.sa.rollback() |
48
8e250e86a670
Css fixes, implemented removal of users, and display draft
Marcin Kuzminski <marcin@python-blog.com>
parents:
47
diff
changeset
|
60 raise |
8e250e86a670
Css fixes, implemented removal of users, and display draft
Marcin Kuzminski <marcin@python-blog.com>
parents:
47
diff
changeset
|
61 return redirect(url('users')) |
8e250e86a670
Css fixes, implemented removal of users, and display draft
Marcin Kuzminski <marcin@python-blog.com>
parents:
47
diff
changeset
|
62 |
47
f6ac79182600
Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
63 def show(self, id, format='html'): |
f6ac79182600
Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
64 """GET /users/id: Show a specific item""" |
f6ac79182600
Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
65 # url('user', id=ID) |
49
3ada2f409c1c
Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
48
diff
changeset
|
66 c.user = self.sa.query(Users).get(id) |
3ada2f409c1c
Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
48
diff
changeset
|
67 |
3ada2f409c1c
Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
48
diff
changeset
|
68 return htmlfill.render( |
3ada2f409c1c
Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
48
diff
changeset
|
69 render('/users_show.html'), |
3ada2f409c1c
Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
48
diff
changeset
|
70 defaults=c.user.__dict__, |
3ada2f409c1c
Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
48
diff
changeset
|
71 encoding="UTF-8", |
3ada2f409c1c
Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
48
diff
changeset
|
72 force_defaults=False |
3ada2f409c1c
Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
48
diff
changeset
|
73 ) |
48
8e250e86a670
Css fixes, implemented removal of users, and display draft
Marcin Kuzminski <marcin@python-blog.com>
parents:
47
diff
changeset
|
74 |
47
f6ac79182600
Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
75 def edit(self, id, format='html'): |
f6ac79182600
Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
76 """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
|
77 # url('edit_user', id=ID) |