def __save_xls_scores(request, file_name, learning_unit_year_id): try: workbook = load_workbook(file_name, read_only=True, data_only=True) except KeyError: messages.add_message(request, messages.ERROR, _('file_must_be_xlsx')) return False worksheet = workbook.active new_scores_number = 0 learning_unit_year = mdl.learning_unit_year.get_by_id( learning_unit_year_id) is_program_manager = mdl.program_manager.is_program_manager(request.user) data_xls = _get_all_data(worksheet) try: data_xls['session'] = _extract_session_number(data_xls) data_xls['academic_year'] = _extract_academic_year(data_xls) except Exception as e: messages.add_message(request, messages.ERROR, _(e.args[0])) return False academic_year_in_database = mdl.academic_year.find_academic_year_by_year( data_xls['academic_year']) if not academic_year_in_database: messages.add_message( request, messages.ERROR, '%s (%s).' % (_('no_data_for_this_academic_year'), data_xls['academic_year'])) return False score_list = score_encoding_list.get_scores_encoding_list( user=request.user, learning_unit_year_id=learning_unit_year_id) offer_acronyms_managed_by_user = { offer_year.acronym for offer_year in score_encoding_list.find_related_offer_years( score_list) } learn_unit_acronyms_managed_by_user = { learning_unit_year.acronym for learning_unit_year in score_encoding_list.find_related_learning_unit_years(score_list) } registration_ids_managed_by_user = score_encoding_list.find_related_registration_ids( score_list) enrollments_grouped = _group_exam_enrollments_by_registration_id_and_learning_unit_year( score_list.enrollments) errors_list = {} # Iterates over the lines of the spreadsheet. for count, row in enumerate(worksheet.rows): if _row_can_be_ignored(row): continue row_number = count + 1 try: _check_intergity_data( row, offer_acronyms_managed=offer_acronyms_managed_by_user, learn_unit_acronyms_managed=learn_unit_acronyms_managed_by_user, registration_ids_managed=registration_ids_managed_by_user, learning_unit_year=learning_unit_year) updated_row = _update_row(request.user, row, enrollments_grouped, is_program_manager) if updated_row: new_scores_number += 1 except Exception as e: errors_list[row_number] = e _show_error_messages(request, errors_list) if new_scores_number: messages.add_message( request, messages.SUCCESS, '%s %s' % (str(new_scores_number), _('score_saved'))) if not is_program_manager: __warn_that_score_responsibles_must_submit_scores( request, learning_unit_year) return True else: messages.add_message(request, messages.ERROR, '%s' % _('no_score_injected')) return False
def __save_xls_scores(request, file_name, learning_unit_year_id): try: workbook = load_workbook(file_name, read_only=True, data_only=True) except KeyError: messages.add_message(request, messages.ERROR, _("The file must be a valid 'XLSX' excel file")) return False worksheet = workbook.active new_scores_number = 0 learning_unit_year = mdl.learning_unit_year.get_by_id(learning_unit_year_id) is_program_manager = mdl.program_manager.is_program_manager(request.user) data_xls = _get_all_data(worksheet) try: data_xls['session'] = _extract_session_number(data_xls) data_xls['academic_year'] = _extract_academic_year(data_xls) except Exception as e: messages.add_message(request, messages.ERROR, _(e.args[0])) return False academic_year_in_database = mdl.academic_year.find_academic_year_by_year(data_xls['academic_year']) if not academic_year_in_database: messages.add_message(request, messages.ERROR, '%s (%s).' % (_("No data for this academic year"), data_xls['academic_year'])) return False score_list = _get_score_list_filtered_by_enrolled_state(learning_unit_year_id, request.user) offer_acronyms_managed_by_user = {offer_year.acronym for offer_year in score_encoding_list.find_related_offer_years(score_list)} learn_unit_acronyms_managed_by_user = {learning_unit_year.acronym for learning_unit_year in score_encoding_list.find_related_learning_unit_years(score_list)} registration_ids_managed_by_user = score_encoding_list.find_related_registration_ids(score_list) enrollments_grouped = _group_exam_enrollments_by_registration_id_and_learning_unit_year(score_list.enrollments) errors_list = {} # Iterates over the lines of the spreadsheet. for count, row in enumerate(worksheet.rows): if _row_can_be_ignored(row): continue row_number = count + 1 try: _check_intergity_data(row, offer_acronyms_managed=offer_acronyms_managed_by_user, learn_unit_acronyms_managed=learn_unit_acronyms_managed_by_user, registration_ids_managed=registration_ids_managed_by_user, learning_unit_year=learning_unit_year) _check_consistency_data(row) updated_row = _update_row(request.user, row, enrollments_grouped, is_program_manager) if updated_row: new_scores_number+=1 except Exception as e: errors_list[row_number] = e _show_error_messages(request, errors_list) if new_scores_number: messages.add_message(request, messages.SUCCESS, '%s %s' % (str(new_scores_number), _('Score saved'))) if not is_program_manager: __warn_that_score_responsibles_must_submit_scores(request, learning_unit_year) return True else: messages.add_message(request, messages.ERROR, '%s' % _("No scores injected")) return False