kelurahan.py 1.46 KB
from sqlalchemy import (
    Column, String, Integer, ForeignKeyConstraint,
)

from ..models import Kecamatan
from ..models import PbbmBase, PbbmDBSession, PBBM_ARGS, CommonModel
from ..tools import FixKelurahan


class Kelurahan(PbbmBase, CommonModel):
    __tablename__ = 'ref_kelurahan'
    kd_propinsi = Column(String(2), primary_key=True)
    kd_dati2 = Column(String(2), primary_key=True)
    kd_kecamatan = Column(String(3), primary_key=True)
    kd_kelurahan = Column(String(3), primary_key=True)
    kd_sektor = Column(String(2))
    nm_kelurahan = Column(String(30))
    no_kelurahan = Column(Integer)
    kd_pos_kelurahan = Column(String(5))
    __table_args__ = (
        ForeignKeyConstraint([kd_propinsi, kd_dati2, kd_kecamatan],
                             [Kecamatan.kd_propinsi, Kecamatan.kd_dati2, Kecamatan.kd_kecamatan]),
        PBBM_ARGS)

    @property
    def id(self):
        return self.kd_propinsi + self.kd_dati2 + self.kd_kecamatan + self.kd_kelurahan

    @property
    def kode(self):
        return self.id

    @classmethod
    def query(cls):
        return PbbmDBSession.query(cls)

    @classmethod
    def query_id(cls, id):
        fxKode = FixKelurahan(id)
        return cls.query(). \
            filter_by(
            kd_propinsi=fxKode['kd_propinsi'],
            kd_dati2=fxKode['kd_dati2'],
            kd_kecamatan=fxKode['kd_kecamatan'],
            kd_kelurahan=fxKode['kd_kelurahan'],
        )