Skip to content

Commit

Permalink
✨ NEW: Attach/detach cakupan
Browse files Browse the repository at this point in the history
  • Loading branch information
Thaza-Kun committed May 30, 2023
1 parent 0ab33a5 commit 8b65966
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 24 deletions.
65 changes: 44 additions & 21 deletions samudra/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,56 @@ def rename(self, new: str) -> "LemmaEditor":
self.to_save.append(_lemma)
return self

def rewrite_konsep(self, index: int, keterangan: str) -> "LemmaEditor":
def replace_konsep(self, index: int, keterangan: str) -> "LemmaEditor":
_konsep: models.Konsep = models.Konsep.get_by_id(self.data.konsep[index].id)
_konsep.keterangan = keterangan
self.to_save.append(_konsep)
return self

def attach_cakupans(self, index: int, cakupans: List[str]) -> "LemmaEditor":
_konsep: models.Konsep = models.Konsep.get_by_id(self.data.konsep[index].id)
_cakupans: List[models.Cakupan] = _konsep.attach(
# TODO USE get_or_init_record for lazy edit
models.Cakupan,
[{"nama": cakupan} for cakupan in cakupans],
)
self.to_save.extend(_cakupans)
return self

def detach_cakupans(self, index: int, cakupans: List[str]) -> "LemmaEditor":
lookup = {
connector.cakupan.nama: connector.cakupan
for connector in self.data.konsep[index].cakupan
}
for cakupan in cakupans:
to_remove = lookup[cakupan]
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)

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


def get_or_init_record(model: pw.Model, *args, **kwargs) -> pw.Model:
"""Gets a record or initializes a new one without saving
Args:
model (pw.Model): A model
Returns:
pw.Model: Model instance
"""
if data := model.get_or_none(*args, **kwargs) is None:
data = model(*args, **kwargs)
return data


class NewLemmaBuilder:
"""A builder to insert new lemma and its related data
Expand All @@ -139,27 +176,13 @@ class NewLemmaBuilder:
"""

def __init__(self, konsep: str, lemma: str, golongan: str) -> None:
self.lemma = self.get_or_new(models.Lemma, nama=lemma)
self.lemma = get_or_init_record(models.Lemma, nama=lemma)
self.golongan = models.GolonganKata.get(id=golongan)
self.konsep = self.get_or_new(
self.konsep = get_or_init_record(
models.Konsep, lemma=self.lemma, golongan=self.golongan, keterangan=konsep
)
self.to_save: List[pw.Model] = [self.lemma, self.golongan, self.konsep]

@staticmethod
def get_or_new(model: pw.Model, *args, **kwargs) -> pw.Model:
"""Gets a record or initializes a new one without saving
Args:
model (pw.Model): A model
Returns:
pw.Model: Model instance
"""
if data := model.get_or_none(*args, **kwargs) is None:
data = model(*args, **kwargs)
return data

def save(self) -> None:
"""Saves the built record."""
while len(self.to_save) != 0:
Expand All @@ -176,8 +199,8 @@ def set_cakupan(self, nama: str) -> "NewLemmaBuilder":
Returns:
NewLemmaBuilder: Returns self to continue building
"""
self.cakupan = self.get_or_new(models.Cakupan, nama=nama)
self.cakupan_x_konsep = self.get_or_new(
self.cakupan = get_or_init_record(models.Cakupan, nama=nama)
self.cakupan_x_konsep = get_or_init_record(
models.CakupanXKonsep, cakupan=self.cakupan, konsep=self.konsep
)
self.to_save.extend([self.cakupan, self.cakupan_x_konsep])
Expand All @@ -193,8 +216,8 @@ def set_kata_asing(self, nama: str, bahasa: str) -> "NewLemmaBuilder":
Returns:
NewLemmaBuilder: Returns self to continue building
"""
self.kata_asing = self.get_or_new(models.KataAsing, nama=nama, bahasa=bahasa)
self.kata_asing_x_konsep = self.get_or_new(
self.kata_asing = get_or_init_record(models.KataAsing, nama=nama, bahasa=bahasa)
self.kata_asing_x_konsep = get_or_init_record(
models.KataAsingXKonsep, kata_asing=self.cakupan, konsep=self.konsep
)
self.to_save.extend([self.kata_asing, self.kata_asing_x_konsep])
Expand Down
18 changes: 15 additions & 3 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,25 @@ def test_lemma_editor(create_data):
assert query.nama == "nama"

# Test edit lemma
newlemma = LemmaEditor(query)
newlemma.rename("baharu").save()
edit = LemmaEditor(query)
edit.rename("baharu").save()
assert LemmaQueryBuilder(lemma="nama").collect() is None

# Test edit konsep
query = LemmaQueryBuilder(konsep="keterangan", lemma="baharu").collect()
assert query.nama == "baharu"
edit = LemmaEditor(query)
edit.rewrite_konsep(0, "ujian suntingan").save()
edit.replace_konsep(0, "ujian suntingan").save()
assert LemmaQueryBuilder(konsep="keterangan").collect() is None

# Test edit cakupan
edit.attach_cakupans(0, ["XX", "XY"])
query = LemmaQueryBuilder(konsep="keterangan", lemma="baharu").collect()
assert query.konsep[0].cakupan[0].cakupan.nama == "cakupan"
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"])
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 8b65966

Please sign in to comment.