annotate pylons_app/controllers/users.py @ 245:a83a1799480c

Reimplemented way of caching repos list, hg model now get's repos objects right from cached dict, this way we skipp creating instances of MercurialRepository and gain performance. Some import cleanup
author Marcin Kuzminski <marcin@python-works.com>
date Thu, 03 Jun 2010 00:04:48 +0200
parents b18f89d6d17f
children 3782a6d698af
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
245
a83a1799480c Reimplemented way of caching repos list, hg model now get's repos objects right from cached dict, this way we skipp creating instances of MercurialRepository and gain performance. Some import cleanup
Marcin Kuzminski <marcin@python-works.com>
parents: 239
diff changeset
2 from pylons import request, session, tmpl_context as c, url
a83a1799480c Reimplemented way of caching repos list, hg model now get's repos objects right from cached dict, this way we skipp creating instances of MercurialRepository and gain performance. Some import cleanup
Marcin Kuzminski <marcin@python-works.com>
parents: 239
diff changeset
3 from pylons.controllers.util import abort, redirect
238
a55c17874486 Rewrite of user managment, improved forms, added some user info
Marcin Kuzminski <marcin@python-works.com>
parents: 235
diff changeset
4 from pylons.i18n.translation import _
245
a83a1799480c Reimplemented way of caching repos list, hg model now get's repos objects right from cached dict, this way we skipp creating instances of MercurialRepository and gain performance. Some import cleanup
Marcin Kuzminski <marcin@python-works.com>
parents: 239
diff changeset
5 from pylons_app.lib import helpers as h
239
b18f89d6d17f Adde draft for permissions systems, made all needed decorators, and checks. For future usage in the system.
Marcin Kuzminski <marcin@python-works.com>
parents: 238
diff changeset
6 from pylons_app.lib.auth import LoginRequired, CheckPermissionAll
235
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
7 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
8 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
9 from pylons_app.model.forms import UserForm
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
10 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
11 import formencode
47
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
12 import logging
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
13
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
14 log = logging.getLogger(__name__)
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
15
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
16 class UsersController(BaseController):
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
17 """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
18 # 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
19 # file has a resource setup:
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
20 # map.resource('user', 'users')
235
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
21 @LoginRequired()
47
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
22 def __before__(self):
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
23 c.admin_user = session.get('admin_user')
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
24 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
25 super(UsersController, self).__before__()
239
b18f89d6d17f Adde draft for permissions systems, made all needed decorators, and checks. For future usage in the system.
Marcin Kuzminski <marcin@python-works.com>
parents: 238
diff changeset
26
b18f89d6d17f Adde draft for permissions systems, made all needed decorators, and checks. For future usage in the system.
Marcin Kuzminski <marcin@python-works.com>
parents: 238
diff changeset
27
47
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()
238
a55c17874486 Rewrite of user managment, improved forms, added some user info
Marcin Kuzminski <marcin@python-works.com>
parents: 235
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)
238
a55c17874486 Rewrite of user managment, improved forms, added some user info
Marcin Kuzminski <marcin@python-works.com>
parents: 235
diff changeset
44 h.flash(_('created user %s') % form_result['username'], category='success')
235
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
45 return redirect(url('users'))
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
46
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
47 except formencode.Invalid as errors:
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
48 c.form_errors = errors.error_dict
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
49 return htmlfill.render(
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
50 render('admin/users/user_add.html'),
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
51 defaults=errors.value,
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
52 encoding="UTF-8")
50
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
53
47
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
54 def new(self, format='html'):
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
55 """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
56 # 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
57 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
58
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
59 def update(self, id):
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
60 """PUT /users/id: Update an existing item"""
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
61 # 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
62 # <input type="hidden" name="_method" value="PUT" />
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
63 # Or using helpers:
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
64 # h.form(url('user', id=ID),
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
65 # method='put')
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
66 # url('user', id=ID)
235
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
67 user_model = UserModel()
238
a55c17874486 Rewrite of user managment, improved forms, added some user info
Marcin Kuzminski <marcin@python-works.com>
parents: 235
diff changeset
68 login_form = UserForm(edit=True)()
50
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
69 try:
235
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
70 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
71 user_model.update(id, form_result)
238
a55c17874486 Rewrite of user managment, improved forms, added some user info
Marcin Kuzminski <marcin@python-works.com>
parents: 235
diff changeset
72 h.flash(_('User updated succesfully'), category='success')
235
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
73 return redirect(url('users'))
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
74
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
75 except formencode.Invalid as errors:
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
76 c.user = user_model.get_user(id)
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
77 c.form_errors = errors.error_dict
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
78 return htmlfill.render(
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
79 render('admin/users/user_edit.html'),
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
80 defaults=errors.value,
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 234
diff changeset
81 encoding="UTF-8")
50
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
82
47
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
83 def delete(self, id):
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
84 """DELETE /users/id: Delete an existing item"""
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
85 # 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
86 # <input type="hidden" name="_method" value="DELETE" />
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
87 # Or using helpers:
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
88 # h.form(url('user', id=ID),
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
89 # method='delete')
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
90 # 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
91 try:
234
a0116e944da1 changed naming convention for db modules.
Marcin Kuzminski <marcin@python-works.com>
parents: 191
diff changeset
92 self.sa.delete(self.sa.query(User).get(id))
49
3ada2f409c1c Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents: 48
diff changeset
93 self.sa.commit()
238
a55c17874486 Rewrite of user managment, improved forms, added some user info
Marcin Kuzminski <marcin@python-works.com>
parents: 235
diff changeset
94 h.flash(_('sucessfully deleted user'), category='success')
48
8e250e86a670 Css fixes, implemented removal of users, and display draft
Marcin Kuzminski <marcin@python-blog.com>
parents: 47
diff changeset
95 except:
49
3ada2f409c1c Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents: 48
diff changeset
96 self.sa.rollback()
48
8e250e86a670 Css fixes, implemented removal of users, and display draft
Marcin Kuzminski <marcin@python-blog.com>
parents: 47
diff changeset
97 raise
8e250e86a670 Css fixes, implemented removal of users, and display draft
Marcin Kuzminski <marcin@python-blog.com>
parents: 47
diff changeset
98 return redirect(url('users'))
8e250e86a670 Css fixes, implemented removal of users, and display draft
Marcin Kuzminski <marcin@python-blog.com>
parents: 47
diff changeset
99
47
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
100 def show(self, id, format='html'):
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
101 """GET /users/id: Show a specific item"""
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
102 # url('user', id=ID)
50
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
103
48
8e250e86a670 Css fixes, implemented removal of users, and display draft
Marcin Kuzminski <marcin@python-blog.com>
parents: 47
diff changeset
104
47
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
105 def edit(self, id, format='html'):
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
106 """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
107 # url('edit_user', id=ID)
234
a0116e944da1 changed naming convention for db modules.
Marcin Kuzminski <marcin@python-works.com>
parents: 191
diff changeset
108 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
109 defaults = c.user.__dict__
50
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
110 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
111 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
112 defaults=defaults,
50
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
113 encoding="UTF-8",
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
114 force_defaults=False
73f413946c14 user managment implementation continued update/delete/create works
Marcin Kuzminski <marcin@python-blog.com>
parents: 49
diff changeset
115 )