38 lines
1002 B
Python
38 lines
1002 B
Python
import requests
|
|
from django.http import Http404
|
|
from django.shortcuts import get_object_or_404
|
|
from patients.models import PatientProfile
|
|
|
|
|
|
class PatientContextMixin:
|
|
"""
|
|
Resolve the patient automatically from:
|
|
1) URL kwarg `patient_id`
|
|
2) Query param `?patient=<uuid|pk>`
|
|
3) Logged-in user's linked PatientProfile
|
|
"""
|
|
patient_kwarg_name = "patient_id"
|
|
patient_query_param = "patient"
|
|
|
|
def get_patient(self):
|
|
tenant = self.request.user.tenant
|
|
|
|
# 1) URL kwarg
|
|
patient_id = self.kwargs.get(self.patient_kwarg_name)
|
|
if patient_id:
|
|
patient = get_object_or_404(
|
|
PatientProfile,
|
|
pk=patient_id
|
|
)
|
|
|
|
return patient
|
|
|
|
# 2) Query param
|
|
patient_qp = self.request.GET.get(self.patient_query_param)
|
|
if patient_qp:
|
|
patient = get_object_or_404(
|
|
PatientProfile,
|
|
pk=patient_qp
|
|
)
|
|
|
|
return patient |