1 """FormEncode validators."""
5 from floof
.model
.users
import User
, UserRelationshipTypes
7 class UniqueExistingRow(formencode
.FancyValidator
):
8 """Given a column object, converts a unique value from that column into the
9 corresponding row object.
11 def __init__(self
, table
, column
):
14 super(formencode
.FancyValidator
, self
).__init__()
16 def _to_python(self
, value
, state
):
18 row
= self
.table
.query
.filter(self
.column
== value
).one()
19 except BaseException
, e
:
20 raise formencode
.Invalid(
28 class UserRelationshipToggleForm(formencode
.Schema
):
29 target_user
= UniqueExistingRow(User
, User
.id)
30 type = formencode
.compound
.Pipe(
31 formencode
.validators
.Int(),
32 formencode
.validators
.OneOf(
33 [v
for (k
, v
) in UserRelationshipTypes
.__dict__
.items()
37 add_remove
= formencode
.validators
.OneOf([u
'add', u
'remove'])