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)) 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() def save_to_db(self): db.session.add(self) db.session.commit() def delete_from_db(self): db.session.delete(self) db.session.commit()
class ItemModel(db.Model): __tabelname__ = 'items' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(80)) price = db.Column(db.Float(precision=2)) # store_id = db.Column(db.Integer, db.ForeignKey('stores.id')) # store = db.relationship('StoreModel') def __init__(self, name, price): self.name = name self.price = price # self.store_id = store_id def json(self): return {'name':self.name,'price':self.price} @classmethod def find_by_name(cls,name): # connection = sqlite3.connect('mydata.db') # cursor = connection.cursor() # # query = "select * from items where name=?" # result = cursor.execute(query, (name,)) # row = result.fetchone() # connection.close() # # if row: # return cls(*row) # cls(row[0],row[1]) return ItemModel.query.filter_by(name=name).first() def save_to_db(self): # def insert(self): # connection = sqlite3.connect('mydata.db') # cursor = connection.cursor() # query = "insert into items values(?,?)" # cursor.execute(query, (self.name, self.price,)) # connection.commit() # connection.close() db.session.add(self) db.session.commit() def delete_from_db(self): db.session.delete(self) db.session.commit() # def update(self): # connection = sqlite3.connect('mydata.db') # cursor = connection.cursor() # query = "update items set price=? where name=?" # cursor.execute(query, (self.price, self.name,)) # connection.commit() # connection.close()
class ItemModel(db.Model, DBActionMixin): __tablename__ = 'items' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(ITEM_NAME_LEN), unique=True) price = db.Column(db.Float(precision=2)) store_id = db.Column(db.Integer, db.ForeignKey('stores.id')) store = db.relationship('StoreModel') def __init__(self, **kwargs): super(ItemModel, self).__init__(**kwargs) def jsonify(self): return {'name': self.name, 'price': self.price} @classmethod def find_by_name(cls, name): return cls.query.filter_by(name=name).first()
class ItemModel(db.Model): __tablename__ = "tblItems" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(80)) price = db.Column(db.Float(precision=2)) store_id = db.Column(db.Integer, db.ForeignKey('tblStores.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": str(self.id), "name": self.name, "price": self.price, "store_id": self.store_id, "store": self.store.name } @classmethod def find_by_id(cls, id): return ItemModel.query.filter_by(id=int(id)).first() @classmethod def find_by_name(cls, name): return ItemModel.query.filter_by(name=name).first() def save_to_db(self): db.session.add(self) db.session.commit() def delete_from_db(self): db.session.delete(self) db.session.commit()
class ItemModel(db.Model): __tablename__ = 'items' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(255)) price = db.Column(db.Float(precision=2)) 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 @classmethod def find_by_name(cls, name): # filtering an item from the db class then returns an object return cls.query.filter_by( name=name).first() # =SELECT * FROM items WHERE name=name LIMIT 1 def save_to_db(self): # saving the item object to the database db.session.add(self) db.session.commit() def delete_from_db(self): db.session.delete(self) db.session.commit() def json(self): return { 'id': self.id, 'name': self.name, 'price': self.price, 'store_id': self.store_id }
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)) # Create a property 'store_id' for every item that matches the StoreModel object # with the same id in stores database (create a SQL table join 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 { "name": self.name, "price": self.price } @classmethod def find_by_name(cls, name): return cls.query.filter_by(name = name).first() #SELECT * FROM __tablename__ WHERE name=name LIMIT 1 def save_to_db(self): #upserting method # SQLAlchemy can translate directly form object to row db.session.add(self) db.session.commit() def delete_from_db(self): db.session.delete(self) db.session.commit()
class AlertModel(db.Model): __tablename__ = 'alerts' id = db.Column(db.Integer, primary_key=True) user = db.Column(db.Integer, db.ForeignKey('users.id')) product = db.Column(db.String(255)) price = db.Column(db.Float(precision=2)) currency = db.Column(db.String(3)) active = db.Column(db.Boolean, default=True) def __init__(self, user_id, product, price, currency): self.user = user_id self.product = product self.price = price self.currency = currency self.active = True def to_dict(self): return { 'id': self.id, 'user': self.user, 'product': self.product, 'price': self.price, 'currency': self.currency, 'active': self.active, } def add_alert(self): db.session.add(self) db.session.commit() def delete_alert(self): db.session.delete(self) db.session.commit() @classmethod def get_alerts_by_user_id(cls, user): alerts = cls.query.filter_by(user=user).all() return alerts @classmethod def get_alert_by_id(cls, _id): alert = cls.query.filter_by(id=_id).first() if alert: return alert return None @classmethod def get_all_alerts(cls): alerts = cls.query.all() return alerts @classmethod def get_all_active_alerts(cls): alerts = cls.query.filter_by(active=True).all() return alerts @classmethod def list_to_dict(cls, alertlist): return [alert.to_dict() for alert in alertlist] def update_info(self, product, price, currency): self.product = product self.price = price self.currency = currency db.session.commit() def change_active(self): self.active = not self.active db.session.commit()
class ItemModel(db.Model): __tablename__ = 'items' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(80)) # 80 characters limit price = db.Column(db.Float(precision=2)) # numbers after the comma store_id = db.Column( db.Integer, db.ForeignKey('stores.id')) # Foreign key to link items & stores store = db.relationship('StoreModel') # Now no joins necessary def __init__(self, name, price, store_id): self.name = name self.price = price self.store_id = store_id def json(self) -> dict: """ Returns the id, name, price and store_id as .json string. :return: {'id': Int, 'name': String, 'price': Int, 'store_id': Int} """ return { 'id': self.id, 'name': self.name, 'price': self.price, 'store_id': self.store_id } @classmethod def find_by_name(cls, name: str) -> object: """ Find an object by its name. :param name: Item name to find. :return: Object of Item-class. """ return cls.query.filter_by( name=name).first() # SELECT * FROM items WHERE name=name LIMIT 1 @classmethod def find_all(cls) -> dict: """ Returns all items in .db :return: All items found in .db """ return cls.query.all() def save_to_db(self) -> None: """ Insert new or update existing object in data base. """ db.session.add(self) db.session.commit() def delete_from_db(self) -> None: """ Delete object from the data base. """ db.session.delete(self) db.session.commit()