From b5d4a07ab2ba8aa2efc7938f1f47f3cdb5c45640 Mon Sep 17 00:00:00 2001 From: neo Date: Tue, 17 Sep 2024 15:19:36 +0200 Subject: [PATCH] remove steps table wip --- src/lib/db/__init__.py | 2 +- src/lib/db/models/recipes.py | 24 ++-------- src/lib/db/models/steps.py | 23 --------- src/ui/crud/crud.py | 5 -- src/ui/recipe_selection/recipe_selection.py | 10 +--- .../recipe_spec_and_step_editor.py | 47 +++++-------------- 6 files changed, 17 insertions(+), 94 deletions(-) delete mode 100644 src/lib/db/models/steps.py diff --git a/src/lib/db/__init__.py b/src/lib/db/__init__.py index fb0510d..d96896e 100644 --- a/src/lib/db/__init__.py +++ b/src/lib/db/__init__.py @@ -6,7 +6,7 @@ import re from playhouse.sqlite_ext import JSONField -from .models import Archive, Log, Recipes, Session, Steps, Users, db +from .models import Archive, Log, Recipes, Session, Users, db models_reference = { "archive": Archive, diff --git a/src/lib/db/models/recipes.py b/src/lib/db/models/recipes.py index 14c7ccc..5279d4e 100644 --- a/src/lib/db/models/recipes.py +++ b/src/lib/db/models/recipes.py @@ -4,46 +4,28 @@ from peewee import BooleanField, TextField from playhouse.sqlite_ext import JSONField from .base_model import BaseModel -from .steps import Steps class Recipes(BaseModel): name = TextField(primary_key=True, unique=True, null=False, default=lambda: uuid().hex) client = TextField(null=True) part_number = TextField(null=False) - spec = JSONField(null=False) # keys inside spec must not overlap withthe model + spec = JSONField(null=False) description = TextField(null=True) archived = BooleanField(null=False, default=False) def get_steps(self): - steps = [] - for step_pk in self.spec.get("steps", []): - steps.append(Steps.get_by_id(step_pk)) - return steps - - def delete_associated_steps(self): - return Steps.delete().where(Steps._meta.primary_key << list(self.spec.get("available_steps", {}).values())).execute() + return self.spec def get_steps_map(self): - steps = {} - for step_name, step_pk in self.spec.get("available_steps", {}).items(): - steps[step_name] = Steps.get_by_id(step_pk) - return steps + return self.spec @classmethod def crud_delete(cls, deleted_rows): - # return cls.delete().where(self._meta.primary_key << deleted_rows).execute() recipes = list(cls.select().where(cls._meta.primary_key << deleted_rows).execute()) for recipe in recipes: - recipe.delete_associated_steps() recipe.delete_instance() return len(recipes) - # @classmethod - # def delete(cls, *args, **kwargs): - # # OVERRIDE DELETION - # # so that deleting a user will only archive it - # return cls.update(archived=True) - class Meta: table_name = "recipes" diff --git a/src/lib/db/models/steps.py b/src/lib/db/models/steps.py deleted file mode 100644 index 4baa839..0000000 --- a/src/lib/db/models/steps.py +++ /dev/null @@ -1,23 +0,0 @@ -from uuid import uuid4 as uuid - -from peewee import BooleanField, TextField -from playhouse.sqlite_ext import JSONField - -from .base_model import BaseModel - - -class Steps(BaseModel): - name = TextField(primary_key=True, unique=True, null=False, default=lambda: uuid().hex) - type = TextField(null=False) - spec = JSONField(null=False) # keys inside spec must not overlap withthe model - description = TextField(null=True) - archived = BooleanField(null=False, default=False) - - # @classmethod - # def delete(cls, *args, **kwargs): - # # OVERRIDE DELETION - # # so that deleting a user will only archive it - # return cls.update(archived=True) - - class Meta: - table_name = "steps" diff --git a/src/ui/crud/crud.py b/src/ui/crud/crud.py index 6eb3e3d..7f0d111 100755 --- a/src/ui/crud/crud.py +++ b/src/ui/crud/crud.py @@ -221,11 +221,6 @@ class Json_External_Dialog_Editor_Cell_Widget(QPushButton, Cell): def render(self, data, field_name=None, row_number=None, crud=None): self.editor.render(data, field_name=field_name, row_number=row_number, crud=crud) - # def _parse(self, action=None, row_number=None, crud=None): - # if action != "commit" and self.connected_modified and not self.is_modified: - # return self.value - # return self.editor.parse(action=action, row_number=row_number, crud=crud) - def parse(self, action=None, row_number=None, crud=None): return self.editor.parse(row_number=row_number, crud=crud) diff --git a/src/ui/recipe_selection/recipe_selection.py b/src/ui/recipe_selection/recipe_selection.py index 9c7b2e1..766eaf8 100755 --- a/src/ui/recipe_selection/recipe_selection.py +++ b/src/ui/recipe_selection/recipe_selection.py @@ -5,7 +5,7 @@ import sys import weakref from glob import glob -from lib.db import Recipes, Steps, Users, db +from lib.db import Recipes, Users, db from PyQt5.QtCore import QTimer, pyqtSignal from PyQt5.QtGui import QKeySequence from PyQt5.QtWidgets import QFileDialog, QMessageBox, QShortcut @@ -306,18 +306,13 @@ class Recipe_Selection(Widget): recipe = Recipes(name=recipe_name, part_number="TEMPORARY") steps = {} for step_type, step_spec in steps_specs.items(): - step = Steps() + step = {} steps[step_type] = step recipe_is_new = True for step_name, step_spec in steps_specs.items(): step = steps[step_name] - # step.type = re.sub(r"^(.*)_[0-9]+$", r"\1", step_name) step.type = step_name step.spec = step_spec - if recipe_is_new: - step.save(force_insert=True) - else: - step.save() recipe.client = row.get("cliente", defaults["cliente"]) recipe.part_number = row.get(part_number_field, defaults["part_number"]) recipe.description = row.get(description_field, defaults["descrizione"]) @@ -498,5 +493,4 @@ class Recipe_Selection(Widget): ) if ret == QMessageBox.Ok: Recipes.delete().execute() - Steps.delete().execute() self.crud.refresh() diff --git a/src/ui/recipe_spec_and_step_editor/recipe_spec_and_step_editor.py b/src/ui/recipe_spec_and_step_editor/recipe_spec_and_step_editor.py index c34536c..e49bd66 100644 --- a/src/ui/recipe_spec_and_step_editor/recipe_spec_and_step_editor.py +++ b/src/ui/recipe_spec_and_step_editor/recipe_spec_and_step_editor.py @@ -1,4 +1,3 @@ -from lib.db import Steps from peewee import IntegrityError from ui.barcodes_step_editor import Barcodes_Step_Editor from ui.connector_step_editor import Connector_Step_Editor @@ -16,7 +15,7 @@ from ui.instruction_step_editor import Instruction_Step_Editor class Recipe_Spec_And_Step_Editor(Editor): def __init__(self, action=None, cell_widget=None, unsupported_steps=None): super().__init__(action=action, cell_widget=cell_widget) - self.map = { + self.steps_map = { "count": { "type": "count", "enable": self.count_enabled_cb, @@ -90,9 +89,9 @@ class Recipe_Spec_And_Step_Editor(Editor): }, } for step_name in unsupported_steps: - self.map.get(step_name, {})["hidden"] = True - self.spec.update({step_name: map["enable"] for step_name, map in self.map.items()}) - for map in self.map.values(): + self.steps_map.get(step_name, {})["hidden"] = True + self.spec.update({step_name: map["enable"] for step_name, map in self.steps_map.items()}) + for map in self.steps_map.values(): editor = map.get("editor", None) if editor is not None: replace_widget(self, map["widget"], editor) @@ -100,10 +99,11 @@ class Recipe_Spec_And_Step_Editor(Editor): self.save_steps_b.clicked.connect(self.save_steps) self.reset_steps_b.clicked.connect(self.reset_steps) self.available_steps = {} + self.recipe = None def set_readonly(self, readonly=True): super().set_readonly(readonly=readonly) - for map in self.map.values(): + for map in self.steps_map.values(): editor = map.get("editor", None) if editor is not None: editor.set_readonly(readonly=readonly) @@ -116,64 +116,40 @@ class Recipe_Spec_And_Step_Editor(Editor): step_editors_autocomplete = autocomplete.get("step_editors", None) super().do_autocomplete(autocomplete) if step_editors_autocomplete is not None: - for step_name, map in self.map.items(): + for step_name, map in self.steps_map.items(): editor = map.get("editor", None) if editor is not None: editor.do_autocomplete(step_editors_autocomplete.get(step_name, None)) def init(self, action=None): - self.get_steps() if action == "add": self.save_steps() def showing_dialog(self): self.reset_steps() - # def connect_modified(self, check_modified): - # super().connect_modified(check_modified) - # for map in self.map.values(): - # editor = map.get("editor", None) - # if editor is not None: - # editor.connect_modified(check_modified) - def render(self, data, field_name=None, row_number=None, crud=None): super().render(data, field_name=field_name, row_number=row_number, crud=crud) self.available_steps = data.get("available_steps", None) - self.get_steps() steps = set(data.get("steps", set())) - for map in self.map.values(): + for map in self.steps_map.values(): step = map.get("step", None) if step is not None: map["enable"].setChecked(step.name in steps and not map.get("hidden", False)) self.render_steps() def parse(self, action=None, row_number=None, crud=None): - # if action == "commit": - # self.save_steps() ret = super().parse(row_number=row_number, crud=crud) ret["steps"] = [] ret["available_steps"] = {} - for step_name, map in self.map.items(): + for step_name, map in self.steps_map.items(): ret["available_steps"][step_name] = map["step"].name if map["enable"].isChecked() and not map.get("hidden", False): ret["steps"].append(map["step"].name) return ret - def get_steps(self): - for step_name, map in self.map.items(): - if step_name in self.available_steps: - try: - map["step"] = Steps.get_by_id(self.available_steps[step_name]) - map["step_is_new"] = False - except Steps.DoesNotExist: - map["step"] = Steps(name=self.available_steps[step_name], type=map["type"], spec={}) - map["step_is_new"] = True - elif map.get("step", None) is None: - map["step"] = Steps(type=map["type"], spec={}) - map["step_is_new"] = True - def render_steps(self): - for step_name, map in self.map.items(): + for step_name, map in self.steps_map.items(): step = map.get("step", None) if step is not None: editor = map.get("editor", None) @@ -181,11 +157,10 @@ class Recipe_Spec_And_Step_Editor(Editor): editor.render(step.spec) def reset_steps(self): - self.get_steps() self.render_steps() def save_steps(self): - for map in self.map.values(): + for map in self.steps_map.values(): step = map.get("step", None) if step is not None: editor = map.get("editor", None)