class StoreModel(db.Model): __tablename__ = 'stores' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(80)) items = db.relationship('ItemModel', lazy='dynamic') def __init__(self, name): self.name = name def json(self): return { 'id': self.id, 'name': self.name, 'items': [item.json() for item in self.items.all()] } @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()
class UserModel(db.Model): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(80)) password = db.Column(db.String(80)) def __init__(self, username, password): self.username = username self.password = password def json(self): return {'id': self.id, 'username': self.username} 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 find_by_username(cls, username): return cls.query.filter_by(username=username).first() @classmethod def find_by_id(cls, _id): return cls.query.filter_by(id=_id).first()
class Policy(Base): __category__ = 'Policy' body = db.Column(db.String) handle = db.Column(db.String) title = db.Column(db.String) url = db.Column(db.String)
class Domain(Base): __category__ = 'Shop' host = db.Column(db.String) ssl_enabled = db.Column(db.Boolean) url = db.Column(db.String) shops = db.relationship('Shop', lazy=True, uselist=True, backref=db.backref('domain', uselist=True))
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() @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()
class Role(Base, DateTimeMixin): __category__ = 'User' name = db.Column(db.String(80), unique=True)
class Tag(Base, DateTimeMixin): __category__ = 'Product' name = db.Column(db.String(255), nullable=False)
from store.extensions import db metafield_self_relation = db.Table( 'metafield_relation', db.Column('my_id', db.Integer, db.ForeignKey('metafield.id')), db.Column('my_parent_id', db.Integer, db.ForeignKey('metafield.id'))) options_products_relation = db.Table( 'options_products', db.Column('product_id', db.Integer(), db.ForeignKey('product.id')), db.Column('option_id', db.Integer(), db.ForeignKey('option.id'))) tags_products_relation = db.Table( 'tags_products', db.Column('product_id', db.Integer(), db.ForeignKey('product.id')), db.Column('tag_id', db.Integer(), db.ForeignKey('tag.id'))) selected_options_products_relation = db.Table( 'selectedinput_products', db.Column('product_id', db.Integer(), db.ForeignKey('product.id')), db.Column('selectedinput_id', db.Integer(), db.ForeignKey('selectedinput.id'))) images_options_relation = db.Table( 'images_options', db.Column('variantoptinos_id', db.Integer(), db.ForeignKey('variantoptions.id')), db.Column('image_id', db.Integer(), db.ForeignKey('image.id'))) variants_options_relation = db.Table( 'variant_options',
class Variantoptions(Base): __category__ = 'Product' available_for_sale = db.Column(db.Boolean, default=False) currently_not_in_stock = db.Column(db.Boolean) requires_shipping = db.Column(db.Boolean) quantity_available = db.Column(db.Integer) compare_at_price = db.Column(db.Float) unit_price = db.Column(db.Float) weight = db.Column(db.Float) price = db.Column(db.Float) namespace = db.Column(db.String) title = db.Column(db.String) sku = db.Column(db.String) key = db.Column(db.String) selected_options = db.relationship('Option', secondary=variants_options_relation, backref=db.backref('variantoptions', lazy='dynamic')) images = db.relationship('Image', secondary=images_options_relation, backref=db.backref('variantoptions', lazy='dynamic')) metafield = db.relationship('Metafield', backref='variant_options', lazy=True, uselist=True) product = db.Column(db.Integer, db.ForeignKey('product.id'))
class NameMixin: name = db.Column(db.String()) @classmethod def get_by_name(cls, name): return cls.query.filter_by(name=name).first()
class DateTimeMixin: created_at = db.Column(db.DateTime(), default=db.func.now()) updated_at = db.Column(db.DateTime(), onupdate=db.func.now())
class Pricerange(Base): __category__ = 'Product' max_variant_price = db.Column(db.Float) min_variant_price = db.Column(db.Float) product = db.Column(db.Integer, db.ForeignKey('product.id'))
class Valuetype(Base): __category__ = 'Product' value = db.Column(db.String) metafields = db.relationship('Metafield', backref='valuetype', lazy=True)
from store.extensions import db shops_shipcountries_relation = db.Table( 'countries_shops', db.Column('shop_id', db.Integer(), db.ForeignKey('shop.id')), db.Column('country_id', db.Integer(), db.ForeignKey('country.id')))
from store.extensions import db friend_relation = db.Table( 'friend_relation', db.Column('my_id', db.Integer, db.ForeignKey('user.id')), db.Column('my_friends_id', db.Integer, db.ForeignKey('user.id'))) best_fried_relation = db.Table( 'best_fried_relation', db.Column('my_id', db.Integer, db.ForeignKey('user.id')), db.Column('my_friends_id', db.Integer, db.ForeignKey('user.id'))) roles_users_relation = db.Table( 'roles_users', db.Column('user_id', db.Integer(), db.ForeignKey('user.id')), db.Column('role_id', db.Integer(), db.ForeignKey('role.id')))
class Country(Base, DateTimeMixin): __category__ = 'Shop' name = db.Column(db.String(255), nullable=False)
class Option(Base): __category__ = 'Product' name = db.Column(db.String)
class Product(Base, DateTimeMixin): __category__ = 'Product' sku = db.Column(db.String(255), nullable=False) title = db.Column(db.String) key = db.Column(db.String(30)) # required product_type = db.Column(db.String) description = db.Column(db.String) handle = db.Column( db.String ) # auto generate from title . like Black Shirt --> Black-Shirt descriptionHtml = db.Column(db.String) vendor = db.Column(db.String) namespace = db.Column(db.String(20), nullable=False) taxable = db.Column(db.Boolean(), default=False) available_for_sale = db.Column(db.Boolean(), default=False) total_inventory = db.Column(db.Integer, nullable=False) created_at = db.Column(db.DateTime(), default=db.func.now()) updated_at = db.Column(db.DateTime(), onupdate=db.func.now(), default=db.func.now()) metafields = db.relationship('Metafield', backref='prod', uselist=True) price_range = db.relationship('Pricerange', backref='pricerange', uselist=True) options = db.relationship('Option', secondary=options_products_relation, backref=db.backref('products', lazy='dynamic')) tags = db.relationship('Tag', secondary=tags_products_relation, backref=db.backref('products', lazy='dynamic')) selected_options = db.relationship( 'Selectedinput', secondary=selected_options_products_relation, backref=db.backref('products', lazy='dynamic')) variant_by_selected_options = db.relationship( 'Variantoptions', lazy=True, uselist=True, backref=db.backref('products', uselist=True)) @classmethod def get_by_sku(cls, sku): return cls.query.filter_by(sku=sku).first()
class Selectedinput(Base): __category__ = 'Product' name = db.Column(db.String) value = db.Column(db.String)