1 from formencode
import *
2 from formencode
import validators
5 _
= validators
._
# dummy translation string
9 class FilteringSchema(Schema
):
10 "Schema with extra fields filtered by default"
11 filter_extra_fields
= True
12 allow_extra_fields
= True
14 # Model-based validators
16 class Unique(validators
.FancyValidator
):
19 Checks if given value is unique to the model.Will check the state: if state object
20 is the same as the instance, or the state contains a property with the same name
21 as the context name. For example:
23 validator = validators.Unique(model.NewsItem, "title", context_name="news_item")
25 This will check if there is an existing instance with the same "title". If there
26 is a matching instance, will check if the state passed into the validator is the
27 same instance, or if the state contains a property "news_item" which is the same
31 __unpackargs__
= ('model', 'attr', "model_name", "context_name", "attribute_name")
33 'notUnique' : _("%(modelName)s already exists with this %(attrName)s"),
40 def validate_python(self
, value
, state
):
41 instance
= self
.model
.get_by(**{self
.attr
: value
})
43 context_name
= self
.context_name
or self
.model
.__name__
.lower()
44 if state
!= instance
and \
45 getattr(state
, context_name
, None) != instance
:
46 attr_name
= self
.attribute_name
or self
.attr
47 raise Invalid(self
.message('notUnique', state
,
48 modelName
=self
.model_name
,
52 validators
.Unique
= Unique