<commit_before>from django.contrib import messages
from django.http import HttpResponseRedirect
from django.views.generic import ListView, View, UpdateView
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext_lazy as _, ugettext
from django.shortcuts import get_object_or_404
from django.contrib.contenttypes.models import ContentType
from django.shortcuts import render_to_response
from django.template import RequestContext
from reversion.models import Version, Revision
import reversion
from django.db.models import get_model

from Lagerregal.utils import PaginationMixin
from devices.models import Device, Room, Manufacturer
from devicetypes.models import Type, TypeAttributeValue


class Globalhistory(PaginationMixin, ListView):
    queryset = Revision.objects.select_related("user").prefetch_related("version_set", "version_set__content_type"
        ).filter().order_by("-date_created")
    context_object_name = "revision_list"
    template_name = 'history/globalhistory.html'

    def get_context_data(self, **kwargs):
        context = super(Globalhistory, self).get_context_data(**kwargs)
        context["breadcrumbs"] = [(reverse("globalhistory"), _("Global edit history"))]
        if context["is_paginated"] and context["page_obj"].number > 1:
            context["breadcrumbs"].append(["", context["page_obj"].number])
        return context

excluded_fields = ["currentlending", "created_at", "archived", "trashed", "inventoried", "bookmarks", "trashed",
                   "last_seen", "creator"]

def cleanup_fielddict(version):
    del version.field_dict["id"]
    if version.field_dict.get("devicetype") != None:
        try:
            version.field_dict["devicetype"] = Type.objects.get(
                pk=version.field_dict["devicetype"])
        except Type.DoesNotExist:
            version.field_dict["devicetype"] = "[deleted]"
    if version.field_dict.get("manufacturer") != None:
        try:
            version.field_dict["manufacturer"] = Manufacturer.objects.get(
                pk=version.field_dict["manufacturer"])
        except Type.DoesNotExist:
            version.field_dict["manufacturer"] = "[deleted]"
    if version.field_dict.get("room") != None:
        try:
            version.field_dict["room"] = Room.objects.get(
                pk=version.field_dict["room"])
        except Type.DoesNotExist:
            version.field_dict["room"] = "[deleted]"

    if version.field_dict.get("device") != None:
        try:
            version.field_dict["device"] = Device.objects.get(
                pk=version.field_dict["device"])
        except Type.DoesNotExist:
            version.field_dict["device"] = "[deleted]"
<commit_msg>Don't crash when historied device is deleted<commit_after>from django.contrib import messages
from django.http import HttpResponseRedirect
from django.views.generic import ListView, View, UpdateView
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext_lazy as _, ugettext
from django.shortcuts import get_object_or_404
from django.contrib.contenttypes.models import ContentType
from django.shortcuts import render_to_response
from django.template import RequestContext
from reversion.models import Version, Revision
import reversion
from django.db.models import get_model

from Lagerregal.utils import PaginationMixin
from devices.models import Device, Room, Manufacturer
from devicetypes.models import Type, TypeAttributeValue


class Globalhistory(PaginationMixin, ListView):
    queryset = Revision.objects.select_related("user").prefetch_related("version_set", "version_set__content_type"
        ).filter().order_by("-date_created")
    context_object_name = "revision_list"
    template_name = 'history/globalhistory.html'

    def get_context_data(self, **kwargs):
        context = super(Globalhistory, self).get_context_data(**kwargs)
        context["breadcrumbs"] = [(reverse("globalhistory"), _("Global edit history"))]
        if context["is_paginated"] and context["page_obj"].number > 1:
            context["breadcrumbs"].append(["", context["page_obj"].number])
        return context

excluded_fields = ["currentlending", "created_at", "archived", "trashed", "inventoried", "bookmarks", "trashed",
                   "last_seen", "creator"]

def cleanup_fielddict(version):
    del version.field_dict["id"]
    if version.field_dict.get("devicetype") != None:
        try:
            version.field_dict["devicetype"] = Type.objects.get(
                pk=version.field_dict["devicetype"])
        except Type.DoesNotExist:
            version.field_dict["devicetype"] = "[deleted]"
    if version.field_dict.get("manufacturer") != None:
        try:
            version.field_dict["manufacturer"] = Manufacturer.objects.get(
                pk=version.field_dict["manufacturer"])
        except Manufacturer.DoesNotExist:
            version.field_dict["manufacturer"] = "[deleted]"
    if version.field_dict.get("room") != None:
        try:
            version.field_dict["room"] = Room.objects.get(
                pk=version.field_dict["room"])
        except Room.DoesNotExist:
            version.field_dict["room"] = "[deleted]"

    if version.field_dict.get("device") != None:
        try:
            version.field_dict["device"] = Device.objects.get(
                pk=version.field_dict["device"])
        except Device.DoesNotExist:
            version.field_dict["device"] = "[deleted]"

{'fraction_non_alphanumeric': 0.11553203139258988, 'fraction_numerical': 0.0014601204599379448, 'mean_word_length': 3.71195184866724, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '7082368', 'n_tokens_mistral': 1571, 'n_tokens_neox': 1521, 'n_words': 316}