Skip to content

Commit

Permalink
feat: added BaseStrictTable for Golongan & Keizinan
Browse files Browse the repository at this point in the history
  • Loading branch information
Thaza-Kun committed Aug 21, 2022
1 parent 0b2310d commit a893b6d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
20 changes: 18 additions & 2 deletions samudra/models/auth/pengguna.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
from peewee import TextField
import enum

from samudra.models.base import BaseTable
from peewee import TextField, BooleanField, ForeignKeyField

from samudra.models.base import BaseTable, BaseStrictTable


class RoleEnum(enum.Enum):
DEFAULT = 'BIASA'


class Keizinan(BaseStrictTable):
peranan = TextField(null=False, unique=True)
baca = BooleanField(null=False)
ubah = BooleanField(null=False)
tambah = BooleanField(null=False)
buang = BooleanField(null=False)


class Pengguna(BaseTable):
nama = TextField(null=False)
kunci = TextField(null=False)
peranan = ForeignKeyField(model=Keizinan, field=Keizinan.peranan, backref='pengguna',
on_delete='set default', default=RoleEnum.DEFAULT.value)

def __repr__(self) -> str:
return f"<model.{self.__class__.__name__}: id={self.id} nama={self.nama} kunci={self.kunci}>"
9 changes: 8 additions & 1 deletion samudra/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class BaseMetadataTable(BaseTable):

@classmethod
def __attach__(
cls, other: BaseTable, values: List[Dict[str, str]]
cls, other: BaseTable, values: List[Dict[str, str]]
) -> pw.ModelSelect:
rows = [cls.get_or_create(**value)[0] for value in values]
model_name = getattr(cls, "key", cls.__name__.lower())
Expand All @@ -36,3 +36,10 @@ def __attach__(
except AttributeError:
raise AttributeError(f"{cls} has no associated connection table")
return getattr(other, model_name)


class BaseStrictTable(BaseTable):
@classmethod
def get_or_create(cls, **kwargs):
raise AttributeError(
f"{cls} is a strict table. Rows can only be defined explicitly by the `Model.create` method.")
16 changes: 12 additions & 4 deletions samudra/models/core/konsep.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@
TimestampField,
BlobField,
ForeignKeyField,
ModelSelect,
ModelSelect, CharField,
)

from models.base import BaseTable, BaseMetadataTable, BaseConnectionTable
from models.base import BaseTable, BaseMetadataTable, BaseConnectionTable, BaseStrictTable
from .lemma import Lemma


class Golongan(BaseStrictTable):
id = CharField(max_length=6, unique=True, null=False)
nama = TextField(null=False)
keterangan = TextField(null=False)


class Konsep(BaseTable):
"""
Konsep model
Expand All @@ -22,8 +28,10 @@ class Konsep(BaseTable):
lemma = ForeignKeyField(
model=Lemma, field=Lemma.id, backref="konsep", on_delete="cascade"
)
golongan = TextField(null=False)
keterangan = TextField(null=True)
# TODO: Create composite key of id and tertib
# TODO: Point golongan to an external table with exhaustive list
golongan = ForeignKeyField(model=Golongan, field=Golongan.id, on_delete='set null', null=True)
keterangan = TextField(null=True, index=True)
# ---
tertib = IntegerField(null=True)

Expand Down

0 comments on commit a893b6d

Please sign in to comment.