首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Django 1.9中重构基于类的视图中的重复方法

如何在Django 1.9中重构基于类的视图中的重复方法
EN

Stack Overflow用户
提问于 2016-12-19 16:14:52
回答 0查看 196关注 0票数 0

我有一个基于类的视图,叫做PalletContentProductionView

代码语言:javascript
复制
@method_decorator(PermissionManager.production_worker_required, name='dispatch')
class PalletContentProductionView(APIView):
    """
    Update a Pallet Content count and/or product
    """
    def put(self, request, pk):
        pallet_content = QuickFind.get_pallet_content_or_404(pallet_content_id=pk)

        def validate_pending_and_created_by_user(pallet_content, user=request.user):
            if not pallet_content.pending():
                raise PermissionDenied("Non-Pending pallet content cannot be updated by production worker")
            EntryFormHelper.throw_403_when_not_created_by_user(pallet_content=pallet_content, user=user)
        return update_pallet_content(request, pallet_content, validate_pending_and_created_by_user)

    """
    Delete pallet only if pallet content is pending and creater by user
    """
    def delete(self, request, pk):
        pallet_content = QuickFind.get_pallet_content_or_404(pallet_content_id=pk)

        def validate_pending_and_created_by_user(pallet_content, user=request.user):
            if pallet_content.pending() is False:
                raise PermissionDenied("Non-Pending pallet content cannot be updated by production worker")
            EntryFormHelper.throw_403_when_not_created_by_user(pallet_content=pallet_content, user=user)
        return delete_pallet_content(request, pallet_content, validate_pending_and_created_by_user)

我想知道如何重构才能避免重复的四行代码?

这四行是:

代码语言:javascript
复制
def validate_pending_and_created_by_user(pallet_content, user=request.user):
        if not pallet_content.pending():
            raise PermissionDenied("Non-Pending pallet content cannot be updated by production worker")
        EntryFormHelper.throw_403_when_not_created_by_user(pallet_content=pallet_content, user=user)

我试着把它拉出来,放在PalletContentProductionView中,并与put和delete方法相邻,但这是错误的。

EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41218314

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档