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 Blacklist(db.Model): """Blacklisted Tokens""" __tablename__ = 'blacklists' id = db.Column(db.Integer, primary_key=True, autoincrement=True) token = db.Column(db.String(200), nullable=False) created_at = db.Column(db.DateTime, default=db.func.current_timestamp()) def __init__(self, token): self.token = token db.create_all()
class Language(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String, unique=True, nullable=False) def key(self): return self.name def __eq__(self, other): if isinstance(other, Language): return self.name == other.name return False def __hash__(self): return hash(self.name) def __repr__(self): return f"<Language {self.name}>"
class Category(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String, nullable=False) def key(self): return self.name def __eq__(self, other): if isinstance(other, Category): return self.name == other.name return False def __hash__(self): return hash(self.name) def __repr__(self): return f"<Category {self.name}>"
class User(UserMixin, db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(64), index=True, unique=True) surname = db.Column(db.String(64), index=True, unique=True) email = db.Column(db.String(120), index=True, unique=True) password_hash = db.Column(db.String(128), index=True, unique=True) Userdictionaries = db.relationship('Dictionary', backref='author', lazy='dynamic') def __repr__(self): return '<User {}'.format(self.username) def set_password(self, password): self.password_hash = generate_password_hash(password) def check_password(self, password): return check_password_hash(self.password_hash, password)
class Product(db.Model): __tablename__ = 'Product' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(80), nullable=False) amount = db.Column(db.Integer, nullable=False) category = db.Column(db.String(80), nullable=False) description = db.Column(db.String(80), nullable=False) productImage = db.Column(db.String(80), nullable=False) def json(self): return { 'id': self.id, 'name': self.name, 'amount': self.amount, 'category': self.category, 'description': self.description, 'productImage': self.productImage } def add_product(_name, _amount, _category, _description, _productImage): new_product = Product(name=_name, amount=_amount, category=_category, description=_description, productImage=_productImage) db.session.add(new_product) db.session.commit() def add_image(_filename, _id): product = Product.query.get(_id) product.productImage = _filename db.session.commit() def get_all_product(): return [Product.json(product) for product in Product.query.all()] def get_product(_id): return [Product.json(Product.query.filter_by(id=_id).first())] def update_product(_id, _name, _amount, _category, _description, _productImageory): product_to_update = Product.query.filter_by(id=_id).first() product_to_update.name = _name product_to_update.amount = _amount product_to_update.category = _category product_to_update.description = _description product_to_update.productImage = _productImageory db.session.commit() def delete_product(_id): Product.query.filter_by(id=_id).delete() db.session.commit() def check_id(_id): product = Product.query.get(_id) if (not product): return False else: return True
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>"
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 User(db.Model): """Model to create a user""" __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True, autoincrement=True) user_name = db.Column(db.String(20), nullable=False, unique=True) user_email = db.Column(db.String(60), nullable=False, unique=True) user_password = db.Column(db.String(80), 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()) businesses = db.relationship('Business', backref='user',lazy='dynamic') def __init__(self, user_email, user_name, user_password): self.user_email = user_email self.user_name = user_name self.user_password = generate_password_hash(user_password, method='sha256') db.create_all()
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