如何在 Django 1.10 中跨 2 个不同的基于 class 的视图重构相似的函数?

How to refactor similar looking functions across 2 different class based views in Django 1.10?

我正在使用代码覆盖,它抱怨我在 2 个不同的基于 class 的视图中有 2 个函数太相似了。

附上代码覆盖错误

以下是突出显示的代码:

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

您只节省了几行代码,但在维护方面可能是值得的。同时,您的验证方法似乎没有 return 任何东西。他们是否提出异常?