4811aa04a6f95e4817b010b9a832c2597a668c25
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_to
8 from routes
import request_config
9 from sqlalchemy
.orm
.exc
import NoResultFound
11 from spline
import model
12 from spline
.model
import meta
13 from spline
.lib
import helpers
as h
14 from spline
.lib
.base
import BaseController
, render
16 log
= logging
.getLogger(__name__
)
19 class ProfileEditForm(Form
):
20 name
= fields
.TextField(u
'Display name', [validators
.Required()])
22 def validate_name(form
, field
):
23 if not 1 < len(field
.data
) <= 20:
24 raise ValidationError("Name can't be longer than 20 characters")
26 any_real_characters
= False
27 for char
in field
.data
:
28 cat
= unicodedata
.category(char
)
30 # Non-spacing marks and spaces don't count as letters
31 if cat
not in ('Mn', 'Zs'):
32 any_real_characters
= True
34 # Disallow control characters, format characters, non-assigned,
35 # private use, surrogates, spacing-combining marks (for Arabic,
36 # etc), enclosing marks (millions sign), line-spacing,
38 # This also, thankfully, includes the RTL characters.
39 if cat
in ('Cc', 'Cf', 'Cn', 'Co', 'Cs', 'Mc', 'Me', 'Zl', 'Zp'):
40 raise ValidationError("Please don't play stupid Unicode tricks")
42 class UsersController(BaseController
):
45 # Return a rendered template
46 # return render('/template.mako')
47 # or, Return a response
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(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(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 if request
.method
!= 'POST' or not c
.form
.validate():
78 return render('/users/profile_edit.mako')
81 c
.page_user
.name
= c
.form
.name
.data
83 meta
.Session
.add(c
.page_user
)
86 h
.flash('Saved your profile.', icon
='tick')
88 redirect_to(controller
='users', action
='profile',
89 id=c
.page_user
.id, name
=c
.page_user
.name
,