def create(self, request): """ * Create a subrecord * Ping our integration upstream interface * Render the created subrecord back to the requester Raise appropriate errors for: * Nonexistant episode * Unexpected fields being passed in """ from opal.models import Episode, PatientSubrecord subrecord = self.model() try: episode = Episode.objects.get(pk=request.data['episode_id']) except Episode.DoesNotExist: return Response('Nonexistant episode', status=status.HTTP_400_BAD_REQUEST) pre = episode.to_dict(request.user) if isinstance(subrecord, PatientSubrecord): del request.data['episode_id'] patient_id = episode.patient.pk request.data['patient_id'] = patient_id try: subrecord.update_from_dict(request.data, request.user) except exceptions.APIError: return Response({'error': 'Unexpected field name'}, status=status.HTTP_400_BAD_REQUEST) episode = Episode.objects.get(pk=episode.pk) post = episode.to_dict(request.user) glossolalia.change(pre, post) return Response(subrecord.to_dict(request.user), status=status.HTTP_201_CREATED)
def update(self, request, item): pre = self._item_to_dict(item, request.user) try: item.update_from_dict(request.data, request.user) except exceptions.APIError: return Response({'error': 'Unexpected field name'}, status=status.HTTP_400_BAD_REQUEST) except exceptions.ConsistencyError: return Response({'error': 'Item has changed'}, status=status.HTTP_409_CONFLICT) glossolalia.change(pre, self._item_to_dict(item, request.user)) return Response(item.to_dict(request.user), status=status.HTTP_202_ACCEPTED)
def test_change_calls_send_upstream(self, sender): with patch('opal.core.glossolalia.INTEGRATING', new=True): change = glossolalia.change({}, {'foo': 'bar'}) self.assertEqual('change', sender.call_args[0][0]) self.assertEqual( 'bar', json.loads(sender.call_args[0][1]['data'])['post']['foo'])
def episode_detail_view(request, pk): try: episode = models.Episode.objects.get(pk=pk) except models.Episode.DoesNotExist: return HttpResponseNotFound() if request.method == 'GET': serialized = episode.to_dict(request.user) return _build_json_response(serialized) data = _get_request_data(request) try: pre = episode.to_dict(request.user) episode.update_from_dict(data, request.user) post = episode.to_dict(request.user) glossolalia.change(pre, post) return _build_json_response(episode.to_dict(request.user, shallow=True)) except exceptions.ConsistencyError: return _build_json_response({'error': 'Item has changed'}, 409)
def destroy(self, request, item): pre = self._item_to_dict(item, request.user) item.delete() glossolalia.change(pre, self._item_to_dict(item, request.user)) return Response('deleted', status=status.HTTP_202_ACCEPTED)
def test_change_not_integratng(self, sender): with patch('opal.core.glossolalia.INTEGRATING', new=False): change = glossolalia.change(None, None) self.assertEqual( None, change) # Mostly a proxy for "Does this stacktrace?" self.assertFalse(sender.called)
def test_change_calls_send_upstream(self, sender): with patch('opal.core.glossolalia.INTEGRATING', new=True): change = glossolalia.change({}, {'foo': 'bar'}) self.assertEqual('change', sender.call_args[0][0]) self.assertEqual('bar', json.loads(sender.call_args[0][1]['data'])['post']['foo'])
def test_change_not_integratng(self, sender): with patch('opal.core.glossolalia.INTEGRATING', new=False): change = glossolalia.change(None, None) self.assertEqual(None, change) # Mostly a proxy for "Does this stacktrace?" self.assertFalse(sender.called)