class Article(db.Model): title = db.Column(db.String(100)) slug = db.Column(db.String(100)) publish_date = db.Column(db.DateTime) last_updated = db.Column(db.DateTime, nullable=True) file_path = db.Column(db.String(255), nullable=True) header_image = db.Column(db.String(255), nullable=True) preview = db.Column(db.Text) html = db.Column(db.Text) author_id = db.foreign_key('User') author = db.relationship('User', back_populates='articles') article_series_id = db.foreign_key('SeriesArticle', nullable=True) article_series = db.relationship('SeriesArticle', back_populates='article', cascade='all, delete-orphan', single_parent=True) series = db.association_proxy( 'article_series', 'series', creator=lambda series: SeriesArticle(series=series)) part = db.association_proxy('article_series', 'part') category_id = db.foreign_key('Category', nullable=True) category = db.relationship('Category', back_populates='articles') article_tags = db.relationship('ArticleTag', back_populates='article', cascade='all, delete-orphan') tags = db.association_proxy('article_tags', 'tag', creator=lambda tag: ArticleTag(tag=tag)) __repr_props__ = ('id', 'title')
class AssetDataVendor(db.Model): """Join table between Asset and DataVendor""" asset_id = db.foreign_key('Asset', primary_key=True) asset = db.relationship('Asset', back_populates='asset_data_vendors') data_vendor_id = db.foreign_key('DataVendor', primary_key=True) data_vendor = db.relationship('DataVendor', back_populates='data_vendor_assets') # vendor-specific ticker (if different from canonical ticker on Asset) _ticker = db.Column('ticker', db.String(16), nullable=True) __repr_props__ = ('asset_id', 'data_vendor_id', 'ticker') def __init__(self, asset=None, data_vendor=None, **kwargs): super().__init__(**kwargs) if asset: self.asset = asset if data_vendor: self.data_vendor = data_vendor @db.hybrid_property def ticker(self): return self._ticker or self.asset.ticker @ticker.setter def ticker(self, ticker): self._ticker = ticker
class IndexDataVendor(db.Model): """Join table between Index and DataVendor""" class Meta: repr = ('index_id', 'data_vendor_id', 'ticker') index_id = db.foreign_key('Index', primary_key=True) index = db.relationship('Index', back_populates='index_data_vendors') data_vendor_id = db.foreign_key('DataVendor', primary_key=True) data_vendor = db.relationship('DataVendor', back_populates='data_vendor_indexes') # vendor-specific index ticker (if different from canonical index ticker) _ticker = db.Column('ticker', db.String(16), nullable=True) def __init__(self, index=None, data_vendor=None, **kwargs): super(IndexDataVendor, self).__init__(**kwargs) if index: self.index = index if data_vendor: self.data_vendor = data_vendor @db.hybrid_property def ticker(self): return self._ticker or self.index.ticker @ticker.setter def ticker(self, ticker): self._ticker = ticker
class DataItemVendor(db.Model): """ join table between DataItem and DataVendor """ class Meta: pk = None data_item_id = db.foreign_key('DataItem', primary_key=True) data_item = db.relationship('DataItem', back_populates='data_item_vendors') data_vendor_id = db.foreign_key('DataVendor', primary_key=True) data_vendor = db.relationship('DataVendor', back_populates='data_vendor_items') priority = db.Column(db.Integer, nullable=True) def __init__(self, data_item=None, data_vendor=None, priority=None, **kwargs): super().__init__(priority=priority, **kwargs) if data_item: self.data_item = data_item if data_vendor: self.data_vendor = data_vendor
class Equity(Asset): class Meta: repr = ('id', 'ticker') __mapper_args__ = { 'polymorphic_identity': AssetType.Equity, } id = db.foreign_key('Asset', primary_key=True) company_name = db.Column(db.String, index=True) company_description = db.Column(db.Text, nullable=True) equity_indexes = db.relationship('EquityIndex', back_populates='equity', cascade='all, delete-orphan') indexes = db.association_proxy( 'equity_indexes', 'index', creator=lambda equity: EquityIndex(equity=equity)) sector_id = db.foreign_key('Sector', nullable=True) sector = db.relationship('Sector', back_populates='equities') industry_id = db.foreign_key('Industry', nullable=True) industry = db.relationship('Industry', back_populates='equities')
class Asset(db.Model): """ Base class for tradable assets. Should not be used directly. """ class Meta: repr = ('id', 'type', 'ticker') type = db.Column(db.Enum(AssetType)) # polymorphic discriminator column __mapper_args__ = { 'polymorphic_on': type, 'polymorphic_identity': AssetType.Asset, } # canonical ticker ticker = db.Column(db.String(16), index=True, unique=True) asset_data_vendors = db.relationship('AssetDataVendor', back_populates='asset', cascade='all, delete-orphan') data_vendors = db.association_proxy( 'asset_data_vendors', 'data_vendor', creator=lambda data_vendor: AssetDataVendor(data_vendor=data_vendor)) asset_watchlists = db.relationship('WatchlistAsset', back_populates='asset') market_id = db.foreign_key('Market') market = db.relationship('Market', back_populates='assets') country = db.association_proxy('market', 'country') currency = db.association_proxy('market', 'currency') exchange = db.association_proxy('market', 'exchange')
class DataVendor(db.Model): class Meta: repr = ('id', 'key', 'name') key = db.Column(db.String(16), index=True, unique=True) name = db.Column(db.String(64), index=True, unique=True) priority = db.Column(db.Integer) data_vendor_assets = db.relationship('AssetDataVendor', back_populates='data_vendor') assets = db.association_proxy( 'data_vendor_assets', 'asset', creator=lambda asset: AssetDataVendor(asset=asset)) data_vendor_indexes = db.relationship('IndexDataVendor', back_populates='data_vendor') indexes = db.association_proxy( 'data_vendor_indexes', 'index', creator=lambda index: IndexDataVendor(index=index)) data_vendor_items = db.relationship('DataItemVendor', back_populates='data_vendor') data_items = db.association_proxy('data_vendor_items', 'data_item')
class Sector(db.Model): class Meta: repr = ('id', 'name') name = db.Column(db.String(32), index=True, unique=True) equities = db.relationship('Equity', back_populates='sector') industries = db.relationship('Industry', back_populates='sector')
class Exercise(db.Model): class Meta: polymorphic = True workout_id = db.foreign_key("Workout") workout = db.relationship("Workout", back_populates='exercises') exercise_type_id = db.foreign_key("ExerciseType") exercise_type = db.relationship("ExerciseType", back_populates='exercises')
class Workout(db.Model): date = db.Column(db.Date) user_id = db.foreign_key("User") user = db.relationship("User", back_populates="workouts") location_id = db.foreign_key("Location") location = db.relationship("Location", back_populates="workouts") exercises = db.relationship('Exercise', back_populates='workout')
class Market(db.Model): abbrev = db.Column(db.String(10), index=True, unique=True) name = db.Column(db.String(64), index=True, unique=True) exchange_id = db.foreign_key('Exchange') exchange = db.relationship('Exchange', back_populates='markets') assets = db.relationship('Asset', back_populates='market') __repr_props__ = ('id', 'abbrev', 'name')
class Industry(db.Model): class Meta: repr = ('id', 'name', 'sector') name = db.Column(db.String(64), index=True, unique=True) equities = db.relationship('Equity', back_populates='industry') sector_id = db.foreign_key('Sector', nullable=True) sector = db.relationship('Sector', back_populates='industries')
class AssetDataVendor(db.Model): """Join table between Asset and DataVendor""" class Meta: repr = ('asset_id', 'data_vendor_id', 'ticker') asset_id = db.foreign_key('Asset', primary_key=True) asset = db.relationship('Asset', back_populates='asset_data_vendors') data_vendor_id = db.foreign_key('DataVendor', primary_key=True) data_vendor = db.relationship('DataVendor', back_populates='data_vendor_assets') # vendor-specific ticker (if different from canonical ticker) _ticker = db.Column('ticker', db.String(16), nullable=True) minutely_last_updated = db.Column(db.DateTime(), nullable=True) daily_last_updated = db.Column(db.DateTime(), nullable=True) weekly_last_updated = db.Column(db.DateTime(), nullable=True) monthly_last_updated = db.Column(db.DateTime(), nullable=True) def __init__(self, asset=None, data_vendor=None, **kwargs): super(AssetDataVendor, self).__init__(**kwargs) if asset: self.asset = asset if data_vendor: self.data_vendor = data_vendor @db.hybrid_property def ticker(self): return self._ticker or self.asset.ticker @ticker.setter def ticker(self, ticker): self._ticker = ticker def last_updated(self, frequency: Frequency): if frequency == Frequency.Monthly: return self.monthly_last_updated elif frequency == Frequency.Weekly: return self.weekly_last_updated elif frequency == Frequency.Daily: return self.daily_last_updated return self.minutely_last_updated def set_last_updated(self, frequency: Frequency, time=None): time = time or utcnow() if frequency == Frequency.Monthly: self.monthly_last_updated = time elif frequency == Frequency.Weekly: self.weekly_last_updated = time elif frequency == Frequency.Daily: self.daily_last_updated = time else: self.minutely_last_updated = time
class Category(db.Model): name = db.Column(db.String(32)) slug = db.Column(db.String(32)) articles = db.relationship('Article', back_populates='category') series = db.relationship('Series', back_populates='category') __repr_props__ = ('id', 'name') def __init__(self, name, **kwargs): super().__init__(**kwargs) self.name = name
class WatchlistAsset(db.Model): asset_id = db.foreign_key('Asset', primary_key=True) asset = db.relationship('Asset', back_populates='asset_watchlists') watchlist_id = db.foreign_key('Watchlist', primary_key=True) watchlist = db.relationship('Watchlist', back_populates='watchlist_assets') def __init__(self, asset=None, watchlist=None, **kwargs): super().__init__(**kwargs) if asset is not None: self.asset = asset if watchlist is not None: self.watchlist = watchlist
class Index(db.Model): class Meta: repr = ('id', 'ticker', 'name') ticker = db.Column(db.String(16), index=True, unique=True) name = db.Column(db.String(64), index=True, unique=True) index_data_vendors = db.relationship('IndexDataVendor', back_populates='index') index_equities = db.relationship('EquityIndex', back_populates='index', cascade='all, delete-orphan') equities = db.association_proxy('index_equities', 'equity', creator=lambda equity: EquityIndex(equity=equity))
class Market(db.Model): class Meta: repr = ('id', 'abbrev', 'name') abbrev = db.Column(db.String(16)) name = db.Column(db.String(64)) assets = db.relationship('Asset', back_populates='market') country_id = db.foreign_key('Country') country = db.relationship('Country', back_populates='markets') currency = db.association_proxy('country', 'currency') exchange_id = db.foreign_key('Exchange') exchange = db.relationship('Exchange', back_populates='markets')
class OneUser(db.Model): class Meta: lazy_mapped = True name = db.Column(db.String) roles = db.relationship('OneRole', back_populates='user')
class Parent(db.Model): class Meta: repr = ('id', 'name') name = db.Column(db.String) children = db.relationship('Child', back_populates='parent')
class Watchlist(db.Model): class Meta: repr = ('id', 'name', 'user') name = db.Column(db.String, unique=True, index=True) user_id = db.foreign_key('User') user = db.relationship('User', back_populates='watchlists') watchlist_assets = db.relationship('WatchlistAsset', back_populates='watchlist', cascade='all, delete-orphan') assets = db.association_proxy( 'watchlist_assets', 'asset', creator=lambda asset: WatchlistAsset(asset=asset))
class Exchange(db.Model): abbrev = db.Column(db.String(10), index=True, unique=True) name = db.Column(db.String(64), index=True, unique=True) markets = db.relationship('Market', back_populates='exchange') __repr_props__ = ('id', 'abbrev', 'name')
class OneRelationship(db.Model): class Meta: lazy_mapped = True name = db.Column(db.String) backrefs = db.relationship('OneBackref', backref=db.backref('relationship'))
class OneParent(db.Model): class Meta: lazy_mapped = True # relationships = {'OneChild': 'children'} name = db.Column(db.String) children = db.relationship('OneChild', back_populates='parent')
class Child(db.Model): class Meta: repr = ('id', 'name') name = db.Column(db.String) parent_id = db.foreign_key('Parent') parent = db.relationship('Parent', back_populates='children')
class EquityIndex(db.Model): """Join table between Equity and Index""" class Meta: repr = ('equity', 'index') equity_id = db.foreign_key('Equity', primary_key=True) equity = db.relationship('Equity', back_populates='equity_indexes') index_id = db.foreign_key('Index', primary_key=True) index = db.relationship('Index', back_populates='index_equities') def __init__(self, equity=None, index=None, **kwargs): super().__init__(**kwargs) if equity: self.equity = equity if index: self.index = index
class User(BaseUser): username = db.Column(db.String(64), unique=True, index=True) first_name = db.Column(db.String(64)) last_name = db.Column(db.String(64)) articles = db.relationship('Article', back_populates='author') __repr_props__ = ('id', 'username', 'email')
class OneRole(db.Model): class Meta: lazy_mapped = True name = db.Column(db.String) user_id = db.foreign_key('OneUser') user = db.relationship('OneUser', back_populates='roles')
class Exchange(db.Model): class Meta: repr = ('id', 'abbrev', 'name') abbrev = db.Column(db.String(16), unique=True) name = db.Column(db.String(64), unique=True) markets = db.relationship('Market', back_populates='exchange')
class Index(db.Model): name = db.Column(db.String(64), index=True, unique=True) ticker = db.Column(db.String(16), index=True, unique=True) equities = db.relationship('Equity', secondary=index_equities, lazy='subquery', back_populates='indexes')
class User(BaseUser): class Meta: repr = ('id', 'username', 'email') username = db.Column(db.String(64), unique=True, index=True) first_name = db.Column(db.String(64)) last_name = db.Column(db.String(64)) watchlists = db.relationship('Watchlist', back_populates='user')