def dispatch(self, request, *args, **kwargs): module_str = "%s:%s" % (request.GET.get("module"), request.GET.get("model")) self.return_url = reverse("shuup_admin:%s.list" % request.GET.get("return_url")) match = resolve(self.return_url) view_context = load("%s:%s" % (match.func.__module__, match.func.__name__)) default_columns = view_context.default_columns self.model = load(module_str) self.settings = ViewSettings(self.model, default_columns, view_context) return super(ListSettingsView, self).dispatch(request, *args, **kwargs)
def __init__(self, url, **kwargs): """ :param url: The URL to navigate to. For convenience, if this contains no slashes, `reverse` is automatically called on it. :type url: str """ if "/" not in url: try: url = reverse(url) except NoReverseMatch: pass self.url = url if "required_permissions" not in kwargs: try: permission = resolve(six.moves.urllib.parse.urlparse(force_text(url)).path).url_name kwargs["required_permissions"] = (permission,) except Resolver404: pass super(URLActionButton, self).__init__(**kwargs)
def get_model_url(object, kind="detail", user=None, required_permissions=None, shop=None, raise_permission_denied=False, **kwargs): """ Get a an admin object URL for the given object or object class by interrogating each admin module. If a user is provided, checks whether user has correct permissions before returning URL. Raises `NoModelUrl` if lookup fails :param object: Model or object class. :type object: class :param kind: URL kind. Currently "new", "list", "edit", "detail". :type kind: str :param user: Optional instance to check for permissions. :type user: django.contrib.auth.models.User|None :param required_permissions: Optional iterable of permission strings. :type required_permissions: Iterable[str]|None :param shop: The shop that owns the resource. :type request: shuup.core.models.Shop|None :param raise_permission_denied: raise PermissionDenied exception if the url is found but user has not permission. If false, None will be returned instead. Default is False. :type raise_permission_denied: bool :return: Resolved URL. :rtype: str """ for module in get_modules(): url = module.get_model_url(object, kind, shop) if not url: continue if user is None: return url from shuup.utils.django_compat import Resolver404, resolve try: if required_permissions is not None: warnings.warn( "Warning! `required_permissions` parameter will be deprecated " "in Shuup 2.0 as unused for this util.", DeprecationWarning, ) permissions = required_permissions else: resolved = resolve(url) from shuup.admin.utils.permissions import get_permissions_for_module_url permissions = get_permissions_for_module_url( module, resolved.url_name) missing_permissions = get_missing_permissions(user, permissions) if not missing_permissions: return url if raise_permission_denied: from django.core.exceptions import PermissionDenied reason = _( "Can't view this page. You do not have the required permission(s): `{permissions}`." ).format(permissions=", ".join(missing_permissions)) raise PermissionDenied(reason) except Resolver404: # what are you doing developer? return url raise NoModelUrl("Error! Can't get object URL of kind %s: %r." % (kind, force_text(object)))