class Category(db.Model): """Category Class""" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(255)) description = db.Column(db.Text) timestamp_deleted = db.Column(db.DateTime) def __init__(self, name, description=None, timestamp_deleted=None): self.name = name self.description = description self.timestamp_deleted = timestamp_deleted def __repr__(self): return "<Category {}>".format(self.name) @property def serialize(self): """Formats for JSON return""" return { "id": self.id, "name": self.name, "description": self.description, "timestamp_deleted": ("" if self.timestamp_deleted is None else self.timestamp_deleted.strftime("%Y-%m-%d")) }
class ReviewSession(db.Model): """Review Class""" __tablename__ = "review_session" real_id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer) customer_id = db.Column(db.Integer) product_id = db.Column(db.Integer) rating = db.Column(db.Numeric) review = db.Column(db.Text) timestamp_created = db.Column(db.DateTime) shown = db.Column(db.Boolean) session_id = db.Column(db.Text) def __init__(self, customer_id, product_id, rating=None, review=None, timestamp_created=None, shown=True, id=None, session_id=None): self.customer_id = customer_id self.product_id = product_id self.rating = rating self.review = review self.timestamp_created = (timestamp_created if timestamp_created is not None else datetime.datetime.utcnow()) self.shown = shown self.id = id self.session_id = session_id def __repr__(self): return "<Review {}>".format(self.id) @property def serialize(self): """Formats for JSON return. This requires data from the Customer and Product models. """ customer_obj = db.session.query(Customer).filter_by( id=self.customer_id, session_id=session.sid).first() product_obj = db.session.query(Product).filter_by( id=self.product_id, session_id=session.sid).first() return { "id": self.id, "customer_id": self.customer_id, "customer_email": customer_obj.email, "product_id": self.product_id, "product_name": product_obj.name, "rating": str(self.rating), "review": self.review, "review_truncated": self.review[:100], "timestamp_created": self.timestamp_created, "shown": self.shown }
class ProductCategory(db.Model): """ProductCategory Class""" id = db.Column(db.Integer, primary_key=True) category_id = db.Column(db.Integer) product_id = db.Column(db.Integer) def __init__(self, category_id, product_id): self.category_id = category_id self.product_id = product_id def __repr__(self): return "<ProductCategory {}>".format(self.id)
class Customer(db.Model): """Customer Class""" id = db.Column(db.Integer, primary_key=True) first_name = db.Column(db.String(255)) last_name = db.Column(db.String(255)) address = db.Column(db.Text) city = db.Column(db.String(255)) state = db.Column(db.String(50)) zip_code = db.Column(db.String(20)) phone = db.Column(db.String(50)) email = db.Column(db.String(255)) password_hash = db.Column(db.Text) def __init__(self, email, password_hash, first_name=None, last_name=None, address=None, city=None, state=None, zip_code=None, phone=None): self.email = email self.password_hash = password_hash self.first_name = first_name self.last_name = last_name self.address = address self.city = city self.state = state self.zip_code = zip_code self.phone = phone def __repr__(self): return "<Customer {}>".format(self.email) @property def serialize(self): """Formats for JSON return""" return { "id": self.id, "first_name": self.first_name, "last_name": self.last_name, "address": self.address, "city": self.city, "state": self.state, "zip_code": self.zip_code, "phone": self.phone, "email": self.email }
class Sale(db.Model): """Sale Class""" id = db.Column(db.Integer, primary_key=True) product_id = db.Column(db.Integer) price = db.Column(db.Numeric) timestamp_created = db.Column(db.DateTime) timestamp_ended = db.Column(db.DateTime) def __init__( self, product_id, price, timestamp_created=None, timestamp_ended=None): self.product_id = product_id self.price = price self.timestamp_created = ( timestamp_created if timestamp_created is not None else datetime.datetime.utcnow() ) self.timestamp_ended = timestamp_ended def __repr__(self): return "<Sale {}>".format(self.id) @property def serialize(self): """Formats for JSON return. This requires data from the Product model. """ product_obj = db.session.query( Product ).filter_by( id=self.product_id ).first() return { "id": self.id, "product_id": self.product_id, "product_name": product_obj.name, "price": str(self.price), "timestamp_created": self.timestamp_created.strftime("%Y-%m-%d"), "timestamp_ended": ( "" if self.timestamp_ended is None else self.timestamp_ended.strftime("%Y-%m-%d") ) }
class ProductCategorySession(db.Model): """ProductCategory Class""" __tablename__ = "product_category_session" real_id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer) category_id = db.Column(db.Integer) product_id = db.Column(db.Integer) session_id = db.Column(db.Text) def __init__(self, category_id, product_id, id=None, session_id=None): self.category_id = category_id self.product_id = product_id self.id = id self.session_id = session_id def __repr__(self): return "<ProductCategory {}>".format(self.id)
class EmployeeSession(db.Model, UserMixin): """Employee Class""" __tablename__ = "employee_session" real_id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer) email = db.Column(db.String(120), unique=True) password_hash = db.Column(db.String(250)) account_image = db.Column(db.Text) is_authenticated = db.Column(db.Boolean) is_active = db.Column(db.Boolean) is_anonymous = db.Column(db.Boolean) session_id = db.Column(db.Text) def __init__(self, email, password_hash, is_authenticated=True, is_active=True, is_anonymous=False, account_image=None, id=None, session_id=None): self.email = email self.password_hash = password_hash self.is_authenticated = is_authenticated self.is_active = is_active self.is_anonymous = is_anonymous self.account_image = account_image self.id = id self.session_id = session_id def __repr__(self): return "<Employee %r>" % self.id def get_id(self): return str(self.id) @property def serialize(self): """Formats for JSON return""" return { "id": self.id, "email": self.email, "account_image": self.account_image }
class Order(db.Model): """Order Class""" id = db.Column(db.Integer, primary_key=True) sale_id = db.Column(db.Integer) customer_id = db.Column(db.Integer) timestamp_created = db.Column(db.DateTime) def __init__(self, sale_id, customer_id, timestamp_created=None): self.sale_id = sale_id self.customer_id = customer_id self.timestamp_created = (timestamp_created if timestamp_created is not None else datetime.datetime.utcnow()) def __repr__(self): return "<Order {}>".format(self.id) @property def serialize(self): """Formats for JSON return. This requires data from the Customer, Sale, and Product models. """ customer_obj = db.session.query(Customer).filter_by( id=self.customer_id).first() sale_obj = db.session.query(Sale).filter_by(id=self.sale_id).first() product_obj = db.session.query(Product).filter_by( id=sale_obj.product_id).first() return { "id": self.id, "sale_id": self.sale_id, "sale_price": str(sale_obj.price), "sale_product": product_obj.name, "customer_id": self.customer_id, "customer_email": customer_obj.email, "timestamp_created": self.timestamp_created }
class ProductSession(db.Model): """Product Class""" __tablename__ = "product_session" real_id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer) name = db.Column(db.String(255)) supplier_id = db.Column(db.Integer) units_in_stock = db.Column(db.Integer) unit_price = db.Column(db.Numeric) part_number = db.Column(db.String(255)) image_name = db.Column(db.String(255)) image = db.Column(db.Text) session_id = db.Column(db.Text) def __init__(self, name, supplier_id=None, units_in_stock=None, unit_price=None, part_number=None, image_name=None, image=None, id=None, session_id=None): self.name = name self.supplier_id = supplier_id self.units_in_stock = units_in_stock self.unit_price = unit_price self.part_number = part_number self.image_name = image_name self.image = image self.id = id self.session_id = session_id def __repr__(self): return "<Product {}>".format(self.name) @property def serialize(self): """Formats for JSON return. This requires data from the Supplier, Category, and ProductCategory models. """ supplier_obj = db.session.query(Supplier).filter_by( id=self.supplier_id, session_id=session.sid).first() supplier_objs = db.session.query(Supplier).filter_by( session_id=session.sid).all() product_category_objs = db.session.query(ProductCategory).filter_by( product_id=self.id, session_id=session.sid).all() category_objs = db.session.query(Category).filter_by( session_id=session.sid).all() return { "id": self.id, "name": self.name, "supplier_id": self.supplier_id, "supplier_name": (None if supplier_obj is None else supplier_obj.name), "supplier_list": [(x.id, x.name) for x in supplier_objs], "units_in_stock": self.units_in_stock, "unit_price": (str(self.unit_price) if self.unit_price is not None else ""), "part_number": self.part_number, "image_name": (self.image_name.split("/")[-1] if self.image_name is not None else self.image_name), "image": self.image, "category_list": [(x.id, x.name) for x in category_objs], "product_category_list": [x.category_id for x in product_category_objs] }
class SupplierSession(db.Model): """Supplier Class""" __tablename__ = "supplier_session" real_id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer) name = db.Column(db.String(255)) contact_name = db.Column(db.String(255)) contact_title = db.Column(db.String(255)) address = db.Column(db.Text) city = db.Column(db.String(255)) zip_code = db.Column(db.String(20)) state = db.Column(db.String(50)) phone = db.Column(db.String(50)) contact_phone = db.Column(db.String(50)) contact_email = db.Column(db.String(255)) session_id = db.Column(db.Text) def __init__( self, name, contact_name=None, contact_title=None, address=None, city=None, zip_code=None, state=None, phone=None, contact_phone=None, contact_email=None, id=None, session_id=None): self.name = name self.contact_name = contact_name self.contact_title = contact_title self.address = address self.city = city self.zip_code = zip_code self.state = state self.phone = phone self.contact_phone = contact_phone self.contact_email = contact_email self.id = id self.session_id = session_id def __repr__(self): return "<Supplier {}>".format(self.name) @property def serialize(self): """Formats for JSON return.""" return { "id": self.id, "name": self.name, "contact_name": self.contact_name, "contact_title": self.contact_title, "address": self.address, "city": self.city, "zip_code": self.zip_code, "state": self.state, "phone": self.phone, "contact_phone": self.contact_phone, "contact_email": self.contact_email }