4 from wtforms
import Form
, ValidationError
, fields
, validators
, widgets
6 from pylons
import config
, request
, response
, session
, tmpl_context
as c
, url
7 from pylons
.controllers
.util
import abort
, redirect
8 from pylons
.decorators
.secure
import authenticate_form
9 from routes
import request_config
10 from sqlalchemy
.orm
.exc
import NoResultFound
12 from spline
.model
import meta
13 from spline
.lib
import helpers
as h
14 from spline
.lib
.base
import BaseController
, render
15 from splinext
.users
import model
as users_model
17 log
= logging
.getLogger(__name__
)
20 class ProfileEditForm(Form
):
21 name
= fields
.TextField(u
'Display name', [validators
.Required()])
23 def validate_name(form
, field
):
24 if not 1 < len(field
.data
) <= 20:
25 raise ValidationError("Name can't be longer than 20 characters")
27 any_real_characters
= False
28 for char
in field
.data
:
29 cat
= unicodedata
.category(char
)
31 # Non-spacing marks and spaces don't count as letters
32 if cat
not in ('Mn', 'Zs'):
33 any_real_characters
= True
35 # Disallow control characters, format characters, non-assigned,
36 # private use, surrogates, spacing-combining marks (for Arabic,
37 # etc), enclosing marks (millions sign), line-spacing,
39 # This also, thankfully, includes the RTL characters.
40 if cat
in ('Cc', 'Cf', 'Cn', 'Co', 'Cs', 'Mc', 'Me', 'Zl', 'Zp'):
41 raise ValidationError("Please don't play stupid Unicode tricks")
43 class UsersController(BaseController
):
46 c
.users
= meta
.Session
.query(users_model
.User
) \
47 .order_by(users_model
.User
.id.asc())
48 return render('/users/list.mako')
50 def profile(self
, id, name
=None):
53 URL is /users/id:name, where 'name' only exists for readability and is
54 entirely optional and ignored.
57 c
.page_user
= meta
.Session
.query(users_model
.User
).get(id)
61 return render('/users/profile.mako')
63 def profile_edit(self
, id, name
=None):
64 """Main user profile editing."""
65 c
.page_user
= meta
.Session
.query(users_model
.User
).get(id)
69 # XXX could use some real permissions
70 if c
.page_user
!= c
.user
:
73 c
.form
= ProfileEditForm(request
.params
,
74 name
=c
.page_user
.name
,
77 return render('/users/profile_edit.mako')
80 def profile_edit_commit(self
, id, name
=None):
81 """Save profile changes."""
82 c
.page_user
= meta
.Session
.query(users_model
.User
).get(id)
86 # XXX could use some real permissions
87 if c
.page_user
!= c
.user
:
90 c
.form
= ProfileEditForm(request
.params
,
91 name
=c
.page_user
.name
,
94 if not c
.form
.validate():
95 return render('/users/profile_edit.mako')
98 c
.page_user
.name
= c
.form
.name
.data
100 meta
.Session
.add(c
.page_user
)
101 meta
.Session
.commit()
103 h
.flash('Saved your profile.', icon
='tick')
106 url(controller
='users', action
='profile',
107 id=c
.page_user
.id, name
=c
.page_user
.name
),