def post(self): """ save allocations to database """ # get the allocation details from request allocation_details = ALLOCATIONS_PARSER.parse_args() # get associated student data student_db_details = StudentsModel.find_student_by_adm( allocation_details['student_adm']) # get associated project data project_db_details = ProjectsModel.fetch_project_by_code( student_db_details.project_code) no_of_trimesters = int(project_db_details.trimesters) curr_month = int(helpers.date_today_month()) due_date = helpers.find_due_date(no_of_trimesters, curr_month) full_date_registered = helpers.date_today() new_allocation = AllocationsModel(allocation_details['allocation_id'], allocation_details['supervisor_id'], allocation_details['student_adm'], project_db_details.project_code, full_date_registered, due_date) new_allocation.save_allocation() # allocation successfully saved, update student's allocated status to True student_db_details.allocated = True student_db_details.update_student() response_obj = { 'status': 'success', 'message': 'Allocation has been successfully made' } return make_response(jsonify(response_obj), 200)
def get(self): # get all students and lecturers with course as common factor students_db_rows = StudentsModel.fetch_all_unallocated_students() student_data = [{ "name": "{} {}".format(student.first_name, student.last_name), "adm": student.student_adm, "course": "degree" if student.projects.degree else "diploma" } for student in students_db_rows] supervisors_db_rows = SupervisorsModel.fetch_all_supervisors() supervisors_data = [{ "name": "{} {}".format(supervisor.first_name, supervisor.last_name), "supervisor_id": supervisor.supervisor_id, "course": helpers.determine_supervisor_course(supervisor.degree, supervisor.diploma), "allocations_count": AllocationsModel.count_allocations(supervisor.supervisor_id) } for supervisor in supervisors_db_rows] response_obj = { "students": student_data, "supervisors": supervisors_data } return make_response(jsonify(response_obj), 200)
def get(self): allocation_details = AllocationsModel.fetch_all_allocations() if allocation_details: return make_response(jsonify(allocation_details)) response_obj = {'status': 'failed', 'message': 'No data was found'} return make_response(jsonify(response_obj))
def post(self, allocation_id=None): """ Add a new progress for the allocation Only supervisor is allowed to perform this action """ if allocation_id is None: # get the request parameters progress_details = PROGRESS_PARSER.parse_args() # ensure the allocation id exists in the database if AllocationsModel.fetch_allocation_by_id( progress_details['allocation_id']): # allocation exists, proceed with adding a new progress new_progress = ProgressModel(progress_details['progress_id'], progress_details['allocation_id'], helpers.date_today(), progress_details['document'], progress_details['comments'], progress_details['marks']) new_progress.add_progress() response_obj = { 'status': 'success', 'message': 'Progress was successfully added' } return make_response(jsonify(response_obj), 200) response_obj = { 'status': 'failed', 'message': 'could not record progress at this time' } return make_response(jsonify(response_obj), 202)
def delete(self, supervisor_id): """delete supervisor from database based on supervisor_id""" supervisor_db_details = SupervisorsModel.find_supervisor_by_id( supervisor_id) if supervisor_db_details: # get all associated students in the allocations table allocation_rows = AllocationsModel.fetch_allocation_by_supervisor_id( supervisor_id) for row in allocation_rows: students_row = StudentsModel.find_student_by_adm( row.student_adm) # update student allocation status to false students_row.allocated = False students_row.update_student() supervisor_db_details.remove_supervisor() response_obj = { 'status': 'success', 'message': 'Supervisor was successfully deleted!' } return make_response(jsonify(response_obj), 200) response_obj = { 'status': 'failed', 'message': 'Supervisor was not found!' } return make_response(jsonify(response_obj), 404)
def get(self): """Get the students associated with the supervisor""" supervisor_id = get_jwt_identity() allocation_rows = AllocationsModel.fetch_allocation_by_supervisor_id( supervisor_id) students_data = [] for row in allocation_rows: student_row = StudentsModel.find_student_by_adm(row.student_adm) students_data.append({ "student_name": "{} {}".format(student_row.first_name, student_row.last_name), "project_code": student_row.project_code, "allocation_id": row.allocation_id, "date_registered": row.date_registered, "due_date": row.due_date }) if len(students_data) > 0: return make_response(jsonify(students_data), 200) response_obj = {'status': 'failed', 'message': 'no data found'} return make_response(jsonify(response_obj), 404)
def get(self, supervisor_id=None): """Get all supervisors details in database""" if supervisor_id is None: # get all supervisors supervisor_rows = SupervisorsModel.fetch_all_supervisors() if supervisor_rows: all_supervisors = [{ "supervisor_id": supervisor.supervisor_id, "first_name": supervisor.first_name, "last_name": supervisor.last_name, "role": supervisor.role, "degree": supervisor.degree, "diploma": supervisor.diploma, "allocations_count": AllocationsModel.count_allocations( supervisor.supervisor_id) } for supervisor in supervisor_rows] return make_response(jsonify(all_supervisors)) else: return make_response( jsonify({ 'status': 'failed', 'message': 'No Supervisors found' })) # get a single supervisor supervisor_db_details = SupervisorsModel.find_supervisor_by_id( supervisor_id) if supervisor_db_details: response_obj = { 'supervisor': supervisor_db_details.supervisor_id, 'first_name': supervisor_db_details.first_name, 'last_name': supervisor_db_details.last_name, 'degree': supervisor_db_details.degree, 'diploma': supervisor_db_details.diploma, 'role': supervisor_db_details.role } return make_response(jsonify(response_obj)) response_obj = { # Supervisor was not found 'status': 'failed', 'message': 'Supervisor; {} was not found'.format(supervisor_id) } return make_response(jsonify(response_obj), 404)
def put(self): """reallocate a student a new supervisor""" allocation_obj_args = ALLOCATIONS_PARSER.parse_args() allocation_db_details = AllocationsModel.fetch_allocation_by_id( allocation_obj_args['allocation_id']) if allocation_db_details: # the allocation exists, proceeding with reallocation allocation_db_details.supervisor_id = allocation_obj_args[ 'supervisor_id'] allocation_db_details.update_allocation() response_obj = { 'status': 'success', 'message': 'reallocation has been successfully completed' } return make_response(jsonify(response_obj), 200) response_obj = { 'status': 'failed', 'message': 'sorry, reallocation could not be completed' } return make_response(jsonify(response_obj), 404)
def post(self): """ mark project as completed archive the allocation and delete the associated data :return: """ allocation_detail = ARCHIVES_PARSER.parse_args() allocation_db_row = AllocationsModel.fetch_allocation_by_id( allocation_detail['allocation_id']) if allocation_db_row: # get the associated student and supervisor row student_db_details = StudentsModel.find_student_by_adm( allocation_db_row.student_adm) supervisor_db_details = SupervisorsModel.find_supervisor_by_id( allocation_db_row.supervisor_id) # store the archive details new_archive = ArchivesModel( allocation_detail['archive_id'], allocation_db_row.student_adm, "{} {}".format(student_db_details.first_name, student_db_details.last_name), "{} {}".format(supervisor_db_details.first_name, supervisor_db_details.last_name), allocation_db_row.project_code, allocation_db_row.date_registered, allocation_db_row.due_date) new_archive.add_archive() # archive details stored, proceed to delete the all associated data (student, allocation, progress) allocation_db_row.remove_allocation() student_db_details.remove_student() response_obj = { 'status': 'success', 'message': 'archive was successfully made' } return make_response(jsonify(response_obj), 200) # archive process completed, return error message response_obj = {'status': 'failed', 'message': 'An error occured'} return make_response(jsonify(response_obj), 404)