diff --git a/cookbook/serializer.py b/cookbook/serializer.py index 8b6cd7740..8bd16c09b 100644 --- a/cookbook/serializer.py +++ b/cookbook/serializer.py @@ -8,7 +8,6 @@ from drf_writable_nested import (UniqueFieldsMixin, WritableNestedModelSerializer) from rest_framework import serializers from rest_framework.exceptions import ValidationError, NotFound -from treebeard.mp_tree import MP_NodeQuerySet from cookbook.models import (Comment, CookLog, Food, Ingredient, Keyword, MealPlan, MealType, NutritionInformation, Recipe, @@ -47,7 +46,7 @@ class CustomDecimalField(serializers.Field): class SpaceFilterSerializer(serializers.ListSerializer): def to_representation(self, data): - if (type(data) == QuerySet and data.query.is_sliced) or type(data) == MP_NodeQuerySet: + if (type(data) == QuerySet and data.query.is_sliced): # if query is sliced it came from api request not nested serializer return super().to_representation(data) if self.child.Meta.model == User: @@ -209,9 +208,9 @@ class KeywordSerializer(UniqueFieldsMixin, serializers.ModelSerializer): return str(obj) def get_image(self, obj): - recipes = obj.recipe_set.all().exclude(image__isnull=True).exclude(image__exact='') + recipes = obj.recipe_set.all().filter(space=obj.space).exclude(image__isnull=True).exclude(image__exact='') if len(recipes) == 0: - recipes = Recipe.objects.filter(keywords__in=Keyword.get_tree(obj)).exclude(image__isnull=True).exclude(image__exact='') # if no recipes found - check whole tree + recipes = Recipe.objects.filter(keywords__in=obj.get_tree(), space=obj.space).exclude(image__isnull=True).exclude(image__exact='') # if no recipes found - check whole tree if len(recipes) != 0: return random.choice(recipes).image.url else: @@ -223,6 +222,7 @@ class KeywordSerializer(UniqueFieldsMixin, serializers.ModelSerializer): def create(self, validated_data): # since multi select tags dont have id's # duplicate names might be routed to create + validated_data['name'] = validated_data['name'].strip() validated_data['space'] = self.context['request'].space obj, created = Keyword.objects.get_or_create(**validated_data) return obj @@ -231,7 +231,7 @@ class KeywordSerializer(UniqueFieldsMixin, serializers.ModelSerializer): # list_serializer_class = SpaceFilterSerializer model = Keyword fields = ('id', 'name', 'icon', 'label', 'description', 'image', 'parent', 'numchild', 'numrecipe', 'created_at', 'updated_at') - read_only_fields = ('id', 'numchild',) + read_only_fields = ('id', 'numchild', 'parent', 'image') class UnitSerializer(UniqueFieldsMixin, serializers.ModelSerializer): diff --git a/cookbook/tests/api/test_api_keyword.py b/cookbook/tests/api/test_api_keyword.py index df6cb5671..0191d15df 100644 --- a/cookbook/tests/api/test_api_keyword.py +++ b/cookbook/tests/api/test_api_keyword.py @@ -167,7 +167,6 @@ def test_add(arg, request, u1_s2): assert r.status_code == 404 -@pytest.mark.django_db(transaction=True) def test_add_duplicate(u1_s1, u1_s2, obj_1, obj_3): assert json.loads(u1_s1.get(reverse(LIST_URL)).content)['count'] == 1 assert json.loads(u1_s2.get(reverse(LIST_URL)).content)['count'] == 1 @@ -255,13 +254,13 @@ def test_move(u1_s1, obj_1, obj_1_1, obj_1_1_1, obj_2, obj_3, space_1): r = u1_s1.put( reverse(MOVE_URL, args=[obj_1.id, 9999]) ) - assert r.status_code == 400 + assert r.status_code == 404 # attempt to move to wrong space r = u1_s1.put( reverse(MOVE_URL, args=[obj_1_1.id, obj_3.id]) ) - assert r.status_code == 400 + assert r.status_code == 404 # run diagnostic to find problems - none should be found with scopes_disabled(): @@ -327,13 +326,13 @@ def test_merge( r = u1_s1.put( reverse(MERGE_URL, args=[obj_1_1.id, 9999]) ) - assert r.status_code == 400 + assert r.status_code == 404 # attempt to move to wrong space r = u1_s1.put( reverse(MERGE_URL, args=[obj_2.id, obj_3.id]) ) - assert r.status_code == 400 + assert r.status_code == 404 # attempt to merge with child r = u1_s1.put( diff --git a/cookbook/views/api.py b/cookbook/views/api.py index 7986bee26..f284f10ed 100644 --- a/cookbook/views/api.py +++ b/cookbook/views/api.py @@ -144,18 +144,18 @@ class TreeMixin(FuzzyFilterMixin): except self.model.DoesNotExist: self.queryset = self.model.objects.none() if root == 0: - self.queryset = self.model.get_root_nodes().filter(space=self.request.space) + self.queryset = self.model.get_root_nodes() | self.model.objects.filter(depth=0) else: - self.queryset = self.model.objects.get(id=root).get_children().filter(space=self.request.space) + self.queryset = self.model.objects.get(id=root).get_children() elif tree: if tree.isnumeric(): try: - self.queryset = self.model.objects.get(id=int(tree)).get_descendants_and_self().filter(space=self.request.space) + self.queryset = self.model.objects.get(id=int(tree)).get_descendants_and_self() except Keyword.DoesNotExist: self.queryset = self.model.objects.none() else: return super().get_queryset() - return self.queryset + return self.queryset.filter(space=self.request.space) @decorators.action(detail=True, url_path='move/(?P[^/.]+)', methods=['PUT'],) @decorators.renderer_classes((TemplateHTMLRenderer, JSONRenderer)) @@ -166,7 +166,7 @@ class TreeMixin(FuzzyFilterMixin): child = self.model.objects.get(pk=pk, space=self.request.space) except (self.model.DoesNotExist): content = {'error': True, 'msg': _(f'No {self.basename} with id {child} exists')} - return Response(content, status=status.HTTP_400_BAD_REQUEST) + return Response(content, status=status.HTTP_404_NOT_FOUND) parent = int(parent) # parent 0 is root of the tree @@ -184,7 +184,7 @@ class TreeMixin(FuzzyFilterMixin): parent = self.model.objects.get(pk=parent, space=self.request.space) except (self.model.DoesNotExist): content = {'error': True, 'msg': _(f'No {self.basename} with id {parent} exists')} - return Response(content, status=status.HTTP_400_BAD_REQUEST) + return Response(content, status=status.HTTP_404_NOT_FOUND) try: with scopes_disabled(): @@ -204,7 +204,7 @@ class TreeMixin(FuzzyFilterMixin): source = self.model.objects.get(pk=pk, space=self.request.space) except (self.model.DoesNotExist): content = {'error': True, 'msg': _(f'No {self.basename} with id {pk} exists')} - return Response(content, status=status.HTTP_400_BAD_REQUEST) + return Response(content, status=status.HTTP_404_NOT_FOUND) if int(target) == source.id: content = {'error': True, 'msg': _('Cannot merge with the same object!')} @@ -215,7 +215,7 @@ class TreeMixin(FuzzyFilterMixin): target = self.model.objects.get(pk=target, space=self.request.space) except (self.model.DoesNotExist): content = {'error': True, 'msg': _(f'No {self.basename} with id {target} exists')} - return Response(content, status=status.HTTP_400_BAD_REQUEST) + return Response(content, status=status.HTTP_404_NOT_FOUND) try: if target in source.get_descendants_and_self(): diff --git a/recipes/settings.py b/recipes/settings.py index 2ad12626c..1d487072b 100644 --- a/recipes/settings.py +++ b/recipes/settings.py @@ -24,7 +24,6 @@ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) SECRET_KEY = os.getenv('SECRET_KEY') if os.getenv('SECRET_KEY') else 'INSECURE_STANDARD_KEY_SET_IN_ENV' DEBUG = bool(int(os.getenv('DEBUG', True))) -DEMO = bool(int(os.getenv('DEMO', False))) SOCIAL_DEFAULT_ACCESS = bool(int(os.getenv('SOCIAL_DEFAULT_ACCESS', False))) SOCIAL_DEFAULT_GROUP = os.getenv('SOCIAL_DEFAULT_GROUP', 'guest')