def change_or_remove_areas_for_program(change, programName, areas): prog = Program.find_by_name(programName) if (prog == None): raise ValueError if (areas == []): return True if (change == "add"): for i in areas: tempArea = Area.find_by_name(i) if (tempArea == None): tempArea = Area(i) tempArea.save_to_db() prog.add_area(tempArea) prog.save_to_db() elif (change == "remove"): for i in areas: tempArea = Area.find_by_name(i) if (tempArea == None): raise ValueError prog.remove_area(tempArea) prog.save_to_db() #This if statement checks to see if the removed Language has any more existing relationships # If there are none, then delete the language from the database if (Area.query.join(Programs_Areas).filter( (Programs_Areas.c.area_id == tempArea.id)).first() == None): db.session.delete(tempArea) prog.save_to_db() else: raise ValueError db.session.commit() return True
def create_new_program(providerName, programName, com, res, intern, cost, cost_stipulations, description, url, areas, terms, languages, locations): #-----------------------------PROVIDER RELATIONSHIP---------------------------- # Check if the provider already exist, if it doesn't then make a new provider if (Provider.get_provider_id(providerName) == -1): prov = Provider(providerName) prov.save_to_db() else: prov = Provider.find_by_name(providerName) # Check if the program already exist, if it doesn't then make a new program if (Program.get_program_id(programName) == -1): prog = Program(programName, com, res, intern, cost, cost_stipulations, description, url) prog.save_to_db() else: prog = Program.find_by_name(programName) #Add the program to the provider prov.add_program(prog) prov.save_to_db() #-----------------------------PROGRAM RELATIONSHIPS---------------------------- # AREA, TERM, LANGUAGES, LOCATION # AREA: # Cyle through all area names: check to see if the area already exist in the db, # if it doesn't, add it to the program and create the relationship. for i in areas: if (Area.get_area_id(i) == -1): tempArea = Area(i) tempArea.save_to_db() else: tempArea = Area.find_by_name(i) prog.add_area(tempArea) prog.save_to_db() #might need save to db methods # TERM: # Cyle through all term names: check to see if the term already exist in the db, # if it doesn't, add it to the program and create the relationship. for i in terms: if (Term.get_term_id(i) == -1): tempTerm = Term(i) tempTerm.save_to_db() else: tempTerm = Term.find_by_name(i) prog.add_term(tempTerm) prog.save_to_db() #might need save to db methods # LANGUAGES: # Cyle through all term names: check to see if the term already exist in the db, # if it doesn't, add it to the program and create the relationship. for i in languages: if (Language.get_language_id(i) == -1): tempLanguage = Language(i) tempLanguage.save_to_db() else: tempLanguage = Language.find_by_name(i) prog.add_language(tempLanguage) prog.save_to_db() #might need save to db methods # LOCATION: # Cyle through all term names: check to see if the term already exist in the db, # if it doesn't, add it to the program and create the relationship. for i in locations: if (Location.get_location_id(i[0], i[1]) == -1): tempLocation = Location(i[0], i[1]) tempLocation.save_to_db() else: tempLocation = Location.find_by_name(i[0], i[1]) prog.add_location(tempLocation) prog.save_to_db()