Skip to content

Commit

Permalink
[#2540] Remove dead middleware code
Browse files Browse the repository at this point in the history
  • Loading branch information
punchagan committed Feb 23, 2017
1 parent 90385b5 commit 1e62cff
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 91 deletions.
92 changes: 3 additions & 89 deletions akvo/rsr/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@
"""

import logging
import json

from django.conf import settings
from django.core.exceptions import DisallowedHost
from django.db.models import Q
from django.core.urlresolvers import (get_resolver, LocaleRegexURLResolver)
from django.shortcuts import redirect

from akvo.rsr.context_processors import extra_context
from akvo.rsr.models import PartnerSite
from django.utils import translation
from django.http import HttpResponseRedirect, HttpResponse


def _is_rsr_host(hostname):
Expand Down Expand Up @@ -52,64 +50,9 @@ def _build_api_link(request, resource, object_id):
)


class DefaultLanguageMiddleware(object):

"""A non working (BROKEN) default language middleware.
A try in supporting default languages, but since this will redirect all url_patterns and
not only i18n ones it's broken.
"""

def __init__(self):
"""."""
self._is_language_prefix_patterns_used = False
for url_pattern in get_resolver(None).url_patterns:
if isinstance(url_pattern, LocaleRegexURLResolver):
self._is_language_prefix_patterns_used = True
break

def is_language_prefix_patterns_used(self):
"""."""
return self._is_language_prefix_patterns_used

def is_i18n_path(self, path):
"""."""
from akvo.urls import localised_patterns
print "=> {}".format(localised_patterns)
from django.core.urlresolvers import resolve, Resolver404
try:
resolve(path, localised_patterns)
return True
except Resolver404:
return False
except TypeError:
return False
return False

def process_request(self, request):
"""Redirect to selected language."""
if not request.rsr_page:
return None

if self.is_i18n_path(request.path):
print "{} was i18n path".format(request.path)
else:
print "{} was not 18n path".format(request.path)

language_from_path = translation.get_language_from_path(request.path_info)
if not language_from_path:
if request.rsr_page.default_language:
lang = request.rsr_page.default_language
return HttpResponseRedirect('/{}{}'.format(lang, request.path))
return HttpResponseRedirect('/en{}'.format(request.path))
return None


class HostDispatchMiddleware(object):
"""RSR page dispatch middleware."""

"""RSR page dispath middleware."""

# def process_request(self, request, cname_domain=False, rsr_page=None):
def process_request(self, request):
"""Route on request."""
request.rsr_page = None
Expand Down Expand Up @@ -191,32 +134,3 @@ def process_response(request, response):
if depth > '1':
return redirect(_build_api_link(request, 'project_extra_deep', object_id))
return response


class NonHtmlDebugToolbarMiddleware(object):
"""
The Django Debug Toolbar usually only works for views that return HTML.
This middleware wraps any non-HTML response in HTML if the request
has a 'debug' query parameter (e.g. http://localhost/foo?debug)
Special handling for json (pretty printing) and
binary data (only show data length)
"""

@staticmethod
def process_response(request, response):
if request.GET.get('debug') == '':
if response['Content-Type'] == 'application/octet-stream':
new_content = '<html><body>Binary Data, ' \
'Length: {}</body></html>'.format(len(response.content))
response = HttpResponse(new_content)
elif response['Content-Type'] != 'text/html':
content = response.content
try:
json_ = json.loads(content)
content = json.dumps(json_, sort_keys=True, indent=2)
except ValueError:
pass
response = HttpResponse('<html><body><pre>{}'
'</pre></body></html>'.format(content))

return response
2 changes: 0 additions & 2 deletions akvo/settings/10-base.conf
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ AUTH_USER_MODEL = 'rsr.User'

MIDDLEWARE_CLASSES = (
'akvo.rsr.middleware.HostDispatchMiddleware',
# 'akvo.rsr.middleware.DefaultLanguageMiddleware',
# 'akvo.rsr.middleware_old.PagesLocaleMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
Expand Down

0 comments on commit 1e62cff

Please sign in to comment.