Validate for unique tempmon probe config key

This commit is contained in:
Lance Edgar 2016-12-10 11:23:22 -06:00
parent 8578a063f3
commit 16f00b02df

View file

@ -28,10 +28,23 @@ from __future__ import unicode_literals, absolute_import
from rattail_tempmon.db import model as tempmon
import formalchemy as fa
from tailbone import forms
from tailbone.db import TempmonSession
from tailbone.views.tempmon import MasterView, ClientFieldRenderer
def unique_config_key(value, field):
probe = field.parent.model
query = TempmonSession.query(tempmon.Probe)\
.filter(tempmon.Probe.config_key == value)
if probe.uuid:
query = query.filter(tempmon.Probe.uuid != probe.uuid)
if query.count():
raise fa.ValidationError("Config key must be unique")
class TempmonProbeView(MasterView):
"""
Master view for tempmon probes.
@ -63,6 +76,7 @@ class TempmonProbeView(MasterView):
readonly=True)
def _preconfigure_fieldset(self, fs):
fs.config_key.set(validate=unique_config_key)
fs.client.set(label="TempMon Client", renderer=ClientFieldRenderer)
fs.appliance_type.set(renderer=forms.renderers.EnumFieldRenderer(self.enum.TEMPMON_APPLIANCE_TYPE))
fs.status.set(renderer=forms.renderers.EnumFieldRenderer(self.enum.TEMPMON_PROBE_STATUS))