From 37f0f7a0b77d43fa19fa8369c8c151a76ad12de7 Mon Sep 17 00:00:00 2001 From: vabene1111 Date: Mon, 6 Jun 2022 18:34:40 +0200 Subject: [PATCH] added endpoint for space switching --- cookbook/serializer.py | 2 +- cookbook/urls.py | 2 ++ cookbook/views/api.py | 21 +++++++++++++++++++-- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/cookbook/serializer.py b/cookbook/serializer.py index bba902e6..331729c4 100644 --- a/cookbook/serializer.py +++ b/cookbook/serializer.py @@ -208,7 +208,7 @@ class UserSpaceSerializer(WritableNestedModelSerializer): class Meta: model = UserSpace - fields = ('id', 'user', 'space', 'groups', 'created_at', 'updated_at',) + fields = ('id', 'user', 'space', 'groups', 'active', 'created_at', 'updated_at',) read_only_fields = ('id', 'created_at', 'updated_at', 'space') diff --git a/cookbook/urls.py b/cookbook/urls.py index 40917e27..5933787c 100644 --- a/cookbook/urls.py +++ b/cookbook/urls.py @@ -117,6 +117,8 @@ urlpatterns = [ path('api/share-link/', api.share_link, name='api_share_link'), path('api/get_facets/', api.get_facets, name='api_get_facets'), path('api/reset-food-inheritance/', api.reset_food_inheritance, name='api_reset_food_inheritance'), + path('api/switch-active-space//', api.switch_active_space, name='api_switch_active_space'), + path('dal/keyword/', dal.KeywordAutocomplete.as_view(), name='dal_keyword'), # TODO is this deprecated? not yet, some old forms remain, could likely be changed to generic API endpoints diff --git a/cookbook/views/api.py b/cookbook/views/api.py index 69d52ab8..a00a3fc8 100644 --- a/cookbook/views/api.py +++ b/cookbook/views/api.py @@ -45,7 +45,7 @@ from cookbook.helper.image_processing import handle_image from cookbook.helper.ingredient_parser import IngredientParser from cookbook.helper.permission_helper import (CustomIsAdmin, CustomIsGuest, CustomIsOwner, CustomIsShare, CustomIsShared, CustomIsUser, - group_required, CustomIsSpaceOwner) + group_required, CustomIsSpaceOwner, switch_user_active_space) from cookbook.helper.recipe_html_import import get_recipe_from_source from cookbook.helper.recipe_search import RecipeFacet, RecipeSearch, old_search from cookbook.helper.shopping_helper import RecipeShoppingEditor, shopping_helper @@ -395,7 +395,7 @@ class UserSpaceViewSet(viewsets.ModelViewSet): return super().destroy(request, *args, **kwargs) def get_queryset(self): - return self.queryset.filter(space=self.request.space) + return self.queryset.filter(user=self.request.user) class UserPreferenceViewSet(viewsets.ModelViewSet): @@ -1178,6 +1178,23 @@ def reset_food_inheritance(request): return Response(str(e), status=status.HTTP_400_BAD_REQUEST) +@api_view(['GET']) +# @schema(AutoSchema()) #TODO add proper schema +@permission_classes([CustomIsAdmin]) +# TODO add rate limiting +def switch_active_space(request, space_id): + """ + api endpoint to switch space function + """ + try: + user_space = get_object_or_404(UserSpace, space=space_id, user=request.user) + switch_user_active_space(request.user, user_space) + return Response(UserSpaceSerializer().to_representation(instance=user_space), status=status.HTTP_200_OK) + except Exception as e: + traceback.print_exc() + return Response(str(e), status=status.HTTP_400_BAD_REQUEST) + + def get_recipe_provider(recipe): if recipe.storage.method == Storage.DROPBOX: return Dropbox