def query(request): '''Gets NLP API response''' if request.method == 'POST': try: data = CamelCaseJSONParser().parse(request) text = data['nlp_text'] except KeyError: return HttpResponseBadRequest() text = check_text(text) if text is None: return JsonResponse(RESPONSE_EMPTY, status=200) client = LanguageServiceClient() # pylint: disable=no-member document = types.Document( content=text, type=enums.Document.Type.PLAIN_TEXT) # pylint: enable=no-member try: google_category_response = client.classify_text(document=document) google_analysis_response = client.analyze_entities(document=document) response = MessageToDict(google_category_response) analysis_response = MessageToDict(google_analysis_response) except InvalidArgument: return JsonResponse(RESPONSE_EMPTY, status=200) response = make_response(response, analysis_response) return JsonResponse(response, status=200) return HttpResponseNotAllowed(['POST'])
def run(self, end_point_tracker_id, shipment_tracker_id): shipment_tracker = ShipmentSyncTracker.objects.get( id=shipment_tracker_id) end_point_tracker = SellerEndPointTracker.objects.get( id=end_point_tracker_id) shipment_tracker.state = ShipmentSyncTracker.STARTED shipment_tracker.save() response = self.fetch(shipment_tracker.seller.access_token, shipment_tracker.shipment_id) if response.status_code == 200: end_point_tracker.remaining_req_limit = int( response.headers["x-ratelimit-remaining"]) end_point_tracker.limit_reset_at = now() + timedelta( seconds=int(response.headers["x-ratelimit-reset"])) end_point_tracker.save() stream = BytesIO(bytes(response.text, 'utf-8')) serializer = ShipmentSerializer( data=CamelCaseJSONParser().parse(stream=stream), context={"seller": shipment_tracker.seller}) serializer.is_valid() serializer.save() shipment_tracker.state = ShipmentSyncTracker.FINISHED shipment_tracker.save() elif response.status_code == 429: end_point_tracker.remaining_req_limit = 0 end_point_tracker.limit_reset_at = now() + timedelta( seconds=int(response.headers["x-ratelimit-reset"])) end_point_tracker.save()
def test_deserialize_with_obj(db_obj): consumer = consumers.SelfContainedTestConsumer() consumer.action = 'subscribe' consumer.parser = CamelCaseJSONParser() # No data consumer.data = payload() assert consumer.deserialize(db_obj, partial=True).validated_data == {} # Partial data consumer.data = payload(counter=270) with pytest.raises(exceptions.ValidationError): consumer.deserialize() data = consumer.deserialize(db_obj, partial=True).validated_data assert data == {'counter': 270} # Complete data consumer.data = payload(id=234, name='Andrew', counter=270) data = consumer.deserialize(db_obj).validated_data assert data == {'name': 'Andrew', 'counter': 270} consumer.data = payload(id=db_obj.pk, name='Andrew', counter=270) data = consumer.deserialize(db_obj).validated_data assert data == {'name': 'Andrew', 'counter': 270}
def consumer_with_custom_serializer_meta(**kwargs): kwargs.update({'model': models.APIModel}) Meta = type('Meta', (), kwargs) Serializer = type('Serializer', (ModelSerializer, ), {'Meta': Meta}) return type( 'TestConsumer', (consumers.SelfContainedTestConsumer, ), { 'view': views.TestViewSet, 'serializer_class': Serializer, 'parser': CamelCaseJSONParser(), })()
def test_deserialize_exception(): consumer = consumers.SelfContainedTestConsumer() consumer.action = 'subscribe' consumer.parser = CamelCaseJSONParser() consumer.data = payload(id=234, city='London', counter=15) with pytest.raises(exceptions.APIException): consumer.deserialize() consumer.data = payload(id=234, name='Natasha', counter='-') with pytest.raises(exceptions.APIException): consumer.deserialize()
def test_deserialize_permissions(): class TestConsumer(consumers.SelfContainedTestConsumer): view = views.TestViewSet def check_permissions(self, obj): raise exceptions.APIException() consumer = TestConsumer() consumer.parser = CamelCaseJSONParser() consumer.data = payload(name='me', counter=0) with pytest.raises(exceptions.APIException): consumer.deserialize()
def test_organization_list_fields(self): """Test list's fields of organization REST API endpoint.""" response = self.client.get('/api/organizations/') self.assertEqual(response.status_code, status.HTTP_200_OK) # dirty hack intead of checking response.data because of issue # djangorestframework-camel-case #26: for organization in CamelCaseJSONParser().parse( BytesIO(response.content)): self.assertIsInstance(organization.pop('id'), int) self.assertIsInstance(organization.pop('name'), str) self.assertIsInstance(organization.pop('slug'), str) self.assertIsInstance(organization.pop('url'), str) self.assertEqual(len(organization), 0)
def post(self, request): data = CamelCaseJSONParser().parse(request) serializer = ShortLayoutDataSerializer(data=data) if serializer.is_valid(): layout, priorities = serializer.save() layout.width = calculate_layout_width(layout.duration_limit) layout.save() save_layout_priorities(layout, priorities) return Response(ShortLayoutDataSerializer(layout).data, status=status.HTTP_200_OK) return Response(ErrorMessageSerializer( message_from_serializer_errors(serializer.errors)).data, status=status.HTTP_400_BAD_REQUEST)
def post(self, request): try: data = CamelCaseJSONParser().parse(request) serializer = FullLayoutDataSerializer(data=data) if serializer.is_valid(): layout_data = serializer.save() layout_obj = layout_data.to_layout() optimize(layout_obj, layout_obj.update_date) layout_data = LayoutData.from_layout(layout_obj) return Response(FullLayoutDataSerializer(layout_data).data, status=status.HTTP_200_OK) raise message_from_serializer_errors(serializer.errors) except ErrorMessage as e: return Response(ErrorMessageSerializer(e).data, status=status.HTTP_400_BAD_REQUEST)
def test_offer_list_fields(self): """Test list's fields of offers REST API endpoint.""" response = self.client.get('/api/offers/') self.assertEqual(response.status_code, status.HTTP_200_OK) # dirty hack intead of checking response.data because of issue # djangorestframework-camel-case #26: for offer in CamelCaseJSONParser().parse(BytesIO(response.content)): self.assertIsInstance(offer.pop('finished_at'), str) self.assertIsInstance(offer.pop('id'), int) self.assertIsInstance(offer.pop('image'), (str, type(None))) self.assertIsInstance(offer.pop('location'), str) self.assertIsInstance(offer.pop('organization'), str) self.assertIsInstance(offer.pop('slug'), str) self.assertIsInstance(offer.pop('started_at'), str) self.assertIsInstance(offer.pop('title'), str) self.assertIsInstance(offer.pop('url'), str) self.assertEqual(len(offer), 0)
def test_receive_exception(db_obj): class TestConsumer(consumers.ExceptionToMessageConsumer): def perform_update(self, serializer): assert isinstance(self.object, models.APIModel) raise exceptions.APIException('Just an error') consumer = TestConsumer() consumer.parser = CamelCaseJSONParser() assert consumer.object is None consumer.receive( ('go', 'update', str(db_obj.pk)), json.dumps({'payload': { 'counter': 301 }}), ) error = consumer.messages.pop() assert isinstance(error, exceptions.APIException) assert error.detail == 'Just an error' assert consumer.object is None
def post(self, request, pk): try: layout = Layout.objects.get(pk=pk) except Layout.DoesNotExist: return Response({}, status=status.HTTP_404_NOT_FOUND) data = CamelCaseJSONParser().parse(request) serializer = FullLayoutDataSerializer(data=data) try: check_in_edit_mode(request, layout) if serializer.is_valid(): layout_data = serializer.save() update_layout_in_db(layout_data, layout) layout_obj = get_layout_from_db(layout) layout_data = LayoutData.from_layout(layout_obj) return Response(FullLayoutDataSerializer(layout_data).data, status=status.HTTP_200_OK) raise message_from_serializer_errors(serializer.errors) except ErrorMessage as e: return Response(ErrorMessageSerializer(e).data, status=status.HTTP_400_BAD_REQUEST)
def test_deserialize_no_obj(): consumer = consumers.SelfContainedTestConsumer() consumer.action = 'subscribe' consumer.parser = CamelCaseJSONParser() # No data consumer.data = payload() assert consumer.deserialize(partial=True).validated_data == {} # Partial data consumer.data = payload(name='Andrew') with pytest.raises(exceptions.ValidationError): consumer.deserialize() data = consumer.deserialize(partial=True).validated_data assert data == {'name': 'Andrew'} # Complete data consumer.data = payload(something=1, name='Andrew', counter=270) data = consumer.deserialize().validated_data assert data == {'name': 'Andrew', 'counter': 270}
def test_get_objects_fields(): consumer = consumers.TestConsumer() consumer.parser = CamelCaseJSONParser() # One field consumer.data = payload(counter=41) # Empty database assert not consumer.get_objects().exists() models.APIModel.objects.bulk_create(( models.APIModel(name='Luke', counter=41), models.APIModel(name='Mary', counter=41), models.APIModel(name='Luke', counter=26), )) # 2 objects objects = list(consumer.get_objects()) assert len(objects) == 2 assert objects[0].counter == objects[1].counter == 41 # 1 object consumer.data = payload(counter=26) objects = list(consumer.get_objects()) assert len(objects) == 1 assert objects[0].name == 'Luke' # Two fields # 1 object consumer.data = payload(name='Luke', counter=41) objects = list(consumer.get_objects()) assert len(objects) == 1 assert objects[0].name == 'Luke' # 0 objects consumer.data = payload(name='Mary', counter=26) assert not consumer.get_objects().exists()
def test_parse(): consumer = consumers.SelfContainedTestConsumer() consumer.parser = CamelCaseJSONParser() consumer.data = b'{"payload":{"field":"v","anotherF":["Espa\u00F1a",""]}}' data = consumer.parse() assert data == {'field': 'v', 'another_f': ['España', '']}