覆盖django-userena中的check_permissions管理命令以修复django-oscar的权限

时间:2014-07-10 20:27:15

标签: django-oscar django-userena

我正在尝试将django-userena与django-oscar一起使用。到目前为止,除了我尝试将非员工用户链接到履行合作伙伴(链接到员工用户工作正常)之外,它运行良好。这是我得到的错误:

Traceback:
File "/Users/shafiquejamal/allfiles/htdocs/venvs/av4env/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  112.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/shafiquejamal/allfiles/htdocs/venvs/av4env/lib/python2.7/site-packages/django/db/transaction.py" in inner
  371.                 return func(*args, **kwargs)
File "/Users/shafiquejamal/allfiles/htdocs/venvs/av4env/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
  22.                 return view_func(request, *args, **kwargs)
File "/Users/shafiquejamal/allfiles/htdocs/venvs/av4env/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
  22.                 return view_func(request, *args, **kwargs)
File "/Users/shafiquejamal/allfiles/htdocs/venvs/av4env/lib/python2.7/site-packages/django/views/generic/base.py" in view
  69.             return self.dispatch(request, *args, **kwargs)
File "/Users/shafiquejamal/allfiles/htdocs/venvs/av4env/lib/python2.7/site-packages/django/views/generic/base.py" in dispatch
  87.         return handler(request, *args, **kwargs)
File "/Users/shafiquejamal/allfiles/htdocs/venvs/av4env/lib/python2.7/site-packages/oscar/apps/dashboard/partners/views.py" in post
  219.         if self.link_user(user, partner):
File "/Users/shafiquejamal/allfiles/htdocs/venvs/av4env/lib/python2.7/site-packages/oscar/apps/dashboard/partners/views.py" in link_user
  206.                 content_type__app_label='partner')
File "/Users/shafiquejamal/allfiles/htdocs/venvs/av4env/lib/python2.7/site-packages/django/db/models/manager.py" in get
  151.         return self.get_queryset().get(*args, **kwargs)
File "/Users/shafiquejamal/allfiles/htdocs/venvs/av4env/lib/python2.7/site-packages/django/db/models/query.py" in get
  310.                 self.model._meta.object_name)

Exception Type: DoesNotExist at /en/store/dashboard/partners/1/users/380/link/
Exception Value: Permission matching query does not exist.

似乎问题出在oscar.apps.dashboards.partners.views

class PartnerUserLinkView(generic.View):

    def link_user(self, user, partner):
        """
        Links a user to a partner, and adds the dashboard permission if needed.

        Returns False if the user was linked already; True otherwise.
        """
        if partner.users.filter(pk=user.pk).exists():
            return False
        partner.users.add(user)
        if not user.is_staff:
            dashboard_access_perm = Permission.objects.get(
                codename='dashboard_access',
                content_type__app_label='partner')
            user.user_permissions.add(dashboard_access_perm)
        return True

Permission.objects.get不会返回对象。这是因为check_permissions中的django-userena命令检查以下权限:

ASSIGNED_PERMISSIONS = {
    'profile':
        (('view_profile', 'Can view profile'),
         ('change_profile', 'Can change profile'),
         ('delete_profile', 'Can delete profile')),
    'user':
        (('change_user', 'Can change user'),
         ('delete_user', 'Can delete user'))
}

,不包括dashboard_access。我尝试将另一个个人资料权限('dashboard_access', _('Can access dashboard')),),添加到ASSIGNED_PERMISSIONS.profile,但这不起作用 - 我仍然遇到同样的错误。我认为问题是即使dashboard_access是权限之一,link_user方法指定content_type__app_label='partner',但content_type__app_label永远不会partner({{ 1}}是奥斯卡应用程序之一)。所以我想我应该覆盖partner管理命令,以检查容纳django-oscar的权限。我的问题是:

  1. 如何在不触及虚拟环境中的代码的情况下覆盖或扩展check_permissions管理命令?我咨询了https://docs.djangoproject.com/en/1.6/howto/custom-management-commands/,并开始将managers.py代码从check_permissions复制到我项目中的userena文件夹,但得到的命令没有实现错误。

  2. 如何修改检查权限以引用'合作伙伴的应用标签?

  3. 我是否正确地解决了这个问题?也许我误解了management/commandsoscar如何一起工作。

1 个答案:

答案 0 :(得分:0)

我想我是在思考这个问题。似乎unlink_user方法删除了link_user方法查找的权限对象,这是后一种方法无法找到权限对象的另一个原因(另一个原因是该对象可能不是创造于第一位)。我通过覆盖link_user方法将一个(不是真的?)解决方案整合在一起,如下所示:

# yourproject/dashboard/partners/views.py

from django.contrib.contenttypes.models import ContentType
from django.contrib.auth.models import Permission

from oscar.apps.dashboard.partners.views import PartnerUserLinkView as CorePartnerUserLinkView

class PartnerUserLinkView(CorePartnerUserLinkView):

    def link_user(self, user, partner):
        """
        Links a user to a partner, and adds the dashboard permission if needed.

        Returns False if the user was linked already; True otherwise.
        """
        if partner.users.filter(pk=user.pk).exists():
            return False
        partner.users.add(user)
        if not user.is_staff:
            try:
                dashboard_access_perm = Permission.objects.get(
                    codename='dashboard_access',
                    content_type__app_label='partner')
            except:
                try:
                    my_content_type = ContentType.objects.get(name='partner',
                        app_label='partner',model='partner')
                except:
                    my_content_type = ContentType.objects.create(name='partner',
                        app_label='partner',model='partner')
                    my_content_type.save()
                my_permission = Permission.objects.create(name='partner',
                    content_type=my_content_type,codename='dashboard_access')
                my_permission.save()
                dashboard_access_perm = Permission.objects.get(
                    codename='dashboard_access',
                    content_type__app_label='partner')
            user.user_permissions.add(dashboard_access_perm)
        return True

这对我有用 - 有了这个黑客,我现在可以将非员工用户链接到履行合作伙伴。