remove steps table wip

This commit is contained in:
neo 2024-09-17 15:19:36 +02:00
parent 9e0bc33904
commit b5d4a07ab2
6 changed files with 17 additions and 94 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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