Skip to content

Commit

Permalink
⚙ FIX: Lazily detach cakupans
Browse files Browse the repository at this point in the history
  • Loading branch information
Thaza-Kun committed May 31, 2023
1 parent 2f8d68f commit 11a1a0d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
10 changes: 7 additions & 3 deletions samudra/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"""

from copy import deepcopy
from typing import List, Optional, Set
from typing import Dict, List, Optional, Set
from peewee import JOIN

import peewee as pw
Expand Down Expand Up @@ -109,6 +109,7 @@ class LemmaEditor:
def __init__(self, data: LemmaData) -> None:
self.data = data
self.to_save: List[pw.Model] = []
self.to_delete: List[pw.Model] = []

def rename(self, new: str) -> "LemmaEditor":
_lemma: models.Lemma = models.Lemma.get_by_id(self.data.id)
Expand Down Expand Up @@ -142,15 +143,18 @@ def detach_cakupans(self, index: int, cakupans: List[str]) -> "LemmaEditor":
record: models.CakupanXKonsep = models.CakupanXKonsep.get(
cakupan=to_remove.id, konsep=self.data.konsep[index].id
)
# TODO Is there any lazy alternative?
record.delete_instance(recursive=False)
self.to_delete.append(record)
return self

def save(self) -> None:
while len(self.to_save) != 0:
record = self.to_save.pop(0)
record.update()
record.save()

while len(self.to_delete) != 0:
self.to_delete.pop(0).delete_instance(recursive=False)


def get_or_init_record(model: pw.Model, *args, **kwargs) -> pw.Model:
"""Gets a record or initializes a new one without saving
Expand Down
2 changes: 1 addition & 1 deletion tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def test_lemma_editor(create_data):
assert query.konsep[0].cakupan[1].cakupan.nama == "XX"
assert query.konsep[0].cakupan[2].cakupan.nama == "XY"
edit = LemmaEditor(query)
edit.detach_cakupans(0, ["XX"])
edit.detach_cakupans(0, ["XX"]).save()
query = LemmaQueryBuilder(konsep="keterangan", lemma="baharu").collect()
assert query.konsep[0].cakupan[0].cakupan.nama == "cakupan"
assert query.konsep[0].cakupan[1].cakupan.nama == "XY"

0 comments on commit 11a1a0d

Please sign in to comment.