Is it possible to override reverse in a Django project?

I have custom logic that should be executed every time the URL is canceled, even for third-party applications. My project is a multi-user web application, and the tenant is identified based on the URL. There is no valid URL that does not include the tenant ID.

I already have a wrapper function around reverse, but now I need a way to tell every installed application to use it. The shell reverseuses a local stream to insert the identifier in the resulting URL. I could write this function as a decorator on reverse, but I do not know where to do the actual design.

Moderately tight restriction: I am already using 3 third-party applications, and I will probably add more. The solution should not require me to change the source code of all these third-party applications. I do not like the idea of ​​saving patches on top of several source trees of third-party developers, if there is an easier way. I can make the documentation very clear what reversewas decorated.

The original question: where can I make a change that ensures that it applies to every challenge reverse?

Possible alternative question: what is the best way to make sure that every URL, including those created by third-party applications, gets a tenant ID? BTW, I am open for a better way to handle this, other than embedding the tenant identifier in the URL; this solution is pretty accurately set in stone right now. Thanks.

Thanks.

+3
source share
1 answer

only so that django reverse is replaced with ur_reverse is

django.core.urlresolvers.reverse = ur_reverse

or if you like decorator syntactic sugar

django.core.urlresolvers.reverse = ur_reverse_decorator(django.core.urlresolvers.reverse )

which I would not advise (and many will scream) if you do not want to change any use of the reverse with ur_reverse

+5
source

Source: https://habr.com/ru/post/1722313/


All Articles