Skip to content

Commit

Permalink
Rating fixes (RyanNoelk#129)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
hermzz authored and RyanNoelk committed Feb 17, 2017
1 parent ff7ac5b commit f8b59c3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 21 deletions.
21 changes: 15 additions & 6 deletions api/v1/recipe/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand Down
11 changes: 11 additions & 0 deletions api/v1/recipe/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

34 changes: 19 additions & 15 deletions frontend/modules/recipe_form/actions/RecipeActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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);
}
Expand All @@ -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,
Expand Down

0 comments on commit f8b59c3

Please sign in to comment.