Advertisement
Guest User

Untitled

a guest
Mar 13th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. from sqlalchemy import create_engine
  2. from sqlalchemy.orm import sessionmaker
  3. from sqlalchemy.ext.declarative import declarative_base
  4. from sqlalchemy.orm import relationship
  5.  
  6. engine = create_engine('postgresql://ubuntu:thinkful@localhost:5432/tbay')
  7. Session = sessionmaker(bind=engine)
  8. session = Session()
  9. Base = declarative_base()
  10.  
  11. from datetime import datetime
  12. from sqlalchemy import Table, Column, Integer, String, Float, DateTime, Date, ForeignKey
  13.  
  14. user_item_table = Table('user_item_association', Base.metadata,
  15. Column('user_id', Integer, ForeignKey('users.id')),
  16. Column('item_id', Integer, ForeignKey('items.id'))
  17. )
  18.  
  19. class User(Base):
  20. __tablename__ = "users"
  21.  
  22. id = Column(Integer, primary_key=True)
  23. username = Column(String, nullable=False)
  24. password = Column(String, nullable=False)
  25. sell_items = relationship("Item",backref="user")
  26. bid_items = relationship("Item", secondary="user_item_association", backref="users")
  27. place_bid = relationship("Bid", backref="user")
  28.  
  29. class Item(Base):
  30. __tablename__ = "items"
  31.  
  32. id = Column(Integer, primary_key=True)
  33. name = Column(String, nullable=False)
  34. description = Column(String)
  35. start_time = Column(DateTime, default=datetime.utcnow)
  36. bids = relationship("Bid", backref="item")
  37. user_id = Column(Integer, ForeignKey('users.id'),nullable=False)
  38.  
  39. class Bid(Base):
  40. __tablename__ = "bids"
  41.  
  42. id = Column(Integer, primary_key=True)
  43. price = Column(Float, nullable=False)
  44. item_id = Column(Integer, ForeignKey('items.id'),nullable=False)
  45. user_id = Column(Integer, ForeignKey('users.id'),nullable=False)
  46.  
  47. Base.metadata.create_all(engine)
  48.  
  49. John = User(username="John",password="Johnpw")
  50. Michael = User(username="Michael",password="Michaelpw")
  51. Amanda = User(username="Amanda",password="Amandapw")
  52.  
  53. Baseball = Item(name="Baseball", user=John)
  54. Guitar = Item(name = "Guitar", user=Amanda)
  55.  
  56. bid1 = Bid(price=6.70, user=Michael, item=Baseball)
  57. bid2 = Bid(price=8.50, user=Amanda, item=Baseball)
  58.  
  59. bid3 = Bid(price=90, user=John, item=Guitar)
  60. bid4 = Bid(price=100, user=Michael, item=Guitar)
  61.  
  62. session.add_all([John, Michael, Amanda, Baseball, bid1, bid2])
  63. session.commit()
  64.  
  65. for item in John.sell_items:
  66. print("{} is selling a {}.".format(item.user.username,item.name))
  67.  
  68. for bid in Baseball.bids:
  69. print("{} has bid ${} for the {}.".format(bid.user.username,bid.price,bid.item.name))
  70.  
  71. def bidmax(item): #Takes any item that users have placed bids on as a parameter
  72. itembids = [] #Creates an empty list
  73. for bid in item.bids:
  74. itembids.append(bid.price) #Strips out the integers from the bid objects and appends them to the list
  75. return max(itembids) #Finds the maximum integer in the list
  76.  
  77. print("The bid of ${} wins!".format(bidmax(Baseball)))
  78.  
  79. print("--------------")
  80.  
  81. for item in Amanda.sell_items:
  82. print("{} is selling a {}.".format(item.user.username,item.name))
  83.  
  84. for bid in Guitar.bids:
  85. print("{} has bid ${} for the {}.".format(bid.user.username,bid.price,bid.item.name))
  86.  
  87. print("The bid of ${} wins!".format(bidmax(Guitar)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement