diff --git a/samudra/interfaces.py b/samudra/interfaces.py index faba341..8a4cd03 100644 --- a/samudra/interfaces.py +++ b/samudra/interfaces.py @@ -116,12 +116,35 @@ 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) @@ -129,6 +152,20 @@ def save(self) -> None: 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 @@ -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: @@ -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]) @@ -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]) diff --git a/tests/test_main.py b/tests/test_main.py index c1681f8..7d44cd4 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -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"