def get_queryset(self): path = self.request._request.path result = re.search(r'.+?(\d+).+?', path) pk = result.group(1) user = get_object_or_404_customed(User, pk=pk) values = Bookmark.objects.filter(user=user) return values
def post(self, request, pk): user = request.user recipe = get_object_or_404_customed(Recipe, pk=pk) instance, created = Bookmark.objects.get_or_create( recipe=recipe, user=user, ) # 2018.11.11 # Get or create default collection # and connect it with bookmark instance which was newly made above if created: collection, _ = Collection.objects.get_or_create( name='default', user=user ) instance.collection = collection instance.save() if not created: instance.delete() return Response( f'User(id:{user.pk})가 recipe(id:{recipe.pk})의 저장을 취소했습니다.', status=status.HTTP_204_NO_CONTENT, ) else: return Response( f'User(id:{user.pk})가 recipe(id:{recipe.pk})를 저장했습니다.', status=status.HTTP_200_OK, )
def has_permission(self, request, view): if request.method in permissions.SAFE_METHODS: return True # POST Method 인 경우 request.user 와 requst uri의 user의 동일 여부 확인 path = request._request.path result = re.search(r'.+?(\d+).+?', path) pk = result.group(1) user = get_object_or_404_customed(User, pk=pk) return user == request.user
def post(self, request, pk): user = request.user recipe = get_object_or_404_customed(Recipe, pk=pk) instance, created = Like.objects.get_or_create( user=user, recipe=recipe, ) if not created: instance.delete() return Response( f'User(id:{user.pk})가 recipe(id:{recipe.pk})에 대한 좋아요를 취소하였습니다.', status=status.HTTP_204_NO_CONTENT, ) else: return Response( f'User(id:{user.pk})가 recipe(id:{recipe.pk})를 좋아합니다.', status=status.HTTP_200_OK, )
def get(self, request, pk): recipe = get_object_or_404_customed(Recipe, pk=pk) bookmarkers = recipe.bookmarker.all() serializer = UserSerializer(bookmarkers, many=True, context=self.request) return Response(serializer.data)
def to_internal_value(self, data): topping_name = data.get('name') topping = get_object_or_404_customed(Toppings, name=topping_name) return topping
def to_internal_value(self, data): bread_name = data.get('name') bread = get_object_or_404_customed(Bread, name=bread_name) return bread
def to_internal_value(self, data): sandwich_name = data.get('name') sandwich = get_object_or_404_customed(Sandwich, name=sandwich_name) return sandwich
def to_internal_value(self, data): cheese_name = data.get('name') cheese = get_object_or_404_customed(Cheese, name=cheese_name) return cheese
def get_queryset(self): user = get_object_or_404_customed(User, pk=self.kwargs['pk']) values = Bookmark.objects.filter(user=user) return values
def to_internal_value(self, data): sauce_name = data.get('name') sauce = get_object_or_404_customed(Sauces, name=sauce_name) return sauce
def get(self, request, pk): # recipe = Recipe.objects.get(pk=pk) recipe = get_object_or_404_customed(Recipe, pk=pk) likers = recipe.liker.all() serializer = UserSerializer(likers, many=True, context=self.request) return Response(serializer.data)