Ejemplo n.º 1
0
class Bids(SurrogatePK, Model):
    """A role for a user."""
    __tablename__ = 'bids'
    price = Column(db.DECIMAL(12), nullable=False)
    timestamp = Column(db.DECIMAL(12), nullable=False, default=int(time()))
    user_id = reference_col('users', nullable=True)
    user = relationship('User', backref='bids')

    item_id = reference_col('items', nullable=True)
    item = relationship('Items', backref='bids')

    def __init__(self, **kwargs):
        """Create instance."""
        db.Model.__init__(self, **kwargs)

    def to_dict(self):
        return {
            "id": self.id,
            "price": self.price,
            "timestamp": self.timestamp,
            "user": self.user,
            "item_id": self.item_id
        }

    @staticmethod
    def current_item_bid(item_id):
        return Bids.query.filter(Bids.item_id == item_id).order_by(
            Bids.timestamp.desc()).first()

    def __repr__(self):
        """Represent instance as a unique string."""
        return '<Bids({name})>'.format(name=self.id)
Ejemplo n.º 2
0
class Items(SurrogatePK, Model):
    """A role for a user."""

    __tablename__ = 'items'
    name = Column(db.String(80), nullable=False)
    image_url = Column(db.String(100), nullable=False)
    expiration_time = Column(db.DECIMAL(16), nullable=False)
    min_bid = Column(db.DECIMAL(8), nullable=False)
    min_increment_bid = Column(db.DECIMAL(8), nullable=False)
    status = Column(db.Integer(), default=1, nullable=False)

    def __init__(self, **kwargs):
        """Create instance."""
        db.Model.__init__(self, **kwargs)

    def to_dict(self):
        bid = Bids.current_item_bid(self.id)
        return {
            "id": self.id,
            "title": self.name,
            "image_url": self.image_url,
            "expiration_time": self.expiration_time,
            "min_bid": self.min_bid,
            "min_increment_bid": self.min_increment_bid,
            "current_bid": bid,
            "status": self.status
        }

    def __repr__(self):
        """Represent instance as a unique string."""
        return '<Items({name})>'.format(name=self.name)