X-Git-Url: http://git.veekun.com/zzz-floof.git/blobdiff_plain/c03a740f65ea24ba6567b0ef75f3785faae37d42..ea3d7fcf702e372eac978d61b9af9dbad46543bb:/floof/forms/validators/unique.py diff --git a/floof/forms/validators/unique.py b/floof/forms/validators/unique.py index 4fb3c8f..9b94869 100644 --- a/floof/forms/validators/unique.py +++ b/floof/forms/validators/unique.py @@ -1,5 +1,5 @@ from formencode import * -from formencode import validators +from formencode import validators import pylons _ = validators._ # dummy translation string @@ -14,29 +14,29 @@ class FilteringSchema(Schema): # Model-based validators class Unique(validators.FancyValidator): - + """ Checks if given value is unique to the model.Will check the state: if state object is the same as the instance, or the state contains a property with the same name as the context name. For example: - + validator = validators.Unique(model.NewsItem, "title", context_name="news_item") - + This will check if there is an existing instance with the same "title". If there is a matching instance, will check if the state passed into the validator is the same instance, or if the state contains a property "news_item" which is the same instance. """ - + __unpackargs__ = ('model', 'attr', "model_name", "context_name", "attribute_name") messages = { 'notUnique' : _("%(modelName)s already exists with this %(attrName)s"), } - + model_name = "Item" attribute_name = None context_name = None - + def validate_python(self, value, state): instance = self.model.get_by(**{self.attr : value}) if instance: @@ -44,9 +44,9 @@ class Unique(validators.FancyValidator): if state != instance and \ getattr(state, context_name, None) != instance: attr_name = self.attribute_name or self.attr - raise Invalid(self.message('notUnique', state, + raise Invalid(self.message('notUnique', state, modelName=self.model_name, - attrName=attr_name), + attrName=attr_name), value, state) - + validators.Unique = Unique