<commit_before>from __future__ import unicode_literals

import base32_crockford
import logging

from django.db.models import F
from django.http import Http404
from django.http import HttpResponsePermanentRedirect
from django.shortcuts import get_object_or_404
from django.utils.timezone import now

from .models import ShortURL
from .models import VanityURL
from .utils import add_query_params


logger = logging.getLogger(__name__)


def redirect(request, key):
    """
    Given the short URL key, update the statistics and redirect the
    user to the destination URL, including available Google Analytics
    parameters.
    """
    try:
        alias = VanityURL.objects.select_related().get(alias=key.upper())
        key_id = alias.redirect.id
    except VanityURL.DoesNotExist:
        try:
            key_id = base32_crockford.decode(key)
        except ValueError as e:
            logger.warning("Error decoding redirect '%s': %s" % (key, e))
            raise Http404

    redirect = get_object_or_404(ShortURL, pk=key_id)
    ShortURL.objects.filter(pk=key_id).update(hits=F('hits') + 1,
                                              last_used=now())

    # Inject Google campaign parameters
    utm_params = {'utm_source': redirect.key,
                  'utm_campaign': redirect.campaign,
                  'utm_content': redirect.content,
                  'utm_medium': redirect.medium}
    url = add_query_params(redirect.long_url, utm_params)

    return HttpResponsePermanentRedirect(url)
<commit_msg>Simplify invalid decode warning text<commit_after>from __future__ import unicode_literals

import base32_crockford
import logging

from django.db.models import F
from django.http import Http404
from django.http import HttpResponsePermanentRedirect
from django.shortcuts import get_object_or_404
from django.utils.timezone import now

from .models import ShortURL
from .models import VanityURL
from .utils import add_query_params


logger = logging.getLogger(__name__)


def redirect(request, key):
    """
    Given the short URL key, update the statistics and redirect the
    user to the destination URL, including available Google Analytics
    parameters.
    """
    try:
        alias = VanityURL.objects.select_related().get(alias=key.upper())
        key_id = alias.redirect.id
    except VanityURL.DoesNotExist:
        try:
            key_id = base32_crockford.decode(key)
        except ValueError as e:
            logger.warning("Error decoding redirect: %s" % e)
            raise Http404

    redirect = get_object_or_404(ShortURL, pk=key_id)
    ShortURL.objects.filter(pk=key_id).update(hits=F('hits') + 1,
                                              last_used=now())

    # Inject Google campaign parameters
    utm_params = {'utm_source': redirect.key,
                  'utm_campaign': redirect.campaign,
                  'utm_content': redirect.content,
                  'utm_medium': redirect.medium}
    url = add_query_params(redirect.long_url, utm_params)

    return HttpResponsePermanentRedirect(url)

{'fraction_non_alphanumeric': 0.07725040916530278, 'fraction_numerical': 0.011129296235679214, 'mean_word_length': 2.9636835278858626, '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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2921352', 'n_tokens_mistral': 892, 'n_tokens_neox': 837, 'n_words': 228}