Coverage for website/utils/admin.py: 92.86%
20 statements
« prev ^ index » next coverage.py v7.6.7, created at 2025-08-14 10:31 +0000
« prev ^ index » next coverage.py v7.6.7, created at 2025-08-14 10:31 +0000
1from django.contrib.admin import ModelAdmin
2from django.core.exceptions import DisallowedRedirect
3from django.http import HttpResponseRedirect
4from django.utils.http import url_has_allowed_host_and_scheme
7def _do_next(request, response):
8 """See DoNextModelAdmin."""
9 if "next" in request.GET:
10 if not url_has_allowed_host_and_scheme(
11 request.GET["next"], allowed_hosts={request.get_host()}
12 ):
13 raise DisallowedRedirect
14 if "_save" in request.POST:
15 return HttpResponseRedirect(request.GET["next"])
16 if response is not None: 16 ↛ 17line 16 didn't jump to line 17 because the condition on line 16 was never true
17 return HttpResponseRedirect(f"{response.url}?{request.GET.urlencode()}")
18 return response
21class DoNextModelAdmin(ModelAdmin):
22 """This class adds processing of a `next` parameter in the urls of the add and change admin forms.
24 If it is set and safe this override will redirect the user to the provided url.
25 """
27 def response_add(self, request, obj, **kwargs):
28 res = super().response_add(request, obj, **kwargs)
29 return _do_next(request, res)
31 def response_change(self, request, obj):
32 res = super().response_change(request, obj)
33 return _do_next(request, res)