Code

Opened 8 years ago

Closed 16 months ago

#2615 closed New feature (duplicate)

[patch] Make reversed url-resolving aware of matched kwargs and extra options

Reported by: norjee@… Owned by: nobody
Component: Core (Other) Version:
Severity: Normal Keywords:
Cc: Triage Stage: Someday/Maybe
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The current reversed url-resolving only returned a matching url. This is fine for simple issues, but often more info is needed.

For example:

  • It is usefull to know which of the supplied kwargs have been matched, so one could, for example, supply the other ones in the query string.
  • An url pattern can have default-arguments. These could be used to create a middleware that redirects to https if, for example, the default argument https:True has been supplied. It would be usefull to build a secure url with reversed url-resolving, but for that, the default arguments need to be known.

This patch introduced a new method, reverse_match (the old reverse method still returns the url, but uses reverse_match to find it), which returns a match-object:

class ReversedMatch:
    """
    url is te resulting url
    matched_args is a list with matched named args
    default_args a hash, with the associated default args
    """
    def __init__(self, url , matched_kwargs , default_args):
        self.url = url
        self.matched_kwargs = matched_kwargs
        self.default_args = default_args
            
    def __add__(self , other):
        new_default_args = self.default_args.copy() 
        new_default_args.update(other.default_args)
        return ReversedMatch(self.url + other.url , self.matched_kwargs + other.matched_kwargs , new_default_args)
    
    def get_unmatched_kwargs(self, kwargs):
        return dict([(k, v) for k , v in kwargs.items() if not k in self.matched_kwargs])
    
    def get_default_args(self):
        return self.default_args
    
    def get_url(self):
        return '/' + self.url

As an example, the follwing method appends builds an url through this new reversed url-lookup, and then appends the unmatched kwargs:

def find_url_for_view(view , *args , **kwargs):
     # url = reverse(view, *args , **kwargs )
     # logger.debug(args)
     # logger.debug(kwargs)
     
     match = reverse_match(view,  args = args ,  kwargs = kwargs)
     url = match.get_url()
     logger.debug(match.get_unmatched_kwargs(kwargs))
     logger.debug(match.get_default_args())
     # logger.debug(re.split(r"[/.]" , url))
     
     # now i want to append a query string
     # i want to use the unmatched kwargs for this
     query_string = "&".join(["=".join([k, str(v)]) for k , v in match.get_unmatched_kwargs(kwargs).items()])
     logger.debug(query_string)
     if len(query_string) > 0:
         query_string = "?" + query_string
         
     return url + query_string

Attachments (2)

url_resolve.diff (5.7 KB) - added by norjee@… 8 years ago.
Patch for urlresolvers.py
urlhelpers.py (3.4 KB) - added by norjee@… 8 years ago.
url_for template tag. More as an example than to be fully useful (it has my own debug code in it)

Download all attachments as: .zip

Change History (8)

Changed 8 years ago by norjee@…

Patch for urlresolvers.py

Changed 8 years ago by norjee@…

url_for template tag. More as an example than to be fully useful (it has my own debug code in it)

comment:1 Changed 7 years ago by SmileyChris

  • Triage Stage changed from Unreviewed to Design decision needed

comment:2 Changed 7 years ago by jacob

  • Triage Stage changed from Design decision needed to Someday/Maybe

comment:3 Changed 3 years ago by lrekucki

  • Severity changed from normal to Normal
  • Type changed from enhancement to New feature

comment:4 Changed 2 years ago by aaugustin

  • UI/UX unset

Change UI/UX from NULL to False.

comment:5 Changed 2 years ago by aaugustin

  • Easy pickings unset

Change Easy pickings from NULL to False.

comment:6 Changed 16 months ago by aaugustin

  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of #16391 which contains more information.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.