class Admin(db.Model): id = db.Column(db.INTEGER, primary_key=True) # column in table for id for user, auto incremented username = db.Column(db.String(40),unique=True,nullable=False) # column in table for the user's username that is entered at login, no repeats password = db.Column(db.String(40),unique=True,nullable=False) # column in table for the user's password that is entered at login, no repeats # initializes instance of UserModel with the data for all columns given as parameters def __init__(self, username, password): self.username = username self.password = password # saves the UserModel to the database def save_to_db(self): db.session.add(self) db.session.commit() # finds a row by specific username given as a parameter @classmethod def find_by_username(cls, username): return cls.query.filter_by(username=username).first() # finds a row by specific id given as a parameter @classmethod def find_by_id(cls, _id): return cls.query.filter_by(id=_id).first()
class Provider(db.Model): __tablename__ = 'Provider' #Individual Attributes id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), unique=True, nullable=False) #In the API, this is used like a list. This is shown below in the add_program method program = db.relationship( 'Program', secondary= Programs_Providers, #This back updates the association table above backref=db.backref( 'programs', lazy=True) #This causes for lazy lookups for effiency ) #Individual Methods def __repr__(self): return "<Provider(id='%d', name='%s')>" % (self.id, self.name) def __init__(self, name): self.name = name def save_to_db(self): db.session.add(self) db.session.commit() #This method adds a program to the relationship def add_program(self, newProgram): self.program.append(newProgram) def remove_program(self, oldProgram): self.program.remove(oldProgram) #Returns the Entity of a Provider with the given id. #All class methods are used by placing the class name belofre the class method: Provider.find_by_id("10") @classmethod def find_by_id(cls, _id): return db.session.query(cls).filter(cls.id == _id).first() # finds a row by specific name given as a parameter @classmethod def find_by_name(cls, _name): return cls.query.filter_by(name=_name).first() @classmethod def return_all_providers(cls): return cls.query.order_by(cls.name).all() @property def serialize(self): return { 'ProviderId': self.id, 'ProviderName': self.name, 'ProviderPrograms': self.programSerial } @property def programSerial(self): return [i.serialize for i in self.program]
class Language(db.Model): __tablename__ = 'Language' #Individual Attributes id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(50), unique=True, nullable=False) #Individual Methods def __repr__(self): return "<Language(id='%d', name='%s')>" % (self.id, self.name) def __init__(self, name): self.name = name def save_to_db(self): db.session.add(self) db.session.commit() @classmethod def find_by_id(cls, _id): return db.session.query(cls).filter(cls.id == _id).first() # finds a row by specific username given as a parameter @classmethod def find_by_name(cls, _name): return cls.query.filter_by(name=_name).first() @property def serialize(self): return {'LanguageId': self.id, 'LanguageName': self.name}
class Location(db.Model): __tablename__='Location' #Individual Attributes id = db.Column(db.Integer, primary_key=True) city = db.Column(db.String(100),nullable=True) country= db.Column(db.String(100),nullable=False) #Individual Methods def __repr__(self): return "<Location(id='%d', city name='%s', country name='%s')>" % (self.id, self.city, self.country) def __init__(self, nameCity,nameCountry): self.city = nameCity self.country= nameCountry def save_to_db(self): db.session.add(self) db.session.commit() @classmethod def get_location_id(cls, nameCity, nameCountry): id = db.session.query(cls).filter(cls.city == nameCity).filter(cls.country == nameCountry).first() if id is None: return -1 else: return id # finds a row by specific id given as a parameter @classmethod def find_by_id(cls, _id): return cls.query.filter_by(id=_id).first() # finds a row by specific username given as a parameter @classmethod def find_by_name(cls, _city, _country): return cls.query.filter_by(city=_city).filter_by(country=_country).first() @property def serialize(self): return{ 'id':self.id, 'city':self.city, 'country':self.country }
class Provider(db.Model): __tablename__='Provider' #Individual Attributes id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100),unique=True,nullable=False) program = db.relationship('Program', secondary=programs, backref=db.backref('programs',lazy=True) ) #Individual Methods def __repr__(self): return "<Provider(id='%d', name='%s')>" % (self.id, self.name) def __init__(self, name): self.name = name def save_to_db(self): db.session.add(self) db.session.commit() def add_program(self, newProgram): self.program.append(newProgram) def remove_program(self, oldProgram): self.program.remove(oldProgram) @classmethod def get_provider_id(cls, name): id = db.session.query(cls).filter(cls.name == name).first() if id is None: return -1 else: return id # finds a row by specific name given as a parameter @classmethod def find_by_name(cls, _name): return cls.query.filter_by(name=_name).first() @classmethod def return_all_providers(cls): return cls.query.order_by(cls.name).all() @property def serialize(self): return{ 'id':self.id, 'name':self.name, 'programs':self.programSerial } @property def programSerial(self): return [i.serialize for i in self.program]
class Term(db.Model): __tablename__='Term' #Individual Attributes id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(10),unique=True,nullable=False) #Individual Methods def __repr__(self): return "<Term(id='%d', name='%s')>" % (self.id, self.name) def __init__(self, name): self.name = name def save_to_db(self): db.session.add(self) db.session.commit() @classmethod def get_term_id(cls,name): id = db.session.query(cls).filter(cls.name == name).first() if id is None: return -1 else: return id # finds a row by specific username given as a parameter @classmethod def find_by_name(cls, _name): return cls.query.filter_by(name=_name).first() @property def serialize(self): return{ 'id':self.id, 'name':self.name }
class Program(db.Model): __tablename__='Program' #Individual Attributes id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100),unique=True,nullable=False) comm_eng = db.Column(db.Boolean,nullable=False) #yes or no research_opp = db.Column(db.Boolean,nullable=False) #yes or no intership_opp = db.Column(db.Boolean,nullable=False) #yes or no date_created = db.Column(db.DateTime, default=db.func.current_timestamp()) date_modified = db.Column(db.DateTime, default=db.func.current_timestamp(), onupdate=db.func.current_timestamp()) cost = db.Column(db.String(15), nullable=True) cost_stipulations = db.Column(db.String(500), nullable=True) description = db.Column(db.String(5000), nullable=True) url = db.Column(db.String(5000), nullable=True) #Relationships area = db.relationship('Area', secondary=areas, backref=db.backref('areas',lazy=True) ) language = db.relationship('Language', secondary=languages, backref=db.backref('languages',lazy=True) ) location = db.relationship('Location', secondary=locations, backref=db.backref('locations',lazy=True) ) term = db.relationship('Term', secondary=terms, backref=db.backref('terms',lazy=True) ) #Individual Methods #optional method to set the porper string representation of the object def __repr__(self): return "<Program(Program ID='%d', Name='%s')>" % (self.id, self.name) def __init__(self, name, com, res, intern, cost, cost_stipulations, description, url): #This initilizes the program specific fields self.name = name self.comm_eng = com self.research_opp = res self.intership_opp = intern self.cost = cost self.cost_stipulations = cost_stipulations self.description = description self.url = url def save_to_db(self): db.session.add(self) db.session.commit() #These methods will be by the API to add and remove any property that falls # under the many to many relationship. def add_language(self, newLanguage): self.language.append(newLanguage) def remove_language(self, oldLanguage): self.language.remove(oldLanguage) def add_area(self, newArea): self.area.append(newArea) def remove_area(self, oldArea): self.area.remove(oldArea) def add_term(self, newTerm): self.term.append(newTerm) def remove_term(self, oldTerm): self.term.remove(oldTerm) def add_location(self, newLocation): self.location.append(newLocation) def remove_location(self, oldLocation): self.location.remove(oldLocation) #These methods will be used for sorting when the user initilizes @classmethod def return_all_programs(cls): return cls.query.order_by(cls.name).all() @classmethod def sort_by_language(cls, desiredLanguage): return cls.query.join(Programs_Languages).join(Language).filter(Programs_Languages.c.language_id == get_language_id(desiredLanguage)).all() @classmethod def sort_by_term(cls, desiredTerm): return cls.query.join(Programs_Terms).join(Term).filter(Programs_Terms.c.term_id == get_term_id(desiredTerm)).all() @classmethod def sort_by_area(cls, desiredArea): return cls.query.join(Programs_Areas).join(Area).filter(Programs_Areas.c.area_id == get_area_id(desiredArea)).all() @classmethod def sort_by_location(cls, desiredCountry): return cls.query.join(Programs_Locations).join(Location).filter(Programs_Locations.c.location_id == get_location_id(desiredCountry)).all() @classmethod def get_program_id(cls, name): id = db.session.query(cls).filter(cls.name == name).first() if id is None: return -1 else: return id # finds a row by specific id given as a parameter @classmethod def find_by_id(cls, _id): return cls.query.filter_by(id=_id).first() # finds a row by specific username given as a parameter @classmethod def find_by_name(cls, _name): return cls.query.filter_by(name=_name).first() @property def serialize(self): #id = db.Column(db.Integer, primary_key=True) #name = db.Column(db.String(100),unique=True,nullable=False) #comm_eng = db.Column(db.Boolean,nullable=False) #yes or no #research_opp = db.Column(db.Boolean,nullable=False) #yes or no #intership_opp = db.Column(db.Boolean,nullable=False) #yes or no #date_created = db.Column(db.DateTime, default=db.func.current_timestamp()) #date_modified = db.Column(db.DateTime, default=db.func.current_timestamp(), #onupdate=db.func.current_timestamp()) #cost = db.Column(db.String(15), nullable=True) #cost_stipulations = db.Column(db.String(500), nullable=True) #description = db.Column(db.String(5000), nullable=True) #url = db.Column(db.String(5000), nullable=True) return{ 'id': self.id, 'name':self.name, 'comm':self.comm_eng, 'research':self.research_opp, 'intern':self.intership_opp, 'create':dump_datetime(self.date_created), 'mod':dump_datetime(self.date_modified), 'cost':self.cost, 'stip':self.cost_stipulations, 'desc':self.description, 'url':self.url, 'area':self.seralize_manyToManyArea, 'lang':self.seralize_manyToManyLang, 'loc':self.seralize_manyToManyLoc, 'term':self.seralize_manyToManyTerm } @property def seralize_manyToManyArea(self): return [i.serialize for i in self.area] @property def seralize_manyToManyLang(self): return [i.serialize for i in self.language] @property def seralize_manyToManyLoc(self): return [i.serialize for i in self.location] @property def seralize_manyToManyTerm(self): return [i.serialize for i in self.term]