<commit_before>import csv
import datetime
import time

from django.conf import settings
from django.contrib import messages
from django.contrib.auth.models import Group
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ImproperlyConfigured
from django.core.exceptions import SuspiciousOperation
from django.db import models
from django.db.models import Q
from django.db.transaction import atomic
from django.http import Http404
from django.http import HttpResponse
from django.http import HttpResponseBadRequest
from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404
from django.shortcuts import render
from django.urls import reverse
from django.urls import reverse_lazy
from django.utils import timezone
from django.utils.timezone import utc
from django.utils.translation import ugettext_lazy as _
from django.views.generic import CreateView
from django.views.generic import DeleteView
from django.views.generic import DetailView
from django.views.generic import FormView
from django.views.generic import ListView
from django.views.generic import UpdateView
from django.views.generic import View
from django.views.generic.detail import BaseDetailView
from django.views.generic.detail import SingleObjectMixin
from django.views.generic.detail import SingleObjectTemplateResponseMixin

from reversion import revisions as reversion
from reversion.models import Version

from devices.forms import VIEWSORTING
from devices.forms import VIEWSORTING_DEVICES
from devices.forms import DeviceForm
from devices.forms import DeviceGroupFilterForm
from devices.forms import DeviceMailForm
from devices.forms import DeviceStorageForm
from devices.forms import DeviceViewForm
from devices.forms import FilterForm
from devices.forms import IpAddressForm
from devices.forms import IpAddressPurposeForm
from devices.forms import LendForm
from devices.forms import ReturnForm
from devices.forms import ViewForm
from devices.models import Bookmark
from devices.models import Building
from devices.models import Device
from devices.models import Lending
from devices.models import Manufacturer
from devices.models import Note
from devices.models import Picture
from devices.models import Room
from devices.models import Template
from devicetags.forms import DeviceTagForm
from devicetags.models import Devicetag
from devicetypes.models import TypeAttribute
from devicetypes.models import TypeAttributeValue
from Lagerregal.utils import PaginationMixin
from mail.models import MailHistory
from mail.models import MailTemplate
from network.models import IpAddress
from users.mixins import PermissionRequiredMixin
from users.models import Lageruser


def get_devices(user, category, department, sorting):
    if category == "all":
        devices = Device.objects.all()
    elif category == "available":
        devices = Device.active().filter(currentlending=None)
    elif category == "lent":
        lendings = Lending.objects.filter(returndate=None)
        devices = Device.objects.filter(lending__in=lendings)
    elif category == "archived":
        devices = Device.objects.exclude(archived=None)
    elif category == "trashed":
        devices = Device.objects.exclude(trashed=None)
    elif category == "overdue":
        lendings = Lending.objects.filter(returndate=None, duedate__lt=datetime.date.today())
        devices = Device.objects.filter(lending__in=lendings)
    elif category == "returnsoon":
        soon = datetime.date.today() + datetime.timedelta(days=10)
        lendings = Lending.objects.filter(returndate=None, duedate__lte=soon,
                                          duedate__gt=datetime.date.today())
        devices = Device.objects.filter(lending__in=lendings)
    elif category == "temporary":
        devices = Device.active().filter(templending=True)
    elif category == "bookmark" and user.is_authenticated:
        devices = user.bookmarks.all()
    elif category == "active":
        devices = Device.active()
    else:
        raise ValueError

    if department == "all":
        pass
    elif department == "my":
        devices = devices.filter(department__in=user.departments.all())
    elif department.isdigit():
        devices = devices.filter(department_id=int(department))
    else:
        raise ValueError

    if sorting not in [s[0] for s in VIEWSORTING_DEVICES]:
        raise ValueError

    if hasattr(user, 'departments'):
        devices = devices.exclude(~Q(department__in=user.departments.all()), is_private=True)
    else:
        devices = devices.exclude(is_private=True)

    return devices.order_by(sorting)


class DeviceList(PermissionRequiredMixin, PaginationMixin, ListView):
    context_object_name = 'device_list'
    template_name = 'devices/device_list.html'
    viewfilter = None
    viewsorting = None
    permission_required = 'devices.view_device'

    def post(self, request):
        '''post-requesting the detail-view of device by id'''
        if 'deviceid' in request.POST:
            return HttpResponseRedirect(reverse('device-detail', kwargs={'pk': request.POST['deviceid']}))
        else:
            return HttpResponseRedirect(reverse('device-list'))

    def get_queryset(self):
        '''method for query results and display it depending on existing filters (viewfilter, department)'''
        self.viewfilter = self.request.GET.get("category", "active")

        if hasattr(self.request.user, 'departments') and self.request.user.departments.count() > 0:
            self.departmentfilter = self.request.GET.get("department", "my")
        else:
            self.departmentfilter = self.request.GET.get("department", "all")

        self.viewsorting = self.request.GET.get("sorting", "name")

        return get_devices(self.request.user, self.viewfilter, self.departmentfilter, self.viewsorting)

    def get_context_data(self, **kwargs):
        '''method for getting context data (filter, time, templates, breadcrumbs)'''
        context = super().get_context_data(**kwargs)

        # getting filters
        context["viewform"] = DeviceViewForm(initial={
            'category': self.viewfilter,
            "sorting": self.viewsorting,
            "department": self.departmentfilter
        })

        context["today"] = datetime.datetime.today()
        context["template_list"] = Template.objects.all()
        context["viewfilter"] = self.viewfilter
        context["breadcrumbs"] = [[reverse("device-list"), _("Devices")]]

        # add page number to breadcrumbs
        if context["is_paginated"] and context["page_obj"].number > 1:
            context["breadcrumbs"].append(["", context["page_obj"].number])
        return context


class ExportCsv(PermissionRequiredMixin, View):<commit_msg>optimize database access<commit_after>import csv
import datetime
import time

from django.conf import settings
from django.contrib import messages
from django.contrib.auth.models import Group
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ImproperlyConfigured
from django.core.exceptions import SuspiciousOperation
from django.db import models
from django.db.models import Q
from django.db.transaction import atomic
from django.http import Http404
from django.http import HttpResponse
from django.http import HttpResponseBadRequest
from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404
from django.shortcuts import render
from django.urls import reverse
from django.urls import reverse_lazy
from django.utils import timezone
from django.utils.timezone import utc
from django.utils.translation import ugettext_lazy as _
from django.views.generic import CreateView
from django.views.generic import DeleteView
from django.views.generic import DetailView
from django.views.generic import FormView
from django.views.generic import ListView
from django.views.generic import UpdateView
from django.views.generic import View
from django.views.generic.detail import BaseDetailView
from django.views.generic.detail import SingleObjectMixin
from django.views.generic.detail import SingleObjectTemplateResponseMixin

from reversion import revisions as reversion
from reversion.models import Version

from devices.forms import VIEWSORTING
from devices.forms import VIEWSORTING_DEVICES
from devices.forms import DeviceForm
from devices.forms import DeviceGroupFilterForm
from devices.forms import DeviceMailForm
from devices.forms import DeviceStorageForm
from devices.forms import DeviceViewForm
from devices.forms import FilterForm
from devices.forms import IpAddressForm
from devices.forms import IpAddressPurposeForm
from devices.forms import LendForm
from devices.forms import ReturnForm
from devices.forms import ViewForm
from devices.models import Bookmark
from devices.models import Building
from devices.models import Device
from devices.models import Lending
from devices.models import Manufacturer
from devices.models import Note
from devices.models import Picture
from devices.models import Room
from devices.models import Template
from devicetags.forms import DeviceTagForm
from devicetags.models import Devicetag
from devicetypes.models import TypeAttribute
from devicetypes.models import TypeAttributeValue
from Lagerregal.utils import PaginationMixin
from mail.models import MailHistory
from mail.models import MailTemplate
from network.models import IpAddress
from users.mixins import PermissionRequiredMixin
from users.models import Lageruser


def get_devices(user, category, department, sorting):
    if category == "all":
        devices = Device.objects.all()
    elif category == "available":
        devices = Device.active().filter(currentlending=None)
    elif category == "lent":
        lendings = Lending.objects.filter(returndate=None)
        devices = Device.objects.filter(lending__in=lendings)
    elif category == "archived":
        devices = Device.objects.exclude(archived=None)
    elif category == "trashed":
        devices = Device.objects.exclude(trashed=None)
    elif category == "overdue":
        lendings = Lending.objects.filter(returndate=None, duedate__lt=datetime.date.today())
        devices = Device.objects.filter(lending__in=lendings)
    elif category == "returnsoon":
        soon = datetime.date.today() + datetime.timedelta(days=10)
        lendings = Lending.objects.filter(returndate=None, duedate__lte=soon,
                                          duedate__gt=datetime.date.today())
        devices = Device.objects.filter(lending__in=lendings)
    elif category == "temporary":
        devices = Device.active().filter(templending=True)
    elif category == "bookmark" and user.is_authenticated:
        devices = user.bookmarks.all()
    elif category == "active":
        devices = Device.active()
    else:
        raise ValueError

    if department == "all":
        pass
    elif department == "my":
        devices = devices.filter(department__in=user.departments.all())
    elif department.isdigit():
        devices = devices.filter(department_id=int(department))
    else:
        raise ValueError

    if sorting not in [s[0] for s in VIEWSORTING_DEVICES]:
        raise ValueError

    if hasattr(user, 'departments'):
        devices = devices.exclude(~Q(department__in=user.departments.all()), is_private=True)
    else:
        devices = devices.exclude(is_private=True)

    return devices.order_by(sorting)


class DeviceList(PermissionRequiredMixin, PaginationMixin, ListView):
    context_object_name = 'device_list'
    template_name = 'devices/device_list.html'
    viewfilter = None
    viewsorting = None
    permission_required = 'devices.view_device'

    def post(self, request):
        '''post-requesting the detail-view of device by id'''
        if 'deviceid' in request.POST:
            return HttpResponseRedirect(reverse('device-detail', kwargs={'pk': request.POST['deviceid']}))
        else:
            return HttpResponseRedirect(reverse('device-list'))

    def get_queryset(self):
        '''method for query results and display it depending on existing filters (viewfilter, department)'''
        self.viewfilter = self.request.GET.get("category", "active")

        if hasattr(self.request.user, 'departments') and self.request.user.departments.count() > 0:
            self.departmentfilter = self.request.GET.get("department", "my")
        else:
            self.departmentfilter = self.request.GET.get("department", "all")

        self.viewsorting = self.request.GET.get("sorting", "name")

        devices = get_devices(self.request.user, self.viewfilter, self.departmentfilter, self.viewsorting)
        return devices.select_related('devicetype', 'room__building', 'group', 'currentlending__owner')

    def get_context_data(self, **kwargs):
        '''method for getting context data (filter, time, templates, breadcrumbs)'''
        context = super().get_context_data(**kwargs)

        # getting filters
        context["viewform"] = DeviceViewForm(initial={
            'category': self.viewfilter,
            "sorting": self.viewsorting,
            "department": self.departmentfilter
        })

        context["today"] = datetime.datetime.today()
        context["template_list"] = Template.objects.all()
        context["viewfilter"] = self.viewfilter
        context["breadcrumbs"] = [[reverse("device-list"), _("Devices")]]

        # add page number to breadcrumbs
        if context["is_paginated"] and context["page_obj"].number > 1:
            context["breadcrumbs"].append(["", context["page_obj"].number])
        return context


class ExportCsv(PermissionRequiredMixin, View):
{'fraction_non_alphanumeric': 0.08722718617255017, 'fraction_numerical': 0.0016112494507104144, 'mean_word_length': 4.061156412157153, 'pattern_counts': {'":': 26, '<': 3, '<?xml version=': 0, '>': 7, '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': '6838987', 'n_tokens_mistral': 3692, 'n_tokens_neox': 3566, 'n_words': 1004}