August 24, 2016 at 12:45 PM
class TestRecipeViewUploads(APITestCase):
def setUp(self):
self.user = get_user_model().objects.create_user(
username='jacob', first_name='jacob', email='ja...@foo.com', password='top_secret')
self.client.login(username='jacob', password='top_secret')
self.client.force_authenticate(self.user)
self.banana_bread = Recipe.objects.create(name="Banana Bread", author=self.user)
self.banana_bread_step1 = Recipe_Step.objects.create(step_number=1, description="step 1", recipe=self.banana_bread, user=self.user)
self.banana_bread_ingredient1 = Recipe_Ingredient.objects.create(ingredient_number=1,
name = "bananas",
user=self.user,
recipe=self.banana_bread)
self.banana_bread_comment = Comment.objects.create(recipe=self.banana_bread, user=self.user, body="yum!")
self.spaghetti = Recipe.objects.create(name="Spaghetti", author=self.user)
self.media_folder = mkdtemp()
def tearDown(self):
rmtree(self.media_folder)
@mock.patch('storages.backends.s3boto.S3BotoStorage', FileSystemStorage)
def test_create_view_creates_and_uploads_image(self):
image = Image.new('RGB', (100, 100))
tmp_file = tempfile.NamedTemporaryFile(suffix='.jpg')
image.save(tmp_file, format="JPEG")
with open(tmp_file.name, 'rb') as fp:
with override_settings(MEDIA_ROOT=self.media_folder):
response = self.client.post('/api/recipes/image/',
data={'name': 'Biscuits and Gravy', 'image': fp},
format='multipart')
print("response=", response)
print("response.data=", response.data)
created_recipe = Recipe.objects.get(name="Biscuits and Gravy")
self.assertTrue(created_recipe.image)
class RecipeImageView(APIView):
authentication_classes = (BasicAuthentication,)
permission_classes = (IsAuthenticated,)
queryset = Recipe.objects.all()
parser_classes = (MultiPartParser, FormParser,)
serializer_class = RecipeImageSerializer
def post(self, request, format=None):
print("request=", request)
print("request.full_data", request._full_data)
print("request.content_type", request.content_type)
print("request.data=", request.data)
print("request.FILES=", request.FILES)
serializer = RecipeImageSerializer(data=request.data)
print("serializer=", serializer)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
class RecipeImageSerializer(TaggitSerializer, serializers.ModelSerializer):
image = serializers.ImageField(max_length=None, use_url=True)
recipe_as_image = serializers.ImageField(max_length=None, use_url=True)
class Meta:
model = Recipe
fields = ('id', 'name', 'slug', 'description', 'image', 'recipe_as_image',)
read_only_fields = ('id','slug',)
You received this message because you are subscribed to a topic in the Google Groups "Django REST framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-rest-framework/UiKioNuavcE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-rest-fram...@googlegroups.com.
August 24, 2016 at 11:30 PM
On Aug 24, 2016, at 9:01 AM, Andrew Backer <awba...@gmail.com> wrote:Try posting something like this for the image:
SimpleUploadedFile(name="my-file.png", content=open(file_name).read())
* what does use_url do? never needed to use that, perhaps thats the issue
* try not specifying the format=, if you are using SimpleUploadedFile
Some other notes:
Look at dj-inmemorystorage, to avoid hitting the file system
# check for ./manage.py test, jenkins, or others on the commandline
IS_TESTING = not {'testserver', 'test', 'jenkins'}.isdisjoint(set(sys.argv[1:2]))
# elsewhere in settings
if IS_TESTING:
DEFAULT_FILE_STORAGE = ‘inmemorystorage.InMemoryStorage'
August 24, 2016 at 11:30 PMOkay, knowing that its working for someone is helpful. It’s possible it’s a problem with my tests.I get errors back:response.data= {'name': ['This field is required.'], 'image': ['No file was submitted.'], 'recipe_as_image': ['No file was submitted.']}
August 25, 2016 at 1:46 AM
The same pattern I’m using works fine for regular django forms, but I will try this (I’m open to anything at this point :-) — just tried it, doesn’t work either.
Don’t know. Was one of the many working examples I looked at that didn’t work for me. I removed it and still get the same failure.a
Tried that. (Just tried it again to be certain :-)
Cool. I had not seen that I’ll check it out.
Greg
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
August 25, 2016 at 12:01 AM
Try posting something like this for the image:
SimpleUploadedFile(name="my-file.png", content=open(file_name).read())
* what does use_url do? never needed to use that, perhaps thats the issue
* try not specifying the format=, if you are using SimpleUploadedFile
Some other notes:
Look at dj-inmemorystorage, to avoid hitting the file system
# check for ./manage.py test, jenkins, or others on the commandline
IS_TESTING = not {'testserver', 'test', 'jenkins'}.isdisjoint(set(sys.argv[1:2]))
# elsewhere in settings
if IS_TESTING:
DEFAULT_FILE_STORAGE = 'inmemorystorage.InMemoryStorage'
August 24, 2016 at 11:30 PM
Okay, knowing that its working for someone is helpful. It’s possible it’s a problem with my tests.I get errors back:
response.data= {'name': ['This field is required.'], 'image': ['No file was submitted.'], 'recipe_as_image': ['No file was submitted.']}
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
You received this message because you are subscribed to a topic in the Google Groups "Django REST framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-rest-framework/UiKioNuavcE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-rest-fram...@googlegroups.com.
'cms.middleware.toolbar.ToolbarMiddleware',
August 25, 2016 at 6:20 AM
It appears to be a conflict with django cms.If I have this middleware installed, it fails.'cms.middleware.toolbar.ToolbarMiddleware',Any ideas what is going on?Greg
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
August 25, 2016 at 4:49 AM
Okay, I created a small project and it works :-(Now to figure out why it works and my larger project doesn’t…Greg
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
August 25, 2016 at 1:49 AM
August 25, 2016 at 6:20 AM
It appears to be a conflict with django cms.If I have this middleware installed, it fails.'cms.middleware.toolbar.ToolbarMiddleware',Any ideas what is going on?Greg
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
August 25, 2016 at 4:49 AM
Okay, I created a small project and it works :-(Now to figure out why it works and my larger project doesn’t…Greg
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
August 25, 2016 at 1:49 AM
You received this message because you are subscribed to a topic in the Google Groups "Django REST framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-rest-framework/UiKioNuavcE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-rest-fram...@googlegroups.com.