class GraduationClasses(db.Model): __tablename__ = "graduation_class" graduation_id = db.Column(db.Integer, primary_key=True) enrollment_id = db.Column(db.Integer, db.ForeignKey('enrollments.enrollment_id')) # su_id = db.Column(db.Integer, db.ForeignKey('students.su_id')) graduation_term = db.relationship("Terms", secondary=termToGrad, backref='termToGrad.term_id', lazy=True) graduated = db.Column(db.Boolean) def __init__(self, graduated): self.graduated = graduated @classmethod def createGraduation(cls, graduated, term, year): print("createGraduation: start") newEntry = cls(graduated) termEntry = Terms.searchForTerm(term, year) if termEntry is None: print( "createGraduation: no matching term entry found, creating new entry" ) termEntry = Terms.createTerm(term, year) else: print("createGraducation: Term found, using existing entry") newEntry.graduation_term.append(termEntry) newEntry.saveToDB() print("createGraduation: success") return newEntry @classmethod def searchForGraduation(cls, **kwargs): if "enrollment_id" in kwargs: result = cls.searchForGraduationByEnrollmentID( kwargs.get("enrollment_id")) if result is not None: return result #TODO:other searches here print("searchForGraduation failed, return NONE") return None @classmethod def searchForGraduationByEnrollmentID(cls, enrollment_id): return db.session.query(cls).filter( cls.enrollment_id == enrollment_id).first() # ToString method def __repr__(self): return "[GRAD_CLASS:id={}, SU_ID={}, ExpGrad={}, ActGrad={}]".format( self.graduation_id, self.su_id, self.expected_term, self.actual_term) def saveToDB(self): '''Quicksave method. Automatically commits and saves entry to db.''' db.session.add(self) db.session.commit()
class Students(db.Model): """Table definition of the Students table. Takes/builds off of base database Model. Acts as the root parent of the database. __init__(self, id) createStudent() -- creates new entry in the Race table, will call __init__ TODO: searches and documentation saveToDB() -- adds and commits instance to database """ __tablename__ = "students" su_id = db.Column(db.Integer, primary_key=True) demographic_id = db.relationship("Demographics", backref='students.demographic_id', lazy=True) hie_id = db.relationship("HighImpactExpierences", backref='students.hie_id', lazy=True) enrollment_id = db.relationship("Enrollments", backref='students.enrollment_id', lazy=True) def __init__(self, id): self.su_id = id # ToString method def __repr__(self): return "[STUDENTS:SU_id={}, Demographic={}, HIE_Taken={}, Enrollment_Status={}]".format( self.su_id, self.demographic_id, self.hie_id, self.enrollment_id) @classmethod def createStudent(cls, su_id, sex, pell_flag, first_gen_flag, first_race, second_race, hie_type, hie_name, hie_course_number, london_flag, dc_flag, city_name, country_name, hie_term, hie_year, fys_flag, fys_aes_term, fys_aes_year, graduated, grad_term, grad_year): """Create a new entry in the Students table and returns the new instance. Extra attributes used for search/finding entries in child tables, Demographics and Races. Parameters: Basic: su_id (int) -- Unique student ID number Demographic Data: sex (char) -- Biological sex ('M' or 'F') pell (bool) -- Flag indicating if entry received Pell Grant first_gen (bool) -- Flag indicating if entry is a first generation college student Race Data: first_race (str) -- first race of entry (default None) second_race (str) -- second race of entry (default None) """ print("createStudent: Start") newEntry = cls(su_id) # call default constructor ## Append Demographic Relationship ## demoEntry = Demographics.searchForDemographic(su_id=su_id) if demoEntry is None: print("createStudent: demographic not found, creating new entry") demoEntry = Demographics.createDemographic(sex, pell_flag, first_gen_flag, first_race, second_race) else: print("createStudent: demographic found, using exisiting entry") newEntry.demographic_id.append(demoEntry) ## End Demographic ## ## Append High Impact Experience Relationship ## hieEntry = HighImpactExpierences.searchForHIE(su_id=su_id) if hieEntry is None: print("createStudent: no HIE entry found, creating new entry") hieEntry = HighImpactExpierences.createHIE(hie_type, hie_name, hie_course_number, london_flag, dc_flag, city_name, country_name, hie_term, hie_year) else: print("createStudent: HIE entry found, using existing entry") newEntry.hie_id.append(hieEntry) ## End HIE ## ## Append Enrollment Relationship ## enrollEntry = Enrollments.searchForEnrollment(su_id=su_id) if enrollEntry is None: print("createStudent: no Enrollment found, creating new entry") enrollEntry = Enrollments.createEnrollment(fys_flag, fys_aes_term, fys_aes_year, graduated, grad_term, grad_year) else: print( "createStudent: enrollment entry found, using existing entry") newEntry.enrollment_id.append(enrollEntry) ## End Enrollment ## newEntry.saveToDB() print("createStudent: Success! Student entry added to table") return newEntry #TODO: makes searches def saveToDB(self): '''Quicksave method. Automatically commits and saves entry to db.''' db.session.add(self) db.session.commit()
class HighImpactExpierences(db.Model): __tablename__ = "high_impact_expierences" hie_id = db.Column(db.Integer, primary_key=True) su_id = db.Column(db.Integer, db.ForeignKey('students.su_id')) hie_type = db.Column(db.String(50), nullable=False) hie_name = db.Column(db.String(50)) hie_course_number = db.Column(db.String(50)) location_id = db.relationship( "Locations", backref='high_impact_expierences.location_id', lazy=True) term_id = db.relationship("Terms", secondary=termToHIE, backref='termToHIE.term_id', lazy='dynamic') def __init__(self, hie_type, hie_name, hie_course_number): self.hie_name = hie_name self.hie_type = hie_type self.hie_course_number = hie_course_number @classmethod def createHIE(cls, hie_type, hie_name, hie_course_number, london_flag, dc_flag, city_name, country_name, hie_term, hie_year): print("createHIE: Start") newEntry = cls(hie_type, hie_name, hie_course_number) # call default constructor ## Append Location Relationship ## locEntry = Locations.searchForLocation(hie_id=newEntry.hie_id) if locEntry is None: print("createHIE: location not found, creating new entry") locEntry = Locations.createLocation(london_flag, dc_flag, city_name, country_name) else: print("createHIE: location found, using exisiting entry") newEntry.location_id.append(locEntry) ## End Location ## ## Append Term Relationship ## termEntry = Terms.searchForTerm(term=hie_term, year=hie_year) if termEntry is None: print("createHIE: term not found, creating new entry") termEntry = Terms.createTerm(hie_term, hie_year) else: print("createHIE: term found, using existing entry") newEntry.term_id.append(termEntry) ## End Term ## newEntry.saveToDB() print("createHIE: HIE entry added to table") return newEntry @classmethod def searchForHIE(cls, **kwargs): if "su_id" in kwargs: result = cls.searchForHIEBySUID(kwargs.get("su_id")) if result is not None: return result if "hie_course_number" in kwargs: result = cls.searchForHIEByCourseNumber( kwargs.get("hie_course_number")) if result is not None: return result #type, name, by location, by term here print("searchForHIE: default endpoint reached, return NONE") return None @classmethod def searchForHIEBySUID(cls, su_id): return db.session.query(cls).filter(cls.su_id == su_id).first() @classmethod def searchForHIEByCourseNumber(cls, course_number): return db.session.query(cls).filter( cls.hie_course_number == course_number).first() # ToString method def __repr__(self): return "[HIE:id={}, SU_ID={}, Type={}, Course_Num={}, Location={}, Term={}]".format( self.hie_id, self.su_id, self.hie_type, self.hie_course_number, self.location_id, self.term_id) def saveToDB(self): '''Quicksave method. Automatically commits and saves entry to db.''' db.session.add(self) db.session.commit()
class Enrollments(db.Model): __tablename__ = "enrollments" enrollment_id = db.Column(db.Integer, primary_key=True) su_id = db.Column(db.Integer, db.ForeignKey('students.su_id')) fys_aes_term = db.relationship("Terms", secondary=termToEnroll, backref='termToEnroll.term_id', lazy=True) fys_flag = db.Column( db.Boolean, nullable=False) # True if had FYS, False if transfered and had AES graduation_id = db.relationship("GraduationClasses", backref='enrollments.graduation_id', lazy=True) # loa_id = db.relationship("LeaveOfAbsences", backref='enrollments.loa_id', lazy=True) def __init__(self, fys_flag): self.fys_flag = fys_flag @classmethod def createEnrollment(cls, fys_flag, fys_aes_term, fys_aes_year, graduated, grad_term, grad_year): print("createEnrollment: start") newEntry = cls(fys_flag) ## Append FYS/AES Term relationship ## fysTermEntry = Terms.searchForTerm(fys_aes_term, fys_aes_year) if fysTermEntry is None: print( "createEnrollment: no matching term entry found, creating new entry" ) fysTermEntry = Terms.createTerm(fys_aes_term, fys_aes_year) else: print("createEnrollment: Term found, using existing entry") newEntry.fys_aes_term.append(fysTermEntry) ## END FYS/AES Relationship ## ## Append Graduation relationship ## gradEntry = GraduationClasses.searchForGraduation( enrollment_id=newEntry.enrollment_id) if gradEntry is None: print( "createEnrollment: no graduation class found, creating new entry" ) gradEntry = GraduationClasses.createGraduation( graduated, grad_term, grad_year) else: print("createEnrollment: graduation found, using existing entry") newEntry.graduation_id.append(gradEntry) newEntry.saveToDB() print("createEnrollment: success") return newEntry @classmethod def searchForEnrollment(cls, **kwargs): if "su_id" in kwargs: return cls.searchForEnrollmentBySUID(kwargs.get("su_id")) #TODO: add other searches print("search default reached, returns NONE") return None @classmethod def searchForEnrollmentBySUID(cls, id): return db.session.query(cls).filter(cls.su_id == id).first() # ToString method def __repr__(self): return "[ENROLLMENT:id={}, SU_ID={}, FYS_Term={}, AES_Term={}, Grad_ID={}]".format( self.enrollment_id, self.su_id, self.fys_term, self.aes_term, self.graduation_id) def saveToDB(self): '''Quicksave method. Automatically commits and saves entry to db.''' db.session.add(self) db.session.commit()
class Locations(db.Model): __tablename__ = "locations" location_id = db.Column(db.Integer, primary_key=True) hie_id = db.Column(db.Integer, db.ForeignKey('high_impact_expierences.hie_id')) city_id = db.relationship("Cities", backref="locations.city_id", lazy=True) country_id = db.relationship("Countries", backref="locations.country_id", lazy=True) london_flag = db.Column(db.Boolean) dc_flag = db.Column(db.Boolean) def __init__(self, london_flag, dc_flag): self.london_flag = london_flag self.dc_flag = dc_flag @classmethod def createLocation(cls, london_flag, dc_flag, city_name, country_name): print("createLocation: creating new location entry") newEntry = cls(london_flag, dc_flag) # call default contructor ## Append city entry relationship ## city = Cities.searchForCity(loc_id=newEntry.location_id, name=city_name) if city is None: print("createLocation: city not found, creating new entry") city = Cities.createCity(city_name) else: print("createLocation: city found, using existing entry") newEntry.city_id.append(city) ## END append city ## ## Append country entry relationship ## country = Countries.searchForCountry(loc_id=newEntry.location_id, name=country_name) if country is None: print("createLocation: country not found, creating new entry") country = Countries.createCountry(country_name) else: print("createLocation: country found, using existing entry") newEntry.country_id.append(country) ## END append country ## newEntry.saveToDB() print("createLocation: success, new entry added") return newEntry @classmethod def searchForLocation(cls, **kwargs): if "hie_id" in kwargs: result = cls.searchForLocationByHIEID(kwargs.get("hie_id")) if result is not None: return result if "city" in kwargs or "country" in kwargs: result = cls.searchForLocationByCityCountry( kwargs.get("city"), kwargs.get("country")) if result is not None: return result print("searchForLocation: default enpoint reached, returns NONE") return None @classmethod def searchForLocationByHIEID(cls, hie_id): return db.session.query(cls).filter(cls.hie_id == hie_id).first() @classmethod def searchForLocationByCityCountry(cls, city, country): city = db.session.query(Cities).filter(Cities.name == city).first() if city is None: country = db.session.query(Countries).filter( Countries.name == country).first() return country return None # ToString method def __repr__(self): return "[LOCATION:id={}, HIE_ID={}, City={}, Country={}, London?={}, DC?={}]".format( self.location_id, self.hie_id, self.city_id, self.country_id, self.london_flag, self.dc_flag) def saveToDB(self): '''Quicksave method. Automatically commits and saves entry to db.''' db.session.add(self) db.session.commit()
class Demographics(db.Model): """Table definition of the Demographics table. Takes/builds off of base database Model. __init__(self, sex, pell_flag, first_year_flag) createDemographic() -- creates new entry in the demographic table, will call __init__ searchForDemographic() -- searches for entry in table based on keyword arguments saveToDB() -- adds and commits instance to database """ __tablename__ = "demographics" demographic_id = db.Column(db.Integer, primary_key=True) su_id = db.Column(db.Integer, db.ForeignKey('students.su_id')) pell_flag = db.Column(db.Boolean, nullable=False) sex = db.Column(db.String(1), nullable=False) race_id = db.relationship("Races", backref='demographics.race_id', lazy=True) first_gen_flag = db.Column(db.Boolean, nullable=False) def __init__(self, sex, pell_flag, first_year_flag): self.pell_flag = pell_flag self.sex = sex self.first_gen_flag = first_year_flag @classmethod def createDemographic(cls, sex, pell, first_gen, first_race, second_race): """Create a new entry in the Demographics table and returns the new instance. Keyword arguments are used to find/create a Race entry relationship. Parameters: sex (char) -- Biological sex ('M' or 'F') pell (bool) -- Flag indicating if entry received Pell Grant first_gen (bool) -- Flag indicating if entry is a first generation college student first_race (str) -- first race of entry (default None) second_race (str) -- second race of entry (default None) """ print("createDemographic: creating new entry in demographics.") newEntry = cls(sex, pell, first_gen) #call default constructor race = Races.searchForRace(first_race=first_race, second_race=second_race) if race is None: print( "createDemographic: Race not found, creating new entry in Race" ) race = Races.createRace(first_race=first_race, second_race=second_race) else: print("createDemographic: race found, using existing entry") newEntry.race_id.append(race) newEntry.saveToDB() print("createDemographic: demographic entry added to table") return newEntry # ToString method def __repr__(self): return "[DEMOGRAPHIC:id={}, SU_ID={}, Sex={}, Race={}, Pell_Grant?={}, First_Gen?={}]".format( self.demographic_id, self.su_id, self.sex, self.race_id, self.pell_flag, self.first_gen_flag) @classmethod def searchForDemographic(cls, **kwargs): """Search through Demographics table based on the passed column attributes. Keyword Arguments: su_id (int) -- Student ID an entry references in the Students Table TODO: other arguments and searches """ if "su_id" in kwargs: result = cls.searchForDemographicBySUID(kwargs.get("su_id")) if result is not None: return result # if "pell_flag" in kwargs: # result = cls.searchForDemographicByPell(kwargs.get("pell_flag")) # if result is not None: # return result # if "first_gen" in kwargs: # result = cls.searchForDemographicByFG(kwargs.get("first_gen")) # if result is not None: # return result # if "sex" in kwargs: # result = cls.searchForDemographicBySex(kwargs.get("sex")) # if result is not None: # return result # if "race_id" in kwargs: # result = cls.searchForDemographicByRaceID(kwargs.get("race_id")) # if result is not None: # return result print("searchDemographic reached default endpoint, returns NONE") return None @classmethod def searchForDemographicBySUID(cls, id): """Returns first match by student ID""" return db.session.query(cls).filter(cls.su_id == id).first() # @classmethod # def searchForDemographicByPell(cls, pell): # return db.session.query(cls).filter(cls.pell_flag == pell) # @classmethod # def searchForDemographicByRaceID(cls, id): # return db.session.query(cls).filter(cls.race_id == id) def saveToDB(self): '''Quicksave method. Automatically commits and saves entry to db.''' db.session.add(self) db.session.commit()