class DistanceModel(db.Model): id = db.Column(db.Integer, primary_key=True) city_1 = db.Column(db.String(100)) city_2 = db.Column(db.String(100)) distance = db.Column(db.Integer) __table_args__ = db.UniqueConstraint('city_1', 'city_2', name='_city_1_city_2'), @classmethod def get_distance(cls, point_1, point_2): distance = db.session.query(cls).filter_by(city_1=point_1, city_2=point_2).first() if not distance: distance = db.session.query(cls).filter_by(city_1=point_2, city_2=point_1).first() return distance.distance if distance else None @classmethod def create_object(cls, city_1, city_2, distance): distance_object = cls(city_1=city_1, city_2=city_2, distance=distance) db.session.add(distance_object) db.session.commit() def __repr__(self): return f"Distance({self.city_1} and {self.city_2} = {self.distance})"
class User(db.Model, UserMixin): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) # User Authentication information username = db.Column(db.String(50), nullable=False, unique=True) password = db.Column(db.String(255), nullable=False, default='') # User Email information email = db.Column(db.String(255), nullable=False, unique=True) email_confirmed_at = db.Column(db.DateTime()) # User information is_enabled = db.Column(db.Boolean(), nullable=False, default=True) trash = db.Column(db.Boolean, nullable=False, default=False) roles = db.relationship('Role', secondary='users_roles') def is_active(self): return self.is_enabled def save(self): db.session.add(self) db.session.commit() @staticmethod def get_by_id(user_id): return User.query.get(user_id) def remove(self): self.trash = True db.session.add(self) db.session.commit() def delete(self): db.session.delete(self) db.session.commit() def restore(self): self.trash = False db.session.add(self) db.session.commit() @staticmethod def get_username(username): return User.query.filter(User.username == username).first() def create_password(self, password): self.password = str( sha256( str(md5(str(password).encode('utf-8')).hexdigest()).encode( 'utf-8')).hexdigest()) def check_password(self, password): return self.password == str( sha256( str(md5(str(password).encode('utf-8')).hexdigest()).encode( 'utf-8')).hexdigest())
class Photo(db.Model): __tablename__ = 'photo' id = db.Column(db.Integer, primary_key=True) filename = db.Column(db.String(200)) alt = db.Column(db.String(200)) trash = db.Column(db.Boolean, default=0, nullable=False) 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 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)
class Location(db.Model): __tablename__ = 'location' id = db.Column(db.Integer, primary_key=True) village = db.Column(db.String(200)) district = db.Column(db.String(200)) lat = db.Column(db.Float) lng = db.Column(db.Float) address = db.Column(db.Text) trash = db.Column(db.Boolean, default=0, nullable=False) 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) def location_form(self, form): self.village = form.village.data self.district = form.district.data self.address = form.address_loc.data self.lat = form.lat.data self.lng = form.lng.data
class Administrator(db.Model): __tablename__ = 'administrator' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(200)) first_name = db.Column(db.String(200)) last_name = db.Column(db.String(200)) desc = db.Column(db.Text) contact = db.Column(db.String(45)) address = db.Column(db.Text) trash = db.Column(db.Boolean, default=0, nullable=False) 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) def admin_form(self, form): self.name = form.admin_name.data self.first_name = form.first_name.data self.last_name = form.last_name.data self.contact = form.contact.data self.address = form.address_admin.data
class Role(db.Model): __tablename__ = 'roles' id = db.Column(db.Integer(), primary_key=True) name = db.Column(db.String(50), nullable=False, server_default=u'', unique=True) # for @roles_accepted() desc = db.Column(db.Text) # for display purposes 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 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 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 Provinsi(db.Model): __tablename__ = 'tb_prov' id = db.Column(db.Integer, primary_key=True) nama = db.Column(db.String(45))
class Role(db.Model): __tablename__ = 'roles' id = db.Column(db.Integer(), primary_key=True) name = db.Column(db.String(50), nullable=False, server_default=u'', unique=True) # for @roles_accepted() label = db.Column(db.Unicode(255), server_default=u'') # for display purposes