You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
2.7 KiB
65 lines
2.7 KiB
# -*- coding: utf-8 -*-
|
|
# from django.contrib.redirects.middleware import RedirectFallbackMiddleware
|
|
import re
|
|
|
|
from django.http import HttpResponsePermanentRedirect
|
|
from django.core.urlresolvers import reverse
|
|
|
|
from exposition.models import Exposition
|
|
from country.models import Country
|
|
from city.models import City
|
|
|
|
|
|
class RedirectFallbackMiddleware(object):
|
|
def process_response(self, request, response):
|
|
if response.status_code != 404:
|
|
return response # No need to check for a redirect for non-404 responses.
|
|
|
|
full_path = request.get_full_path()
|
|
|
|
redirects = [
|
|
(r'^/(?P<event_type>expo|conference)/(?P<slug>[^/]*)/$', check_events),
|
|
(r'^/places/(?P<slug>[^/]*)/$', check_places),
|
|
]
|
|
|
|
for regex, handler in redirects:
|
|
check = re.compile(regex)
|
|
match = check.match(full_path)
|
|
if match:
|
|
response = handler(**match.groupdict())
|
|
if response is not None:
|
|
return response
|
|
return response
|
|
|
|
|
|
def check_events(event_type, slug):
|
|
'''
|
|
Event redirects (ticket 1498: Внепланово со скайпа - редиректы 301)
|
|
https://expomap.ru/expo/dusseldorf/ -> https://expomap.ru/expo/city/dusseldorf/
|
|
https://expomap.ru/expo/moscow/ -> https://expomap.ru/expo/country/moscow/
|
|
https://expomap.ru/conference/berlin/ -> https://expomap.ru/conference/city/berlin/
|
|
https://expomap.ru/conference/moscow/ -> https://expomap.ru/conference/country/moscow/
|
|
'''
|
|
types = {
|
|
'expo': 'expo',
|
|
'conference': 'conf'
|
|
}
|
|
if City.objects.filter(url=slug).exists():
|
|
return HttpResponsePermanentRedirect(reverse(types.get(event_type) + '_city', kwargs={'slug': slug}))
|
|
if Country.objects.filter(url=slug).exists():
|
|
return HttpResponsePermanentRedirect(reverse(types.get(event_type) + '_country', kwargs={'slug': slug}))
|
|
return None
|
|
|
|
|
|
def check_places(slug):
|
|
"""
|
|
Правила для старых ошибок (ticket 1498: Внепланово со скайпа - редиректы 301)
|
|
https://expomap.ru/places/russia/ -> https://expomap.ru/places/country/russia/
|
|
https://expomap.ru/places/moscow/ -> https://expomap.ru/places/city/moscow/
|
|
- где пропущен параметр city и country - тоже автоматической склейки по 301
|
|
"""
|
|
if City.objects.filter(url=slug).exists():
|
|
return HttpResponsePermanentRedirect(reverse('place_city', kwargs={'slug': slug}))
|
|
if Country.objects.filter(url=slug).exists():
|
|
return HttpResponsePermanentRedirect(reverse('place_country', kwargs={'slug': slug}))
|
|
return None
|
|
|