@@ -386,9 +386,30 @@ def get_booking_requests(request):
386386@permission_classes ([IsAuthenticated ]) 
387387@authentication_classes ([TokenAuthentication ]) 
388388def  get_active_bookings (request ):
389+     # intenders 
390+     intenders  =  User .objects .all ()
391+     user  =  request .user 
392+     vhcaretaker  =  request .user .holds_designations .filter (
393+         designation__name = 'VhCaretaker' ).exists ()
394+     vhincharge  =  request .user .holds_designations .filter (
395+         designation__name = 'VhIncharge' ).exists ()
396+ 
397+     # finding designation of user 
398+     user_designation  =  "Intender" 
399+     if  vhincharge :
400+         user_designation  =  "VhIncharge" 
401+     elif  vhcaretaker :
402+         user_designation  =  "VhCaretaker" 
403+ 
389404    if  request .method  ==  'GET' :
390-         active_bookings  =  BookingDetail .objects .select_related (
391-             'intender' , 'caretaker' ).filter (status = "Confirmed" )
405+         print ("User Designation: " , user_designation )
406+ 
407+         if  user_designation  in  ["VhIncharge" , "VhCaretaker" ]:
408+             # Fetch all confirmed bookings for VhCaretaker or VhIncharge 
409+             active_bookings  =  BookingDetail .objects .select_related ('intender' , 'caretaker' ).filter ( Q (status = "Forward" ) |  Q (status = "CheckedIn" ) |  Q (status = "Pending" ), booking_to__gte = datetime .datetime .today ())
410+         else :
411+             # Filter active bookings for the logged-in user (intender) 
412+             active_bookings  =  BookingDetail .objects .select_related ('intender' , 'caretaker' ).filter ( Q (status = "Forward" ) |  Q (status = "CheckedIn" ) |  Q (status = "Pending" ), booking_to__gte = datetime .datetime .today ())
392413
393414        # Serialize the queryset to a list of dictionaries 
394415        bookings_list  =  [
@@ -409,6 +430,7 @@ def get_active_bookings(request):
409430        return  JsonResponse ({'error' : 'Invalid request method' }, status = 400 )
410431
411432
433+ 
412434# @login_required(login_url='/accounts/login/') 
413435# def get_active_bookings(request): 
414436#     if request.method == 'POST': 
@@ -420,18 +442,114 @@ def get_active_bookings(request):
420442#         return HttpResponseRedirect('/visitorhostel/') 
421443
422444
423- @login_required (login_url = '/accounts/login/' ) 
445+ @api_view (['GET' ]) 
446+ @permission_classes ([IsAuthenticated ]) 
447+ @authentication_classes ([TokenAuthentication ]) 
424448def  get_inactive_bookings (request ):
425-     if  request .method  ==  'POST' :
426-         inactive_bookings  =  BookingDetail .objects .select_related ('intender' , 'caretaker' ).filter (
427-             Q (status = "Cancelled" ) |  Q (status = "Rejected" ) |  Q (status = "Complete" ))
449+     # intenders 
450+     intenders  =  User .objects .all ()
451+     user  =  request .user 
452+     vhcaretaker  =  request .user .holds_designations .filter (
453+         designation__name = 'VhCaretaker' ).exists ()
454+     vhincharge  =  request .user .holds_designations .filter (
455+         designation__name = 'VhIncharge' ).exists ()
456+ 
457+     # finding designation of user 
458+     user_designation  =  "Intender" 
459+     if  vhincharge :
460+         user_designation  =  "VhIncharge" 
461+     elif  vhcaretaker :
462+         user_designation  =  "VhCaretaker" 
463+ 
464+     if  request .method  ==  'GET' :
465+         print ("User Designation: " , user_designation )
466+ 
467+         if  user_designation  in  ["VhIncharge" , "VhCaretaker" ]:
468+             # Fetch all cancelled bookings for VhCaretaker or VhIncharge 
469+             cancelled_bookings  =  BookingDetail .objects .select_related ('intender' , 'caretaker' ).filter (Q (status = "Canceled" ) |  Q (status = "Rejected" ))
470+         else :
471+             # Filter cancelled bookings for the logged-in user (intender) 
472+             cancelled_bookings  =  BookingDetail .objects .select_related ('intender' , 'caretaker' ).filter (Q (status = "Canceled" ) |  Q (status = "Rejected" ), intender = request .user )
473+ 
474+         # Serialize the queryset to a list of dictionaries 
475+         bookings_list  =  [
476+             {
477+                 'id' : booking .id ,
478+                 'intender' : booking .intender .first_name ,
479+                 'email' : booking .intender .email ,
480+                 'bookingFrom' : booking .booking_from .isoformat () if  booking .booking_from  else  None ,
481+                 'bookingTo' : booking .booking_to .isoformat () if  booking .booking_to  else  None ,
482+                 'category' : booking .visitor_category ,
483+                 # 'status': booking.status,  # Optional, if you need to include it 
484+             }
485+             for  booking  in  cancelled_bookings 
486+         ]
428487
429-         return  render ( request ,  "vhModule/visitorhostel.html" , { 'inactive_bookings 'inactive_bookings })
488+         return  JsonResponse ({ 'cancelled_bookings 'bookings_list })
430489    else :
431-         return  HttpResponseRedirect ('/visitorhostel/' )
490+         return  JsonResponse ({'error' : 'Invalid request method' }, status = 400 )
491+ 
492+ 
493+ # @login_required(login_url='/accounts/login/') 
494+ # def get_inactive_bookings(request): 
495+ #     if request.method == 'POST': 
496+ #         inactive_bookings = BookingDetail.objects.select_related('intender', 'caretaker').filter( 
497+ #             Q(status="Cancelled") | Q(status="Rejected") | Q(status="Complete")) 
498+ 
499+ #         return render(request, "vhModule/visitorhostel.html", {'inactive_bookings': inactive_bookings}) 
500+ #     else: 
501+ #         return HttpResponseRedirect('/visitorhostel/') 
432502
433503# Method for making booking request 
434504
505+ @api_view (['GET' ]) 
506+ @permission_classes ([IsAuthenticated ]) 
507+ @authentication_classes ([TokenAuthentication ]) 
508+ def  get_completed_bookings (request ):
509+     # intenders 
510+     intenders  =  User .objects .all ()
511+     user  =  request .user 
512+     vhcaretaker  =  request .user .holds_designations .filter (
513+         designation__name = 'VhCaretaker' ).exists ()
514+     vhincharge  =  request .user .holds_designations .filter (
515+         designation__name = 'VhIncharge' ).exists ()
516+ 
517+     # Determine the user's designation 
518+     user_designation  =  "Intender" 
519+     if  vhincharge :
520+         user_designation  =  "VhIncharge" 
521+     elif  vhcaretaker :
522+         user_designation  =  "VhCaretaker" 
523+ 
524+     if  request .method  ==  'GET' :
525+         print ("User Designation: " , user_designation )
526+ 
527+         if  user_designation  in  ["VhIncharge" , "VhCaretaker" ]:
528+             # Fetch all completed bookings for VhCaretaker or VhIncharge 
529+             completed_bookings  =  BookingDetail .objects .select_related (
530+                 'intender' , 'caretaker' ).filter (check_out__lt = datetime .datetime .today (), intender = user ).order_by ('booking_from' ).reverse ()
531+         else :
532+             # Filter completed bookings for the logged-in user (intender) 
533+             completed_bookings  =  BookingDetail .objects .select_related (
534+                 'intender' , 'caretaker' ).filter (check_out__lt = datetime .datetime .today (), intender = user ).order_by ('booking_from' ).reverse ()
535+ 
536+         # Serialize the queryset to a list of dictionaries 
537+         bookings_list  =  [
538+             {
539+                 'id' : booking .id ,
540+                 'intender' : booking .intender .first_name ,
541+                 'email' : booking .intender .email ,
542+                 'bookingFrom' : booking .booking_from .isoformat () if  booking .booking_from  else  None ,
543+                 'bookingTo' : booking .booking_to .isoformat () if  booking .booking_to  else  None ,
544+                 'category' : booking .visitor_category ,
545+             }
546+             for  booking  in  completed_bookings 
547+         ]
548+ 
549+         return  JsonResponse ({'completed_bookings' : bookings_list })
550+     else :
551+         return  JsonResponse ({'error' : 'Invalid request method' }, status = 400 )
552+ 
435553
436554@login_required (login_url = '/accounts/login/' ) 
437555def  get_booking_form (request ):
@@ -820,7 +938,9 @@ def confirm_booking(request):
820938        return  HttpResponseRedirect ('/visitorhostel/' )
821939
822940
823- @login_required (login_url = '/accounts/login/' ) 
941+ @api_view (['POST' ]) 
942+ @permission_classes ([IsAuthenticated ]) 
943+ @authentication_classes ([TokenAuthentication ]) 
824944def  cancel_booking (request ):
825945    if  request .method  ==  'POST' :
826946        user  =  request .user 
@@ -879,8 +999,10 @@ def cancel_booking_request(request):
879999
8801000
8811001# rehject a booking request 
882- 
883- @login_required (login_url = '/accounts/login/' ) 
1002+ @csrf_exempt  
1003+ @api_view (['POST' ]) 
1004+ @permission_classes ([IsAuthenticated ]) 
1005+ @authentication_classes ([TokenAuthentication ]) 
8841006def  reject_booking (request ):
8851007    if  request .method  ==  'POST' :
8861008        booking_id  =  request .POST .get ('booking-id' )
0 commit comments