haikal/tours/views.py
2025-06-22 13:25:54 +03:00

55 lines
1.7 KiB
Python

import os
import json
from django.shortcuts import render, get_object_or_404
from django.http import JsonResponse
from django.contrib.auth.decorators import login_required
from django.conf import settings
from .models import Tour, TourCompletion
@login_required
def tour_list(request):
tours = Tour.objects.filter(is_active=True)
return render(request, "tours/tour_list.html", {"tours": tours})
@login_required
def get_tour_data(request, slug):
tour = get_object_or_404(Tour, slug=slug, is_active=True)
# Check if user has already completed this tour
completed = TourCompletion.objects.filter(tour=tour, user=request.user).exists()
# Load the tour data from JSON file
tour_file_path = os.path.join(
settings.BASE_DIR, "static", "js", "tours", tour.tour_file
)
try:
with open(tour_file_path, "r") as f:
tour_data = json.load(f)
except (FileNotFoundError, json.JSONDecodeError):
return JsonResponse({"error": "Tour data not found or invalid"}, status=404)
return JsonResponse({"tour": tour_data, "completed": completed})
@login_required
def mark_tour_completed(request, slug):
if request.method != "POST":
return JsonResponse({"error": "Method not allowed"}, status=405)
tour = get_object_or_404(Tour, slug=slug, is_active=True)
# Mark the tour as completed for this user
TourCompletion.objects.get_or_create(tour=tour, user=request.user)
return JsonResponse({"status": "success"})
@login_required
def start_tour_view(request, slug):
tour = get_object_or_404(Tour, slug=slug, is_active=True)
# Redirect to the page where the tour should start
return render(request, "tours/start_tour.html", {"tour": tour})