python - 如何在 Django 1.10 中跨 2 个不同的基于类的 View 重构类似的函数?

标签 python django strategy-pattern

我正在使用代码覆盖,它提示我在 2 个不同的基于类的 View 中有 2 个函数,它们太相似了。

附件是代码覆盖错误 enter image description here

下面是突出显示的代码:

class PalletContentPickup(APIView):
    """
    Picking up a pallet content to transfer to exiting pallet content
    """
    def put(self, request, pk):
        count = request.data['count']
        pallet_id = request.data['pallet_id']
        from_pallet_content = QuickFind.get_pallet_content_or_404(pallet_content_id=pk)
        to_pallet = QuickFind.get_pallet_or_404(pallet_id=pallet_id)

        Transfer.validate_if_can_pickup_pallet_content(from_pallet_content, to_pallet, request.user)

        to_pallet_content = QuickFind.get_or_create_pallet_content(pallet=to_pallet, product=from_pallet_content.product)
        ExitFormHelper.create_exit_form_line_item_on_pallet_content_if_no_exit_form_line(to_pallet_content, request.user)
        Transfer.previous_pallet_content_to_new_pallet_content(from_pallet_content, to_pallet_content, count)

        serializer = PalletSerializer(from_pallet_content.pallet)
        return Response({"data": serializer.data}, status=status.HTTP_202_ACCEPTED)


class PalletContentPutback(APIView):
    """
    Put back pallet content to an approved pallet
    """
    def put(self, request, pk):
        count = request.data['count']
        pallet_id = request.data['pallet_id']
        from_pallet_content = QuickFind.get_pallet_content_or_404(pallet_content_id=pk)
        to_pallet = QuickFind.get_pallet_or_404(pallet_id=pallet_id)

        Transfer.validate_if_can_putback_pallet_content(from_pallet_content, to_pallet, request.user)

        to_pallet_content = QuickFind.get_or_create_pallet_content(pallet=to_pallet, product=from_pallet_content.product)
        ExitFormHelper.create_exit_form_line_item_on_pallet_content_if_no_exit_form_line(to_pallet_content, request.user)
        Transfer.previous_pallet_content_to_new_pallet_content(from_pallet_content, to_pallet_content, count)

        serializer = PalletSerializer(from_pallet_content.pallet)
        return Response({"data": serializer.data}, status=status.HTTP_202_ACCEPTED)

我读到了strategy pattern in Python

不确定我是否应该在这里应用策略模式,如果是,如何应用?因为url中的例子仍然不能帮助我准确地理解如何在这里执行策略模式。

最佳答案

据我所知,这两个类之间只有一行区别。这样你就可以保持简单

class PalletContent(object):
    """
    Put back pallet content to an approved pallet
    """
    def do_action(self, request, pk, action):
        count = request.data['count']
        pallet_id = request.data['pallet_id']
        from_pallet_content = QuickFind.get_pallet_content_or_404(pallet_content_id=pk)
        to_pallet = QuickFind.get_pallet_or_404(pallet_id=pallet_id)

        if action == 'putback':
            Transfer.validate_if_can_putback_pallet_content(from_pallet_content, to_pallet, request.user)
        else:
            Transfer.validate_if_can_pickup_pallet_content(from_pallet_content, to_pallet, request.user)

        to_pallet_content = QuickFind.get_or_create_pallet_content(pallet=to_pallet, product=from_pallet_content.product)
        ExitFormHelper.create_exit_form_line_item_on_pallet_content_if_no_exit_form_line(to_pallet_content, request.user)
        Transfer.previous_pallet_content_to_new_pallet_content(from_pallet_content, to_pallet_content, count)

        serializer = PalletSerializer(from_pallet_content.pallet)
        return Response({"data": serializer.data}, status=status.HTTP_202_ACCEPTED)

class PalletContentPickup(APIView, PalletContent):
     def put(self,request,pk):
         self.do_action(request,pk,'pickup')

class PalletContentPutback(APIView, PalletContent):
     def put(self,request,pk):
         self.do_action(request,pk,'putback')

您只节省了几行代码,但在维护方面可能是值得的。同时您的验证方法似乎没有返回任何内容。他们会提出异常(exception)吗?

关于python - 如何在 Django 1.10 中跨 2 个不同的基于类的 View 重构类似的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41373966/

相关文章:

python - python/django 中巨型 heredoc 的上下文替代

design-patterns - 谁应该实例化和分配运行时策略实现?

clojure - 如何在 Clojure 中抽象出实现细节?

python - .get 元组与字典

python - 安装 pip 在 python < 3.6 中不起作用

python - CVXOPT L1 范数近似 - ldB 的非法值

python - 在 Django View 中诊断 AttributeError

python - 带有 PK 的 Django REST POST 到 HyperlinkedModelSerializer,如何将 PK 转换为 URL?

design-patterns - 策略模式 VS 装饰模式

python - 寻找循环中的最大值