We get the most relevant feedback about a doctor depending on the speciality
, intervention_type
and sub_intervention_type
a patient is going to use.
The ordering is important here, we match feedback on speciality
and sub_intervention_type
first. If that's not available, try less specific matches.
I wonder if there is a better/prettier way to do this.
I thought about having each condition (e.g. feedback.intervention_type == intervention_type
) as a method of a class.
def get_feedback_condition_method_names(self):
return [method_name for method_name in dir(self) if method_name.startswith("feedback_condition")]
We could then loop through get_feedback_condition_method_names()
and execute the methods in a loop. This way it's easy to add additional conditions, but we probably won't need to add additional conditions, so the below "functional" would be OK and the above would be overkill. Feedback welcome.
Code for review:
def get_doctor_feedback(*, doctor, speciality, intervention_type, sub_intervention_type):
feedbacks = DoctorFeedback.objects.filter(doctor=doctor)
result = [
feedback for feedback in feedbacks
if feedback.speciality == speciality and feedback.sub_intervention_type == sub_intervention_type
]
if not result:
result = [
feedback for feedback in feedbacks
if feedback.sub_intervention_type == sub_intervention_type
]
if not result:
result = [
feedback for feedback in feedbacks
if feedback.intervention_type == intervention_type
]
if not result:
result = [
feedback for feedback in feedbacks
if feedback.speciality == speciality
]
if not result:
result = feedbacks
if not result:
return doctor.default_feedback
return result.sort(key=lambda feedback: feedback.rating)