gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 15:50:22 26 4
gpt4 key购买 nike

我正在使用代码覆盖,它提示我在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com