added endpoint for space switching

This commit is contained in:
vabene1111 2022-06-06 18:34:40 +02:00
parent e2b887b449
commit 37f0f7a0b7
3 changed files with 22 additions and 3 deletions

View File

@ -208,7 +208,7 @@ class UserSpaceSerializer(WritableNestedModelSerializer):
class Meta: class Meta:
model = UserSpace 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') read_only_fields = ('id', 'created_at', 'updated_at', 'space')

View File

@ -117,6 +117,8 @@ urlpatterns = [
path('api/share-link/<int:pk>', api.share_link, name='api_share_link'), path('api/share-link/<int:pk>', api.share_link, name='api_share_link'),
path('api/get_facets/', api.get_facets, name='api_get_facets'), 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/reset-food-inheritance/', api.reset_food_inheritance, name='api_reset_food_inheritance'),
path('api/switch-active-space/<int:space_id>/', api.switch_active_space, name='api_switch_active_space'),
path('dal/keyword/', dal.KeywordAutocomplete.as_view(), name='dal_keyword'), 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 # TODO is this deprecated? not yet, some old forms remain, could likely be changed to generic API endpoints

View File

@ -45,7 +45,7 @@ from cookbook.helper.image_processing import handle_image
from cookbook.helper.ingredient_parser import IngredientParser from cookbook.helper.ingredient_parser import IngredientParser
from cookbook.helper.permission_helper import (CustomIsAdmin, CustomIsGuest, CustomIsOwner, from cookbook.helper.permission_helper import (CustomIsAdmin, CustomIsGuest, CustomIsOwner,
CustomIsShare, CustomIsShared, CustomIsUser, 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_html_import import get_recipe_from_source
from cookbook.helper.recipe_search import RecipeFacet, RecipeSearch, old_search from cookbook.helper.recipe_search import RecipeFacet, RecipeSearch, old_search
from cookbook.helper.shopping_helper import RecipeShoppingEditor, shopping_helper from cookbook.helper.shopping_helper import RecipeShoppingEditor, shopping_helper
@ -395,7 +395,7 @@ class UserSpaceViewSet(viewsets.ModelViewSet):
return super().destroy(request, *args, **kwargs) return super().destroy(request, *args, **kwargs)
def get_queryset(self): def get_queryset(self):
return self.queryset.filter(space=self.request.space) return self.queryset.filter(user=self.request.user)
class UserPreferenceViewSet(viewsets.ModelViewSet): class UserPreferenceViewSet(viewsets.ModelViewSet):
@ -1178,6 +1178,23 @@ def reset_food_inheritance(request):
return Response(str(e), status=status.HTTP_400_BAD_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): def get_recipe_provider(recipe):
if recipe.storage.method == Storage.DROPBOX: if recipe.storage.method == Storage.DROPBOX:
return Dropbox return Dropbox