def setup_validations(config): config.block_set_validation_repository(validation) from block.komet.pyramid.interfaces import ICreating, IUpdating from block.komet.utils import nameof class UniqueNameConflict(Exception): pass config.block_add_error_mapping({ UniqueNameConflict: "name: {} is conflict." }) @validation.config((ICreating, nameof(User)), "name", positionals=["session"], optionals=["id"]) @validation.config((IUpdating, nameof(User)), "name", positionals=["session"], optionals=["id"]) def unique_name(data, session, id=None): qs = session.query(User).filter_by(name=data["name"]) if id: qs = qs.filter(User.id != id) if qs.count() > 0: raise UniqueNameConflict(data["name"])
def route_name_fn(Model): return "{}.detail".format(nameof(Model).lower())
def pattern_fn(Model): return "/api/{}s/{{id}}".format(nameof(Model).lower())
def route_name_fn(Model): return "{}.index".format(nameof(Model).lower())