0

我正在尝试将 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_permissionsin 命令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_labelpartnerpartner是奥斯卡应用程序之一)。所以我想我应该重写check_permissions管理命令来检查适应 django-oscar 的权限。我的问题是:

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

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

  3. 我什至可以正确地解决这个问题吗?oscar也许我误解了如何或是否userena可以一起工作。

4

1 回答 1

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

这对我有用 - 通过这个 hack,我现在可以将非员工用户与履行合作伙伴联系起来。

于 2014-07-10T22:31:10.940 回答