class Compound(db.Model): # 複方 __tablename__ = 'compound' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(50)) description = db.Column(db.String(1000)) source = db.Column(db.String(20)) group = db.Column(db.Integer) medicine = db.relationship('Medicine', secondary=medicine_compound, backref='compound', cascade='all') def __repr__(self): return "Compound(%r)" % self.name
class Medicine(db.Model): # 中藥 __tablename__ = 'medicine' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(20), nullable=False, unique=True) radical = db.Column(db.String(20)) alias = db.relationship('Alias', backref='medicine', cascade='all, delete-orphan') property = db.relationship('Property', backref='medicine', cascade='all, delete-orphan') # compound = db.relationship('Compound', # secondary=medicine_compound, # backref='medicine', # cascade='all') def __init__(self, name, radical='', alias=[], property=[], compound=[]): self.name = name self.radical = radical self.alias = alias self.property = property self.compound = compound @staticmethod def get(name): me = Medicine.query.filter_by(name=name).first() if me is None: me = Medicine(name=name) return me def save_or_update(self): with session_scope() as session: me = Medicine.query.filter_by(name=self.name).first() if me is not None: me.name = self.name me.radical = self.radical me.alias = self.alias me.property = self.property me.compound = self.compound db.session.add(me) else: session.add(self) def __repr__(self): return "Medicine(%r, %r)" % (self.name, self.radical)
class Alias(db.Model): # 別名 __tablename__ = 'alias' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(20), nullable=False) medicine_id = db.Column(db.Integer, db.ForeignKey('medicine.id'), nullable=False) @staticmethod def get(name, medicine_id=None): alias = None if medicine_id is None: alias = Alias.query.filter_by(name=name).first() else: alias = Alias.query.filter_by(name=name, medicine_id=medicine_id).first() return alias def __repr__(self): return "Alias(%r)" % self.name
from medicine.flask_app import db # from medicine.models import medicine, compound medicine_compound = db.Table( 'medicine_compound', db.Column('medicine_id', db.Integer, db.ForeignKey('medicine.id'), primary_key=True), db.Column('compound_id', db.Integer, db.ForeignKey('compound.id'), primary_key=True))
class Property(db.Model): # 性 __tablename__ = 'property' id = db.Column(db.Integer, primary_key=True) position = db.Column(db.String(20)) # 部位 cold = db.Column(db.Integer) # 寒 cool = db.Column(db.Integer) # 涼 warm = db.Column(db.Integer) # 溫 hot = db.Column(db.Integer) # 熱 neutral = db.Column(db.Integer) # 平 sour = db.Column(db.Integer) # 酸 sweet = db.Column(db.Integer) # 甘 bitter = db.Column(db.Integer) # 苦 salty = db.Column(db.Integer) # 鹹 pungent = db.Column(db.Integer) # 辣 poison = db.Column(db.Integer) # 毒 medicine_id = db.Column(db.Integer, db.ForeignKey('medicine.id')) # medicine = db.relationship("Medicine", back_populates='property') @staticmethod def get(position, medicine_id=None): property = None if medicine_id is not None: property = Property.query.filter_by(position=position, medicine_id=medicine_id).first() return property def __repr__(self): return "Property(%r, %r)" % (self.position, self.medicine_id)