Commit 28fc2606 authored by Yakubov, Sergey's avatar Yakubov, Sergey
Browse files

Merge branch '7-improve-model-update-for-pydantic' into 'main'

Improve model update for pydantic objects

Closes #7

See merge request ndip/public-packages/py-mvvm!7
parents e31e330f fb527be2
Loading
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -98,6 +98,8 @@ docs-publish:
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
      when: always
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
      when: never
    - when: manual
  script:
    - |
+1 −1
Original line number Diff line number Diff line
@@ -20,7 +20,7 @@ repos:
  hooks:
    - id: mypy
      name: mypy
      entry: poetry run mypy src
      entry: poetry run mypy .
      language: system
      types: [python]
      verbose: true
+1 −1
Original line number Diff line number Diff line
[tool.poetry]
name = "mvvm-lib"
version = "0.4.0"
version = "0.4.1"
description = "A Python Package for Model-View-ViewModel pattern"
authors = ["Yakubov, Sergey <yakubovs@ornl.gov>"]
readme = "README.md"
+2 −2
Original line number Diff line number Diff line
@@ -10,7 +10,7 @@ from ..utils import rsetattr
try:
    from PyQt6.QtCore import QObject, pyqtSignal
except Exception:
    print("PyQt6 is missing. You could install 'py-mvvm[pyqt6]' to fix it")
    print("PyQt6 is missing. You should install 'mvvm-lib[pyqt6]' to fix it")
    exit(1)

import inspect
@@ -39,7 +39,7 @@ class Communicator(QObject):
        self.callback_after_update = callback_after_update

    def _update_viewmodel_callback(self, key: Optional[str] = None, value: Any = None) -> None:
        if issubclass(type(value), BaseModel):
        if issubclass(type(self.viewmodel_linked_object), BaseModel):
            model = self.viewmodel_linked_object.copy(deep=True)
            rsetattr(model, key or "", value)
            try:
+8 −5
Original line number Diff line number Diff line
@@ -178,22 +178,25 @@ class StateConnection:

                @self.state.change(state_variable_name)
                def update_viewmodel_callback(**kwargs: dict) -> None:
                    success = True
                    updated = True
                    if self.viewmodel_linked_object and issubclass(type(self.viewmodel_linked_object), BaseModel):
                        json_str = json.dumps(kwargs[state_variable_name])
                        try:
                            model = self.viewmodel_linked_object.model_validate_json(json_str)
                            if model != self.viewmodel_linked_object:
                                for field, value in model:
                                    setattr(self.viewmodel_linked_object, field, value)
                            else:
                                updated = False
                        except Exception:
                            success = False
                            updated = False
                    elif isinstance(self.viewmodel_linked_object, dict):
                        self.viewmodel_linked_object.update(kwargs[state_variable_name])
                    elif is_callable(self.viewmodel_linked_object):
                        cast(Callable, self.viewmodel_linked_object)(kwargs[state_variable_name])
                    else:
                        raise Exception("cannot update", self.viewmodel_linked_object)
                    if self.viewmodel_callback_after_update and success:
                    if self.viewmodel_callback_after_update and updated:
                        self.viewmodel_callback_after_update(state_variable_name)

    def update_in_view(self, value: Any) -> None: