Add validation for unique name when creating new Setting

This commit is contained in:
Lance Edgar 2017-05-19 10:42:05 -05:00
parent 242bcd7603
commit 5cb3f15616

View file

@ -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))