<commit_before>from django.http import HttpResponseRedirect, Http404
from django.conf import settings
from django.core.urlresolvers import resolve, Resolver404
from exceptions import IndexError
from models import URL
import operator, re

class AliasFallbackMiddleware(object):
    def process_response(self, request, response):
        if response.status_code != 404:
            return response

        try:
            if hasattr(settings, 'ALIASES_MAP_ARGS') and settings.ALIASES_MAP_ARGS is False:
                alias = URL.objects.get(location=request.path_info)
            else:
                # I'm not a fan of raw SQL queries, but this should work in any
                # SQL server (I think). If you know how to do this in django's
                # ORM, let me know. If this doesn't work and/or you don't need
                # this functionality, you can always set
                # settings.ALIASES_MAP_ARGS to False and avoid this.
                #
                # This will not work in SQLite.

                alias = URL.objects.raw('SELECT * FROM aliases_url WHERE location LIKE "%s%%" ORDER BY LENGTH(location) DESC LIMIT 1', [request.path_info])

                try:
                    alias = alias[0]
                except IndexError:
                    alias = None

            if alias is not None:<commit_msg>So, that wasn't supposed to be descending after all.<commit_after>from django.http import HttpResponseRedirect, Http404
from django.conf import settings
from django.core.urlresolvers import resolve, Resolver404
from exceptions import IndexError
from models import URL
import operator, re

class AliasFallbackMiddleware(object):
    def process_response(self, request, response):
        if response.status_code != 404:
            return response

        try:
            if hasattr(settings, 'ALIASES_MAP_ARGS') and settings.ALIASES_MAP_ARGS is False:
                alias = URL.objects.get(location=request.path_info)
            else:
                # I'm not a fan of raw SQL queries, but this should work in any
                # SQL server (I think). If you know how to do this in django's
                # ORM, let me know. If this doesn't work and/or you don't need
                # this functionality, you can always set
                # settings.ALIASES_MAP_ARGS to False and avoid this.
                #
                # This will not work in SQLite.

                alias = URL.objects.raw('SELECT * FROM aliases_url WHERE location LIKE "%s%%" ORDER BY LENGTH(location) LIMIT 1', [request.path_info])

                try:
                    alias = alias[0]
                except IndexError:
                    alias = None

            if alias is not None:
{'fraction_non_alphanumeric': 0.06405563689604685, 'fraction_numerical': 0.008052708638360176, 'mean_word_length': 2.0066006600660065, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '472516', 'n_tokens_mistral': 739, 'n_tokens_neox': 678, 'n_words': 268}