-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Revert support for Django 1.10 and Django 1.11, replaced django.contrib.redirects for SEO-redirects with subdomain support #21
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
0c3c84e
Added Django 1.11 to travis.yml
tangochin 3360c84
Rollback
tangochin 33806c3
Removed django.contrib.redirects for seo-redirects
tangochin 922b7c5
New redirect model and middleware
tangochin 4526f28
Create and select redirect data with subdomains
tangochin 124d23a
Fixed database error, added more tests
tangochin ca4ab01
Revert support for Django 1.10+
tangochin e4acb6b
Removed excess code, update middleware settings, readme, update version
tangochin a1e6b2f
Update travis config
tangochin 9848972
Merge remote-tracking branch 'upstream/master'
tangochin ba34ee5
Added Python 3.4 and Python 3.5 to travis config
tangochin 260e0b5
Fixed Redirect model
tangochin 9df77a0
pep 440 for version
tangochin File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,89 @@ | ||
import re | ||
from logging import getLogger | ||
|
||
from django import http | ||
from django.apps import apps | ||
from django.db.models import Q | ||
from django.conf import settings | ||
from django.http import Http404 | ||
from django.utils.deprecation import MiddlewareMixin | ||
from django.contrib.sites.shortcuts import get_current_site | ||
from django.core.exceptions import ImproperlyConfigured | ||
|
||
from .models import Redirect | ||
from .utils import handle_seo_redirects | ||
|
||
|
||
logger = getLogger(__name__) | ||
|
||
|
||
# TODO: replace after removing support for old versions of Django | ||
class MiddlewareMixin(object): | ||
""" | ||
This mixin a full copy of Django 1.10 django.utils.deprecation.MiddlewareMixin. | ||
Needed for compatibility reasons. | ||
""" | ||
def __init__(self, get_response=None): | ||
self.get_response = get_response | ||
super(MiddlewareMixin, self).__init__() | ||
|
||
def __call__(self, request): | ||
response = None | ||
if hasattr(self, 'process_request'): | ||
response = self.process_request(request) | ||
if not response: | ||
response = self.get_response(request) | ||
if hasattr(self, 'process_response'): | ||
response = self.process_response(request, response) | ||
return response | ||
|
||
|
||
class RedirectsMiddleware(MiddlewareMixin): | ||
|
||
def process_exception(self, request, exception): | ||
if not getattr(settings, 'SEO_USE_REDIRECTS', False): | ||
return | ||
|
||
if request.method == 'GET' and isinstance(exception, Http404): | ||
if request.method == 'GET' and isinstance(exception, http.Http404): | ||
handle_seo_redirects(request) | ||
return | ||
|
||
|
||
class RedirectFallbackMiddleware(MiddlewareMixin): | ||
# Defined as class-level attributes to be subclassing-friendly. | ||
response_gone_class = http.HttpResponseGone | ||
response_redirect_class = http.HttpResponsePermanentRedirect | ||
|
||
def __init__(self, get_response=None): | ||
if not apps.is_installed('django.contrib.sites'): | ||
raise ImproperlyConfigured( | ||
'You cannot use RedirectFallbackMiddleware when ' | ||
'django.contrib.sites is not installed.' | ||
) | ||
super(RedirectFallbackMiddleware, self).__init__(get_response) | ||
|
||
def process_response(self, request, response): | ||
# No need to check for a redirect for non-404 responses. | ||
if response.status_code != 404: | ||
return response | ||
|
||
subdomain = getattr(request, 'subdomain', '') | ||
full_path = request.get_full_path() | ||
current_site = get_current_site(request) | ||
|
||
redirect = Redirect.objects.filter( | ||
Q(site=current_site), | ||
Q(old_path=full_path), | ||
Q(subdomain=subdomain) | Q(all_subdomains=True) | ||
).order_by('all_subdomains').first() | ||
|
||
if redirect is None and settings.APPEND_SLASH and not request.path.endswith('/'): | ||
redirect = Redirect.objects.filter( | ||
Q(site=current_site), | ||
Q(old_path=request.get_full_path(force_append_slash=True)), | ||
Q(subdomain=subdomain) | Q(all_subdomains=True) | ||
).order_by('all_subdomains').first() | ||
if redirect is not None: | ||
if redirect.new_path == '': | ||
return self.response_gone_class() | ||
return self.response_redirect_class(redirect.new_path) | ||
|
||
# No redirect was found. Return the response. | ||
return response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
# -*- coding:utf-8 -*- | ||
__version__ = "2.5.0+whyfly.1" | ||
__version__ = '2.6.0+whyfly.1' |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
А зачем это было сделано? Нужно руками версию проитерировать