uptd.py 1.56 KB
from sqlalchemy import Column, ForeignKey, Integer, String
from opensipkd.pjdl.models import PAD_TABLE_ARGS
from opensipkd.models import User
from opensipkd.pjdl.models.kecamatan import PjdlKecamatan
from . import Base, DefaultModel
from sqlalchemy import (
    Column,
    String
)
from sqlalchemy.ext.hybrid import hybrid_property


class PjdlUptd(Base, DefaultModel):
    __tablename__ = 'uptd'
    __table_args__ = PAD_TABLE_ARGS
    kode = Column(String(20), nullable=False, unique=True)
    nama = Column(String(50), nullable=False, unique=True)

    @classmethod
    def query_kode(cls, kode):
        return cls.query().filter_by(kode=kode)

    @classmethod
    def query_nama(cls, nama):
        return cls.query().filter_by(nama=nama)

    @classmethod
    def query_register(cls, order_field="nama"):
        return cls.query_from(columns=[cls.singkatan, cls.nama]).\
            order_by(getattr(cls, order_field))

    @classmethod
    def get_list(cls):
        r = []
        q = cls.query().order_by(cls.nama)
        for row in q:
            g = (str(row.id), f"{row.kode}: {row.nama}")
            r.append(g)
        return r


class PjdlUptdWilayah(Base, DefaultModel):
    __tablename__ = 'uptd_wilayah'
    __table_args__ = PAD_TABLE_ARGS
    uptd_id = Column(Integer, ForeignKey(PjdlUptd.id))
    kecamatan_id = Column(Integer, ForeignKey(PjdlKecamatan.id))


class PjdlUserUptd(Base, DefaultModel):
    __tablename__ = 'user_uptd'
    __table_args__ = PAD_TABLE_ARGS
    uptd_id = Column(Integer, ForeignKey(PjdlUptd.id))
    user_id = Column(Integer, ForeignKey(User.id))