<commit_before># -*- coding: utf-8 -*-
from datetime import datetime

from django.core.urlresolvers import reverse_lazy

from clock.shifts.models import Shift


def get_return_url(request, default_success):
    """
    Checks whether the user should be returned to the default_success view or to a special one. Is mostly used for the
    shift list views, as they can get filtered by month/year and contract ID. After updating/adding one, the user should
    be redirected to either:
        1) The standard shift list view (if no filters were specified)
        2) The previous visited view (if he updated / created a shift in the same month / contract)
        3) A filtered view which corresponds to the date/contract of the updated/added shift
    """
    if "shift" in request.session['last_visited'] and request.session['last_kwargs']:
        last_view = request.session['current_view_name']
        try:
            last_view = request.session['last_view_name']
        except KeyError:
            pass

        last_view = "shift:archive_month_contract_numeric"

        try:
            return_kwargs = {
                'year': request.session['last_kwargs']['year'],
                'month': request.session['last_kwargs']['month'],
            }
        except KeyError:
            return_kwargs = {
                'year': datetime.now().strftime("%Y"),
                'month': datetime.now().strftime("%m")
            }

        try:
            return_kwargs['contract'] = request.session['last_kwargs'][
                'contract']
        except KeyError:
            return_kwargs['contract'] = '00'

        return reverse_lazy(last_view, kwargs=return_kwargs)
    return reverse_lazy(default_success)

<commit_msg>Fix rare bug by using the `try` and `except` statement<commit_after># -*- coding: utf-8 -*-
from datetime import datetime

from django.core.urlresolvers import reverse_lazy

from clock.shifts.models import Shift


def get_return_url(request, default_success):
    """
    Checks whether the user should be returned to the default_success view or to a special one. Is mostly used for the
    shift list views, as they can get filtered by month/year and contract ID. After updating/adding one, the user should
    be redirected to either:
        1) The standard shift list view (if no filters were specified)
        2) The previous visited view (if he updated / created a shift in the same month / contract)
        3) A filtered view which corresponds to the date/contract of the updated/added shift
    """
    try:
        last_visited = "shift" in request.session['last_visited']
    except KeyError:
        last_visited = False

    if last_visited and request.session['last_kwargs']:
        last_view = request.session['current_view_name']
        try:
            last_view = request.session['last_view_name']
        except KeyError:
            pass

        last_view = "shift:archive_month_contract_numeric"

        try:
            return_kwargs = {
                'year': request.session['last_kwargs']['year'],
                'month': request.session['last_kwargs']['month'],
            }
        except KeyError:
            return_kwargs = {
                'year': datetime.now().strftime("%Y"),
                'month': datetime.now().strftime("%m")
            }

        try:
            return_kwargs['contract'] = request.session['last_kwargs'][
                'contract']
        except KeyError:
            return_kwargs['contract'] = '00'

        return reverse_lazy(last_view, kwargs=return_kwargs)
    return reverse_lazy(default_success)


{'fraction_non_alphanumeric': 0.09239280774550485, 'fraction_numerical': 0.0033195020746887966, 'mean_word_length': 2.476923076923077, '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': '5792050', 'n_tokens_mistral': 958, 'n_tokens_neox': 933, 'n_words': 304}