From 8b9c7daaae2bd2be24dc7f4c5a238e38aa7bf35e Mon Sep 17 00:00:00 2001 From: vabene1111 Date: Mon, 29 Jun 2020 12:27:54 +0200 Subject: [PATCH] fixed commas + desktop flex design --- cookbook/serializer.py | 23 +++++++++++++-- .../templates/forms/edit_internal_recipe.html | 29 +++++++++---------- 2 files changed, 35 insertions(+), 17 deletions(-) diff --git a/cookbook/serializer.py b/cookbook/serializer.py index 862a24d88..d9c09a479 100644 --- a/cookbook/serializer.py +++ b/cookbook/serializer.py @@ -1,13 +1,31 @@ from django.contrib.auth.models import User from drf_writable_nested import WritableNestedModelSerializer, UniqueFieldsMixin from rest_framework import serializers -from rest_framework.exceptions import APIException +from rest_framework.exceptions import APIException, ValidationError from rest_framework.fields import CurrentUserDefault from cookbook.models import MealPlan, MealType, Recipe, ViewLog, UserPreference, Storage, Sync, SyncLog, Keyword, Unit, Ingredient, Comment, RecipeImport, RecipeBook, RecipeBookEntry, ShareLink, CookLog, Food, Step from cookbook.templatetags.custom_tags import markdown +class CustomDecimalField(serializers.Field): + """ + Custom decimal field to normalize useless decimal places and allow commas as decimal separators + """ + + def to_representation(self, value): + return value.normalize() + + def to_internal_value(self, data): + if type(data) == int or type(data) == float: + return data + elif type(data) == str: + try: + return float(data.replace(',', '')) + except ValueError: + raise ValidationError('A valid number is required') + + class UserNameSerializer(serializers.ModelSerializer): class Meta: model = User @@ -60,10 +78,11 @@ class FoodSerializer(UniqueFieldsMixin, serializers.ModelSerializer): class IngredientSerializer(WritableNestedModelSerializer): food = FoodSerializer() unit = UnitSerializer() + amount = CustomDecimalField() class Meta: model = Ingredient - fields = '__all__' + fields = ('id', 'food', 'unit', 'amount', 'note', 'order') class StepSerializer(WritableNestedModelSerializer): diff --git a/cookbook/templates/forms/edit_internal_recipe.html b/cookbook/templates/forms/edit_internal_recipe.html index 7da2c83bf..cc03e1b0a 100644 --- a/cookbook/templates/forms/edit_internal_recipe.html +++ b/cookbook/templates/forms/edit_internal_recipe.html @@ -88,7 +88,7 @@
-
+ + -
-
-
-
+
+
-
- +
+
-
+
-
+
-
+
-
-
-
+