mirror of
https://github.com/bec-project/bec_widgets.git
synced 2025-07-13 19:21:50 +02:00
feat(#495): add validation against existing device names
This commit is contained in:
@ -5,6 +5,7 @@ from bec_lib.atlas_models import Device as DeviceConfigModel
|
|||||||
from bec_lib.config_helper import CONF as DEVICE_CONF_KEYS
|
from bec_lib.config_helper import CONF as DEVICE_CONF_KEYS
|
||||||
from bec_lib.config_helper import ConfigHelper
|
from bec_lib.config_helper import ConfigHelper
|
||||||
from bec_lib.logger import bec_logger
|
from bec_lib.logger import bec_logger
|
||||||
|
from pydantic import ValidationError, field_validator
|
||||||
from qtpy.QtCore import QSize, Qt, QThreadPool, Signal
|
from qtpy.QtCore import QSize, Qt, QThreadPool, Signal
|
||||||
from qtpy.QtWidgets import (
|
from qtpy.QtWidgets import (
|
||||||
QApplication,
|
QApplication,
|
||||||
@ -77,6 +78,7 @@ class DeviceConfigDialog(BECWidget, QDialog):
|
|||||||
if self._action == "update":
|
if self._action == "update":
|
||||||
self._form._validity.setVisible(False)
|
self._form._validity.setVisible(False)
|
||||||
else:
|
else:
|
||||||
|
self._set_schema_to_check_devices()
|
||||||
self._form._validity.setVisible(True)
|
self._form._validity.setVisible(True)
|
||||||
self._form.validity_proc.connect(self.enable_buttons_for_validity)
|
self._form.validity_proc.connect(self.enable_buttons_for_validity)
|
||||||
self._add_overlay()
|
self._add_overlay()
|
||||||
@ -86,6 +88,17 @@ class DeviceConfigDialog(BECWidget, QDialog):
|
|||||||
self._form.validate_form()
|
self._form.validate_form()
|
||||||
self._overlay_widget.setVisible(False)
|
self._overlay_widget.setVisible(False)
|
||||||
|
|
||||||
|
def _set_schema_to_check_devices(self):
|
||||||
|
class _NameValidatedConfigModel(DeviceConfigModel):
|
||||||
|
@field_validator("name")
|
||||||
|
@staticmethod
|
||||||
|
def _validate_name(value: str, *_):
|
||||||
|
if value in self.dev:
|
||||||
|
raise ValueError(f"A device with name {value} already exists!")
|
||||||
|
return value
|
||||||
|
|
||||||
|
self._form.set_schema(_NameValidatedConfigModel)
|
||||||
|
|
||||||
def _add_form(self):
|
def _add_form(self):
|
||||||
self._form_widget = QWidget()
|
self._form_widget = QWidget()
|
||||||
self._form_widget.setLayout(self._layout)
|
self._form_widget.setLayout(self._layout)
|
||||||
|
@ -55,7 +55,9 @@ class DeviceConfigForm(PydanticModelForm):
|
|||||||
qapp.theme_signal.theme_updated.connect(self.set_pretty_display_theme) # type: ignore
|
qapp.theme_signal.theme_updated.connect(self.set_pretty_display_theme) # type: ignore
|
||||||
|
|
||||||
def set_schema(self, schema: type[BaseModel]):
|
def set_schema(self, schema: type[BaseModel]):
|
||||||
raise TypeError("This class doesn't support changing the schema")
|
if not issubclass(schema, DeviceConfigModel):
|
||||||
|
raise TypeError("This class doesn't support changing the schema")
|
||||||
|
super().set_schema(schema)
|
||||||
|
|
||||||
def set_data(self, data: DeviceConfigModel): # type: ignore # This class locks the type
|
def set_data(self, data: DeviceConfigModel): # type: ignore # This class locks the type
|
||||||
super().set_data(data)
|
super().set_data(data)
|
||||||
|
Reference in New Issue
Block a user