Created
August 18, 2015 21:20
-
-
Save s-ben/7bd13c7d07de217da4a6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sqlalchemy import create_engine | |
from sqlalchemy.orm import sessionmaker | |
from sqlalchemy.ext.declarative import declarative_base | |
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, Float, String, DateTime, ForeignKey | |
from sqlalchemy.orm import relationship, backref | |
bid_table = Table('bid_association', Base.metadata, | |
Column('auction_id', Integer, ForeignKey('auction.id')), | |
# Column('user_id', Integer, ForeignKey('user.id')), | |
# Column('item_id', Integer, ForeignKey('item.id')), | |
Column('bid_id', Integer, ForeignKey('bid.id')) | |
) | |
class Item(Base): | |
__tablename__ = "item" | |
id = Column(Integer, primary_key=True) | |
name = Column(String, nullable=False) | |
description = Column(String) | |
start_time = Column(DateTime, default=datetime.utcnow) | |
user_id = Column(Integer, ForeignKey('user.id'), nullable=False) | |
bids = relationship("Bid", backref="item") | |
class User(Base): | |
__tablename__ = "user" | |
id = Column(Integer, primary_key=True) | |
name = Column(String, nullable=False) | |
password = Column(String, nullable=False) | |
items = relationship("Item", backref="user") | |
bids = relationship("Bid", backref="user") | |
class Bid(Base): | |
__tablename__ = "bid" | |
id = Column(Integer, primary_key=True) | |
price = Column(Float, nullable=False) | |
item_id = Column(Integer, ForeignKey('item.id'), nullable=False) | |
user_id = Column(Integer, ForeignKey('user.id'), nullable=False) | |
# item = relationship("Item", backref="bid") | |
# user = relationship("User", backref="bid") | |
class Auction(Base): | |
__tablename__ = "auction" | |
id = Column(Integer, primary_key=True) | |
# users = relationship("User", secondary="bid_association", | |
# backref="user") | |
# items = relationship("Item", secondary="bid_association", | |
# backref="item") | |
bids = relationship("Bid", secondary="bid_association", | |
backref="bid") | |
Base.metadata.create_all(engine) | |
dave = User(name= "Dave", password = "pass") | |
craig = User(name = "Craig", password = "pass") | |
kim = User(name = "Kim", password = "pass1") | |
item1 = Item(name="baseball", user=dave) | |
item2 = Item(name="glove", user=dave) | |
bid1 = Bid(price=1, user = kim, item = item1 ) | |
bid2 = Bid(price=1.5, user = craig, item = item1 ) | |
# print bid1.user_id | |
# print session.query(User).get(1) | |
auction1 = Auction(bids = [bid1, bid2]) | |
session.add_all([dave, craig, bid1, bid2, kim, item1, item2, auction1]) | |
session.commit() | |
bid_price = [] | |
bid_name = [] | |
max_bid_price = 0 | |
max_bid = None | |
for bid in auction1.bids: | |
if bid.price > max_bid_price: | |
max_bid_price = bid.price | |
max_bid = bid | |
if max_bid: | |
print "max bid = " | |
print max_bid_price | |
print "winner = " | |
print session.query(User).get(max_bid.user_id).name | |
else: | |
print "no bids placed" | |
# name = session.query(User).get(bid.user_id).name | |
# bid_name.append(name) | |
for bid in item1.bids: | |
print bid.user.name | |
# print session.query(User).get(bid.user_id).name |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment