class MedicalOffice(db.Model): __tablename__ = 'medical_office' office_id = db.Column(db.Integer, primary_key=True, nullable=False, unique=True) owner_id = db.Column(UUID(as_uuid=True), db.ForeignKey('user.user_id'), nullable=False) office_name = db.Column(db.String(50), nullable=False) location_id = db.Column(db.Integer, db.ForeignKey('location.location_id'), nullable=True) office_phone_number = db.Column(db.String(15), nullable=False) def __init__(self, **args): self.office_id = args.get('office_id') self.owner_id = args.get('owner_id') self.office_name = args.get('office_name') self.location_id = args.get('location_id') self.office_phone_number = args.get('office_phone_number') @staticmethod def getAllMedicalOffices(): return MedicalOffice().query.all() @staticmethod def getMedicalOfficesbyOwnerId(oid): return MedicalOffice().query.filter_by(owner_id=oid).all() @staticmethod def getMedicalOfficeById(oid): return MedicalOffice().query.filter_by(office_id=oid).first() @staticmethod def getMedicalOfficeByAddressId(aid): return MedicalOffice().query.filter_by(address_id=aid).first() def create(self): db.session.add(self) db.session.commit() return self
class Location(db.Model): REQUIRED_PARAMETERS = {'lattitude', 'longitude'} __tablename__ = 'location' location_id = db.Column(db.Integer, unique=True, nullable = False, primary_key=True) lattitude = db.Column(db.Float, nullable = False) longitude = db.Column(db.Float, nullable = False) closest_address_id = db.Column(db.Integer, db.ForeignKey('address.address_id'), nullable = True) def __init__(self, **args): self.lattitude = args.get('lattitude') self.longitude = args.get('longitude') self.closest_address_id = args.get('closest_address_id') @property def pk(self): return self.location_id @staticmethod def getLocationsWithinOneKilometerRadius(json): return Location().query.filter((func.degrees( func.acos( func.sin(func.radians(json['lattitude'])) * func.sin(func.radians(Location.lattitude)) + func.cos(func.radians(json['lattitude'])) * func.cos(func.radians(Location.lattitude)) * func.cos(func.radians(json['longitude']-Location.longitude)) ) ) * 60 * 1.1515 * 1.609344) <= 1).all() @staticmethod def getAllLocations(): return Location().query.all() @staticmethod def getLocationById(lid): return Location().query.filter_by(location_id=lid).first() @staticmethod def getLocationByLattitudeAndLongitude(json): return Location().query.filter_by(lattitude = json['lattitude'], longitude = json['longitude']).first() @staticmethod def getLocationsRelativeToAddress(aid): return Location().query.filter_by(closest_address_id=aid).all() def create(self): db.session.add(self) db.session.commit() return self
class Appointment(db.Model): __tablename__ = 'appointment' appt_id = db.Column(db.Integer, primary_key=True) start_time = db.Column(db.String(50), nullable=False) end_time = db.Column(db.String(50), nullable=False) confirmation_number = db.Column(db.Integer, nullable=True) business_id = db.Column(db.Integer, db.ForeignKey('businesses.business_id'), nullable=False) business_name = db.Column(db.String(50), nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('users.user_id'), nullable=True) number_of_customers = db.Column(db.Integer, nullable=True) def __init__(self, **args): self.start_time = args.get('start_time') self.end_time = args.get('end_time') self.confirmation_number = args.get('confirmation_number') self.business_id = args.get('business_id') self.business_name = args.get('business_name') self.user_id = args.get('user_id') self.number_of_customers = args.get('number_of_customers') @property def primaryKey(self): return self.appt_id @staticmethod def getAppointments(): return Appointment().query.all() @staticmethod def getApptById(aid): return Appointment().query.filter_by(appt_id=aid).first() @staticmethod def getApptByUser(uid): return Appointment().query.filter_by(user_id=uid).all() @staticmethod def getApptByBusiness(bid): return Appointment().query.filter_by(business_id=bid).all() @staticmethod def getApptByStartTime(dt): return Appointment().query.filter_by(start_time=dt).all() def create(self): db.session.add(self) db.session.commit() return self @staticmethod def updateAppt(aid, uid): appt = Appointment.getApptById(aid) appt.user_id = uid appt.confirmation_number = randint(0,1000) db.session.commit() return appt @staticmethod def cancelAppt(aid): appt = Appointment.getApptById(aid) appt.user_id = None appt.confirmation_number = None db.session.commit() return appt
class VisitedLocation(db.Model): REQUIRED_PARAMETERS = {'user_id', 'location_id', 'date_visited'} __tablename__ = 'visited_location' __table_args__ = (db.UniqueConstraint('user_id', 'location_id', 'date_visited'), ) user_id = db.Column(UUID(as_uuid=True), db.ForeignKey('user.user_id'), nullable=False, primary_key=True) location_id = db.Column(db.Integer, db.ForeignKey('location.location_id'), nullable=False, primary_key=True) date_visited = db.Column(db.Date, nullable=False, primary_key=True) def __init__(self, **args): self.user_id = args.get('user_id') self.location_id = args.get('location_id') self.date_visited = args.get('date_visited') '''Value object: we have to search a specific visited location by it's attributes''' @staticmethod def getSpecificVisitedLocation(uid, lid, date): return VisitedLocation().query.filter_by(user_id=uid, location_id=lid, date_visited=date).first() '''Retrieve individuals who visited the location with the specified id''' @staticmethod def getVisitedLocationByLocationId(lid): return VisitedLocation().query.filter_by(location_id=lid).all() @staticmethod def getAllVisitedLocations(): return VisitedLocation().query.all() @staticmethod def getLocationsVisitedByUserId(uid): return VisitedLocation().query.filter_by(user_id=uid).join( Location, Location.location_id == VisitedLocation.location_id).all() @staticmethod def getVisitedLocationsRelativeToAddress(aid): return VisitedLocation().query.join( Location, Location.location_id == VisitedLocation.location_id).filter_by( closest_address_id=aid).all() def create(self): db.session.add(self) db.session.commit() return self @staticmethod def deleteVisitedLocation(uid, lid, date): visited_location = VisitedLocation.getSpecificVisitedLocation( uid, lid, date) if not visited_location: return None db.session.delete(visited_location) db.session.commit() return visited_location
class Businesses(db.Model): __tablename__ = 'businesses' business_id = db.Column(db.Integer, primary_key=True) business_name = db.Column(db.String(100), nullable=False) address = db.Column(db.String(150), nullable=False) city = db.Column(db.String(50), nullable=False) zip_code = db.Column(db.String(5), nullable=False) business_email = db.Column(db.String(150), nullable=False) business_phone = db.Column(db.String(12), nullable=False) max_capacity = db.Column(db.Integer, nullable=False) owner_id = db.Column(db.Integer, db.ForeignKey('users.user_id'), nullable=False) service_id = db.Column(db.Integer, db.ForeignKey('services.service_id'), nullable=True) appointment = db.relationship("Appointment") def __init__(self, **args): self.business_name = args.get('business_name') self.business_email = args.get('business_email') self.business_phone = args.get('business_phone') self.address = args.get('address') self.city = args.get('city') self.zip_code = args.get('zip_code') self.max_capacity = args.get('max_capacity') self.owner_id = args.get('owner_id') self.service_id = args.get('service_id') @property def primaryKey(self): return self.business_id @staticmethod def getBusinesses(): return Businesses().query.all() @staticmethod def getBusinessById(bid): return Businesses().query.filter_by(business_id=bid).first() @staticmethod def getAllBusinessesByCity(ct): return Businesses().query.filter_by(city=ct).all() @staticmethod def getAllBusinessesByZipCode(zc): return Businesses().query.filter_by(zip_code=zc).all() @staticmethod def getAllBusinessesByMaxCapacity(mc): return db.session.query(Businesses).filter(Businesses.max_capacity>=mc) @staticmethod def getBusinessesByService(sid): return Businesses().query.filter_by(service_id=sid).all() @staticmethod def getBusinessByOwner(uid): return Businesses().query.filter_by(owner_id=uid).first() def create(self): db.session.add(self) db.session.commit() return self
class Patient(db.Model): REQUIRED_PARAMETERS = {'user_id', 'office_id'} __tablename__ = 'patient' __table_args__ = (db.UniqueConstraint('user_id', 'office_id'), ) user_id = db.Column(UUID(as_uuid=True), db.ForeignKey('user.user_id'), nullable=False, primary_key=True) office_id = db.Column(db.Integer, db.ForeignKey('medical_office.office_id'), nullable=False, primary_key=True) date_registered = db.Column(db.Date, nullable=False) has_died = db.Column(db.Boolean, default=False) def __init__(self, **args): self.user_id = args.get('user_id') self.office_id = args.get('office_id') self.date_registered = args.get('date_registered') @staticmethod def getAllPatients(): return Patient().query.all() @staticmethod def getPatientsByOfficeId(oid): return Patient().query.filter_by(office_id=oid).all() '''Retrieves a single individual that may have a record in multiple offices''' @staticmethod def getPatientByUserId(pid): return Patient().query.filter_by(user_id=pid).all() '''Retrieves a specific patient record in an office''' @staticmethod def getPatientByOfficeAndUserId(oid, uid): return Patient().query.filter_by(user_id=uid, office_id=oid).first() @staticmethod def getDeathPatients(): return Patient().query.filter_by(has_died=True).all() def create(self): self.date_registered = datetime.now() db.session.add(self) db.session.commit() return self @staticmethod def deletePatient(oid, uid): patient = Patient.getPatientByOfficeAndUserId(oid, uid) if not patient: return None db.session.delete(patient) db.session.commit() return patient
class User(db.Model): REQUIRED_PARAMETERS = { 'gender_id', 'address_id', 'full_name', 'birthdate', 'phone_number', 'email', 'password', 'active' } __tablename__ = 'user' user_id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4, unique=True, nullable=False) gender_id = db.Column( db.Integer, nullable=False) #0 is male, 1 is female and 2 is non binary address_id = db.Column(db.Integer, db.ForeignKey('address.address_id'), nullable=False) full_name = db.Column(db.String(41), nullable=False) birthdate = db.Column(db.Date, nullable=False) phone_number = db.Column(db.String(13), nullable=False) email = db.Column(db.String(50), nullable=False) password = db.Column(db.String(128), nullable=False) active = db.Column(db.Boolean, nullable=False, default=False) patient = db.relationship("Patient", foreign_keys='Patient.user_id') doctor = db.relationship("Doctor", foreign_keys='Doctor.user_id') def __init__(self, **args): self.full_name = args.get('full_name') self.birthdate = args.get('birthdate') self.phone_number = args.get('phone_number') self.email = args.get('email') self.password = args.get('password') self.gender_id = args.get('gender_id') self.address_id = args.get('address_id') self.active = args.get("active") def get_user_token(self, expires_sec=1800): s = Serializer(app.config['SECRET_KEY'], expires_sec) return s.dumps({'email': self.email}).decode('utf-8') @staticmethod def verify_user_token(token): s = Serializer(app.config['SECRET_KEY']) try: email = s.loads(token)['email'] except: return None return email @property def pk(self): return self.user_id @staticmethod def getAllUsers(): return User().query.all() @staticmethod def getUsersByAddressId(aid): return User().query.filter_by(address_id=aid).all() @staticmethod def getUsersByGender(gid): return User().query.filter_by(gender_id=gid).all() @staticmethod def getUserById(uid): return User().query.filter_by(user_id=uid).first() @staticmethod def getUserByEmail(uemail): return User().query.filter_by(email=uemail).first() @staticmethod def updateUserInfo(**args): user = User.getUserById(args.get('user_id')) user.phone_number = args.get('phone_number') user.email = args.get('email') user.password = args.get('password') user.address_id = args.get('address_id') db.session.commit() return user def activateUser(self): self.active = True db.session.commit() return self def create(self): db.session.add(self) db.session.commit() return self