class Jobs(db.Model): __tablename__ = 'jobs' id = db.Column(db.Integer, primary_key=True) url = db.Column(db.Text, nullable=False) image = db.Column(db.Text, nullable=False) title_job = db.Column(db.String(100), nullable=False) company = db.Column(db.String(100), nullable=False) location = db.Column(db.String(100), nullable=False) posted = db.Column(db.String(64), nullable=False) contents = db.Column(db.Text, nullable=False) contents_text = db.Column(db.Text, nullable=False) concepts = db.Column(db.Text, nullable=True) keywords = db.Column(db.Text, nullable=True) created_at = db.Column(db.DateTime, default=datetime.now) applicants = db.relationship('Applicants', backref='job', cascade='all,delete-orphan') overall_score = db.relationship('OverallScore', backref='job', uselist=False, cascade='all,delete-orphan') user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) def save_to_db(self) -> None: db.session.add(self) db.session.commit() def delete_from_db(self) -> None: db.session.delete(self) db.session.commit()
class User(db.Model): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), nullable=False) fullname = db.Column(db.String(100), nullable=True) phone = db.Column(db.String(20), nullable=True) email = db.Column(db.String(100), unique=True, index=True, nullable=False) password = db.Column(db.String(100), nullable=True) terms = db.Column(db.Boolean, default=False) role = db.Column(db.Integer, default=1) avatar = db.Column(db.String(100), default='default.png') created_at = db.Column(db.DateTime, default=datetime.now) updated_at = db.Column(db.DateTime, default=datetime.now) country_id = db.Column(db.Integer, db.ForeignKey('countries.id'), nullable=True) confirmation = db.relationship('Confirmation', backref='user', uselist=False, cascade='all,delete-orphan') wishlists = db.relationship('Wishlist', backref='user', cascade='all,delete-orphan') comments = db.relationship('Comment', backref='user', cascade='all,delete-orphan') replies = db.relationship('Reply', backref='user', cascade='all,delete-orphan') def __init__(self, **args): self.name = args['name'] self.email = args['email'] self.terms = args['terms'] if 'avatar' in args: self.avatar = args['avatar'] if 'password' in args: self.password = bcrypt.generate_password_hash( args['password']).decode("utf-8") def check_pass(self, password: str) -> bool: return bcrypt.check_password_hash(self.password, password) def hash_password(self, password: str) -> "User": self.password = bcrypt.generate_password_hash(password).decode("utf-8") def change_update_time(self) -> "User": self.updated_at = datetime.now() def save_to_db(self) -> None: db.session.add(self) db.session.commit() def delete_from_db(self) -> None: db.session.delete(self) db.session.commit()
class User(db.Model): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(100), nullable=False) email = db.Column(db.String(100), unique=True, index=True, nullable=False) password = db.Column(db.String(100), nullable=True) role = db.Column(db.Integer, default=1) avatar = db.Column(db.String(100), default='default.png') created_at = db.Column(db.DateTime, default=func.now()) updated_at = db.Column(db.DateTime, default=func.now()) confirmation = db.relationship('Confirmation', backref='user', uselist=False, cascade='all,delete-orphan') wishlists = db.relationship('Property', secondary=Wishlist, backref=db.backref('user_wishlists')) def __init__(self, **args): self.username = args['username'] self.email = args['email'] if 'avatar' in args: self.avatar = args['avatar'] if 'password' in args: self.password = bcrypt.generate_password_hash( args['password']).decode("utf-8") @classmethod def check_wishlist(cls, property_id: int, user_id: int) -> Wishlist: return db.session.query(Wishlist) \ .filter(Wishlist.c.property_id == property_id, Wishlist.c.user_id == user_id) \ .first() @classmethod def loved_properties(cls, limit: int) -> List[Tuple[int, int]]: return db.session.query(Wishlist.c.property_id.label('wishlist_id'), func.count(Wishlist.c.property_id).label('count_total')) \ .group_by('wishlist_id') \ .order_by(desc('count_total')) \ .limit(limit).all() def get_wishlist_property(self, per_page: int, page: int, **args) -> Wishlist: from services.models.PropertyModel import Property stmt = db.session.query(Wishlist).filter(Wishlist.c.user_id == self.id).join(Property) \ .order_by(desc(Wishlist.c.id)) if (type_id := args['type_id']): stmt = stmt.filter(Property.type_id == type_id) if (status := args['status']): filters = [ Property.status.like(f"%{x}%") for x in status.split(',') ] stmt = stmt.filter(or_(*filters))
class Users(db.Model): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(100), unique=True, index=True, nullable=False) email = db.Column(db.String(100), unique=True, index=True, nullable=False) password = db.Column(db.String(100), nullable=False) avatar = db.Column(db.String(100), default='default.png') member = db.Column(db.String(64), default='free') url_job = db.Column(db.Integer, default=2) url_profile = db.Column(db.Integer, default=5) company_name = db.Column(db.String(100), nullable=True) company_site = db.Column(db.String(100), nullable=True) position = db.Column(db.String(100), nullable=True) created_at = db.Column(db.DateTime, default=datetime.now) updated_at = db.Column(db.DateTime, default=datetime.now) confirmation = db.relationship('Confirmation', backref='user', uselist=False, cascade='all,delete-orphan') jobs = db.relationship('Jobs', backref='user', cascade='all,delete-orphan') def __init__(self, username: str, email: str, password: str): self.username = username self.email = email self.password = bcrypt.generate_password_hash(password).decode("utf-8") def check_pass(self, password: str) -> bool: return bcrypt.check_password_hash(self.password, password) def hash_password(self, password: str) -> "Users": self.password = bcrypt.generate_password_hash(password).decode("utf-8") def change_update_time(self) -> "Users": self.updated_at = datetime.now() def update_profile(self, username: str, company_name: str, company_site: str, position: str) -> "Users": self.username = username self.company_name = company_name self.company_site = company_site self.position = position def save_to_db(self) -> None: db.session.add(self) db.session.commit() def delete_from_db(self) -> None: db.session.delete(self) db.session.commit()
class Region(db.Model): __tablename__ = 'regions' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), unique=True, index=True, nullable=False) slug = db.Column(db.Text, unique=True, index=True, nullable=False) image = db.Column(db.String(100), nullable=False) description = db.Column(db.Text, nullable=False) properties = db.relationship('Property', backref='region', cascade='all,delete-orphan') def __init__(self, **data): self.name = data['name'] self.slug = slugify(self.name, lowercase=False) self.image = data['image'] self.description = data['description'] def update_data_in_db(self, **data) -> "Region": self.name = data['name'] self.slug = slugify(self.name, lowercase=False) self.description = data['description'] if data['image']: self.image = data['image'] def save_to_db(self) -> None: db.session.add(self) db.session.commit() def delete_from_db(self) -> None: db.session.delete(self) db.session.commit()
class Comment(db.Model): __tablename__ = 'comments' id = db.Column(db.Integer, primary_key=True) subject = db.Column(db.Text, nullable=False) commentable_id = db.Column(db.Integer, nullable=False) commentable_type = db.Column(db.String(30), nullable=False) created_at = db.Column(db.DateTime, default=datetime.now) user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) replies = db.relationship('Reply', backref='comment', cascade='all,delete-orphan') def __init__(self, subject: str, commentable_id: int, commentable_type: str, user_id: int): self.subject = subject self.commentable_id = commentable_id self.commentable_type = commentable_type self.user_id = user_id def save_to_db(self) -> None: db.session.add(self) db.session.commit() def delete_from_db(self) -> None: db.session.delete(self) db.session.commit()
class Applicants(db.Model): __tablename__ = 'applicants' id = db.Column(db.Integer, primary_key=True) url = db.Column(db.String(100), nullable=False) image = db.Column(db.Text, nullable=True) name = db.Column(db.String(100), nullable=False) current_job = db.Column(db.Text, nullable=False) address = db.Column(db.String(100), nullable=True) languages = db.Column(db.String(100), nullable=True) licenses = db.Column(db.Text, nullable=True) skills = db.Column(db.Text, nullable=True) honors = db.Column(db.Text, nullable=True) qualify = db.Column(db.Boolean, default=False) score = db.Column(db.Float, default=0) created_at = db.Column(db.DateTime, default=datetime.now) updated_at = db.Column(db.DateTime, default=datetime.now) experiences = db.relationship('Experiences', backref='applicant', cascade='all,delete-orphan') educations = db.relationship('Educations', backref='applicant', cascade='all,delete-orphan') detail_score = db.relationship('DetailScore', backref='applicant', uselist=False, cascade='all,delete-orphan') job_id = db.Column(db.Integer, db.ForeignKey('jobs.id'), nullable=False) def save_to_db(self) -> None: db.session.add(self) db.session.commit() def delete_from_db(self) -> None: db.session.delete(self) db.session.commit()
class Country(db.Model): __tablename__ = 'countries' id = db.Column(db.Integer,primary_key=True) code = db.Column(db.String(2),unique=True,index=True,nullable=False) name = db.Column(db.String(100),unique=True,index=True,nullable=False) users = db.relationship('User',backref='country',cascade='all,delete-orphan') def save_to_db(self) -> None: db.session.add(self) db.session.commit() def delete_from_db(self) -> None: db.session.delete(self) db.session.commit()
class Type(db.Model): __tablename__ = 'types' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), unique=True, index=True, nullable=False) properties = db.relationship('Property', backref='type', cascade='all,delete-orphan') def save_to_db(self) -> None: db.session.add(self) db.session.commit() def delete_from_db(self) -> None: db.session.delete(self) db.session.commit()
class Activity(db.Model): __tablename__ = 'activities' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), unique=True, index=True, nullable=False) slug = db.Column(db.Text, unique=True, index=True, nullable=False) price = db.Column(db.Integer, nullable=False) discount = db.Column(db.Integer, nullable=True) min_person = db.Column(db.Integer, nullable=False) image = db.Column(db.String(100), nullable=False) image2 = db.Column(db.String(100), nullable=True) image3 = db.Column(db.String(100), nullable=True) image4 = db.Column(db.String(100), nullable=True) description = db.Column(db.Text, nullable=False) duration = db.Column(db.String(100), nullable=False) include = db.Column(db.Text, nullable=False) pickup = db.Column(db.String(100), nullable=False) information = db.Column(db.Text, nullable=False) created_at = db.Column(db.DateTime, default=datetime.now) updated_at = db.Column(db.DateTime, default=datetime.now) category_id = db.Column(db.Integer, db.ForeignKey('categories.id'), nullable=False) wishlists = db.relationship('Wishlist', backref='activity', cascade='all,delete-orphan') def __init__(self, **data): self.name = data['name'] self.slug = slugify(self.name) self.price = data['price'] self.min_person = data['min_person'] self.image = data['image'] self.description = data['description'] self.duration = data['duration'] self.include = data['include'] self.pickup = data['pickup'] self.information = data['information'] self.category_id = data['category_id'] if 'discount' in data: self.discount = data['discount'] if 'image2' in data: self.image2 = data['image2'] if 'image3' in data: self.image3 = data['image3'] if 'image4' in data: self.image4 = data['image4'] def update_data_in_db(self, **data) -> "Activity": self.name = data['name'] self.slug = slugify(self.name) self.price = data['price'] self.min_person = data['min_person'] self.description = data['description'] self.duration = data['duration'] self.include = data['include'] self.pickup = data['pickup'] self.information = data['information'] self.category_id = data['category_id'] if 'discount' in data: self.discount = data['discount'] if 'image' in data: self.image = data['image'] if 'image2' in data: self.image2 = data['image2'] if 'image3' in data: self.image3 = data['image3'] if 'image4' in data: self.image4 = data['image4'] def change_update_time(self) -> "Activity": self.updated_at = datetime.now() def save_to_db(self) -> None: db.session.add(self) db.session.commit() def delete_from_db(self) -> None: db.session.delete(self) db.session.commit()
class Property(db.Model): __tablename__ = 'properties' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), unique=True, index=True, nullable=False) slug = db.Column(db.Text, unique=True, index=True, nullable=False) images = db.Column(db.Text, nullable=False) property_for = db.Column(db.String(15), nullable=False) period = db.Column(db.String(50), nullable=True) status = db.Column(db.String(30), nullable=True) youtube = db.Column(db.String(100), nullable=True) description = db.Column(db.Text, nullable=False) hotdeal = db.Column(db.Boolean, default=False) bedroom = db.Column(db.Integer, nullable=True) bathroom = db.Column(db.Integer, nullable=True) building_size = db.Column(db.Float, nullable=True) land_size = db.Column(db.Float, nullable=False) location = db.Column(db.Text, nullable=False) latitude = db.Column(db.Float, nullable=False) longitude = db.Column(db.Float, nullable=False) created_at = db.Column(db.DateTime, default=func.now()) updated_at = db.Column(db.DateTime, default=func.now()) type_id = db.Column(db.Integer, db.ForeignKey('types.id'), nullable=False) region_id = db.Column(db.Integer, db.ForeignKey('regions.id'), nullable=False) price = db.relationship('PropertyPrice', backref='property', uselist=False, cascade='all,delete-orphan') facilities = db.relationship("Facility", secondary=PropertyFacility, backref=db.backref('properties')) def __init__(self, **args): self.name = args['name'] self.slug = args['slug'] self.images = args['images'] self.property_for = args['property_for'] self.description = args['description'] self.hotdeal = args['hotdeal'] self.land_size = args['land_size'] self.location = args['location'] self.latitude = args['latitude'] self.longitude = args['longitude'] self.type_id = args['type_id'] self.region_id = args['region_id'] if 'youtube' in args: self.youtube = args['youtube'] if 'status' in args: self.status = args['status'] if 'period' in args: self.period = args['period'] if 'bedroom' in args: self.bedroom = args['bedroom'] if 'bathroom' in args: self.bathroom = args['bathroom'] if 'building_size' in args: self.building_size = args['building_size'] def update_data_in_db(self, **args) -> "Property": self.name = args['name'] self.slug = args['slug'] self.property_for = args['property_for'] self.description = args['description'] self.hotdeal = args['hotdeal'] self.land_size = args['land_size'] self.location = args['location'] self.latitude = args['latitude'] self.longitude = args['longitude'] self.type_id = args['type_id'] self.region_id = args['region_id'] if 'youtube' in args: self.youtube = args['youtube'] if 'images' in args: self.images = f"{self.images},{args['images']}" if 'status' in args: self.status = args['status'] if 'period' in args: self.period = args['period'] if 'bedroom' in args: self.bedroom = args['bedroom'] if 'bathroom' in args: self.bathroom = args['bathroom'] if 'building_size' in args: self.building_size = args['building_size'] def change_update_time(self) -> "Property": self.updated_at = func.now() @classmethod def search_by_location(cls, **kwargs) -> "Property": if 'type_id' in kwargs: return cls.query.filter(cls.type_id == kwargs['type_id'], cls.location.like('%' + kwargs['q'] + '%')) \ .limit(20).all() else: return cls.query.filter(cls.location.like('%' + kwargs['q'] + '%')).limit(20).all() @classmethod def filter_by_slug(cls, slug: str) -> "Property": return cls.query.options(orm.joinedload('facilities'),orm.joinedload('price')) \ .filter_by(slug=slug).first_or_404("Property not found") @classmethod def load_similar_listing_random(cls, type_id: int) -> "Property": return cls.query.options(orm.joinedload('facilities'),orm.joinedload('price')) \ .filter_by(type_id=type_id) \ .order_by(func.random()) \ .limit(5).all() @hybrid_method def distance(self, lat, lng): return gc_distance(lat, lng, self.latitude, self.longitude) @distance.expression def distance(cls, lat, lng): return gc_distance(lat, lng, cls.latitude, cls.longitude, math=func) @classmethod def search_properties(cls, per_page: int, page: int, **args) -> "Property": from services.models.FacilityModel import Facility from services.models.PropertyPriceModel import PropertyPrice if args['lat'] and args['lng'] and args['radius']: stmt = db.session.query( cls, cls.distance(args['lat'], args['lng']).label('distance')) else: stmt = db.session.query(cls).order_by(desc(cls.id)) if (prop_id := args['prop_id']): stmt = stmt.filter(cls.id == prop_id) if (region_id := args['region_id']): stmt = stmt.filter(cls.region_id == region_id) if (type_id := args['type_id']): stmt = stmt.filter(cls.type_id == type_id)