class Dictionary(db.Model): id = db.Column(db.Integer, primary_key=True) UserID = db.Column(db.Integer, db.ForeignKey('user.id')) Userdictionary = db.Column(db.VARCHAR) timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow) def __repr__(self): return '<User {}'.format(self.Userdictionary)
class Review(db.Model): """Model to create a review""" __tablename__ = 'reviews' id = db.Column(db.Integer, primary_key=True, autoincrement=True) review_title = db.Column(db.String(60), nullable=False) review_msg = db.Column(db.Text, nullable=False) business_id = db.Column(db.Integer, db.ForeignKey('businesses.id')) user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) created_at = db.Column(db.DateTime, default=db.func.current_timestamp()) def __init__(self, review_title, review_msg, business_id, user_id): self.review_title = review_title self.review_msg = review_msg self.business_id = business_id self.user_id = user_id db.create_all()
class Resource(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String, nullable=False) url = db.Column(URLType, nullable=False, unique=True) category_id = db.Column(db.Integer, db.ForeignKey('category.id'), nullable=False) category = db.relationship('Category') languages = db.relationship('Language', secondary=language_identifier) paid = db.Column(db.Boolean, default=False) notes = db.Column(db.String) upvotes = db.Column(db.INTEGER, default=0) downvotes = db.Column(db.INTEGER, default=0) times_clicked = db.Column(db.INTEGER, default=0) def key(self): return self.url def __eq__(self, other): if isinstance(other, Resource): if self.name != other.name: return False if self.url != other.url: return False if self.paid != other.paid: return False if self.notes != other.notes: return False if self.category != other.category: return False if self.languages != other.languages: return False return True return False def __hash__(self): return hash(self.url) def __repr__(self): return f"<Resource \n\tName: {self.name}\n\tLanguages: {self.languages}\n\tCategory: {self.category}\n\tURL: {self.url}\n>"
from sqlalchemy_utils import URLType from resources import db ''' event_id = db.Column('event_id', db.Integer, db.ForeignKey('event.id'), nullable=False) event = db.relationship('Event') partner_id = db.Column('partner_id', db.Integer, db.ForeignKey('partner.id')) partner = db.relationship('Partner') ''' language_identifier = db.Table( 'language_identifier', db.Column('resource_id', db.Integer, db.ForeignKey('resource.id')), db.Column('language_id', db.Integer, db.ForeignKey('language.id'))) class Resource(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String, nullable=False) url = db.Column(URLType, nullable=False, unique=True) category_id = db.Column(db.Integer, db.ForeignKey('category.id'), nullable=False) category = db.relationship('Category') languages = db.relationship('Language', secondary=language_identifier) paid = db.Column(db.Boolean, default=False) notes = db.Column(db.String) upvotes = db.Column(db.INTEGER, default=0) downvotes = db.Column(db.INTEGER, default=0) times_clicked = db.Column(db.INTEGER, default=0)
class Business(db.Model): """Model to create a business""" __tablename__ = 'businesses' id = db.Column(db.Integer, primary_key=True, autoincrement=True) business_name = db.Column(db.String(60), nullable=False, unique=True) business_profile = db.Column(db.Text, nullable=False) location = db.Column(db.String(60), nullable=False) category = db.Column(db.String(60), nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) created_at = db.Column(db.DateTime, default=db.func.current_timestamp()) updated_at = db.Column(db.DateTime, default=db.func.current_timestamp( ), onupdate=db.func.current_timestamp()) reviews = db.relationship('Review', backref='business', lazy='dynamic', cascade="all, delete-orphan") def __init__(self, business_name, business_profile,\ location, category, user_id): self.business_name = business_name self.business_profile = business_profile self.location = location self.category = category self.user_id = user_id db.create_all() @staticmethod def businesses_to_json(businesses): """Returns businesses in a JSON format""" businesses_results = [] for business in businesses: userid = business.user_id user = User.query.filter_by(id=userid).first() username = user.user_name output = { 'BusinessName': business.business_name, 'BusinessProfile': business.business_profile, 'Location': business.location, 'Category': business.category, 'CreatedBy' : username, 'id' : business.id } businesses_results.append(output) return businesses_results @staticmethod def businesses_found_message(businesses): message = {'Businesses': businesses} resp = jsonify(message) resp.status_code = 200 return resp @staticmethod def businesses_not_found_message(businesses): """Return Message when no business is found""" message = {'message': 'No businesses found'} resp = jsonify(message) resp.status_code = 404 return resp @staticmethod def limit_less_zero(limit): """Return Message when Limit is 0 or negative""" if limit <=0: message = {'message': 'Limit should be a positive integer greater than 0'} resp = jsonify(message) resp.status_code = 403 return resp @staticmethod def search_businesses(search_term="", location="", category="", page=1): """Searches for businesses based on the parameters provided by the user""" if search_term: """search for business based on a search term q""" businesses = Business.query.filter(Business.\ business_name.ilike("%{}%".format(search_term))).order_by(Business.created_at) else: businesses = Business.query.order_by(Business.created_at) if category: """filter businesses based on category""" businesses = businesses.filter(Business.\ category.ilike("%{}%".format(category))) if location: """filter businesses based on location""" businesses = businesses.filter(Business.\ location.ilike("%{}%".format(location))) businesses_list = businesses.paginate(per_page=3, page=page, error_out=False) businesses = businesses_list.items next_page = businesses_list.next_num if businesses_list.has_next else None prev_page = businesses_list.prev_num if businesses_list.has_prev else None total_pages = businesses_list.pages current_page = businesses_list.page businesses = Business.businesses_to_json(businesses) message = {'Businesses': businesses, 'prevPage': prev_page, 'nextPage' : next_page, 'totalPages' : total_pages, 'currentPage' : current_page } response = jsonify(message) response.status_code = 200 if len(businesses) == 0: response = Business.businesses_not_found_message(businesses) return response