class Scheduler(Model): """ .base.Model provides: id (primary key) created_at (creation date) .address.Address.Model provides: addresses (collection through secondary table) """ userprofile_id = db.Column(db.Integer, db.ForeignKey('userprofile.id')) userprofile = db.relationship("UserProfile", back_populates='scheduler') appointments = db.relationship('Appointment', backref='scheduler', lazy=True) manager_id = db.Column(db.Integer, db.ForeignKey('manager.id')) active = db.Column(db.Boolean, default=False)
class Provider(Model): """ .base.Model provides: id (primary key) created_at (creation date) .appointment.Appointment.Model provides appointments (through secondary table) """ records = db.relationship('Record', backref='provider', lazy=True) userprofile_id = db.Column(db.Integer, db.ForeignKey('userprofile.id')) userprofile = db.relationship("UserProfile", back_populates='provider') manager_id = db.Column(db.Integer, db.ForeignKey('manager.id')) active = db.Column(db.Boolean, default=False)
class Measurement(Model): """ Base Model Provides: id (primary key) created_at (creation date) """ name = db.Column(db.String(256), nullable=False) description = db.Column(db.String(2000), nullable=True) value = db.Column(db.String(256), nullable=False) units = db.Column(db.String(256), nullable=False) valid = db.Column(db.Boolean, nullable=False, default=True) record_id = db.Column(db.Integer, db.ForeignKey('record.id'), nullable=False) def __repr__(self): return f'<Measurement {self.id} with record_id: {self.record_id} value: {self.value} units: {self.units}>' @property def dictionary(self): return { 'id': self.id, 'created_at': self.created_at, 'name': self.name, 'description': self.description, 'value': self.value, 'units': self.units, 'valid': self.valid, 'record_id': self.record.id, }
class Record(Model): """ Base Model Provides: id (primary key) created_at (creation date) """ patron_id = db.Column(db.Integer, db.ForeignKey('patron.id'), nullable=False) measurements = db.relationship('Measurement', backref='measurements', lazy=True) appointment_id = db.Column(db.Integer, db.ForeignKey('appointment.id')) provider_id = db.Column(db.Integer, db.ForeignKey('provider.id')) def __repr__(self): return f'<Record {self.id} ' \ f'patron_id: {self.patron_id} ' \ f'provider_id: {self.provider_id} ' \ f'appointment_id: {self.appointment_id}>' @property def measurements(self): return Measurement.query.filter(Measurement.record_id == self.id) \ .order_by(Measurement.created_at) @property def measurements_serialized(self): return [measurement.dictionary for measurement in self.measurements] @property def number_of_measurements(self): return self.measurements.count() @property def dictionary(self): return { 'id': self.id, 'created_at': self.created_at, 'patron': self.patron.dicrionary, 'provider': self.provider.dictionary, 'measurement_count': self.number_of_measurements, 'measurements': self.measurements_serialized, 'appointment': self.appointment, }
class Appointment(Model): """ .base.Model provides: id (primary key) created_at (creation date) """ name = db.Column(db.String(256), nullable=False) records = db.relationship('Record', backref='appointment', lazy=True) confirmed = db.Column(db.Boolean, default=False, nullable=False) held = db.Column(db.Boolean, default=False, nullable=False) cancelled = db.Column(db.Boolean, default=False, nullable=False) scheduler_id = db.Column(db.Integer, db.ForeignKey('scheduler.id'), nullable=False) address_id = db.Column(db.Integer, db.ForeignKey('address.id')) providers = db.relationship("Provider", secondary=provider_appointment_association_table, backref="appointments") @declared_attr def start_time(self): return db.Column(ArrowType, default=utcnow, nullable=False, index=True) def __repr__(self): return f'<Appointment {self.id} with start_time: {self.start_time.isoformat()}>' @property def all_records(self): return Record.query.filter(Record.appointment_id == self.id) \ .order_by(Record.created_at) @property def all_records_serialized(self): return [record.dictionary for record in self.all_records] @property def dictionary(self): return { 'id': self.id, 'created_at': self.created_at, 'name': self.name, 'address': self.address.dictionary, 'start_time': self.start_time.isoformat(), 'confirmed': self.confirmed, 'cancelled': self.cancelled, 'held': self.held, 'records': self.all_records_serialized, }
class Patron(Model): """ .base.Model provides: id (primary key) created_at (creation date) """ records = db.relationship('Record', backref='patron', lazy=True) # 1 to many userprofile_id = db.Column(db.Integer, db.ForeignKey('userprofile.id')) userprofile = db.relationship("UserProfile", back_populates='patron') def __repr__(self): return f'<Patron {self.id}: user_id: {self.user}>' @property def dictionary(self): return { 'id': self.id, 'created_at': str(self.created_at.to(TIMEZONE)), 'records': self.id, 'userprofile_id': self.userprofile_id, 'userprofile': self.userprofile.dictionary, }
from .base import Model from mymed.db import db __all__ = ('Address',) user_address_association_table = db.Table('user_address_association', Model.metadata, db.Column('address_id', db.Integer, db.ForeignKey('address.id')), db.Column('userprofile_id', db.Integer, db.ForeignKey('userprofile.id')), ) scheduler_address_association_table = db.Table('scheduler_address_association', Model.metadata, db.Column('address_id', db.Integer, db.ForeignKey('address.id')), db.Column('scheduler_id', db.Integer, db.ForeignKey('scheduler.id')), ) manager_address_association_table = db.Table('manager_address_association', Model.metadata, db.Column('address_id', db.Integer, db.ForeignKey('address.id')), db.Column('manager_id', db.Integer, db.ForeignKey('manager.id')), ) class Address(Model): """ Base Model Provides: id (primary key) created_at (creation date) """ nickname = db.Column(db.String(), nullable=False) line1 = db.Column(db.String(), nullable=False)
from arrow import utcnow from sqlalchemy_utils import ArrowType from sqlalchemy.ext.declarative import declared_attr from .base import Model from .record import Record from mymed.db import db __all__ = ('Appointment',) provider_appointment_association_table = db.Table('provider_appointment_association', Model.metadata, db.Column('provider_id', db.Integer, db.ForeignKey('provider.id')), db.Column('appointment_id', db.Integer, db.ForeignKey('appointment.id')), ) class Appointment(Model): """ .base.Model provides: id (primary key) created_at (creation date) """ name = db.Column(db.String(256), nullable=False) records = db.relationship('Record', backref='appointment', lazy=True) confirmed = db.Column(db.Boolean, default=False, nullable=False) held = db.Column(db.Boolean, default=False, nullable=False) cancelled = db.Column(db.Boolean, default=False, nullable=False) scheduler_id = db.Column(db.Integer, db.ForeignKey('scheduler.id'), nullable=False) address_id = db.Column(db.Integer, db.ForeignKey('address.id'))