Example #1
0
    def test_from_settings(self, stringport):
        mock_backend = MagicMock('Mock Backend')
        stringport.return_value = mock_backend

        with self.settings(OPAL_SEARCH_BACKEND='mybackend'):
            backend = queries.create_query(self.user, [])
            self.assertEqual(mock_backend.return_value, backend)
            mock_backend.assert_called_with(self.user, [])
Example #2
0
    def test_from_settings(self, stringport):
        mock_backend = MagicMock('Mock Backend')
        stringport.return_value = mock_backend

        with self.settings(OPAL_SEARCH_BACKEND='mybackend'):
            backend = queries.create_query(self.user, [])
            self.assertEqual(mock_backend.return_value, backend)
            mock_backend.assert_called_with(self.user, [])
Example #3
0
def simple_search_view(request):
    page_number = int(request.GET.get("page_number", 1))
    query_string = request.GET.get("query")
    if not query_string:
        return _build_json_response({'error': "No search terms"}, 400)

    query = queries.create_query(request.user, query_string)
    result = query.fuzzy_query()
    return _build_json_response(_add_pagination(result, page_number))
Example #4
0
 def test_database_flow(self, requests_mock):
     patient = omodels.Patient.objects.create()
     patient.create_episode()
     demographics = patient.demographics_set.first()
     demographics.hospital_number = "1231111"
     demographics.first_name = "Sue"
     demographics.save()
     query = queries.create_query(self.user, self.criteria)
     result = query.patients_as_json()[0]
     self.assertEqual(result["demographics"][0]["first_name"], "Sue")
     self.assertFalse(requests_mock.called)
Example #5
0
 def test_database_flow(self, requests_mock):
     patient = omodels.Patient.objects.create()
     patient.create_episode()
     demographics = patient.demographics_set.first()
     demographics.hospital_number = "1231111"
     demographics.first_name = "Sue"
     demographics.save()
     query = queries.create_query(self.user, self.criteria)
     result = query.patients_as_json()[0]
     self.assertEqual(result["demographics"][0]["first_name"], "Sue")
     self.assertFalse(requests_mock.called)
Example #6
0
    def test_recusive_gloss_search(self, requests_mock):
        return_1 = copy(self.returned_gloss_result)
        return_1["duplicate_patient"] = [dict(new_id="1000")]

        return_2 = copy(self.returned_gloss_result)
        return_2["duplicate_patient"] = [dict(new_id="1001")]
        return_2["demographics"][0]["first_name"] = "Sue"
        return_2["demographics"][0]["hospital_number"] = "1000"

        return_3 = copy(self.returned_gloss_result)
        return_3["demographics"][0]["first_name"] = "Sarah"
        return_3["demographics"][0]["hospital_number"] = "1001"

        side_effects = []

        for result in [return_1, return_2, return_3]:
            response = MagicMock()
            response.content = json.dumps({
                "status": "success",
                "messages": result
            })
            side_effects.append(response)

        requests_mock.side_effect = side_effects
        query = queries.create_query(self.user, self.criteria)
        result = query.patients_as_json()[0]
        self.assertEqual(requests_mock.call_count, 3)
        merged = [{
            'duplicate_patient': [],
            'demographics': [{
                'first_name': u'Sarah',
                'post_code': None,
                'surname': u'Smith',
                'gp_practice_code': None,
                'title': u'Ms',
                'hospital_number': 1001,
                'marital_status': None,
                'date_of_death': None,
                'sex': None,
                'external_system': 'Carecast',
                'date_of_birth': u'12/12/1983',
                'death_indicator': False,
                'middle_name': None,
                'hospital_number': "1001",
                'ethnicity': None
            }]
        }]

        expected = copy(self.returned_gloss_result)
        expected["demographics"][0]['hospital_number'] = "1231111"
        expected["duplicate_patient"] = [{u'new_id': "1001"}]
        expected["merged"] = merged
        self.assertEqual(result, expected)
Example #7
0
    def test_recusive_gloss_search(self, requests_mock):
        return_1 = copy(self.returned_gloss_result)
        return_1["duplicate_patient"] = [dict(new_id="1000")]

        return_2 = copy(self.returned_gloss_result)
        return_2["duplicate_patient"] = [dict(new_id="1001")]
        return_2["demographics"][0]["first_name"] = "Sue"
        return_2["demographics"][0]["hospital_number"] = "1000"

        return_3 = copy(self.returned_gloss_result)
        return_3["demographics"][0]["first_name"] = "Sarah"
        return_3["demographics"][0]["hospital_number"] = "1001"

        side_effects = []

        for result in [return_1, return_2, return_3]:
            response = MagicMock()
            response.content = json.dumps({
                "status": "success",
                "messages": result
            })
            side_effects.append(response)

        requests_mock.side_effect = side_effects
        query = queries.create_query(self.user, self.criteria)
        result = query.patients_as_json()[0]
        self.assertEqual(requests_mock.call_count, 3)
        merged = [{
            'duplicate_patient': [],
            'demographics': [{
                'first_name': u'Sarah',
                'post_code': None,
                'surname': u'Smith',
                'gp_practice_code': None,
                'title': u'Ms',
                'hospital_number': 1001,
                'marital_status': None,
                'date_of_death': None,
                'sex': None,
                'external_system': 'Carecast',
                'date_of_birth': u'12/12/1983',
                'death_indicator': False,
                'middle_name': None,
                'hospital_number': "1001",
                'ethnicity': None}]
        }]

        expected = copy(self.returned_gloss_result)
        expected["demographics"][0]['hospital_number'] = "1231111"
        expected["duplicate_patient"] = [{u'new_id': "1001"}]
        expected["merged"] = merged
        self.assertEqual(result, expected)
Example #8
0
    def post(self, *args, **kwargs):
        request_data = _get_request_data(self.request)
        page_number = 1

        if "page_number" in request_data[0]:
            page_number = request_data[0].pop("page_number", 1)

        query = queries.create_query(
            self.request.user,
            request_data,
        )
        eps = query.get_patient_summaries()

        return _build_json_response(_add_pagination(eps, page_number))
Example #9
0
def patient_search_view(request):
    hospital_number = request.GET.get("hospital_number")

    if hospital_number is None:
        return json_response({'error': "No search terms"}, 400)

    criteria = [{
        "queryType": "Equals",
        "query": hospital_number,
        "field": "Hospital Number",
        'combine': 'and',
        'column': u'demographics',
    }]

    query = queries.create_query(request.user, criteria)
    return json_response(query.patients_as_json())
Example #10
0
    def post(self, *args, **kwargs):
        if getattr(settings, 'EXTRACT_ASYNC', None):
            criteria = _get_request_data(self.request)['criteria']
            extract_id = async_extract(self.request.user, json.loads(criteria))
            return json_response({'extract_id': extract_id})

        query = queries.create_query(self.request.user,
                                     json.loads(self.request.POST['criteria']))
        episodes = query.get_episodes()
        fname = zip_archive(episodes, query.description(), self.request.user)
        resp = HttpResponse(open(fname, 'rb').read())
        disp = 'attachment; filename="{0}extract{1}.zip"'.format(
            settings.OPAL_BRAND_NAME,
            datetime.datetime.now().isoformat())
        resp['Content-Disposition'] = disp
        return resp
Example #11
0
def patient_search_view(request):
    hospital_number = request.GET.get("hospital_number")

    if hospital_number is None:
        return _build_json_response({'error': "No search terms"}, 400)

    criteria = [{
        "queryType": "Equals",
        "query": hospital_number,
        "field": "Hospital Number",
        'combine': 'and',
        'column': u'demographics',
    }]

    query = queries.create_query(request.user, criteria)
    return _build_json_response(query.patients_as_json())
Example #12
0
    def post(self, *args, **kwargs):
        request_data = _get_request_data(self.request)
        page_number = 1

        if not request_data:
            return json_response(dict(error="No search criteria provied"),
                                 status_code=status.HTTP_400_BAD_REQUEST)

        if "page_number" in request_data[0]:
            page_number = request_data[0].pop("page_number", 1)

        query = queries.create_query(
            self.request.user,
            request_data,
        )
        patient_summaries = query.get_patient_summaries()

        return json_response(_add_pagination(patient_summaries, page_number))
Example #13
0
    def test_gloss_query_flow(self, requests_mock):
        requests_mock.return_value = MagicMock()
        requests_mock.return_value.content = json.dumps({
            "status": "success",
            "messages": self.returned_gloss_result
        })
        self.assertFalse(omodels.Patient.objects.exists())

        query = queries.create_query(self.user, self.criteria)
        result = query.patients_as_json()[0]

        expected = copy(self.returned_gloss_result)
        expected["demographics"][0]["hospital_number"] = "1231111"
        expected["demographics"][0]["external_system"] = "Carecast"
        self.assertEqual(result, expected)
        requests_mock.assert_called_once_with(
            "http://0.0.0.0:6767/api/demographics/1231111"
        )
Example #14
0
    def post(self, *args, **kwargs):
        if getattr(settings, 'EXTRACT_ASYNC', None):
            criteria = _get_request_data(self.request)['criteria']
            extract_id = async_extract(
                self.request.user,
                json.loads(criteria)
            )
            return json_response({'extract_id': extract_id})

        query = queries.create_query(
            self.request.user, json.loads(self.request.POST['criteria'])
        )
        episodes = query.get_episodes()
        fname = zip_archive(episodes, query.description(), self.request.user)
        resp = HttpResponse(open(fname, 'rb').read())
        disp = 'attachment; filename="{0}extract{1}.zip"'.format(
            settings.OPAL_BRAND_NAME, datetime.datetime.now().isoformat())
        resp['Content-Disposition'] = disp
        return resp
Example #15
0
    def test_gloss_query_flow(self, requests_mock):
        requests_mock.return_value = MagicMock()
        requests_mock.return_value.content = json.dumps({
            "status":
            "success",
            "messages":
            self.returned_gloss_result
        })
        self.assertFalse(omodels.Patient.objects.exists())

        query = queries.create_query(self.user, self.criteria)
        result = query.patients_as_json()[0]

        expected = copy(self.returned_gloss_result)
        expected["demographics"][0]["hospital_number"] = "1231111"
        expected["demographics"][0]["external_system"] = "Carecast"
        self.assertEqual(result, expected)
        requests_mock.assert_called_once_with(
            "http://0.0.0.0:6767/api/demographics/1231111")
Example #16
0
    def post(self, *args, **kwargs):
        request_data = _get_request_data(self.request)
        page_number = 1

        if not request_data:
            return json_response(
                dict(error="No search criteria provied"),
                status_code=status.HTTP_400_BAD_REQUEST
            )

        if "page_number" in request_data[0]:
            page_number = request_data[0].pop("page_number", 1)

        query = queries.create_query(
            self.request.user,
            request_data,
        )
        patient_summaries = query.get_patient_summaries()

        return json_response(_add_pagination(patient_summaries, page_number))
Example #17
0
def simple_search_view(request):
    page_number = int(request.GET.get("page_number", 1))
    query_string = request.GET.get("query")
    if not query_string:
        return json_response({'error': "No search terms"}, 400)

    query = queries.create_query(request.user, query_string)
    patients = query.fuzzy_query()
    paginated = _add_pagination(patients, page_number)
    paginated_patients = paginated["object_list"]

    # on postgres it blows up if we don't manually manage this
    if not paginated_patients:
        paginated_patients = models.Patient.objects.none()
    episodes = models.Episode.objects.filter(
        id__in=paginated_patients.values_list("episode__id", flat=True))
    paginated["object_list"] = query.get_aggregate_patients_from_episodes(
        episodes)

    return json_response(paginated)
Example #18
0
def simple_search_view(request):
    page_number = int(request.GET.get("page_number", 1))
    query_string = request.GET.get("query")
    if not query_string:
        return json_response({'error': "No search terms"}, 400)

    query = queries.create_query(request.user, query_string)
    patients = query.fuzzy_query()
    paginated = _add_pagination(patients, page_number)
    paginated_patients = paginated["object_list"]

    # on postgres it blows up if we don't manually manage this
    if not paginated_patients:
        paginated_patients = models.Patient.objects.none()
    episodes = models.Episode.objects.filter(
        id__in=paginated_patients.values_list("episode__id", flat=True)
    )
    paginated["object_list"] = query.get_aggregate_patients_from_episodes(
        episodes
    )

    return json_response(paginated)
Example #19
0
def extract(user, criteria):
    from opal.core.search import queries, extract
    query = queries.create_query(user, criteria)
    episodes = query.get_episodes()
    fname = extract.zip_archive(episodes, query.description(), user)
    return fname