From f8b59c3b2e77d87e2814001d4e440baeba1b46c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hermann=20K=C3=A4ser?= Date: Fri, 17 Feb 2017 04:03:42 +0000 Subject: [PATCH] Rating fixes (#129) * Stops from overwriting rating if no value is set When PATCH-ing the photo data there is no rating set and so the value gets set to the default 0. This isn't right, so let's let the ORM set the value to 0 when creating a recipe and otherwise leave it to whaveter it was before when updating if no rating is provided. * Checks to make sure we're uploading an image When editing a recipe that already has a photo the data.photo field is populated with the URL of the image. If we're actually uploading an image it'll be a "File" object instead. * Limits rating value when creating recipes --- api/v1/recipe/serializers.py | 21 ++++++++---- api/v1/recipe/tests.py | 11 ++++++ .../recipe_form/actions/RecipeActions.js | 34 +++++++++++-------- 3 files changed, 45 insertions(+), 21 deletions(-) diff --git a/api/v1/recipe/serializers.py b/api/v1/recipe/serializers.py index 55efa755..a187104d 100644 --- a/api/v1/recipe/serializers.py +++ b/api/v1/recipe/serializers.py @@ -60,12 +60,13 @@ def update(self, instance, validated_data): direction_data = validated_data.pop('directions', None) tag_data = validated_data.pop('tags', None) - rating = int(validated_data.get('rating', 0)) - if rating < 0: - rating = 0 - elif rating > 5: - rating = 5 - validated_data['rating'] = rating + if 'rating' in validated_data: + rating = int(validated_data.get('rating', 0)) + if rating < 0: + rating = 0 + elif rating > 5: + rating = 5 + validated_data['rating'] = rating for attr, value in validated_data.items(): setattr(instance, attr, value) @@ -109,6 +110,14 @@ def create(self, validated_data): direction_data = validated_data.pop('directions', None) tag_data = validated_data.pop('tags', None) + if 'rating' in validated_data: + rating = int(validated_data.get('rating', 0)) + if rating < 0: + rating = 0 + elif rating > 5: + rating = 5 + validated_data['rating'] = rating + # Create the recipe. # Use the log-in user as the author. recipe = Recipe.objects.create( diff --git a/api/v1/recipe/tests.py b/api/v1/recipe/tests.py index 619c4839..5b80c307 100644 --- a/api/v1/recipe/tests.py +++ b/api/v1/recipe/tests.py @@ -49,3 +49,14 @@ def test_rating_under_success(self): self.assertEqual(response.rating, 0) + def test_no_rating_supplied_success(self): + data = deepcopy(self.data) + + del data['rating'] + + recipe = mock.Mock(spec=Recipe) + recipe.rating = 3 + response = self.serializer.update(recipe, data) + + self.assertEqual(response.rating, 3) + diff --git a/frontend/modules/recipe_form/actions/RecipeActions.js b/frontend/modules/recipe_form/actions/RecipeActions.js index e771666e..c31444c4 100644 --- a/frontend/modules/recipe_form/actions/RecipeActions.js +++ b/frontend/modules/recipe_form/actions/RecipeActions.js @@ -7,7 +7,7 @@ import {serverURLs} from '../../common/config' class RecipeActions { submit(data) { let photo = false; - if (data.photo){ + if (typeof data.photo == "object") { photo = data.photo; } @@ -23,21 +23,9 @@ class RecipeActions { .end((err, res) => { if (!err && res) { //send the image once the file has been created - var id = res.body.id; + if (photo) { - request - .patch(serverURLs.recipe + id + "/") - .attach('photo', photo) - .set('Authorization', 'Token ' + AuthStore.getToken()) - .end((err, res) => { - if (!err && res) { - this.handleSubmit(res.body.id); - } else { - console.error(serverURLs.recipe, err.toString()); - console.error(res.body); - this.error(res.body); - } - }); + this.submitPhoto(res, photo); } else { this.handleSubmit(res.body.id); } @@ -49,6 +37,22 @@ class RecipeActions { }); } + submitPhoto(res, photo) { + request + .patch(serverURLs.recipe + res.body.id + "/") + .attach('photo', photo) + .set('Authorization', 'Token ' + AuthStore.getToken()) + .end((err, res) => { + if (!err && res) { + this.handleSubmit(res.body.id); + } else { + console.error(serverURLs.recipe, err.toString()); + console.error(res.body); + this.error(res.body); + } + }); + } + handleSubmit(new_recipe_id) { AppDispatcher.dispatch({ actionType: RecipeConstants.SUBMIT,