163 lines
4.5 KiB
Python
163 lines
4.5 KiB
Python
from sqlalchemy import create_engine, Column, Integer, String, ForeignKey, Float, DateTime
|
|
from sqlalchemy.ext.declarative import declarative_base
|
|
from sqlalchemy.orm import relationship, sessionmaker
|
|
import sys
|
|
from flask import Flask, render_template
|
|
|
|
# Create the Flask application
|
|
app = Flask(__name__)
|
|
|
|
# Create the database engine
|
|
engine = create_engine('sqlite:///scraper_design.db')
|
|
|
|
# Create a session factory
|
|
Session = sessionmaker(bind=engine)
|
|
|
|
# Create a base class
|
|
Base = declarative_base()
|
|
|
|
# Define the product class
|
|
class Product(Base):
|
|
__tablename__ = 'product'
|
|
id = Column(Integer, primary_key=True)
|
|
name = Column(String)
|
|
SKU_PLU_UPC_number = Column(String)
|
|
price_histories = relationship("PriceHistory", back_populates="product")
|
|
|
|
# Define the retailer class
|
|
class Retailer(Base):
|
|
__tablename__ = 'retailer'
|
|
id = Column(Integer, primary_key=True)
|
|
name = Column(String)
|
|
products = relationship("Product", secondary="retailer_product")
|
|
|
|
# Define the price class
|
|
class Price(Base):
|
|
__tablename__ = 'price'
|
|
id = Column(Integer, primary_key=True)
|
|
MOQ = Column(Integer)
|
|
price = Column(Float)
|
|
|
|
# Define the scraperjob class
|
|
class ScraperJob(Base):
|
|
__tablename__ = 'scraper_job'
|
|
id = Column(Integer, primary_key=True)
|
|
start_time = Column(DateTime)
|
|
end_time = Column(DateTime)
|
|
parameters = Column(String)
|
|
statistics = Column(String)
|
|
|
|
# Define the price history class
|
|
class PriceHistory(Base):
|
|
__tablename__ = 'price_history'
|
|
id = Column(Integer, primary_key=True)
|
|
location = Column(String)
|
|
zip_code = Column(Integer)
|
|
city_name = Column(String)
|
|
price_at_time = Column(DateTime)
|
|
price = Column(Float)
|
|
scraper_job_id = Column(Integer, ForeignKey('scraper_job.id'))
|
|
product_id = Column(Integer, ForeignKey('product.id'))
|
|
product = relationship("Product", back_populates="price_histories")
|
|
scraper_job = relationship("ScraperJob")
|
|
|
|
# Define the user class
|
|
class User(Base):
|
|
__tablename__ = 'user'
|
|
id = Column(Integer, primary_key=True)
|
|
username = Column(String)
|
|
credentials = Column(String)
|
|
creation_date = Column(DateTime)
|
|
modification_date = Column(DateTime)
|
|
|
|
class RetailerProduct(Base):
|
|
__tablename__ = 'retailer_product'
|
|
retailer_id = Column(Integer, ForeignKey('retailer.id'), primary_key=True)
|
|
product_id = Column(Integer, ForeignKey('product.id'), primary_key=True)
|
|
|
|
# Create the database tables
|
|
Base.metadata.create_all(engine)
|
|
|
|
# Create command-line arguments for the user class
|
|
def main():
|
|
if len(sys.argv) < 2:
|
|
print("Usage: python user_tool.py [create|edit|delete]")
|
|
return
|
|
|
|
command = sys.argv[1]
|
|
|
|
if command == "create":
|
|
if len(sys.argv) != 3:
|
|
print("Usage: python user_tool.py create [username]")
|
|
return
|
|
|
|
username = sys.argv[2]
|
|
|
|
# Create a new user object
|
|
user = User(username=username)
|
|
|
|
# Add the user to the database
|
|
session = Session()
|
|
session.add(user)
|
|
session.commit()
|
|
|
|
print("User created successfully.")
|
|
|
|
elif command == "edit":
|
|
if len(sys.argv) != 4:
|
|
print("Usage: python user_tool.py edit [user_id] [new_username]")
|
|
return
|
|
|
|
user_id = int(sys.argv[2])
|
|
new_username = sys.argv[3]
|
|
|
|
# Retrieve the user from the database
|
|
session = Session()
|
|
user = session.query(User).get(user_id)
|
|
|
|
if user:
|
|
# Update the user object
|
|
user.username = new_username
|
|
session.commit()
|
|
print("User updated successfully.")
|
|
else:
|
|
print("User not found.")
|
|
|
|
elif command == "delete":
|
|
if len(sys.argv) != 3:
|
|
print("Usage: python user_tool.py delete [user_id]")
|
|
return
|
|
|
|
user_id = int(sys.argv[2])
|
|
|
|
# Retrieve the user from the database
|
|
session = Session()
|
|
user = session.query(User).get(user_id)
|
|
|
|
if user:
|
|
# Delete the user from the database
|
|
session.delete(user)
|
|
session.commit()
|
|
print("User deleted successfully.")
|
|
else:
|
|
print("User not found.")
|
|
|
|
else:
|
|
print("Invalid command.")
|
|
|
|
if __name__ == "__main__":
|
|
main()
|
|
|
|
@app.route('/')
|
|
def homepage():
|
|
# Create a session
|
|
session = Session()
|
|
|
|
# Get all the classes from SQLAlchemy
|
|
classes = Base.__subclasses__()
|
|
|
|
# Render the template with the classes
|
|
return render_template('test3.html', classes=classes)
|
|
|
|
if __name__ == '__main__':
|
|
app.run() |