def accept_exchange(request, timetable_slug, exchange_id): selected_timetable = get_object_or_404(Timetable, slug=timetable_slug) student = Student.from_user(request.user) exchange = get_object_or_404(Exchange, id=exchange_id) if exchange.is_finalized(): header = "Exchange already finalized." message = "This exchange has already been finalized." elif exchange.is_cancelled(): header = "Exchange cancelled." message = "This exchange is cancelled." else: subject = Activity.from_timetable_activity( exchange.allocation_from.activityRealization.activity).subject try: any_fulfilled = process_new_exchange_request( selected_timetable, student, exchange.initiator_student if exchange.get_type() == ExchangeType.SPECIFIC_STUDENT else None, {subject.id: exchange.allocation_from}) except FormProcessingError as e: return render( request, "exchange/exchange_create_result.html", { "selected_timetable": selected_timetable, "header": e.header, "message": e.message }) if any_fulfilled: header = "Exchange fulfilled!" message = "Your request was fulfilled. Check your new slot on your timetable." else: header = "Request added to the queue." message = "Your request could not be fulfilled immediately and was placed into the queue for it to " \ "be fulfilled in the future. Check back at a later date!" return render( request, "exchange/exchange_create_result.html", { "selected_timetable": selected_timetable, "header": header, "message": message })
def landing_student(request, timetable_slug): selected_timetable = get_object_or_404(Timetable, slug=timetable_slug) student = Student.from_user(request.user) student_exchanges = get_student_exchanges(selected_timetable, student) pending_exchanges = [ ex for ex in student_exchanges if not ex.is_finalized() ] completed_exchanges = [ex for ex in student_exchanges if ex.is_finalized()] student_subjects = get_student_subject_list(selected_timetable, student) available_subject_exchanges_allocations = [] for s in student_subjects: # don't show the subject if the student doesn't attend any lab cycles try: a = get_current_student_subject_allocation(selected_timetable, student, s, ["LAB", "LV", "AV"]) except: continue available_subject_exchanges_allocations.append( (s, get_subject_exchanges(selected_timetable, s), a)) return render( request, "exchange/student_main.html", { 'selected_timetable': selected_timetable.__dict__, 'user': request.user.__dict__, 'student': student, 'pending_exchanges': pending_exchanges, 'completed_exchanges': completed_exchanges, 'available_subject_exchanges_allocations': available_subject_exchanges_allocations, })
def cancel_exchange(request, timetable_slug, exchange_id): selected_timetable = get_object_or_404(Timetable, slug=timetable_slug) exchange = get_object_or_404(Exchange, id=exchange_id) if getattr(request.user, 'teacher', None) is not None: cancellable = True else: student = Student.from_user(request.user) cancellable = is_exchange_cancellable(exchange, student) if cancellable: controllers.cancel_exchange(exchange) header = "Exchange cancelled." message = "The selected exchange has been cancelled." else: header = "Cancellation unauthorised," message = "You do not have permission to cancel this exchange." return render( request, "exchange/exchange_create_result.html", { "selected_timetable": selected_timetable, "header": header, "message": message })
def test_get_student_from_user_does_not_exist(self): admin_user = User.objects.get(username="******") with self.assertRaises(Student.DoesNotExist): Student.from_user(admin_user)
def test_get_student_from_user_exists(self): result = Student.from_user(self.known_student_user) self.assertEqual(self.known_student_student, result)
def user_is_student(user): return Student.from_user(user) is not None
def create_exchange_student(request, timetable_slug): selected_timetable = get_object_or_404(Timetable, slug=timetable_slug) student = Student.from_user(request.user) subjects = get_student_subject_list(selected_timetable, student) subject_available_allocation_map = {} subject_attending_allocation_map = {} for subject in subjects: activity_types = ["LAB", "LV", "AV"] try: student_allocation = get_current_student_subject_allocation( selected_timetable, student, subject, activity_types) allocations = get_student_subject_other_allocations( selected_timetable, student, subject, activity_types) subject_available_allocation_map[subject] = allocations subject_attending_allocation_map[subject] = student_allocation except Allocation.DoesNotExist: # don't show subjects the student doesn't have attend labs for pass if request.method == "POST": form = ExchangeCreationForm(subject_available_allocation_map, subject_attending_allocation_map, data=request.POST) if form.is_valid(): try: requested_student_string = form.get_requested_student() requested_student = None if requested_student_string: requested_student = parse_student_from_ambiguous_identifier( requested_student_string) any_fulfilled = process_new_exchange_request( selected_timetable, student, requested_student, form.get_subject_transfers(keep_empty=False)) except FormProcessingError as e: return render( request, "exchange/exchange_create_result.html", { "selected_timetable": selected_timetable, "header": e.header, "message": e.message }) if any_fulfilled: header = "Exchange fulfilled immediately!" message = "Your request was fulfilled immediately. Check your new slot on your timetable." else: header = "Request added to the queue." message = "Your request could not be fulfilled immediately and was placed into the queue for it to " \ "be fulfilled in the future. Check back at a later date!" return render( request, "exchange/exchange_create_result.html", { "selected_timetable": selected_timetable, "header": header, "message": message }) # otherwise fall through to rendering the same form, with the data filled out, as is tradition else: form = ExchangeCreationForm(subject_available_allocation_map, subject_attending_allocation_map) return render(request, "exchange/exchange_create.html", { "selected_timetable": selected_timetable, "form": form })