class Wisata(db.Model): __tablename__ = 'wisata' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(200)) desc = db.Column(db.Text) features = db.relationship('Feature', secondary='features_wisata', backref=db.backref('wisata', lazy='dynamic')) location_id = db.Column(db.Integer, db.ForeignKey('location.id', ondelete='CASCADE')) location = db.relationship('Location', foreign_keys=location_id, backref=db.backref('location_wisata')) admin_id = db.Column(db.Integer, db.ForeignKey('administrator.id', ondelete='CASCADE')) admin = db.relationship('Administrator', foreign_keys=admin_id, backref=db.backref('administrator_wisata')) trash = db.Column(db.Boolean, default=0, nullable=False) photos = db.relationship('Photo', secondary='photo_wisata', backref=db.backref('wisata', lazy='dynamic')) create_at = db.Column(db.DateTime, default=func.current_timestamp()) update_at = db.Column(db.DateTime, onupdate=func.now()) delete_at = db.Column(db.DateTime) @staticmethod def get_all_data(search="", page=1, perpage=15): return Wisata.query.filter(Wisata.name.like( '%{}%'.format(search))).paginate(page, perpage, False) @staticmethod def get_by_id(wisata_id): return Wisata.query.get(wisata_id) def wisata_form(self, form): self.name = form.name.data self.desc = form.desc.data def save(self): db.session.add(self) db.session.commit()
class Feature(db.Model): __tablename__ = 'feature' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(200)) count_feature = db.Column(db.Integer) desc = db.Column(db.Text) trash = db.Column(db.Boolean, default=0, nullable=False) photos = db.relationship('Photo', secondary='photo_feature_wisata', backref=db.backref('feature', lazy='dynamic')) create_at = db.Column(db.DateTime, default=func.current_timestamp()) update_at = db.Column(db.DateTime, onupdate=func.now()) delete_at = db.Column(db.DateTime)
class Alamat(db.Model): __tablename__ = 'tb_alamat' id = db.Column(db.Integer, primary_key=True) id_dusun = db.Column(db.Integer, db.ForeignKey('tb_dusun.id', ondelete='cascade')) dusun = db.relationship('Dusun', foreign_keys=id_dusun, backref=db.backref('dusun_alamat')) rt_rw = db.Column(db.String(10)) kode_pos = db.Column(db.String(5)) @staticmethod def get_by_id(id_alamat): return Alamat.query.get(id_alamat) def desa(self): if self.dusun: return self.dusun.desa return None def kec(self): if self.dusun and self.dusun.desa: return self.dusun.desa.kec return None def kab(self): if self.dusun and self.dusun.desa and self.dusun.desa.kec: return self.dusun.desa.kec.kab return None def prov(self): if self.dusun and self.dusun.desa \ and self.dusun.desa.kec \ and self.dusun.desa.kec.kab: return self.dusun.desa.kec.kab.prov return None def get_all(search='', page=1): return Alamat.query.join(Dusun, Alamat.id_dusun == Dusun.id).join( Desa, Dusun.id_desa == Desa.id).join(Kec, Desa.id_kec == Kec.id).filter( or_(Dusun.nama.like('%{}%'.format(search)), Desa.nama.like('%{}%'.format(search)), Kec.nama.like('%{}%'.format(search)))).paginate( page, per_page=10) def save(self): db.session.add(self) db.session.commit()
class Dusun(db.Model): __tablename__ = 'tb_dusun' id = db.Column(db.Integer, primary_key=True) nama = db.Column(db.String(45)) id_desa = db.Column(db.Integer, db.ForeignKey('tb_desa.id', ondelete='cascade')) desa = db.relationship('Desa', foreign_keys=id_desa, backref=db.backref('desa_dusun')) @staticmethod def get_by_desa(desa_id, search=''): return Dusun.query.filter( and_(Dusun.id_desa == desa_id, or_(Dusun.nama.like('%{}%'.format(search))))).all()
class Desa(db.Model): __tablename__ = 'tb_desa' id = db.Column(db.Integer, primary_key=True) nama = db.Column(db.String(45)) id_kec = db.Column(db.Integer, db.ForeignKey('tb_kec.id', ondelete='cascade')) kec = db.relationship('Kec', foreign_keys=id_kec, backref=db.backref('kec_desa')) @staticmethod def get_by_kec(kec_id, search=''): return Desa.query.filter( and_(Desa.id_kec == kec_id, or_(Desa.nama.like('%{}%'.format(search))))).all()
class Kec(db.Model): __tablename__ = 'tb_kec' id = db.Column(db.Integer, primary_key=True) nama = db.Column(db.String(45)) id_kab = db.Column(db.Integer, db.ForeignKey('tb_kab.id', ondelete='cascade')) kab = db.relationship('Kab', foreign_keys=id_kab, backref=db.backref('kab_kec')) @staticmethod def get_by_kab(kab_id, search=''): return Kec.query.filter( and_(Kec.id_kab == kab_id, or_(Kec.nama.like('%{}%'.format(search))))).all()
class Kab(db.Model): __tablename__ = 'tb_kab' id = db.Column(db.Integer, primary_key=True) nama = db.Column(db.String(45)) id_prov = db.Column(db.Integer, db.ForeignKey('tb_prov.id', ondelete='cascade')) prov = db.relationship('Provinsi', foreign_keys=id_prov, backref=db.backref('prov_kab')) @staticmethod def get_by_prov(prov_id, search=''): return Kab.query.filter( and_(Kab.id_prov == prov_id, or_(Kab.nama.like('%{}%'.format(search))))).all()
class User(db.Model, UserMixin): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(255), nullable=False, server_default=u'', unique=True) # User authentication information (required for Flask-User) email = db.Column(db.Unicode(255), nullable=False, server_default=u'', unique=True) email_confirmed_at = db.Column(db.DateTime()) password = db.Column(db.String(255), nullable=False, server_default='') # reset_password_token = db.Column(db.String(100), nullable=False, server_default='') active = db.Column(db.Boolean(), nullable=False, server_default='0') # User information active = db.Column('is_active', db.Boolean(), nullable=False, server_default='0') first_name = db.Column(db.Unicode(50), nullable=False, server_default=u'') last_name = db.Column(db.Unicode(50), nullable=False, server_default=u'') # Relationships roles = db.relationship('Role', secondary='users_roles', backref=db.backref('users', lazy='dynamic')) @staticmethod def get_by_id(user_id): return User.query.get(user_id) @staticmethod def get_by_username(username): return User.query.filter(User.username==username).first() def generate_password(self, password): self.password = sha256( str( md5( str(password).encode('utf-8') ).hexdigest() ).encode('utf-8') ).hexdigest() def check_password(self, password): return self.password == sha256( str( md5( str(password).encode('utf-8') ).hexdigest() ).encode('utf-8') ).hexdigest()
class Keluarga(db.Model): __tablename__ = 'tb_keluarga' id = db.Column(db.Integer, primary_key=True) no_kk = db.Column(db.String(45)) nama_kk = db.Column(db.String(45)) id_alamat = db.Column(db.Integer, db.ForeignKey('tb_alamat.id', ondelete='cascade')) alamat = db.relationship('Alamat', foreign_keys=id_alamat, backref=db.backref('keluarga_alamat')) @staticmethod def get_all(page=1, search=''): return Keluarga.query.filter( or_(Keluarga.nama_kk.like('%{}%'.format(search)), Keluarga.no_kk.like('%{}%'.format(search)))).paginate( page, per_page=10)