From 3d05f661b31ad360e5b5bc619cfbbe9c38028d44 Mon Sep 17 00:00:00 2001 From: L-Nafaryus Date: Tue, 3 Aug 2021 22:59:51 +0500 Subject: [PATCH] Fix: db fields conversion Mod: separated update functions --- anisotropy/core.py | 167 ++++++++++++++++++++++++++++--------------- anisotropy/models.py | 26 ++++--- 2 files changed, 121 insertions(+), 72 deletions(-) diff --git a/anisotropy/core.py b/anisotropy/core.py index 71632c8..236b700 100644 --- a/anisotropy/core.py +++ b/anisotropy/core.py @@ -138,6 +138,7 @@ class Anisotropy(object): from math import sqrt structures = deepcopy(self.env["structures"]) + self.params = [] for structure in structures: _theta = structure["geometry"]["theta"] @@ -194,7 +195,7 @@ class Anisotropy(object): ) geometry = dict( theta = theta, - direction = direction, + direction = [ float(num) for num in direction ], r0 = r0, L = L, radius = radius, @@ -236,11 +237,109 @@ class Anisotropy(object): ]) + def _updateStructure(self, src: dict, queryMain) -> int: + raw = deepcopy(src["geometry"]) + raw.update( + name = src["name"], + path = src["path"] + ) + + with self.db.atomic(): + if not queryMain.exists(): + tabID = Structure.create(**raw) + + else: + req = queryMain.dicts().get() + tabID = req["id"] + + query = ( + Structure.update(**raw) + .where( + Structure.name == req["name"], + Structure.direction == req["direction"], + Structure.theta == req["theta"] + ) + ) + query.execute() + + return tabID + + def _updateMesh(self, src: dict, queryMain, structureID) -> int: + raw = deepcopy(src) + + with self.db.atomic(): + if not queryMain.exists(): + tabID = Mesh.create( + structure_id = structureID, + **raw + ) + + else: + req = queryMain.dicts().get() + tabID = req["mesh_id"] + + query = ( + Mesh.update(**raw) + .where( + Mesh.structure_id == req["id"] + ) + ) + query.execute() + + return tabID + + def _updateSubMesh(self, srcs: list, queryMain, meshID) -> None: + for src in srcs: + raw = deepcopy(src) + + with self.db.atomic(): + if not queryMain.exists(): + tabID = SubMesh.create( + mesh_id = meshID, + **raw + ) + + else: + req = queryMain.dicts().get() + tabID = req["mesh_id"] + + query = ( + SubMesh.update(**raw) + .where( + SubMesh.mesh_id == req["mesh_id"] + SubMesh.name == src["name"] + ) + ) + query.execute() + + def _updateMeshResult(self, src: dict, queryMain, meshID) -> None: + raw = deepcopy(src) + + with self.db.atomic(): + if not queryMain.exists(): + tabID = MeshResult.create( + mesh_id = meshID, + **raw + ) + + else: + req = queryMain.dicts().get() + tabID = req["mesh_id"] + + query = ( + Mesh.update(**raw) + .where( + Mesh.mesh_id == req["mesh_id"] + ) + ) + query.execute() + @timer def updateDB(self): for entry in self.params: - query = (Structure - .select() + query = ( + Structure + .select(Structure, Mesh) .join(Mesh, JOIN.INNER, on = (Mesh.structure_id == Structure.id)) .where( Structure.name == entry["name"], @@ -248,67 +347,19 @@ class Anisotropy(object): Structure.theta == entry["geometry"]["theta"] ) ) - - s = deepcopy(entry["geometry"]) - s.update( - name = entry["name"], - path = entry["path"] - ) - - m = deepcopy(entry["mesh"]) - - sm = deepcopy(entry.get("submesh", [])) - - mr = deepcopy(entry.get("meshResult", {})) - - if not query.exists(): - with self.db.atomic(): - stab = Structure.create(**s) - - m.update(structure_id = stab) - mtab = Mesh.create(**m) - - for item in sm: - smtab = SubMesh.create(**item, mesh_id = mtab) - - mr.update(mesh_id = mtab) - mrtab = MeshResult.create(**mr) - - else: - with self.db.atomic(): - (Structure.update(**s) - .where( - Structure.name == entry["name"], - Structure.direction == str(entry["geometry"]["direction"]), - Structure.theta == entry["geometry"]["theta"] - ) - .execute()) - - (Mesh.update(**m) - .where( - Mesh.structure_id == query.get().id - ) - .execute()) - - for item in sm: - (SubMesh.update(**item) - .where( - SubMesh.mesh_id == query.get().mesh_id, - SubMesh.name == item["name"] - ) - .execute()) - - (MeshResult.update(**mr) - .where( - MeshResult.mesh_id == query.get().mesh_id) - .execute()) + + # TODO: empty entries + structureID = self._updateStructure(entry, query) + meshID = self._updateMesh(entry["mesh"], query, structureID) + self._updateSubMesh(entry.get("submesh", []), query, meshID) + self._updateMeshResult(entry.get("meshresults", {}), query, meshID) @timer def updateFromDB(self): squery = Structure.select().order_by(Structure.id) mquery = Mesh.select().order_by(Mesh.structure_id) - + self.params = [] for s, m in zip(squery.dicts(), mquery.dicts()): name = s.pop("name") diff --git a/anisotropy/models.py b/anisotropy/models.py index 4f79b31..a94b77f 100644 --- a/anisotropy/models.py +++ b/anisotropy/models.py @@ -9,15 +9,12 @@ class ListField(Field): def python_value(self, value): pval = [] - for ch in value[1 : -1].split(","): + for entry in value[1 : -1].split(","): try: - pval.append(float(ch)) + pval.append(float(entry)) except: - pass - - finally: - pval.append(ch.strip().replace("'", "")) + pval.append(entry.strip().replace("'", "")) return pval @@ -47,6 +44,7 @@ class Structure(BaseModel): class Mesh(BaseModel): + mesh_id = PrimaryKeyField() structure_id = ForeignKeyField(Structure, backref = "meshes") maxSize = FloatField(null = True) @@ -105,14 +103,14 @@ class MeshResult(BaseModel): surfaceArea = FloatField(null = True) volume = FloatField(null = True) - - elements = FloatField(null = True) - edges = FloatField(null = True) - faces = FloatField(null = True) - volumes = FloatField(null = True) - tetrahedrons = FloatField(null = True) - prisms = FloatField(null = True) - pyramids = FloatField(null = True) + + elements = IntegerField(null = True) + edges = IntegerField(null = True) + faces = IntegerField(null = True) + volumes = IntegerField(null = True) + tetrahedrons = IntegerField(null = True) + prisms = IntegerField(null = True) + pyramids = IntegerField(null = True) calculationTime = TimeField(null = True)