Skip to content

Commit

Permalink
Merge pull request #219 from ChanTsune/feature/django-4.1
Browse files Browse the repository at this point in the history
Feature/django 4.1
  • Loading branch information
ChanTsune authored Aug 20, 2022
2 parents 1ea8b2c + 46f6fa7 commit ac39767
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 20 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ jobs:
- '3.1.*'
- '3.2.*'
- '4.0.*'
- '4.1.*'
runs-on: ubuntu-latest
env:
DJANGO_VERSION: ${{ matrix.django-version }}
Expand Down
54 changes: 34 additions & 20 deletions django_boost/views/mixins.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import json
from datetime import timedelta

import django
from django.contrib.auth.mixins import AccessMixin
from django.contrib.auth.views import (SuccessURLAllowedHostsMixin,
logout_then_login, redirect_to_login)
from django.contrib.auth.views import (logout_then_login, redirect_to_login)
from django.core.exceptions import ImproperlyConfigured
from django.http import Http404, JsonResponse
from django.urls import reverse
Expand Down Expand Up @@ -31,26 +31,40 @@ def dispatch(self, request, *args, **kwargs):
return super().dispatch(request, *args, **kwargs)


class DynamicRedirectMixin(SuccessURLAllowedHostsMixin):
if django.VERSION >= (4, 1):
from django.contrib.auth.views import RedirectURLMixin

redirect_field_name = 'next'
class DynamicRedirectMixin(RedirectURLMixin):
success_url = None

def get_success_url(self):
url = self.get_redirect_url()
return url or super().get_success_url()

def get_redirect_url(self):
"""Return the user-originating redirect URL if it's safe."""
redirect_to = self.request.POST.get(
self.redirect_field_name,
self.request.GET.get(self.redirect_field_name, '')
)
url_is_safe = url_has_allowed_host_and_scheme(
url=redirect_to,
allowed_hosts=self.get_success_url_allowed_hosts(),
require_https=self.request.is_secure(),
)
return redirect_to if url_is_safe else ''
def get_success_url(self):
url = self.get_redirect_url()
return url or self.success_url or super().get_success_url()


else:
from django.contrib.auth.views import SuccessURLAllowedHostsMixin

class DynamicRedirectMixin(SuccessURLAllowedHostsMixin):

redirect_field_name = 'next'

def get_success_url(self):
url = self.get_redirect_url()
return url or super().get_success_url()

def get_redirect_url(self):
"""Return the user-originating redirect URL if it's safe."""
redirect_to = self.request.POST.get(
self.redirect_field_name,
self.request.GET.get(self.redirect_field_name, '')
)
url_is_safe = url_has_allowed_host_and_scheme(
url=redirect_to,
allowed_hosts=self.get_success_url_allowed_hosts(),
require_https=self.request.is_secure(),
)
return redirect_to if url_is_safe else ''


class RedirectToDetailMixin:
Expand Down

0 comments on commit ac39767

Please sign in to comment.