old_contents: from flask import request
  from radar.organisations import get_radar_organisation
  
  from radar_api.serializers.recruitment_stats import DataPointsSerializer, CohortRecruitmentRequestSerializer, \
      OrganisationRecruitmentRequestSerializer
  from radar.models import CohortPatient, OrganisationPatient
  from radar.recruitment_stats import recruitment_by_month
  from radar.validation.core import ValidationError
  from radar.views.core import response_json, ApiView
  
  
  class CohortRecruitmentStatsView(ApiView):
      @response_json(DataPointsSerializer)
      def get(self):
          serializer = CohortRecruitmentRequestSerializer()
          args = serializer.args_to_value(request.args)
  
          cohort = args.get('cohort')
  
          if cohort is None:
              raise ValidationError({'cohort': 'This field is required.'})
  
          points = recruitment_by_month(CohortPatient.created_date, [CohortPatient.cohort == cohort])
  
          return {'points': points}
  
  
  class OrganisationRecruitmentStatsView(ApiView):
      @response_json(DataPointsSerializer)
      def get(self):
          serializer = OrganisationRecruitmentRequestSerializer()
          args = serializer.args_to_value(request.args)
  
          organisation = args.get('organisation')
  
          if organisation is None:
              raise ValidationError({'organisation': 'This field is required.'})
  
          points = recruitment_by_month(OrganisationPatient.created_date, [OrganisationPatient.organisation == organisation])
  
          return {'points': points}
  
  
  class PatientRecruitmentStatsView(ApiView):
      @response_json(DataPointsSerializer)
      def get(self):
          organisation = get_radar_organisation()
          points = recruitment_by_month(OrganisationPatient.created_date, [OrganisationPatient.organisation == organisation])
          return {'points': points}
  
  
  def register_views(app):
      app.add_url_rule('/cohort-recruitment-stats', view_func=CohortRecruitmentStatsView.as_view('cohort_recruitment_stats'))
      app.add_url_rule('/organisation-recruitment-stats', view_func=OrganisationRecruitmentStatsView.as_view('organisation_recruitment_stats'))
      app.add_url_rule('/patient-recruitment-stats', view_func=PatientRecruitmentStatsView.as_view('patient_recruitment_stats'))
  
new_contents: from flask import request
  
  from radar.cohorts import get_radar_cohort
  from radar_api.serializers.recruitment_stats import DataPointsSerializer, CohortRecruitmentRequestSerializer, \
      OrganisationRecruitmentRequestSerializer
  from radar.models import CohortPatient, OrganisationPatient
  from radar.recruitment_stats import recruitment_by_month
  from radar.validation.core import ValidationError
  from radar.views.core import response_json, ApiView
  
  
  class CohortRecruitmentStatsView(ApiView):
      @response_json(DataPointsSerializer)
      def get(self):
          serializer = CohortRecruitmentRequestSerializer()
          args = serializer.args_to_value(request.args)
  
          cohort = args.get('cohort')
  
          if cohort is None:
              raise ValidationError({'cohort': 'This field is required.'})
  
          points = recruitment_by_month(CohortPatient.created_date, [CohortPatient.cohort == cohort])
  
          return {'points': points}
  
  
  class OrganisationRecruitmentStatsView(ApiView):
      @response_json(DataPointsSerializer)
      def get(self):
          serializer = OrganisationRecruitmentRequestSerializer()
          args = serializer.args_to_value(request.args)
  
          organisation = args.get('organisation')
  
          if organisation is None:
              raise ValidationError({'organisation': 'This field is required.'})
  
          points = recruitment_by_month(OrganisationPatient.created_date, [OrganisationPatient.organisation == organisation])
  
          return {'points': points}
  
  
  class PatientRecruitmentStatsView(ApiView):
      @response_json(DataPointsSerializer)
      def get(self):
          cohort = get_radar_cohort()
          points = recruitment_by_month(CohortPatient.recruited_date, [CohortPatient.cohort == cohort])
          return {'points': points}
  
  
  def register_views(app):
      app.add_url_rule('/cohort-recruitment-stats', view_func=CohortRecruitmentStatsView.as_view('cohort_recruitment_stats'))
      app.add_url_rule('/organisation-recruitment-stats', view_func=OrganisationRecruitmentStatsView.as_view('organisation_recruitment_stats'))
      app.add_url_rule('/patient-recruitment-stats', view_func=PatientRecruitmentStatsView.as_view('patient_recruitment_stats'))
  
subject: Fix radar recruitment graph