class Auction(db.Model): ''' This class represents a single auction. ''' id = db.Column(db.String(250), primary_key=True) item_id = db.Column(db.String(250), db.ForeignKey('item.id')) current_price = db.Column(db.DECIMAL, default=decimal.Decimal("0.00")) current_winner = db.Column(db.String(250), db.ForeignKey('user.id')) # the time at which this auction began accepting bids, NOT the time the auction was created start_time = db.Column(db.DateTime) auction_end = db.Column(db.DateTime) task_id = db.Column(db.String(250)) active = db.Column(db.Boolean) history = db.relationship('Bidhistory', backref='auction', lazy='dynamic') autobidders = db.relationship('Autobidder', backref='auction', lazy='dynamic') # implicit property 'attached_autobidders' created by the Autobidder class # implicit property 'past_bids' created by the BidHistory class # sig = deactivate.s((id), countdown=10) # task = None def __init__(self, id, item, start_time): self.id = id self.item = item self.start_time = start_time self.auction_end = start_time self.active = False def add(self): db.session.add(self) return session_commit() def put(self): return session_commit()
class Autobidder(db.Model): ''' This class models an auto bidder, which places bids on an auction automatically on behalf of its creator. ''' id = db.Column(db.String(250), primary_key=True) user_id = db.Column(db.String(250), db.ForeignKey('user.id'), nullable=True) auction_id = db.Column(db.String(250), db.ForeignKey('auction.id'), nullable=True) remaining_bids = db.Column(db.Integer, nullable=False) create_time = db.Column(db.DateTime) last_bid_time = db.Column(db.DateTime) def __init__(self, id, user_id, auction_id, remaining_bids): self.id = id self.user_id = user_id self.auction = auction_id self.remaining_bids = remaining_bids self.create_time = datetime.datetime.utcnow() def get_by_id(id): return Auction.query.filter_by(id=id).first() def add(self): db.session.add(self) return session_commit() def put(self): return session_commit()
class Item(db.Model): id = db.Column(db.String(250), primary_key=True) sale_id = db.Column(db.String(250), db.ForeignKey('user.id')) name = db.Column(db.String(250), nullable=False) quantity_in_stock = db.Column(db.Integer, nullable=False) base_price = db.Column(db.DECIMAL, nullable=False) product_url = db.Column(db.String(250)) image_url = db.Column(db.String(250)) auctions = db.relationship('Auction', backref='item', lazy='dynamic') def __init__(self, id, name, quantity_in_stock, base_price, product_url, image_url): self.id = id self.name = name self.quantity_in_stock = quantity_in_stock self.base_price = base_price self.product_url = product_url self.image_url = image_url @staticmethod def get_by_id(id): ''' Generates a list of items whose id is contained in the {ids} list ''' return Item.query.filter_by(id=id).first() def update_quantity(self, new_quantity): ''' Update the quantity of this item in stock ''' self.quantity_in_stock = new_quantity self.put() def update_price(self, new_price): ''' Update the price of the item ''' if new_price is None or new_price < 0: raise Exception( "Argument 'new_price' cannot be None or less than 0") self.base_price = new_price self.put() @staticmethod def get(name): return Item.query.filter_by(name=name).first() def add(self): db.session.add(self) return session_commit() def put(): return session_commit()
class Address(db.Model): id = db.Column(db.String(25), primary_key=True) email = db.Column(db.String(24)) active = db.Column(db.Integer) user_id = db.Column(db.String(24), db.ForeignKey('uuser.id')) users = db.relationship("Uuser", backref=db.backref("addresses", lazy='dynamic')) def __init__(self, id, email, user_id): self.id = id self.email = email self.user_id = user_id self.active = False
class Bidhistory(db.Model): ''' This class models a record of a bid placed, either directly by a user clicking the bid button in the front end, or by an autobidder bidding on the user's behalf. ''' id = db.Column(db.String(250), primary_key=True) transaction_time = db.Column(db.DateTime) auction_id = db.Column(db.String(250), db.ForeignKey('auction.id')) user_id = db.Column(db.String(250), db.ForeignKey('user.id')) def __init__(self, id, auction, user_id): self.id = id self.transaction_time = datetime.datetime.utcnow() self.auction = auction self.user_id = user_id def add(self): db.session.add(self) return session_commit() def put(self): return session_commit()