def get_url(self, context): try: self.view_name = self.view_name.resolve(context) except AttributeError: pass return URLNode.render(self, context), {}
def get_url(self, context): try: self.view_name = self.view_name.resolve(context) except AttributeError: pass return URLNode.render(self, context), context['request'].GET.copy()
def get_url(self, context): try: self.view_name = self.view_name.resolve(context) except AttributeError: pass return URLNode.render(self, context), context['request'].GET.copy()
def resolve(self, context, resolved_object=None): AccessControlList = apps.get_model( app_label='acls', model_name='AccessControlList' ) # Try to get the request object the faster way and fallback to the # slower method. try: request = context.request except AttributeError: request = Variable('request').resolve(context) current_path = request.META['PATH_INFO'] current_view = resolve(current_path).view_name # ACL is tested agains the resolved_object or just {{ object }} if not if not resolved_object: try: resolved_object = Variable('object').resolve(context=context) except VariableDoesNotExist: pass # If this link has a required permission check that the user has it # too if self.permissions: if resolved_object: try: AccessControlList.objects.check_access( permissions=self.permissions, user=request.user, obj=resolved_object, related=self.permissions_related ) except PermissionDenied: return None else: try: Permission.check_permissions( requester=request.user, permissions=self.permissions ) except PermissionDenied: return None # Check to see if link has conditional display function and only # display it if the result of the conditional display function is # True if self.condition: if not self.condition(context): return None resolved_link = ResolvedLink(current_view=current_view, link=self) if self.view: view_name = Variable('"{}"'.format(self.view)) if isinstance(self.args, list) or isinstance(self.args, tuple): # TODO: Don't check for instance check for iterable in try/except # block. This update required changing all 'args' argument in # links.py files to be iterables and not just strings. args = [Variable(arg) for arg in self.args] else: args = [Variable(self.args)] # If we were passed an instance of the view context object we are # resolving, inject it into the context. This help resolve links for # object lists. if resolved_object: context['resolved_object'] = resolved_object try: kwargs = self.kwargs(context) except TypeError: # Is not a callable kwargs = self.kwargs kwargs = {key: Variable(value) for key, value in kwargs.items()} # Use Django's exact {% url %} code to resolve the link node = URLNode( view_name=view_name, args=args, kwargs=kwargs, asvar=None ) try: resolved_link.url = node.render(context) except Exception as exception: logger.error( 'Error resolving link "%s" URL; %s', self.text, exception ) elif self.url: resolved_link.url = self.url # This is for links that should be displayed but that are not clickable if self.conditional_disable: resolved_link.disabled = self.conditional_disable(context) else: resolved_link.disabled = False # Lets a new link keep the same URL query string of the current URL if self.keep_query: # Sometimes we are required to remove a key from the URL QS parsed_url = furl( force_str( request.get_full_path() or request.META.get( 'HTTP_REFERER', resolve_url(settings.LOGIN_REDIRECT_URL) ) ) ) for key in self.remove_from_query: try: parsed_url.query.remove(key) except KeyError: pass # Use the link's URL but with the previous URL querystring new_url = furl(resolved_link.url) new_url.args = parsed_url.querystr resolved_link.url = new_url.url resolved_link.context = context return resolved_link
def resolve(self, context, resolved_object=None): AccessControlList = apps.get_model(app_label='acls', model_name='AccessControlList') request = Variable('request').resolve(context) current_path = request.META['PATH_INFO'] current_view = resolve(current_path).view_name # ACL is tested agains the resolved_object or just {{ object }} if not if not resolved_object: try: resolved_object = Variable('object').resolve(context=context) except VariableDoesNotExist: pass # If this link has a required permission check that the user has it # too if self.permissions: if resolved_object: try: AccessControlList.objects.check_access( permissions=self.permissions, user=request.user, obj=resolved_object, related=self.permissions_related) except PermissionDenied: return None else: try: Permission.check_permissions(requester=request.user, permissions=self.permissions) except PermissionDenied: return None # Check to see if link has conditional display function and only # display it if the result of the conditional display function is # True if self.condition: if not self.condition(context): return None resolved_link = ResolvedLink(current_view=current_view, link=self) if self.view: view_name = Variable('"{}"'.format(self.view)) if isinstance(self.args, list) or isinstance(self.args, tuple): # TODO: Don't check for instance check for iterable in try/except # block. This update required changing all 'args' argument in # links.py files to be iterables and not just strings. args = [Variable(arg) for arg in self.args] else: args = [Variable(self.args)] # If we were passed an instance of the view context object we are # resolving, inject it into the context. This help resolve links for # object lists. if resolved_object: context['resolved_object'] = resolved_object try: kwargs = self.kwargs(context) except TypeError: # Is not a callable kwargs = self.kwargs kwargs = {key: Variable(value) for key, value in kwargs.items()} # Use Django's exact {% url %} code to resolve the link node = URLNode(view_name=view_name, args=args, kwargs=kwargs, asvar=None) try: resolved_link.url = node.render(context) except Exception as exception: logger.error('Error resolving link "%s" URL; %s', self.text, exception) elif self.url: resolved_link.url = self.url # This is for links that should be displayed but that are not clickable if self.conditional_disable: resolved_link.disabled = self.conditional_disable(context) else: resolved_link.disabled = False # Lets a new link keep the same URL query string of the current URL if self.keep_query: # Sometimes we are required to remove a key from the URL QS parsed_url = furl( force_str(request.get_full_path() or request.META.get( 'HTTP_REFERER', resolve_url(settings.LOGIN_REDIRECT_URL)))) for key in self.remove_from_query: try: parsed_url.query.remove(key) except KeyError: pass # Use the link's URL but with the previous URL querystring new_url = furl(resolved_link.url) new_url.args = parsed_url.querystr resolved_link.url = new_url.url resolved_link.context = context return resolved_link
def resolve(self, context, resolved_object=None): request = Variable('request').resolve(context) current_path = request.META['PATH_INFO'] current_view = resolve(current_path).view_name # ACL is tested agains the resolved_object or just {{ object }} if not if not resolved_object: try: resolved_object = Variable('object').resolve(context=context) except VariableDoesNotExist: pass # If this link has a required permission check that the user have it # too if self.permissions: try: Permission.check_permissions(request.user, self.permissions) except PermissionDenied: # If the user doesn't have the permission, and we are passed # an instance, check to see if the user has at least ACL # access to the instance. if resolved_object: try: AccessControlList.objects.check_access( self.permissions, request.user, resolved_object) except PermissionDenied: return None else: return None # Check to see if link has conditional display function and only # display it if the result of the conditional display function is # True if self.condition: if not self.condition(context): return None resolved_link = ResolvedLink(current_view=current_view, link=self) view_name = Variable('"{}"'.format(self.view)) if isinstance(self.args, list) or isinstance(self.args, tuple): # TODO: Don't check for instance check for iterable in try/except # block. This update required changing all 'args' argument in # links.py files to be iterables and not just strings. args = [Variable(arg) for arg in self.args] else: args = [Variable(self.args)] # If we were passed an instance of the view context object we are # resolving, inject it into the context. This help resolve links for # object lists. if resolved_object: context['resolved_object'] = resolved_object try: kwargs = self.kwargs(context) except TypeError: # Is not a callable kwargs = self.kwargs kwargs = {key: Variable(value) for key, value in kwargs.iteritems()} # Use Django's exact {% url %} code to resolve the link node = URLNode(view_name=view_name, args=args, kwargs=kwargs, asvar=None) try: resolved_link.url = node.render(context) except Exception as exception: logger.error('Error resolving link "%s" URL; %s', self.text, exception) # This is for links that should be displayed but that are not clickable if self.conditional_disable: resolved_link.disabled = self.conditional_disable(context) else: resolved_link.disabled = False # Lets a new link keep the same URL query string of the current URL if self.keep_query: # Sometimes we are required to remove a key from the URL QS previous_path = smart_unicode( urllib.unquote_plus( smart_str(request.get_full_path()) or smart_str( request.META.get('HTTP_REFERER', reverse( settings.LOGIN_REDIRECT_URL))))) query_string = urlparse.urlparse(previous_path).query parsed_query_string = urlparse.parse_qs(query_string) for key in self.remove_from_query: try: del parsed_query_string[key] except KeyError: pass resolved_link.url = '%s?%s' % (urlquote( resolved_link.url), urlencode(parsed_query_string, doseq=True)) return resolved_link
def render(self, context): url = URLNode.render(self, context).replace(self.fb_callback, '') return urljoin("http://apps.xiaonei.com/%s/" % self.app_name, url)
def resolve(self, context, resolved_object=None): request = Variable('request').resolve(context) current_path = request.META['PATH_INFO'] current_view = resolve(current_path).view_name # ACL is tested agains the resolved_object or just {{ object }} if not if not resolved_object: try: resolved_object = Variable('object').resolve(context=context) except VariableDoesNotExist: pass # If this link has a required permission check that the user have it # too if self.permissions: try: Permission.check_permissions(request.user, self.permissions) except PermissionDenied: # If the user doesn't have the permission, and we are passed # an instance, check to see if the user has at least ACL # access to the instance. if resolved_object: try: AccessControlList.objects.check_access( self.permissions, request.user, resolved_object ) except PermissionDenied: return None else: return None # Check to see if link has conditional display function and only # display it if the result of the conditional display function is # True if self.condition: if not self.condition(context): return None resolved_link = ResolvedLink(current_view=current_view, link=self) view_name = Variable('"{}"'.format(self.view)) if isinstance(self.args, list) or isinstance(self.args, tuple): # TODO: Don't check for instance check for iterable in try/except # block. This update required changing all 'args' argument in # links.py files to be iterables and not just strings. args = [Variable(arg) for arg in self.args] else: args = [Variable(self.args)] # If we were passed an instance of the view context object we are # resolving, inject it into the context. This help resolve links for # object lists. if resolved_object: context['resolved_object'] = resolved_object try: kwargs = self.kwargs(context) except TypeError: # Is not a callable kwargs = self.kwargs kwargs = {key: Variable(value) for key, value in kwargs.iteritems()} # Use Django's exact {% url %} code to resolve the link node = URLNode( view_name=view_name, args=args, kwargs=kwargs, asvar=None ) try: resolved_link.url = node.render(context) except Exception as exception: logger.error( 'Error resolving link "%s" URL; %s', self.text, exception ) # This is for links that should be displayed but that are not clickable if self.conditional_disable: resolved_link.disabled = self.conditional_disable(context) else: resolved_link.disabled = False # Lets a new link keep the same URL query string of the current URL if self.keep_query: # Sometimes we are required to remove a key from the URL QS previous_path = smart_unicode( urllib.unquote_plus( smart_str( request.get_full_path() ) or smart_str( request.META.get( 'HTTP_REFERER', reverse(settings.LOGIN_REDIRECT_URL) ) ) ) ) query_string = urlparse.urlparse(previous_path).query parsed_query_string = urlparse.parse_qs(query_string) for key in self.remove_from_query: try: del parsed_query_string[key] except KeyError: pass resolved_link.url = '%s?%s' % ( urlquote(resolved_link.url), urlencode(parsed_query_string, doseq=True) ) return resolved_link