relationship getters between models
This commit is contained in:
parent
d9f0b90474
commit
6d65763117
1 changed files with 25 additions and 1 deletions
|
|
@ -1,5 +1,5 @@
|
||||||
from app import db
|
from app import db
|
||||||
from sqlalchemy import Column, INTEGER, String
|
from sqlalchemy import Column, INTEGER, String, select
|
||||||
|
|
||||||
class Vendor(db.Model):
|
class Vendor(db.Model):
|
||||||
__tablename__ = 'vendor'
|
__tablename__ = 'vendor'
|
||||||
|
|
@ -7,11 +7,27 @@ class Vendor(db.Model):
|
||||||
name = Column('name', String(), nullable=False)
|
name = Column('name', String(), nullable=False)
|
||||||
bc_id = Column('bc_id', INTEGER(), nullable=True)
|
bc_id = Column('bc_id', INTEGER(), nullable=True)
|
||||||
|
|
||||||
|
def get_budget_category(self):
|
||||||
|
return db.session.execute(select(BudgetCategory).where(BudgetCategory.id==self.bc_id)).first()
|
||||||
|
|
||||||
|
def get_line_items(self):
|
||||||
|
return db.session.execute(select(LineItem).where(LineItem.vendor_id==self.id)).all()
|
||||||
|
|
||||||
class BudgetCategory(db.Model):
|
class BudgetCategory(db.Model):
|
||||||
__tablename__ = 'budget_category'
|
__tablename__ = 'budget_category'
|
||||||
id = Column('id', INTEGER(), primary_key=True, autoincrement=True)
|
id = Column('id', INTEGER(), primary_key=True, autoincrement=True)
|
||||||
name = Column('name', String(), nullable=False)
|
name = Column('name', String(), nullable=False)
|
||||||
|
|
||||||
|
def get_vendors(self):
|
||||||
|
return db.session.execute(select(Vendor).where(Vendor.bc_id==self.id)).all()
|
||||||
|
|
||||||
|
def get_line_items(self):
|
||||||
|
line_items = []
|
||||||
|
vendors = self.get_vendors()
|
||||||
|
for vendor in vendors:
|
||||||
|
line_items += vendor.get_line_items()
|
||||||
|
return line_items
|
||||||
|
|
||||||
class LineItem(db.Model):
|
class LineItem(db.Model):
|
||||||
__tablename__ = 'line_item'
|
__tablename__ = 'line_item'
|
||||||
id = Column('id', INTEGER(), primary_key=True, autoincrement=True)
|
id = Column('id', INTEGER(), primary_key=True, autoincrement=True)
|
||||||
|
|
@ -23,3 +39,11 @@ class LineItem(db.Model):
|
||||||
confirmation_code = Column('confirmation_code', INTEGER(), nullable=True)
|
confirmation_code = Column('confirmation_code', INTEGER(), nullable=True)
|
||||||
note = Column('note', String(), nullable=True)
|
note = Column('note', String(), nullable=True)
|
||||||
|
|
||||||
|
def get_parent_line(self):
|
||||||
|
return db.session.execute(select(LineItem).where(LineItem.id==self.parent_line_item_id)).first()
|
||||||
|
|
||||||
|
def get_children_lines(self):
|
||||||
|
return db.session.execute(select(LineItem).where(LineItem.parent_line_item_id==self.id)).all()
|
||||||
|
|
||||||
|
def get_vendor(self):
|
||||||
|
return db.session.execute(select(Vendor).where(Vendor.id==self.vendor_id)).first()
|
||||||
Loading…
Add table
Add a link
Reference in a new issue