如何在Django1.10中跨两个不同的基于类的视图重构相似的函数?

2024-04-28 17:24:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用codecoverage,它抱怨我在两个不同的基于类的视图中有两个太相似的函数。你知道吗

附件是codecoverage错误 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中的示例仍然不能帮助我准确地了解如何在这里执行策略模式。你知道吗


Tags: ortofromiddatagetifrequest
1条回答
网友
1楼 · 发布于 2024-04-28 17:24:58

在我看来,这两个班只有一条线不同。所以你可以通过

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')

您只节省了几行代码,但在维护方面可能是值得的。同时,您的validate方法似乎没有返回任何内容。他们提出了例外吗?你知道吗

相关问题 更多 >