From 5cb3f15616c2d1ece821bd597fd1079e95d40ab6 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Fri, 19 May 2017 10:42:05 -0500 Subject: [PATCH] Add validation for unique name when creating new Setting --- tailbone/views/settings.py | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/tailbone/views/settings.py b/tailbone/views/settings.py index f971d6dc..a20bb7d3 100644 --- a/tailbone/views/settings.py +++ b/tailbone/views/settings.py @@ -30,9 +30,18 @@ import re from rattail.db import model +import formalchemy as fa + +from tailbone.db import Session from tailbone.views import MasterView +def unique_name(value, field): + setting = Session.query(model.Setting).get(value) + if setting: + raise fa.ValidationError("Setting name must be unique") + + class SettingsView(MasterView): """ Master view for the settings model. @@ -51,15 +60,17 @@ class SettingsView(MasterView): ], readonly=True) - def configure_fieldset(self, fs): - fs.configure( - include=[ - fs.name, - fs.value, - ]) + def _preconfigure_fieldset(self, fs): + fs.name.set(validate=unique_name) if self.editing: fs.name.set(readonly=True) + def configure_fieldset(self, fs): + fs.configure(include=[ + fs.name, + fs.value, + ]) + def editable_instance(self, setting): if self.rattail_config.demo(): return not bool(self.feedback.match(setting.name))