From a89358e3d642bd444e8ef83471bb2b3b8b9ae5b1 Mon Sep 17 00:00:00 2001 From: Alexander Burdeiny Date: Wed, 3 Aug 2016 11:34:19 +0300 Subject: [PATCH] =?UTF-8?q?1498:=20=D0=92=D0=BD=D0=B5=D0=BF=D0=BB=D0=B0?= =?UTF-8?q?=D0=BD=D0=BE=D0=B2=D0=BE=20=D1=81=D0=BE=20=D1=81=D0=BA=D0=B0?= =?UTF-8?q?=D0=B9=D0=BF=D0=B0=20-=20=D1=80=D0=B5=D0=B4=D0=B8=D1=80=D0=B5?= =?UTF-8?q?=D0=BA=D1=82=D1=8B=20301?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conference/urls.py | 4 +-- exposition/urls.py | 4 +-- place_exposition/urls.py | 4 +-- proj/middleware.py | 65 ++++++++++++++++++++++++++++++++++++++++ settings/old_urls.py | 1 + support/dev/settings.py | 1 + support/prod/settings.py | 2 +- 7 files changed, 74 insertions(+), 7 deletions(-) create mode 100644 proj/middleware.py diff --git a/conference/urls.py b/conference/urls.py index d71ab5c9..f7e4729e 100644 --- a/conference/urls.py +++ b/conference/urls.py @@ -35,7 +35,7 @@ urlpatterns = patterns('', url(r'^conference/country/(?P[^/]*)/page/(?P\d+)/$', ConferenceCountryCatalog.as_view(), {'meta_id':23}), url(r'^conference/country/(?P[^/]*)/(?P\d+)/(?P[^/]*)/$', ConferenceCountryCatalog.as_view(), {'meta_id':25}), url(r'^conference/country/(?P[^/]*)/(?P\d+)/$', ConferenceCountryCatalog.as_view(), {'meta_id':24}), - url(r'^conference/country/(?P[^/]*)/$', ConferenceCountryCatalog.as_view(), {'meta_id':23}), + url(r'^conference/country/(?P[^/]*)/$', ConferenceCountryCatalog.as_view(), {'meta_id':23}, name='conf_country'), # city catalog url(r'^conference/city/$', ConferenceByCity.as_view(), {'meta_id':52}), url(r'^conference/city/(?P[^/]*)/(?P\d+)/(?P[^/]*)/page/(?P\d+)/$', ConferenceCityCatalog.as_view(), {'meta_id':28}), @@ -43,7 +43,7 @@ urlpatterns = patterns('', url(r'^conference/city/(?P[^/]*)/page/(?P\d+)/$', ConferenceCityCatalog.as_view(), {'meta_id':26}), url(r'^conference/city/(?P[^/]*)/(?P\d+)/(?P[^/]*)/$', ConferenceCityCatalog.as_view(), {'meta_id':28}), url(r'^conference/city/(?P[^/]*)/(?P\d+)/$', ConferenceCityCatalog.as_view(), {'meta_id':27}), - url(r'^conference/city/(?P[^/]*)/$', ConferenceCityCatalog.as_view(), {'meta_id':26}), + url(r'^conference/city/(?P[^/]*)/$', ConferenceCityCatalog.as_view(), {'meta_id':26}, name='conf_city'), # theme catalog url(r'^conference/theme/$', ConferenceByTheme.as_view(), {'meta_id':50}), url(r'^conference/theme/(?P[^/]*)/country/(?P[^/]*)/(?P\d+)/(?P[^/]*)/page/(?P\d+)/$', ConferenceThemeCatalog.as_view()), diff --git a/exposition/urls.py b/exposition/urls.py index fdf29e00..f5df260e 100644 --- a/exposition/urls.py +++ b/exposition/urls.py @@ -37,7 +37,7 @@ urlpatterns = patterns('', url(r'^expo/country/(?P[^/]*)/page/(?P\d+)/$', ExpoCountryCatalog.as_view(), {'meta_id':5}), url(r'^expo/country/(?P[^/]*)/(?P\d+)/(?P[^/]*)/$', ExpoCountryCatalog.as_view(), {'meta_id':7}), url(r'^expo/country/(?P[^/]*)/(?P\d+)/$', ExpoCountryCatalog.as_view(), {'meta_id':6}), - url(r'^expo/country/(?P[^/]*)/$', ExpoCountryCatalog.as_view(), {'meta_id':5}), + url(r'^expo/country/(?P[^/]*)/$', ExpoCountryCatalog.as_view(), {'meta_id':5}, name='expo_country'), # city catalog url(r'^expo/city/$', ExpositionByCity.as_view(), {'meta_id':53}), url(r'^expo/city/(?P[^/]*)/(?P\d+)/(?P[^/]*)/page/(?P\d+)/$', ExpoCityCatalog.as_view(), {'meta_id':10}), @@ -45,7 +45,7 @@ urlpatterns = patterns('', url(r'^expo/city/(?P[^/]*)/page/(?P\d+)/$', ExpoCityCatalog.as_view(), {'meta_id':8}), url(r'^expo/city/(?P[^/]*)/(?P\d+)/(?P[^/]*)/$', ExpoCityCatalog.as_view(), {'meta_id':10}), url(r'^expo/city/(?P[^/]*)/(?P\d+)/$', ExpoCityCatalog.as_view(), {'meta_id':9}), - url(r'^expo/city/(?P[^/]*)/$', ExpoCityCatalog.as_view(), {'meta_id':8}), + url(r'^expo/city/(?P[^/]*)/$', ExpoCityCatalog.as_view(), {'meta_id':8}, name='expo_city'), # theme catalog url(r'^expo/theme/$', ExpositionByTheme.as_view(), {'meta_id':55}), url(r'^expo/theme/(?P[^/]*)/country/(?P[^/]*)/(?P\d+)/(?P[^/]*)/page/(?P\d+)/$', ExpoThemeCatalog.as_view(), {'meta_id':44}), diff --git a/place_exposition/urls.py b/place_exposition/urls.py index 908e4ee6..c9b64235 100644 --- a/place_exposition/urls.py +++ b/place_exposition/urls.py @@ -15,11 +15,11 @@ urlpatterns = patterns('', url(r'^country/$', PlaceByCountry.as_view(), {'meta_id': 49}), url(r'^country/(?P.*)/page/(?P\d+)/$', PlaceCountryCatalog.as_view(), {'meta_id': 49}), - url(r'^country/(?P.*)/$', PlaceCountryCatalog.as_view(), {'meta_id': 49}), + url(r'^country/(?P.*)/$', PlaceCountryCatalog.as_view(), {'meta_id': 49}, name='place_country'), url(r'^city/$', PlaceByCity.as_view(), {'meta_id': 48}), url(r'^city/(?P.*)/page/(?P\d+)/$', PlaceCityCatalog.as_view(), {'meta_id': 48}), - url(r'^city/(?P.*)/$', PlaceCityCatalog.as_view(), {'meta_id': 48}), + url(r'^city/(?P.*)/$', PlaceCityCatalog.as_view(), {'meta_id': 48}, name='place_city'), url(r'^(?P.*)/photo/page/(?P\d+)/$', PlacePhoto.as_view(), {'meta_id': 91}), url(r'^(?P.*)/photo/$', PlacePhoto.as_view(), {'meta_id': 91}), diff --git a/proj/middleware.py b/proj/middleware.py new file mode 100644 index 00000000..33ce635f --- /dev/null +++ b/proj/middleware.py @@ -0,0 +1,65 @@ +# -*- 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'^/(?Pexpo|conference)/(?P[^/]*)/$', check_events), + (r'^/places/(?P[^/]*)/$', 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 diff --git a/settings/old_urls.py b/settings/old_urls.py index f64a48d1..987a3e20 100644 --- a/settings/old_urls.py +++ b/settings/old_urls.py @@ -39,6 +39,7 @@ urlpatterns = patterns('', url(r'^newsp.php/news_id/(?P\d+)/(?P.*)$', old_redirect, {'redirect_url': '/news/{news_p}/'}), # users url(r'^users/(?P.*)$', old_redirect, {'redirect_url': '/{user}/'}), + url(r'^user/(?P.*)$', old_redirect, {'redirect_url': '/{user}/'}), url(r'^account_edit.php$', old_profile), url(r'^myexpo.php$', old_redirect, {'redirect_url': '/profile/calendar/'}), url(r'^newsletter2.php?email=(?P.*)$', old_redirect, {'redirect_url': '/profile/settings/'}), diff --git a/support/dev/settings.py b/support/dev/settings.py index def623f3..e6585d3b 100644 --- a/support/dev/settings.py +++ b/support/dev/settings.py @@ -153,6 +153,7 @@ MIDDLEWARE_CLASSES = ( # Uncomment the next line for simple clickjacking protection: 'django.middleware.clickjacking.XFrameOptionsMiddleware', 'django.contrib.redirects.middleware.RedirectFallbackMiddleware', + 'proj.middleware.RedirectFallbackMiddleware', ) diff --git a/support/prod/settings.py b/support/prod/settings.py index 679749e6..b3664880 100644 --- a/support/prod/settings.py +++ b/support/prod/settings.py @@ -153,7 +153,7 @@ MIDDLEWARE_CLASSES = ( # Uncomment the next line for simple clickjacking protection: 'django.middleware.clickjacking.XFrameOptionsMiddleware', 'django.contrib.redirects.middleware.RedirectFallbackMiddleware', - + 'proj.middleware.RedirectFallbackMiddleware', )