def _update_or_create_proposal(request, learning_unit_year, proposal=None): person = get_object_or_404(Person, user=request.user) proposal_base_form = ProposalBaseForm(request.POST or None, person, learning_unit_year, proposal) if request.method == 'POST': if proposal_base_form.is_valid(): proposal = proposal_base_form.save() display_success_messages( request, _("You proposed a modification of type %(type)s for the learning unit %(acronym)s." ) % { 'type': proposal.get_type_display(), 'acronym': learning_unit_year.acronym }) return redirect('learning_unit', learning_unit_year_id=learning_unit_year.id) else: show_error_message_for_form_invalid(request) context = proposal_base_form.get_context() if proposal: return render(request, 'learning_unit/proposal/update_modification.html', context) return render(request, 'learning_unit/proposal/create_modification.html', context)
def _update_group(request, education_group_year, root, groupelementyear_formset): # TODO :: IMPORTANT :: Fix urls patterns to get the GroupElementYear_id and the root_id in the url path ! # TODO :: IMPORTANT :: Need to update form to filter on list of parents, not only on the first direct parent form_education_group_year = GroupForm(request.POST or None, instance=education_group_year, user=request.user) html_page = "education_group/update_groups.html" has_content = len(groupelementyear_formset.queryset) > 0 if request.method == 'POST': if form_education_group_year.is_valid() and ( not has_content or groupelementyear_formset.is_valid()): return _common_success_redirect( request, form_education_group_year, root, groupelementyear_formset if has_content else None) else: show_error_message_for_form_invalid(request) return render( request, html_page, { "education_group_year": education_group_year, "form_education_group_year": form_education_group_year.forms[forms.ModelForm], "form_education_group": form_education_group_year.forms[EducationGroupModelForm], 'group_element_years': groupelementyear_formset, 'show_minor_major_option_table': education_group_year.is_minor_major_option_list_choice })
def _update_or_create_suppression_proposal(request, learning_unit_year, proposal=None): person = get_object_or_404(Person, user=request.user) proposal_type = ProposalType.SUPPRESSION.name initial = _get_initial(learning_unit_year, proposal, person, proposal_type=proposal_type) max_year = _get_max_year(learning_unit_year, proposal) form_end_date = LearningUnitProposalEndDateForm(request.POST or None, learning_unit_year, max_year=max_year, person=person) form_proposal = ProposalLearningUnitForm(request.POST or None, person=person, instance=proposal, initial=initial) if request.method == 'POST': if form_end_date.is_valid() and form_proposal.is_valid(): with transaction.atomic(): form_proposal.save() # For the proposal, we do not update learning_unit_year form_end_date.save(update_learning_unit_year=False) display_success_messages( request, _("You proposed a modification of type %(type)s for the learning unit %(acronym)s." ) % { 'type': ProposalType.SUPPRESSION.value, 'acronym': learning_unit_year.acronym }) return redirect('learning_unit', learning_unit_year_id=learning_unit_year.id) else: show_error_message_for_form_invalid(request) context = get_learning_unit_identification_context(learning_unit_year.id, person) context.update({ 'person': person, 'form_end_date': form_end_date, 'form_proposal': form_proposal, }) if proposal: return render(request, 'learning_unit/proposal/update_suppression.html', context) return render(request, 'learning_unit/proposal/create_suppression.html', context)
def _update_training(request, education_group_year, root, groupelementyear_formset): # TODO :: IMPORTANT :: Fix urls patterns to get the GroupElementYear_id and the root_id in the url path ! # TODO :: IMPORTANT :: Need to update form to filter on list of parents, not only on the first direct parent form_education_group_year = TrainingForm(request.POST or None, user=request.user, instance=education_group_year) coorganization_formset = None forms_valid = all([ form_education_group_year.is_valid(), groupelementyear_formset.is_valid() ]) if has_coorganization(education_group_year): coorganization_formset = OrganizationFormset( data=request.POST or None, form_kwargs={ 'education_group_year': education_group_year, 'user': request.user }, queryset=education_group_year.coorganizations) forms_valid = forms_valid and coorganization_formset.is_valid() if request.method == 'POST': if forms_valid: if has_coorganization(education_group_year): coorganization_formset.save() return _common_success_redirect(request, form_education_group_year, root, groupelementyear_formset) else: show_error_message_for_form_invalid(request) return render( request, "education_group/update_trainings.html", { "education_group_year": education_group_year, "form_education_group_year": form_education_group_year.forms[forms.ModelForm], "form_education_group": form_education_group_year.forms[EducationGroupModelForm], "form_coorganization": coorganization_formset, "form_hops": form_education_group_year.hops_form, "show_coorganization": has_coorganization(education_group_year), "show_diploma_tab": form_education_group_year.show_diploma_tab(), 'can_change_coorganization': perms.is_eligible_to_change_coorganization( person=request.user.person, education_group=education_group_year, ), 'group_element_years': groupelementyear_formset, "is_finality_types": education_group_year.is_finality })
def create_learning_unit(request, academic_year_id): person = get_object_or_404(Person, user=request.user) if request.POST.get('academic_year'): academic_year_id = request.POST.get('academic_year') academic_year = get_object_or_404(AcademicYear, pk=academic_year_id) postponement_form = LearningUnitPostponementForm( person=person, start_postponement=academic_year, data=request.POST or None) if request.method == 'POST': if postponement_form.is_valid(): return _save_and_redirect(postponement_form, request) else: show_error_message_for_form_invalid(request) return render(request, "learning_unit/simple/creation.html", postponement_form.get_context())
def update_learning_unit(request, learning_unit_year_id): learning_unit_year = get_object_or_404(LearningUnitYear, pk=learning_unit_year_id) person = get_object_or_404(Person, user=request.user) learning_unit_full_instance = None if learning_unit_year.subtype == learning_unit_year_subtypes.PARTIM: learning_unit_full_instance = learning_unit_year.parent.learning_unit # TODO :: clean code ; end_postponement could be removed from kwargs in this following # LearningUnitPostponementForm instantiation + in the template form end_postponement = learning_unit_year.academic_year if not bool( int(request.POST.get('postponement', 1))) else None postponement_form = LearningUnitPostponementForm( person=person, start_postponement=learning_unit_year.academic_year, end_postponement=end_postponement, learning_unit_instance=learning_unit_year.learning_unit, learning_unit_full_instance=learning_unit_full_instance, data=request.POST or None, external=learning_unit_year.is_external(), ) if request.method == 'POST': if postponement_form.is_valid(): # Update current learning unit year _save_form_and_display_messages(request, postponement_form, learning_unit_year) return redirect('learning_unit', learning_unit_year_id=learning_unit_year_id) else: show_error_message_for_form_invalid(request) context = postponement_form.get_context() context["learning_unit_year"] = learning_unit_year context["is_update"] = True template = 'learning_unit/simple/update.html' return render(request, template, context)
def _update_mini_training(request, education_group_year, root, groupelementyear_formset): # TODO :: IMPORTANT :: Fix urls patterns to get the GroupElementYear_id and the root_id in the url path ! # TODO :: IMPORTANT :: Need to upodate form to filter on list of parents, not only on the first direct parent form = MiniTrainingForm(request.POST or None, instance=education_group_year, user=request.user) if request.method == 'POST': if form.is_valid() and groupelementyear_formset.is_valid(): return _common_success_redirect(request, form, root, groupelementyear_formset) else: show_error_message_for_form_invalid(request) return render( request, "education_group/update_minitrainings.html", { "form_education_group_year": form.forms[forms.ModelForm], "education_group_year": education_group_year, "form_education_group": form.forms[EducationGroupModelForm], 'group_element_years': groupelementyear_formset })
def get_proposal_learning_unit_creation_form(request, academic_year): person = get_object_or_404(Person, user=request.user) academic_year_pk = request.POST.get('academic_year', academic_year) academic_year = get_object_or_404(AcademicYear, pk=academic_year_pk) proposal_form = CreationProposalBaseForm(request.POST or None, person, academic_year) if request.method == 'POST': if proposal_form.is_valid(): proposal = proposal_form.save() success_msg = _( "Proposal learning unit <a href='%(link)s'> %(acronym)s (%(academic_year)s) </a> successfuly created.")\ % { 'link': reverse("learning_unit", kwargs={'learning_unit_year_id': proposal.learning_unit_year.id}), 'acronym': proposal.learning_unit_year.acronym, 'academic_year': proposal.learning_unit_year.academic_year } display_success_messages(request, success_msg, extra_tags='safe') return redirect('learning_unit', learning_unit_year_id=proposal.learning_unit_year.pk) else: show_error_message_for_form_invalid(request) return render(request, "learning_unit/proposal/creation.html", proposal_form.get_context())