""" Utility views for miscellaneous API endpoints """ from rest_framework.views import APIView from rest_framework.response import Response from rest_framework import permissions class GeoDetectView(APIView): """ Detect user's country from request headers. Uses Cloudflare's CF-IPCountry header if behind Cloudflare, otherwise falls back to checking X-Country-Code or other headers. This endpoint is used by the frontend to determine which signup page to show (/signup for global, /signup/pk for Pakistan). """ permission_classes = [permissions.AllowAny] def get(self, request): # Try Cloudflare header first (most reliable when behind CF) country_code = request.META.get('HTTP_CF_IPCOUNTRY', '') # Fallback to X-Country-Code header (can be set by load balancer/CDN) if not country_code: country_code = request.META.get('HTTP_X_COUNTRY_CODE', '') # Fallback to GeoIP2 if available (Django GeoIP2 middleware) if not country_code: country_code = getattr(request, 'country_code', '') # Default to empty string (frontend will treat as global) country_code = country_code.upper() if country_code else '' return Response({ 'success': True, 'data': { 'country_code': country_code, 'detected': bool(country_code), } })