def online_encoding_submission(request, learning_unit_year_id): scores_list = score_encoding_list.get_scores_encoding_list(user=request.user, learning_unit_year_id=learning_unit_year_id) submitted_enrollments = [] draft_scores_not_sumitted_yet = scores_list.enrollment_draft_not_submitted not_submitted_enrollments = set([ex for ex in scores_list.enrollments if not ex.is_final]) for exam_enroll in draft_scores_not_sumitted_yet: if (exam_enroll.score_draft is not None and exam_enroll.score_final is None) \ or (exam_enroll.justification_draft and not exam_enroll.justification_final): submitted_enrollments.append(exam_enroll) not_submitted_enrollments.remove(exam_enroll) if exam_enroll.is_draft: if exam_enroll.score_draft is not None: exam_enroll.score_final = exam_enroll.score_draft if exam_enroll.justification_draft: exam_enroll.justification_final = exam_enroll.justification_draft exam_enroll.full_clean() with transaction.atomic(): exam_enroll.save() mdl.exam_enrollment.create_exam_enrollment_historic(request.user, exam_enroll) # Send mail to all the teachers of the submitted learning unit on any submission all_encoded = len(not_submitted_enrollments) == 0 learning_unit_year = mdl.learning_unit_year.get_by_id(learning_unit_year_id) attributions = mdl_attr.attribution.Attribution.objects.filter(learning_unit_year=learning_unit_year) persons = list(set([attribution.tutor.person for attribution in attributions])) send_mail.send_mail_after_scores_submission(persons, learning_unit_year.acronym, submitted_enrollments, all_encoded) return HttpResponseRedirect(reverse('online_encoding', args=(learning_unit_year_id,)))
def _get_common_encoding_context(request, learning_unit_year_id): scores_list = score_encoding_list.get_scores_encoding_list( user=request.user, learning_unit_year_id=learning_unit_year_id) score_responsibles = mdl_attr.attribution.find_all_responsibles_by_learning_unit_year( scores_list.learning_unit_year) tutors = mdl.tutor.find_by_learning_unit(scores_list.learning_unit_year) \ .exclude(id__in=[score_responsible.id for score_responsible in score_responsibles]) is_coordinator = mdl_attr.attribution.is_score_responsible( request.user, scores_list.learning_unit_year) is_program_manager = mdl.program_manager.is_program_manager(request.user) context = { 'section': 'scores_encoding', 'is_program_manager': is_program_manager, 'score_responsibles': list(score_responsibles), 'tutors': list(tutors), 'is_coordinator': is_coordinator, 'draft_scores_not_submitted': len(scores_list.enrollment_draft_not_submitted), 'exam_enrollments_encoded': len(scores_list.enrollment_encoded), 'total_exam_enrollments': _get_count_still_enrolled(scores_list.enrollments), 'progress': scores_list.progress, 'progress_int': scores_list.progress_int } context.update(scores_list.__dict__) return context
def online_encoding_form(request, learning_unit_year_id=None): template_name = "online_encoding_form.html" if request.method == 'POST': updated_enrollments = None encoded_enrollment_ids = _extract_id_from_post_data(request) # Get only encoded from database scores_list_encoded = score_encoding_list.get_scores_encoding_list( user=request.user, learning_unit_year_id=learning_unit_year_id, enrollments_ids=encoded_enrollment_ids) # Append value encoded by user scores_list_encoded.enrollments = _extract_encoded_values_from_post_data( request, scores_list_encoded.enrollments) updated_enrollments = _update_enrollments(request, scores_list_encoded, updated_enrollments) context = _get_common_encoding_context(request, learning_unit_year_id) if messages.get_messages(request): context = _preserve_encoded_values(request, context) else: template_name = "online_encoding.html" send_messages_to_notify_encoding_progress( context["enrollments"], context["learning_unit_year"], context["is_program_manager"], updated_enrollments) else: context = _get_common_encoding_context(request, learning_unit_year_id) return render(request, template_name, context)
def online_encoding_form(request, learning_unit_year_id=None): template_name = "online_encoding_form.html" if request.method == 'POST': updated_enrollments = None encoded_enrollment_ids = _extract_id_from_post_data(request) # Get only encoded from database scores_list_encoded = score_encoding_list.get_scores_encoding_list( user=request.user, learning_unit_year_id=learning_unit_year_id, enrollments_ids=encoded_enrollment_ids) # Append value encoded by user scores_list_encoded.enrollments = _extract_encoded_values_from_post_data( request, scores_list_encoded.enrollments) try: updated_enrollments = score_encoding_list.update_enrollments( scores_encoding_list=scores_list_encoded, user=request.user) except Exception as e: error_msg = e.messages[0] if isinstance(e, ValidationError) else e.args[0] messages.add_message(request, messages.ERROR, _(error_msg)) context = _get_common_encoding_context(request, learning_unit_year_id) if messages.get_messages(request): context = _preserve_encoded_values(request, context) else: template_name = "online_encoding.html" send_messages_to_notify_encoding_progress(context["enrollments"], context["learning_unit_year"], context["is_program_manager"], updated_enrollments) else: context = _get_common_encoding_context(request, learning_unit_year_id) return layout.render(request, template_name, context)
def _get_score_list_filtered_by_enrolled_state(learning_unit_year_id, a_user): score_list = score_encoding_list.get_scores_encoding_list( user=a_user, learning_unit_year_id=learning_unit_year_id, only_enrolled=True ) return score_list
def online_double_encoding_validation(request, learning_unit_year_id=None): if request.method == 'POST': updated_enrollments = None scores_list_encoded = _get_score_encoding_list_with_only_enrollment_modified( request, learning_unit_year_id) try: updated_enrollments = score_encoding_list.update_enrollments( scores_encoding_list=scores_list_encoded, user=request.user) except Exception as e: error_msg = e.messages[0] if isinstance( e, ValidationError) else e.args[0] messages.add_message(request, messages.ERROR, _(error_msg)) if updated_enrollments: is_program_manager = mdl.program_manager.is_program_manager( request.user) scores_list_encoded = score_encoding_list.get_scores_encoding_list( user=request.user, learning_unit_year_id=learning_unit_year_id) send_messages_to_notify_encoding_progress( scores_list_encoded.enrollments, scores_list_encoded.learning_unit_year, is_program_manager, updated_enrollments) return HttpResponseRedirect( reverse('online_encoding', args=(learning_unit_year_id, )))
def export_xls(request, learning_unit_year_id): is_program_manager = mdl.program_manager.is_program_manager(request.user) scores_list = score_encoding_list.get_scores_encoding_list(request.user, learning_unit_year_id=learning_unit_year_id) scores_list = score_encoding_list.filter_without_closed_exam_enrollments(scores_list, is_program_manager) if scores_list.enrollments: return score_encoding_export.export_xls(scores_list.enrollments) else: messages.add_message(request, messages.WARNING, _('no_student_to_encode_xls')) return HttpResponseRedirect(reverse('online_encoding', args=(learning_unit_year_id,)))
def _append_search_to_specific_criteria_context(request, context): scores_list = score_encoding_list.get_scores_encoding_list( user=request.user, registration_id=context['registration_id'], student_last_name=context['last_name'], student_first_name=context['first_name'], justification=context['justification'], offer_year_id=context['offer_year_id']) context.update(scores_list.__dict__) if not scores_list.enrollments: messages.add_message(request, messages.WARNING, _('No result!'))
def notes_printing(request, learning_unit_year_id=None, tutor_id=None, offer_id=None): is_program_manager = mdl.program_manager.is_program_manager(request.user) scores_list_encoded = score_encoding_list.get_scores_encoding_list( user=request.user, learning_unit_year_id=learning_unit_year_id, tutor_id=tutor_id, offer_year_id=offer_id ) tutor = mdl.tutor.find_by_user(request.user) if not is_program_manager else None sheet_data = score_encoding_sheet.scores_sheet_data(scores_list_encoded.enrollments, tutor=tutor) return paper_sheet.print_notes(sheet_data)
def bulk_send_messages_to_notify_encoding_progress(request, updated_enrollments, is_program_manager): if is_program_manager: mail_already_sent_by_learning_unit = set() for enrollment in updated_enrollments: learning_unit_year = enrollment.learning_unit_enrollment.learning_unit_year if learning_unit_year in mail_already_sent_by_learning_unit: continue scores_list = score_encoding_list.get_scores_encoding_list(user=request.user, learning_unit_year_id=learning_unit_year.id) send_messages_to_notify_encoding_progress(scores_list.enrollments, learning_unit_year, is_program_manager, updated_enrollments) mail_already_sent_by_learning_unit.add(learning_unit_year)
def _get_score_encoding_list_with_only_enrollment_modified( request, learning_unit_year_id=None): encoded_enrollment_ids = _extract_id_from_post_data(request) scores_list_encoded = score_encoding_list.get_scores_encoding_list( user=request.user, learning_unit_year_id=learning_unit_year_id, enrollments_ids=encoded_enrollment_ids) if encoded_enrollment_ids: scores_list_encoded.enrollments = _extract_encoded_values_from_post_data( request, scores_list_encoded.enrollments) else: scores_list_encoded.enrollments = [] return scores_list_encoded
def online_double_encoding_validation(request, learning_unit_year_id=None): if request.method == 'POST': updated_enrollments = None scores_list_encoded = _get_score_encoding_list_with_only_enrollment_modified( request, learning_unit_year_id) updated_enrollments = _update_enrollments(request, scores_list_encoded, updated_enrollments) if updated_enrollments: is_program_manager = mdl.program_manager.is_program_manager( request.user) scores_list_encoded = score_encoding_list.get_scores_encoding_list( user=request.user, learning_unit_year_id=learning_unit_year_id) send_messages_to_notify_encoding_progress( scores_list_encoded.enrollments, scores_list_encoded.learning_unit_year, is_program_manager, updated_enrollments) return HttpResponseRedirect( reverse('online_encoding', args=(learning_unit_year_id, )))
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 _get_score_list_filtered_by_enrolled_state(learning_unit_year_id, a_user): score_list = score_encoding_list.get_scores_encoding_list( user=a_user, learning_unit_year_id=learning_unit_year_id, only_enrolled=True) return score_list