def new(request): form = CohortForm(request.POST or None) errors = [] if form.is_valid(): cohort = form.save() copy_cohort.copy_from_origin(cohort) return redirect(reverse('internship')) else: errors.append(form.errors) display_errors(request, errors) context = {'form': form, 'page_title': _('Add cohort'), 'form_new': True} return render(request, 'cohort/cohort_form.html', context)
def master_save(request, cohort_id): current_cohort = shortcuts.get_object_or_404(cohort.Cohort, pk=cohort_id) allocated_master = internship_master.get_by_id( request.POST.get("id")) if request.POST.get("id") else None form_master = MasterForm(request.POST, instance=allocated_master) person = allocated_master.person if allocated_master else None if not person and request.POST.get('existing-person-id'): person = Person.objects.get(pk=request.POST.get('existing-person-id')) form_person = InternshipPersonForm(request.POST, instance=person) person_address = person.personaddress_set.first() if person else None form_person_address = InternshipPersonAddressForm(request.POST or None, instance=person_address) errors = [] hospital = "" if form_master.is_valid() and form_person.is_valid( ) and form_person_address.is_valid(): allocated_master = form_master.instance if _validate_allocations(request): person = form_person.save() address = form_person_address.save(commit=False) address.person = person address.save() master = form_master.save() master.person = person master.save() master_allocation.clean_allocations(current_cohort, allocated_master) allocations = _build_allocations(request, allocated_master) _save_allocations(allocations) hospital = _extract_hospital_id(allocations) else: errors.append(form_master.errors) errors.append(form_person.errors) errors.append(form_person_address.errors) messages.add_message( request, messages.ERROR, _('A master must be affected to at least one hospital or one specialty with a role.' ), "alert-danger") else: errors.append(form_master.errors) errors.append(form_person.errors) errors.append(form_person_address.errors) display_errors(request, errors) if errors: return master_form(request=request, cohort_id=current_cohort.id, allocated_master=allocated_master) return HttpResponseRedirect("{}?hospital={}".format( reverse("internships_masters", args=[current_cohort.id]), hospital))
def import_students(request, cohort_id): cohort = get_object_or_404(mdl_int.cohort.Cohort, pk=cohort_id) form = StudentsImportActionForm(request.POST, request.FILES) errors = [] if form.is_valid(): file_upload = form.cleaned_data['file_upload'] differences = import_xlsx(cohort, BytesIO(file_upload.read())) if differences: return internships_student_import_update(request, cohort_id, differences) else: errors.append(form.errors) display_errors(request, errors) return HttpResponseRedirect( reverse('internships_student_resume', kwargs={"cohort_id": cohort_id}))
def student_save(request, cohort_id): cohort = get_object_or_404(mdl_int.cohort.Cohort, pk=cohort_id) student = mdl_int.internship_student_information.InternshipStudentInformation( cohort=cohort) form = StudentInformationForm(request.POST, instance=student) errors = [] if form.is_valid(): form.save() else: errors.append(form.errors) display_errors(request, errors) return HttpResponseRedirect( reverse("internship_student_form", args=[cohort_id])) return HttpResponseRedirect( reverse("internships_student_resume", args=[cohort_id]))
def edit(request, cohort_id): cohort = get_object_or_404(Cohort, pk=cohort_id) form = CohortForm(data=request.POST or None, instance=cohort) errors = [] if form.is_valid(): form.save() return redirect(reverse('internship')) else: errors.append(form.errors) display_errors(request, errors) context = { 'form': form, 'page_title': _('Edit cohort'), } return render(request, 'cohort/cohort_form.html', context)
def period_save(request, cohort_id, period_id): cohort = get_object_or_404(Cohort, pk=cohort_id) period = get_object_or_404(Period, pk=period_id, cohort_id=cohort_id) form = PeriodForm(data=request.POST, instance=period) errors = [] if (form.is_valid()): form.save() messages.add_message(request, messages.SUCCESS, "{} : {}".format(_('Period edited'), period.name), "alert-success") else: errors.append(form.errors) display_errors(request, errors) context = { 'form': form, 'cohort': cohort, 'url_form': reverse('period_new', kwargs={'cohort_id': cohort.id}), } return render(request, "period_create.html", context) kwargs = {'cohort_id': cohort.id} return HttpResponseRedirect(reverse('internships_periods', kwargs=kwargs))
def modification_student(request, cohort_id, student_id, internship_id=-1, speciality_id="-1"): cohort = get_object_or_404(mdl_int.cohort.Cohort, pk=cohort_id) student = mdl.student.find_by_id(student_id) if int(internship_id) < 1: internship = mdl_int.internship.Internship.objects.filter( cohort=cohort, pk__gte=1).order_by('speciality__name').first() internship_id = internship.id else: internship = mdl_int.internship.Internship.objects.get( pk=internship_id) speciality = _get_speciality(internship_id, speciality_id, student) if speciality: speciality_id = speciality.id internships_offers = mdl_int.internship_offer.find_by_speciality( speciality).select_related( "organization", "speciality", ) offer_preference_formset = _initialize_offer_preference_formset( internships_offers) formset = offer_preference_formset() if request.method == 'POST': formset = offer_preference_formset(request.POST) if formset.is_valid(): messages.add_message(request, messages.SUCCESS, _("Choices saved"), "alert-success") _remove_previous_choices(student, internship) _save_student_choices(formset, student, internship, speciality, cohort) else: display_errors(request, formset.errors) student_choices = mdl_int.internship_choice.search_by_student_or_choice( student=student, internship=internship).select_related("organization") internships = mdl_int.internship.Internship.objects.filter(cohort=cohort, pk__gte=1)\ .order_by("speciality__name", "name")\ .select_related("speciality") zipped_data = _prepare_template_data(formset, student_choices, internships_offers, speciality, student, internship_id) information = mdl_int.internship_student_information.find_by_person( student.person, cohort) context = { "internships": internships, "speciality_form": SpecialityForm(cohort=cohort, specialty_id=speciality_id), "formset": formset, "offers_forms": zipped_data, "internship": internship, "speciality_id": int(speciality_id), "student": student, "information": information, "cohort": cohort } return render(request, "internship_modification_student.html", context)