コード例 #1
0
ファイル: api.py プロジェクト: khoaanh2212/nextChef
    def post(self, request, book_id, recipe_id):
        try:
            from django.db.models import get_model

            model_class = get_model('books', 'Book')
            book = model_class.objects.get(pk=book_id)

            if request.user != book.chef:
                return Response({
                    'success': False,
                },
                                status=status.HTTP_403_FORBIDDEN)

            recipe = Recipes.objects.get(id=recipe_id)

            if recipe.chef != book.chef and \
                    not ChefsHasRecipes.objects.filter(chef=book.chef, recipe=recipe).exists():
                ChefsHasRecipes.objects.create(chef=book.chef, recipe=recipe)
                Notification.create_new_copy_recipe(recipe, book.chef)

            if not book.has_recipe(recipe):
                book.add_recipe(recipe)
                # Events.track(request.user.email, Events.EVENT_RECIPE_TO_BOOK)
                cache = get_cache('default')
                key = CacheUtils.get_key(CacheUtils.CHEF_BOOKS,
                                         chef_id=request.user.id)
                cache.set(key, None)
                return Response({
                    'success': True,
                    'added': True
                },
                                status=status.HTTP_200_OK)
            else:
                book.delete_recipe(recipe)
                cache = get_cache('default')
                key = CacheUtils.get_key(CacheUtils.CHEF_BOOKS,
                                         chef_id=request.user.id)
                cache.set(key, None)
                return Response({
                    'success': True,
                    'added': False
                },
                                status=status.HTTP_200_OK)

        except:
            return Response({
                'success': False,
            },
                            status=status.HTTP_400_BAD_REQUEST)
コード例 #2
0
ファイル: recipes.py プロジェクト: khoaanh2212/nextChef
    def copy(self, request, *args, **kwargs):
        """
        Copy recipe
        """
        if 'pk' not in kwargs:
            raise Http404()

        recipe = get_object_or_404(Recipes, pk=kwargs['pk'])
        if recipe.chef == request.user:
            return self.invalid_request(
                'The chef is already the owner of this recipe')
        if recipe.draft:
            return self.invalid_request('The recipe is a draft')
        if recipe.private:
            return self.invalid_request('The recipe is private')
        if recipe.is_in_book_for_sale:
            return self.invalid_request(
                'A recipe in book for sale cannot be copied')

        if not ChefsHasRecipes.objects.filter(chef=request.user,
                                              recipe=recipe).exists():
            ChefsHasRecipes.objects.create(chef=request.user, recipe=recipe)
            recipe.update_adds()
            Notification.create_new_copy_recipe(recipe, request.user)

        if 'books' in request.GET:
            # In the original PHP code there was two odd things:
            # 1. The request returned a 400 error if one of the books ids did not exist. But
            #    it created the relation (recipe, chef) anyway and all the relations (recipe, book)
            #    previously processed until failing id
            #    - Here we will just ignore the non existing books
            # 2. It allowed to add the recipe to a book that was not owned by the user sending
            #    the request, the only requirement was the book existed
            #    - Here we do not allow that and will just ignore the book
            books_ids = request.GET['books']
            books = Books.objects.filter(chef=request.user, pk__in=books_ids)
            for book in books:
                book.add_recipe(recipe)

        serializer = ApiRecipeSerializer(recipe)
        serializer.user = request.user
        data = self.remove_unwanted_data(serializer.data)
        return Response({'recipe': data})
コード例 #3
0
ファイル: api.py プロジェクト: khoaanh2212/nextChef
    def post(self, request, *args, **kwargs):
        """
        Add recipe to book
        """
        if 'pk' in kwargs:
            raise Http404()

        chef = request.user

        book = get_object_or_404(Book, pk=kwargs['book_pk'])
        if book.chef != chef:
            self.raise_invalid_credentials()

        recipe_id = request.REQUEST.get('recipe')
        try:
            recipe = Recipes.objects.get(pk=recipe_id)
        except Recipes.DoesNotExist:
            return self.invalid_request('Recipe not exists')

        if recipe.chef != chef and (recipe.private or recipe.draft):
            return self.invalid_request('Invalid recipe')

        if recipe.is_in_book_for_sale:
            return self.invalid_request(
                'A recipe in a book for sale cannot be in other books')

        if book.book_type == Book.TO_SELL and recipe.chef != chef:
            return self.invalid_request(
                'A book for sale cannot has recipes from other chefs')

        if BookHasRecipes.objects.filter(book=book, recipe=recipe).exists():
            return self.invalid_request('Recipe already in book')
        BookHasRecipes.objects.create(book=book, recipe=recipe)

        if recipe.chef != chef and \
                not ChefsHasRecipes.objects.filter(chef=chef, recipe=recipe).exists():
            ChefsHasRecipes.objects.create(chef=chef, recipe=recipe)
            Notification.create_new_copy_recipe(recipe, chef)

        # TODO: Update ChefsHasRecipes table accordingly

        return Response({'response': {'return': True}})