Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from sqlalchemy import create_engine
- from sqlalchemy.orm import sessionmaker
- from sqlalchemy.ext.declarative import declarative_base
- from sqlalchemy.orm import relationship
- engine = create_engine('postgresql://ubuntu:thinkful@localhost:5432/tbay')
- Session = sessionmaker(bind=engine)
- session = Session()
- Base = declarative_base()
- from datetime import datetime
- from sqlalchemy import Table, Column, Integer, String, Float, DateTime, Date, ForeignKey
- user_item_table = Table('user_item_association', Base.metadata,
- Column('user_id', Integer, ForeignKey('users.id')),
- Column('item_id', Integer, ForeignKey('items.id'))
- )
- class User(Base):
- __tablename__ = "users"
- id = Column(Integer, primary_key=True)
- username = Column(String, nullable=False)
- password = Column(String, nullable=False)
- sell_items = relationship("Item",backref="user")
- bid_items = relationship("Item", secondary="user_item_association", backref="users")
- place_bid = relationship("Bid", backref="user")
- class Item(Base):
- __tablename__ = "items"
- id = Column(Integer, primary_key=True)
- name = Column(String, nullable=False)
- description = Column(String)
- start_time = Column(DateTime, default=datetime.utcnow)
- bids = relationship("Bid", backref="item")
- user_id = Column(Integer, ForeignKey('users.id'),nullable=False)
- class Bid(Base):
- __tablename__ = "bids"
- id = Column(Integer, primary_key=True)
- price = Column(Float, nullable=False)
- item_id = Column(Integer, ForeignKey('items.id'),nullable=False)
- user_id = Column(Integer, ForeignKey('users.id'),nullable=False)
- Base.metadata.create_all(engine)
- John = User(username="John",password="Johnpw")
- Michael = User(username="Michael",password="Michaelpw")
- Amanda = User(username="Amanda",password="Amandapw")
- Baseball = Item(name="Baseball", user=John)
- Guitar = Item(name = "Guitar", user=Amanda)
- bid1 = Bid(price=6.70, user=Michael, item=Baseball)
- bid2 = Bid(price=8.50, user=Amanda, item=Baseball)
- bid3 = Bid(price=90, user=John, item=Guitar)
- bid4 = Bid(price=100, user=Michael, item=Guitar)
- session.add_all([John, Michael, Amanda, Baseball, bid1, bid2])
- session.commit()
- for item in John.sell_items:
- print("{} is selling a {}.".format(item.user.username,item.name))
- for bid in Baseball.bids:
- print("{} has bid ${} for the {}.".format(bid.user.username,bid.price,bid.item.name))
- def bidmax(item): #Takes any item that users have placed bids on as a parameter
- itembids = [] #Creates an empty list
- for bid in item.bids:
- itembids.append(bid.price) #Strips out the integers from the bid objects and appends them to the list
- return max(itembids) #Finds the maximum integer in the list
- print("The bid of ${} wins!".format(bidmax(Baseball)))
- print("--------------")
- for item in Amanda.sell_items:
- print("{} is selling a {}.".format(item.user.username,item.name))
- for bid in Guitar.bids:
- print("{} has bid ${} for the {}.".format(bid.user.username,bid.price,bid.item.name))
- print("The bid of ${} wins!".format(bidmax(Guitar)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement