def check_registration_eligibility(mat_no, acad_session): # does this check for the supplied mat_no in the academic session: acad_session current_level = utils.get_level(mat_no) res_poll = utils.result_poll(mat_no) reg_poll = utils.course_reg_poll(mat_no) course_reg_exists = course_reg_utils.course_reg_for_session(mat_no, acad_session, reg_poll) if course_reg_exists: return 'Course Registration already exists', 403 s_personal_info = course_reg_utils.process_personal_info(mat_no) table_to_populate = course_reg_utils.get_table_to_populate(current_level, acad_session, res_poll, reg_poll) probation_status, prev_catg = course_reg_utils.get_probation_status_and_prev_category(res_poll, acad_session) graduation_status = s_personal_info['grad_status'] # handle special cases error_text = '' if graduation_status and graduation_status == 1: error_text = 'Student cannot carry out course reg as he has graduated' elif table_to_populate == '': # or int(table_to_populate[-3:]) + 100 > 800 error_text = 'Student cannot carry out course reg as he has exceeded the 8-year limit' elif prev_catg not in 'ABC': error_text = 'Student cannot carry out course reg as his previous category is {}'.format(prev_catg) if error_text != '': return error_text, 403 ret_obj = (table_to_populate, current_level, probation_status, s_personal_info) return ret_obj, 200
def res_poll_for_session(acad_session, res_poll=None, mat_no=None): """supply mat_no if res_poll is not given""" if not res_poll: res_poll = utils.result_poll(mat_no) ind_res = [[ind, res] for ind, res in enumerate(res_poll) if res and res['session'] == acad_session] ind_res = ({}, '') if not ind_res else (ind_res[0][1], 'Result{}'.format(100 * (ind_res[0][0] + 1))) return ind_res
def get(mat_no, sep_carryovers=False): person = personal_info.get(mat_no) results = utils.result_poll(mat_no) student_details = { "dept": utils.get_dept(), "results": [], "credits": [], "categories": [], "unregd": [], "carryovers": [], "failed_courses": [] } keys = [ "date_of_birth", "mode_of_entry", "session_admitted", "surname", "grad_status", "session_grad", "is_symlink", "othernames", "sex" ] student_details.update({key: person[key] for key in keys}) for idx, result in enumerate(results): if result: # Handle unregistered results courses unregd_result = { "first_sem": [], "second_sem": [], 'level': result["level"], 'session': result["session"], "table": itol(idx) } unregd_arr = [spc_fn(x) for x in csv_fn(result["unregd"])] unregd_formatted = format_results(unregd_arr) unregd_result["first_sem"] += unregd_formatted[0][0] unregd_result["second_sem"] += unregd_formatted[0][1] student_details["unregd"].append(unregd_result) # Handle registered results courses result_arr, lvl_result, co_result = [], deepcopy( unregd_result), deepcopy(unregd_result) for code in [ key for key in result if match("[A-Z][A-Z][A-Z][0-9][0-9][0-9]", key) and result[key] ]: result_arr.append((code, *csv_fn(result[code]))) co_arr = [spc_fn(x) for x in csv_fn(result["carryovers"])] failed_crs = [[], []] if sep_carryovers: co_formatted = format_results(co_arr) co_result["first_sem"] += co_formatted[0][0] co_result["second_sem"] += co_formatted[0][1] failed_crs = co_formatted[4] student_details["carryovers"].append(co_result) formatted = format_results(result_arr, *co_formatted[2:4]) else: formatted = format_results(result_arr + co_arr) lvl_result["first_sem"] += formatted[0][0] lvl_result["second_sem"] += formatted[0][1] student_details["results"].append(lvl_result) [failed_crs[ind].extend(formatted[4][ind]) for ind in range(2)] student_details["credits"].append( (formatted[1], formatted[2], formatted[3])) student_details["failed_courses"].append(failed_crs) student_details["categories"].append(result["category"]) return student_details
def add_single_result_record(index, result_details, result_errors_file, course_details_dict, level=None): """ :param index: position of entry in the larger list --for tracking :param result_details: [course_code, session_written, mat_no, score] :param result_errors_file: file object in write or append mode for logging important errors :param course_details_dict: :param level: student's course-adviser assigned level """ error_log = [] try: course_code, session_taken, mat_no, score = result_details session_taken, score = map(int, [session_taken, score]) entry_session = utils.get_DB(mat_no) except: return handle_errors('Invalid inputs at index {}'.format(index), error_log, result_errors_file, result_details) grade = utils.compute_grade(score, entry_session) current_level = utils.get_level(mat_no) # Error check on level, grade and score error_text = '' if level: levels = [600, 700, 800] if level == 600 else [level] if current_level not in levels: error_text = "You are not allowed to enter results for {} at index {} whose current level is " \ "{}".format(mat_no, index, current_level) if not (-1 <= score <= 100) and not error_text: error_text = 'Unexpected score for {}, "{}", for {} at index {}; ' \ 'result not added'.format(course_code, score, mat_no, index) if not grade and not error_text: error_text = '{0} at index {1} was not found in the database'.format( mat_no, index) if error_text: return handle_errors(error_text, error_log, result_errors_file, result_details) # Get course details if course_code in course_details_dict and course_details_dict[course_code]: course_dets = course_details_dict[course_code] else: # if there was error in initializing course_details_dict, individual calls would be made course_dets = course_details.get(course_code) if not course_dets: # fail on non-existent course(s) error_text = '{} at index {} was not found in the database'.format( course_code, index) return handle_errors(error_text, error_log, result_errors_file, result_details) course_credit = course_dets['credit'] course_level = course_dets['level'] is_unusual = False # Get course reg course_registration = course_reg_for_session(mat_no, session_taken) or { 'level': current_level, 'courses': [] } courses_registered = course_registration['courses'] level_written = course_registration['level'] if not courses_registered: is_unusual = True error_log = handle_errors( 'No course registration found for {0} at index {1} for the {2}/{3} ' 'session'.format(mat_no, index, session_taken, session_taken + 1), error_log) elif course_code not in courses_registered: is_unusual = True error_log = handle_errors( '{0} at index {1} did not register {2} in the {3}/{4} session' ''.format(mat_no, index, course_code, session_taken, session_taken + 1), error_log) # Get the result table for the session res_poll = utils.result_poll(mat_no) result_record, table_to_populate = res_poll_for_session( session_taken, res_poll) session = utils.load_session(utils.get_DB(mat_no)) if not result_record: if is_unusual and grade == 'ABS': return handle_errors( 'Unregistered course {} with grade "ABS" cannot be added for ' '{}'.format(course_code, mat_no), error_log) table_to_populate = get_table_to_populate(course_registration, res_poll) result_xxx_schema = getattr(session, table_to_populate + 'Schema')() params = mat_no, session_taken, courses_registered, result_xxx_schema, level_written result_record = prepare_new_results_table(params) else: result_xxx_schema = getattr(session, table_to_populate + 'Schema')() # Check if a previous entry for the course exists in the current session and updates the value # of "previous_grade" while logging the changes to be made previous_grade = get_previous_grade_and_log_changes( result_details, result_record, is_unusual) # add score to result object if is_unusual or result_record['unusual_results']: unusual_results = result_record['unusual_results'].split(',') index = [ ind for ind, x in enumerate(unusual_results) if x.split(' ')[0] == course_code ] if index: unusual_results.pop(index[0]) if is_unusual and grade != "ABS": unusual_results.append('{} {} {}'.format(course_code, score, grade)) while '' in unusual_results: unusual_results.remove('') result_record['unusual_results'] = ','.join(unusual_results) if not is_unusual: if course_code in result_record: result_record[course_code] = '{},{}'.format(score, grade) else: carryovers = result_record['carryovers'].split( ',') if result_record['carryovers'] else [''] index = [ ind for ind, x in enumerate(carryovers) if x.split(' ')[0] == course_code ] if index: carryovers.pop(index[0]) carryovers.append('{} {} {}'.format(course_code, score, grade)) while '' in carryovers: carryovers.remove('') result_record['carryovers'] = ','.join(carryovers) # get the session category owed_courses_exist = check_owed_courses_exists( mat_no, level_written, grade, course_dets) if not is_unusual else True if not courses_registered: tcr, tcp = 0, 0 else: if grade not in ['F', 'ABS'] and previous_grade in [ 'F', 'ABS', '' ] and not is_unusual: result_record['tcp'] += course_credit elif grade in ['F', 'ABS'] and previous_grade not in [ 'F', 'ABS', '' ] and not is_unusual: result_record['tcp'] -= course_credit tcr, tcp = course_registration['tcr'], result_record['tcp'] res_record = result_xxx_schema.load(result_record) res_record.category = utils.compute_category(tcr, res_record, owed_courses_exist) db_session = result_xxx_schema.Meta.sqla_session db_session.add(res_record) if grade == 'ABS': delete_if_empty(res_record, result_xxx_schema) db_session.commit() db_session.close() # update GPA - Credits table if not is_unusual: update_gpa_credits(mat_no, grade, previous_grade, course_credit, course_level) return error_log
def post_course_reg(data): level_options = course_reg_utils.get_optional_courses(data['course_reg_level']) level_options = utils.dictify(level_options[0] + level_options[1]) courses, person_options = [], [] tcr = [0, 0] for idx, sem in enumerate(['first_sem', 'second_sem']): for course_obj in data['courses'][sem]: courses.append(course_obj[0]) tcr[idx] += course_obj[2] if course_obj[0] in level_options: person_options.append('{} {}'.format(idx+1, course_obj[0])) courses = sorted(courses) mat_no = data['mat_no'] table_to_populate = data['table_to_populate'] course_reg_session = data['course_reg_session'] session = utils.load_session(utils.get_DB(mat_no)) course_reg_xxx_schema = getattr(session, table_to_populate + 'Schema')() table_columns = course_reg_xxx_schema.load_fields.keys() registration = {} for col in table_columns: if col in courses: registration[col] = '1' courses.remove(col) elif col not in ['carryovers', 'mat_no', 'tcr', 'level', 'session', 'probation', 'fees_status', 'others']: registration[col] = '0' registration.update({ 'carryovers': ','.join(courses), 'mat_no': mat_no, 'tcr': sum(tcr), 'level': data['course_reg_level'], 'session': course_reg_session, 'probation': data['probation_status'], 'fees_status': data['fees_status'], 'others': data['others'] }) course_registration = course_reg_xxx_schema.load(registration) db_session = course_reg_xxx_schema.Meta.sqla_session if person_options: person = session.PersonalInfo.query.filter_by(mat_no=mat_no).first() person.option = ','.join(person_options) db_session.add(person) db_session.add(course_registration) db_session.commit() db_session.close() success_text = 'course registration successful' # Here we check if there were any stray results waiting in unusual results for this session session_results = [x for x in utils.result_poll(mat_no) if x and (x['session'] == course_reg_session)] if session_results and ('unusual_results' in session_results[0]) and session_results[0]['unusual_results']: unusual_results = [utils.spc_fn(x) for x in utils.csv_fn(session_results[0]['carryovers'])] unusual_results = [[x[0], course_reg_session, mat_no, x[1]] for x in unusual_results] log = results.add_result_records(unusual_results) if log[0]: success_text += '; results for unregistered courses still remain in database' print('\n====>> ', success_text) return success_text, 200