Ejemplo n.º 1
0
class TempModel(db.Model):
    __tablename__ = "temps"

    uid = db.Column(db.Integer, primary_key=True)
    timestamp = db.Column(db.String(80))
    value = db.Column(db.Float(precision=4))

    def __init__(self, timestamp, value):
        self.timestamp = timestamp
        self.value = value

    def json(self):
        return {
            "uid": self.uid,
            "timestamp": self.timestamp,
            "value": self.value
        }

    @classmethod
    def find_by_uid(cls, uid):
        return cls.query.filter_by(
            uid=uid).first()  # SELECT * FROM items WHERE name=name, limit=1

    def save_to_db(self):
        db.session.add(self)
        db.session.commit()

    def delete_from_db(self):
        db.session.delete(self)
        db.session.commit()

    @classmethod
    def return_all(cls):
        return cls.query.order_by().all()
Ejemplo n.º 2
0
class ItemModel(db.Model):
    __tablename__ = 'items'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(80))
    price = db.Column(db.Float(precision=2))

    # Relationship
    store_id = db.Column(db.Integer, db.ForeignKey('stores.id'))
    store = db.relationship('StoreModel')

    def __init__(self, name, price, store_id):
        self.name = name
        self.price = price
        self.store_id = store_id

    def json(self):
        return {
            'id': self.id,
            'name': self.name,
            'price': self.price,
            'store_id': self.store_id
        }

    @classmethod
    def find_by_name(cls, name):
        return cls.query.filter_by(name=name).first()

    @classmethod
    def find_all(cls):
        return cls.query.all()

    def save_to_db(self):
        db.session.add(self)
        db.session.commit()

    def delete_from_db(self):
        db.session.delete(self)
        db.session.commit()