""" API views for consent-related operations. """ from django.http import JsonResponse from django.views.decorators.http import require_GET from django.contrib.auth.decorators import login_required from .models import Patient, ConsentTemplate @login_required @require_GET def get_consent_content(request): """ API endpoint to get consent content based on patient and consent type. Query Parameters: - patient_id: UUID of the patient - consent_type: Type of consent (GENERAL_TREATMENT, SERVICE_SPECIFIC, etc.) - language: 'en' or 'ar' (optional, defaults to 'en') Returns: JSON response with consent content or error message """ patient_id = request.GET.get('patient_id') consent_type = request.GET.get('consent_type') language = request.GET.get('language', 'en') # Validate parameters if not patient_id or not consent_type: return JsonResponse({ 'success': False, 'error': 'Missing required parameters: patient_id and consent_type' }, status=400) try: # Get patient patient = Patient.objects.get( id=patient_id, tenant=request.user.tenant ) except Patient.DoesNotExist: return JsonResponse({ 'success': False, 'error': 'Patient not found' }, status=404) try: # Get the latest active consent template for this type template = ConsentTemplate.objects.filter( tenant=request.user.tenant, consent_type=consent_type, is_active=True ).order_by('-version').first() if not template: return JsonResponse({ 'success': False, 'error': f'No consent template found for type: {consent_type}' }, status=404) # Get populated content content = template.get_populated_content(patient, language) return JsonResponse({ 'success': True, 'content': content, 'template_id': str(template.id), 'template_version': template.version, 'title': template.title_en if language == 'en' else template.title_ar or template.title_en }) except Exception as e: return JsonResponse({ 'success': False, 'error': f'Error loading consent template: {str(e)}' }, status=500)