datatracker/ietf/middleware.py
Henrik Levkowetz aa5e61d958 Updated all urlpatterns to use ietf.utils.urls.url() instead of django's,
in order to autogenerate dotted path url pattern names.  Updated a number
of url reverses to use dotted path, and removed explicit url pattern names
as needed.

Changed some imports to prevent import of ietf.urls before django
initialization was complete.


Changed 3 cases of form classes being curried to functions; django 1.10
didn't accept that.

Started converting old-style middleware classes to new-style middleware
functions (incomplete).

Tweaked a nomcom decorator to preserve function names and attributes, like
a good decorator should.

Replaced the removed django templatetag 'removetags' with our own version
which uses bleach, and does sanitizing in addition to removing explicitly
mentionied html tags.

Rewrote the filename argument handling in a management command which had
broken with the upgrade.
 - Legacy-Id: 12818
2017-02-11 14:43:01 +00:00

60 lines
2.2 KiB
Python

# Copyright The IETF Trust 2007, All Rights Reserved
from django.db import connection
from django.shortcuts import render
from django.http import HttpResponsePermanentRedirect
from ietf.utils.log import log
from ietf.utils.mail import log_smtp_exception
import re
import smtplib
import unicodedata
# old-style middleware
class SQLLogMiddleware(object):
def process_response(self, request, response):
for q in connection.queries:
if re.match('(update|insert)', q['sql'], re.IGNORECASE):
log(q['sql'])
return response
# new-style middleware
def sql_log_middleware_factory(get_response):
def sql_log_middleware(request):
response = get_response(request)
for q in connection.queries:
if re.match('(update|insert)', q['sql'], re.IGNORECASE):
log(q['sql'])
return response
class SMTPExceptionMiddleware(object):
def process_exception(self, request, exception):
if isinstance(exception, smtplib.SMTPException):
(extype, value, tb) = log_smtp_exception(exception)
return render(request, 'email_failed.html',
{'exception': extype, 'args': value, 'traceback': "".join(tb)} )
return None
def smtp_exception_middleware_factory(get_response):
def smtp_exception_middleware(request):
response = get_response(request)
return response
class RedirectTrailingPeriod(object):
def process_response(self, request, response):
if response.status_code == 404 and request.path.endswith("."):
return HttpResponsePermanentRedirect(request.path.rstrip("."))
return response
class UnicodeNfkcNormalization(object):
def process_request(self, request):
"""Do Unicode NFKC normalization to turn ligatures into individual characters.
This was prompted by somebody actually requesting an url for /wg/ipfix/charter
where the 'fi' was composed of an \ufb01 ligature...
There are probably other elements of a request which may need this normalization
too, but let's put that in as it comes up, rather than guess ahead.
"""
request.META["PATH_INFO"] = unicodedata.normalize('NFKC', request.META["PATH_INFO"])
request.path_info = unicodedata.normalize('NFKC', request.path_info)
return None