Add unique check for "name" when creating new Role

This commit is contained in:
Lance Edgar 2018-09-19 10:00:59 -05:00
parent 84db66a60c
commit c5fef6b954

View file

@ -2,7 +2,7 @@
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2017 Lance Edgar
# Copyright © 2010-2018 Lance Edgar
#
# This file is part of Rattail.
#
@ -65,10 +65,22 @@ class RolesView(PrincipalMasterView):
g.set_sort_defaults('name')
g.set_link('name')
def unique_name(self, node, value):
query = self.Session.query(model.Role)\
.filter(model.Role.name == value)
if self.editing:
role = self.get_instance()
query = query.filter(model.Role.uuid != role.uuid)
if query.count():
raise colander.Invalid(node, "Name must be unique")
def configure_form(self, f):
super(RolesView, self).configure_form(f)
role = f.model_instance
# name
f.set_validator('name', self.unique_name)
# permissions
self.tailbone_permissions = self.request.registry.settings.get('tailbone_permissions', {})
f.set_renderer('permissions', PermissionsRenderer(permissions=self.tailbone_permissions))