93 lines
2.8 KiB
Python
93 lines
2.8 KiB
Python
"""
|
|
Core views - Health check and utility views
|
|
"""
|
|
from django.contrib.auth.decorators import login_required
|
|
from django.db import connection
|
|
from django.http import JsonResponse
|
|
from django.shortcuts import redirect, render
|
|
from django.views.decorators.cache import never_cache
|
|
from django.views.decorators.http import require_GET, require_POST
|
|
|
|
|
|
@never_cache
|
|
@require_GET
|
|
def health_check(request):
|
|
"""
|
|
Health check endpoint for monitoring and load balancers.
|
|
Returns JSON with status of various services.
|
|
"""
|
|
health_status = {
|
|
'status': 'ok',
|
|
'services': {}
|
|
}
|
|
|
|
# Check database connection
|
|
try:
|
|
with connection.cursor() as cursor:
|
|
cursor.execute("SELECT 1")
|
|
health_status['services']['database'] = 'ok'
|
|
except Exception as e:
|
|
health_status['status'] = 'error'
|
|
health_status['services']['database'] = f'error: {str(e)}'
|
|
|
|
# Check Redis/Celery (optional - don't fail if not available)
|
|
try:
|
|
from django_celery_beat.models import PeriodicTask
|
|
PeriodicTask.objects.count()
|
|
health_status['services']['celery_beat'] = 'ok'
|
|
except Exception:
|
|
health_status['services']['celery_beat'] = 'not_configured'
|
|
|
|
# Return appropriate status code
|
|
status_code = 200 if health_status['status'] == 'ok' else 503
|
|
|
|
return JsonResponse(health_status, status=status_code)
|
|
|
|
|
|
@login_required
|
|
def select_hospital(request):
|
|
"""
|
|
Hospital selection page for PX Admins.
|
|
|
|
Allows PX Admins to switch between hospitals.
|
|
Stores selected hospital in session.
|
|
"""
|
|
# Only PX Admins should access this page
|
|
if not request.user.is_px_admin():
|
|
return redirect('dashboard:dashboard')
|
|
|
|
from apps.organizations.models import Hospital
|
|
|
|
hospitals = Hospital.objects.all().order_by('name')
|
|
|
|
# Handle hospital selection
|
|
if request.method == 'POST':
|
|
hospital_id = request.POST.get('hospital_id')
|
|
if hospital_id:
|
|
try:
|
|
hospital = Hospital.objects.get(id=hospital_id)
|
|
request.session['selected_hospital_id'] = str(hospital.id)
|
|
# Redirect to referring page or dashboard
|
|
next_url = request.POST.get('next', request.GET.get('next', '/'))
|
|
return redirect(next_url)
|
|
except Hospital.DoesNotExist:
|
|
pass
|
|
|
|
context = {
|
|
'hospitals': hospitals,
|
|
'selected_hospital_id': request.session.get('selected_hospital_id'),
|
|
'next': request.GET.get('next', '/'),
|
|
}
|
|
|
|
return render(request, 'core/select_hospital.html', context)
|
|
|
|
|
|
@login_required
|
|
def no_hospital_assigned(request):
|
|
"""
|
|
Error page for users without a hospital assigned.
|
|
|
|
Users without a hospital assignment cannot access the system.
|
|
"""
|
|
return render(request, 'core/no_hospital_assigned.html', status=403)
|