def test_delete_admission_by_id(self): with open(dbc.DB_FILE_PATH, "r+") as f: f.truncate() to_save1 = dbc.AdmissionResult() to_save1.candidate_cnp = 987654321 to_save1.allocation = dbc.AdmissionStatus.FEE to_save1.specialization_id = 0 to_save1.final_score = 8.6 to_save2 = dbc.AdmissionResult() to_save2.candidate_cnp = 1234567 to_save2.allocation = dbc.AdmissionStatus.FREE to_save2.specialization_id = 0 to_save2.final_score = 9.0 dbc.save_admission_result_for_candidate(to_save1) dbc.save_admission_result_for_candidate(to_save2) dbc.delete_admission_result_for_candidate(987654321) found1 = dbc.get_admission_result_for_candidate(987654321) found2 = dbc.get_admission_result_for_candidate(1234567) self.assertEqual(to_save2.to_xml(), found2.to_xml()) self.assertEqual(None, found1)
def test_get_all_results(self): with open(dbc.DB_FILE_PATH, "r+") as f: f.truncate() to_save = dbc.Specialization() to_save.capacity = 92 to_save.name = "new specialization" to_save.identifier = 0 result = dbc.save_specialization(to_save) to_save1 = dbc.AdmissionResult() to_save1.candidate_cnp = 987654321 to_save1.allocation = dbc.AdmissionStatus.FEE to_save1.specialization_id = 0 to_save1.final_score = 8.6 to_save2 = dbc.AdmissionResult() to_save2.candidate_cnp = 1234567 to_save2.allocation = dbc.AdmissionStatus.FREE to_save2.specialization_id = 0 to_save2.final_score = 9.0 dbc.save_admission_result_for_candidate(to_save1) dbc.save_admission_result_for_candidate(to_save2) found = dbc.get_admission_results() self.assertEqual(2, len(found)) found = dbc.get_admission_results(1) self.assertEqual(2, len(found))
def calculate_results(): specializations = DbC.get_all_specializations() candidates = DbC.get_all_candidates() repartition = [] specs = {} opt_arr = {} for item in specializations: specs[item.identifier] = {} specs[item.identifier]["name"] = item.name specs[item.identifier]["capacity"] = item.capacity specs[item.identifier]["free_spots"] = item.capacity for item in candidates: r = DbC.AdmissionResult() r.candidate_cnp = item.cnp r.final_score = max( item.info_grade, item.math_grade ) * 0.3 + item.high_school_avg_grade * 0.2 + 0.5 * item.admission_grade r.specialization_id = item.first_option r.allocation = DbC.AdmissionStatus.UNPROCESSED repartition.append(r) opt_arr[str(item.cnp)] = {} opt_arr[str(item.cnp)]["first_option"] = item.first_option opt_arr[str(item.cnp)]["second_option"] = item.second_option repartition = sorted(repartition, key=lambda x: ( x.specialization_id, (-1) * x.final_score, )) for item in repartition: if item.final_score < 5: item.allocation = DbC.AdmissionStatus.REJECTED continue if specs[item.specialization_id]["free_spots"] > 2: item.allocation = DbC.AdmissionStatus.FREE specs[item.specialization_id]["free_spots"] -= 1 elif specs[item.specialization_id]["free_spots"] > 0: item.allocation = DbC.AdmissionStatus.FEE specs[item.specialization_id]["free_spots"] -= 1 else: item.specialization_id = opt_arr[str( item.candidate_cnp)]["second_option"] if specs[item.specialization_id]["free_spots"] > 2: item.allocation = DbC.AdmissionStatus.FREE specs[item.specialization_id]["free_spots"] -= 1 elif specs[item.specialization_id]["free_spots"] > 0: item.allocation = DbC.AdmissionStatus.FEE specs[item.specialization_id]["free_spots"] -= 1 else: item.allocation = DbC.AdmissionStatus.REJECTED # print("Candidate CNP: ", item.candidate_cnp) # print("Admission Grade: ", item.final_score) # print("AdmissionResult: ", item.allocation) # print("Specialization: ", specs[item.specialization_id]["name"]) # print("Specialization ID: ", item.specialization_id) return repartition
def test_save_candidate_incorrect_data(self): with open(dbc.DB_FILE_PATH, "r+") as f: f.truncate() to_save = dbc.Candidate() to_save.surname = 1234 result = dbc.save_candidate(to_save) self.assertEqual('OK', result)
def test_save_candidate_misiing_data(self): with open(dbc.DB_FILE_PATH, "r+") as f: f.truncate() to_save = dbc.Candidate() result = dbc.save_candidate(to_save) found = dbc.get_candidate_by_id(0) self.assertEqual('OK', result) self.assertEqual(to_save.to_xml(), found.to_xml()) found.print()
def test_save_admission_no_data(self): with open(dbc.DB_FILE_PATH, "r+") as f: f.truncate() to_save = dbc.AdmissionResult() result = dbc.save_admission_result_for_candidate(to_save) found = dbc.get_admission_result_for_candidate(0) self.assertEqual('OK', result) self.assertEqual(to_save.to_xml(), found.to_xml()) found.print()
def test_save_new_specialization(self): with open(dbc.DB_FILE_PATH, "r+") as f: f.truncate() to_save = dbc.Specialization() to_save.capacity = 92 to_save.name = "new specialization" to_save.identifier = 1 result = dbc.save_specialization(to_save) saved = dbc.get_specialization_by_id(1) self.assertEqual(to_save.to_xml(), saved.to_xml()) self.assertEqual('OK', result)
def test_save_specialization_no_id(self): with open(dbc.DB_FILE_PATH, "r+") as f: f.truncate() to_save = dbc.Specialization() to_save.name = 'No id' to_save.capacity = 92 result = dbc.save_specialization(to_save) found = dbc.get_specialization_by_id(-1) self.assertEqual('OK', result) self.assertEqual(to_save.to_xml(), found.to_xml()) found.print()
def test_save_specialization_incorrect_data(self): with open(dbc.DB_FILE_PATH, "r+") as f: f.truncate() to_save = dbc.Specialization() to_save.identifier = 1 to_save.name = 4567 to_save.capacity = 'Incorrect capacity' result = dbc.save_specialization(to_save) found = dbc.get_specialization_by_id(1) self.assertEqual('OK', result) self.assertEqual(to_save.to_xml(), found.to_xml()) found.print()
def test_save_duplicate_specialization(self): with open(dbc.DB_FILE_PATH, "r+") as f: f.truncate() to_save = dbc.Specialization() to_save.capacity = 92 to_save.name = "new specialization" to_save.identifier = 1 dbc.save_specialization(to_save) result = dbc.save_specialization(to_save) saved = dbc.get_all_specializations() self.assertEqual(1, len(saved)) self.assertEqual('ALREADY_EXISTING', result)
def specializations(): actions = menu_actions.menu specializations = DbC.get_all_specializations() return render_template('specializations_list.html', title='Specializations List', actions=actions, specializations=specializations)
def candidates(): actions = menu_actions.menu candidates = DbC.get_all_candidates(1) return render_template('candidates_list.html', title='Candidates List', actions=actions, candidates=candidates)
def test_save_duplicate_admission(self): with open(dbc.DB_FILE_PATH, "r+") as f: f.truncate() to_save = dbc.AdmissionResult() to_save.candidate_cnp = 987654321 to_save.allocation = dbc.AdmissionStatus.FEE to_save.specialization_id = 0 to_save.final_score = 8.6 dbc.save_admission_result_for_candidate(to_save) result = dbc.save_admission_result_for_candidate(to_save) saved = dbc.get_admission_results() self.assertEqual(1, len(saved)) self.assertEqual('OK', result)
def set_results(): results = calculate_results() for item in results: if DbC.save_admission_result_for_candidate(item) != "OK": raise "Error in repartition processing!" print("Repartition completed successfully.")
def test_get_candidate_by_id(self): with open(dbc.DB_FILE_PATH, "r+") as f: f.truncate() to_save1 = dbc.Candidate() to_save1.cnp = 123456789 to_save1.first_name = "student1 in town" to_save1.surname = " name1 " to_save1.email = "*****@*****.**" to_save1.info_grade = 6.7 to_save1.math_grade = 8.9 to_save1.high_school_avg_grade = 9.5 to_save1.admission_grade = 7.8 to_save1.first_option = 2 to_save1.second_option = 1 to_save2 = dbc.Candidate() to_save2.cnp = 987654321 to_save2.first_name = " student2 in town" to_save2.surname = " name2" to_save2.email = "*****@*****.**" to_save2.info_grade = 6.7 to_save2.math_grade = 8.9 to_save2.high_school_avg_grade = 9.5 to_save2.admission_grade = 7.8 to_save2.first_option = 2 to_save2.second_option = 1 dbc.save_candidate(to_save1) dbc.save_candidate(to_save2) found = dbc.get_candidate_by_id(123456789) self.assertEqual(to_save1.to_xml(), found.to_xml())
def test_save_new_candidate(self): with open(dbc.DB_FILE_PATH, "r+") as f: f.truncate() to_save = dbc.Candidate() to_save.cnp = 987654321 to_save.first_name = "new student in town" to_save.surname = " mhm " to_save.email = "*****@*****.**" to_save.info_grade = 6.7 to_save.math_grade = 8.9 to_save.high_school_avg_grade = 9.5 to_save.admission_grade = 7.8 to_save.first_option = 2 to_save.second_option = 1 result = dbc.save_candidate(to_save) saved = dbc.get_candidate_by_id(987654321) self.assertEqual(to_save.to_xml(), saved.to_xml()) self.assertEqual('OK', result)
def add_candidate(): form = AddCandidateForm() actions = menu_actions.menu if form.validate_on_submit(): new_candidate = DbC.Candidate() new_candidate.cnp = form.cnp.data new_candidate.first_name = form.first_name.data new_candidate.surname = form.surname.data new_candidate.email = form.email.data new_candidate.info_grade = form.info_grade.data new_candidate.math_grade = form.math_grade.data new_candidate.high_school_avg_grade = form.high_school_avg_grade.data new_candidate.admission_grade = form.admission_grade.data new_candidate.first_option = form.first_option.data new_candidate.second_option = form.second_option.data DbC.save_candidate(new_candidate) flash('Candidate with CNP {} submitted.'.format(form.cnp.data)) return redirect('/index') return render_template('candidate_form.html', title='Add Candidate', form=form, actions=actions)
def test_save_duplicate_candidate(self): with open(dbc.DB_FILE_PATH, "r+") as f: f.truncate() to_save = dbc.Candidate() to_save.cnp = 987654321 to_save.first_name = "new student in town" to_save.surname = " mhm " to_save.email = "*****@*****.**" to_save.info_grade = 6.7 to_save.math_grade = 8.9 to_save.high_school_avg_grade = 9.5 to_save.admission_grade = 7.8 to_save.first_option = 2 to_save.second_option = 1 dbc.save_candidate(to_save) result = dbc.save_candidate(to_save) saved = dbc.get_all_candidates() self.assertEqual(1, len(saved)) self.assertEqual('ALREADY_EXISTING', result)
def test_delete_specialization_by_id(self): with open(dbc.DB_FILE_PATH, "r+") as f: f.truncate() to_save1 = dbc.Specialization() to_save1.capacity = 11 to_save1.name = "new specialization 1" to_save1.identifier = 1 to_save2 = dbc.Specialization() to_save2.capacity = 22 to_save2.name = "new specialization 2" to_save2.identifier = 2 dbc.save_specialization(to_save1) dbc.save_specialization(to_save2) dbc.delete_specialization_by_id(2) found1 = dbc.get_specialization_by_id(1) found2 = dbc.get_specialization_by_id(2) self.assertEqual(to_save1.to_xml(), found1.to_xml()) self.assertEqual(None, found2)
def test_get_all_specializations(self): with open(dbc.DB_FILE_PATH, "r+") as f: f.truncate() to_save1 = dbc.Specialization() to_save1.capacity = 11 to_save1.name = "new specialization 1" to_save1.identifier = 1 to_save2 = dbc.Specialization() to_save2.capacity = 22 to_save2.name = "new specialization 2" to_save2.identifier = 2 dbc.save_specialization(to_save1) dbc.save_specialization(to_save2) found = dbc.get_all_specializations() self.assertEqual(2, len(found))
def test_update_specialization_not_found(self): with open(dbc.DB_FILE_PATH, "r+") as f: f.truncate() original = dbc.Specialization() original.capacity = 11 original.name = "new specialization 1" original.identifier = 1 dbc.save_specialization(original) updated = dbc.Specialization() updated.capacity = 21 updated.name = "updated specialization 1" updated.identifier = 2 result = dbc.update_specialization(updated) found = dbc.get_specialization_by_id(1) self.assertEqual(original.to_xml(), found.to_xml()) self.assertEqual('Entry not found', result)
def test_update_specialization_not_found(self): with open(dbc.DB_FILE_PATH, "r+") as f: f.truncate() original = dbc.AdmissionResult() original.candidate_cnp = 987654321 original.allocation = dbc.AdmissionStatus.FEE original.specialization_id = 0 original.final_score = 8.6 dbc.save_admission_result_for_candidate(original) updated = dbc.AdmissionResult() updated.candidate_cnp = 12345 updated.allocation = dbc.AdmissionStatus.UNPROCESSED updated.specialization_id = 0 updated.final_score = 0.0 result = dbc.update_admission_result_for_candidate(updated) found = dbc.get_admission_result_for_candidate(987654321) self.assertEqual(original.to_xml(), found.to_xml()) self.assertEqual('Entry not found', result)
def test_update_specialization_not_found(self): with open(dbc.DB_FILE_PATH, "r+") as f: f.truncate() original = dbc.Candidate() original.cnp = 123456789 original.first_name = "student1 in town" original.surname = " name1 " original.email = "*****@*****.**" original.info_grade = 6.7 original.math_grade = 8.9 original.high_school_avg_grade = 9.5 original.admission_grade = 7.8 original.first_option = 2 original.second_option = 1 dbc.save_candidate(original) updated = dbc.Candidate() updated.cnp = 987654321 updated.first_name = "this is my name" updated.surname = " my surname " updated.email = " i got a new mail" updated.info_grade = 6.7 updated.math_grade = 8.9 updated.high_school_avg_grade = 9.5 updated.admission_grade = 7.8 updated.first_option = 2 updated.second_option = 1 result = dbc.update_candidate(updated) found = dbc.get_candidate_by_id(123456789) self.assertEqual(original.to_xml(), found.to_xml()) self.assertEqual('Entry not found', result)
def test_get_all_candidates(self): with open(dbc.DB_FILE_PATH, "r+") as f: f.truncate() sp1 = dbc.Specialization() sp1.capacity = 92 sp1.name = "specialization 1" sp1.identifier = 1 sp2 = dbc.Specialization() sp2.capacity = 92 sp2.name = "specialization 2" sp2.identifier = 2 dbc.save_specialization(sp1) dbc.save_specialization(sp2) to_save1 = dbc.Candidate() to_save1.cnp = 123456789 to_save1.first_name = "student1 in town" to_save1.surname = " name1 " to_save1.email = "*****@*****.**" to_save1.info_grade = 6.7 to_save1.math_grade = 8.9 to_save1.high_school_avg_grade = 9.5 to_save1.admission_grade = 7.8 to_save1.first_option = 2 to_save1.second_option = 1 to_save2 = dbc.Candidate() to_save2.cnp = 987654321 to_save2.first_name = " student2 in town" to_save2.surname = " name2" to_save2.email = "*****@*****.**" to_save2.info_grade = 6.7 to_save2.math_grade = 8.9 to_save2.high_school_avg_grade = 9.5 to_save2.admission_grade = 7.8 to_save2.first_option = 2 to_save2.second_option = 1 dbc.save_candidate(to_save1) dbc.save_candidate(to_save2) found = dbc.get_all_candidates() self.assertEqual(2, len(found)) found = dbc.get_all_candidates(1) self.assertEqual(2, len(found))
def launch(): parser = argparse.ArgumentParser() parser.add_argument('-m', '--mode', required=True) parser.add_argument( '--api', required=False, action='store_true', help= "flag to toggle using the api to fetch grib files rather than downloading manually" ) parser.add_argument('-p', '--path', required=False, help="path to grib file to be processed") parser.add_argument('-tn', '--tablename', required=False, help='extract data from provided table name') parser.add_argument('-c', '--country', required=False, help='country around which to form a bounding box') args = parser.parse_args() db_controller = DatabaseController() mode = args.mode.lower() # mode 'a' for analysis, mode 'e' for extraction if mode == 'e': if not args.tablename: print( "Usage Error: Argument --tablename required when in extract mode" ) return if not args.path and not args.api: print( "Usage Error: No path or api flag provided provided. Please provide a path to a grib2 file or include the --api flag to use the copernicus api" ) return if args.path and args.api: print( "Usage Error: Please use ONLY the --api flag OR the --path argument, not both" ) return country = None bounding_box = None if args.country: country = args.country bounding_box = fetch_bounding_box(country) grib_files = [] wgrib_controller = WgribController() if args.path: if os.path.exists(args.path): raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), args.path) if os.path.isfile(args.path): grib_file_path = args.path grib_files.append(grib_file_path) elif os.path.isdir: for file in os.listdir(args.path): filename = os.fsdecode(file) if filename.endswith(".grib2"): grib_files.append(filename) elif args.api: api_controller = CopernicusApi() api_grib_files = api_controller.get_grib_data() grib_files.extend(api_grib_files) if not args.country or bounding_box is None: print( "Warning: You have not inputted a country. This may result in csv files and databases that are multiple GB in size\n Are you sure you want to continue? [Y/n]" ) if input().lower() != "y": quit() tablename = args.tablename db_controller.set_table_name(tablename) if bounding_box is None and args.country: if input( "No bounding box could be found for that country. Would you like to extract the entire file to the database? [Y/n]: " ).lower() != "y": quit() table_exists = db_controller.table_exists(tablename) if table_exists: print("A table has already been created with that name") print("Press 1 to add this grib data to the existing database") print( "Press 2 to delete this table and populate it with the given file" ) choice = input() if choice == '2': db_controller.drop_table() elif choice == '3': closest_point = db_controller.get_closest_point_data( args.lon, args.lat) db_controller.extract_data_for_point(closest_point[0], closest_point[1]) extract_data_from_grib(wgrib_controller, db_controller, grib_files, bounding_box, table_exists, country) analysis_controller = AnalysisController(db_controller) analysis_controller.start()
def get_results(): all_results = DbC.get_admission_results(1) return all_results