diff --git a/FusionIIIT/applications/visitor_hostel/views.py b/FusionIIIT/applications/visitor_hostel/views.py index 1042ac934..1aea81082 100644 --- a/FusionIIIT/applications/visitor_hostel/views.py +++ b/FusionIIIT/applications/visitor_hostel/views.py @@ -30,6 +30,12 @@ from rest_framework.permissions import IsAuthenticated from rest_framework.authentication import TokenAuthentication from rest_framework.decorators import api_view, permission_classes,authentication_classes +from django.http import JsonResponse +from .models import BookingDetail # Make sure to import your BookingDetail model +from django.utils import timezone +from rest_framework.permissions import IsAuthenticated +from rest_framework.authentication import TokenAuthentication +from rest_framework.decorators import api_view, permission_classes,authentication_classes # from .forms import InventoryForm @@ -351,16 +357,42 @@ def get_booking_requests(request): # getting active bookings - -@login_required(login_url='/accounts/login/') +@api_view(['GET']) +@permission_classes([IsAuthenticated]) +@authentication_classes([TokenAuthentication]) def get_active_bookings(request): - if request.method == 'POST': + if request.method == 'GET': active_bookings = BookingDetail.objects.select_related( 'intender', 'caretaker').filter(status="Confirmed") - return render(request, "vhModule/visitorhostel.html", {'active_bookings': active_bookings}) + # Serialize the queryset to a list of dictionaries + bookings_list = [ + { + 'id': booking.id, + 'intender': booking.intender.first_name, + 'email': booking.intender.email, + 'bookingFrom': booking.booking_from.isoformat() if booking.booking_from else None, + 'bookingTo': booking.booking_to.isoformat() if booking.booking_to else None, + 'category': booking.visitor_category, + # 'status': booking.status, + } + for booking in active_bookings + ] + + return JsonResponse({'active_bookings': bookings_list}) else: - return HttpResponseRedirect('/visitorhostel/') + return JsonResponse({'error': 'Invalid request method'}, status=400) + + +# @login_required(login_url='/accounts/login/') +# def get_active_bookings(request): +# if request.method == 'POST': +# active_bookings = BookingDetail.objects.select_related( +# 'intender', 'caretaker').filter(status="Confirmed") + +# return render(request, "vhModule/visitorhostel.html", {'active_bookings': active_bookings}) +# else: +# return HttpResponseRedirect('/visitorhostel/') @login_required(login_url='/accounts/login/')