network_inventory/src/licenses/views.py

82 lines
2.6 KiB
Python
Raw Normal View History

2020-01-11 18:23:15 +01:00
from django.contrib.auth.decorators import login_required
2020-06-28 23:31:35 +02:00
from django.contrib.auth.mixins import LoginRequiredMixin
from django.shortcuts import get_object_or_404
2020-01-11 17:54:31 +01:00
from django.shortcuts import render
2020-06-28 23:31:35 +02:00
from django.urls import reverse
from django.views.generic import CreateView
2020-06-28 23:59:50 +02:00
from django.views.generic import DeleteView
2020-01-11 17:54:31 +01:00
2020-01-11 18:23:15 +01:00
from django_tables2 import RequestConfig
from customers.decorators import customer_view_permission
2020-06-28 23:31:35 +02:00
from computers.models import Computer
2020-01-11 18:23:15 +01:00
from .models import ComputerLicense
from .models import UserLicense
2020-06-28 23:31:35 +02:00
from .models import LicenseWithComputer
2020-01-11 18:23:15 +01:00
from .tables import ComputerLicensesTable
from .tables import UserLicensesTable
@login_required
@customer_view_permission
def licenses_table_view(request, pk):
user_licenses = UserLicensesTable(UserLicense.objects.filter(customer=pk))
computer_licenses = ComputerLicensesTable(
2022-03-27 14:50:44 +02:00
ComputerLicense.objects.filter(customer=pk)
)
RequestConfig(request).configure(user_licenses)
RequestConfig(request).configure(computer_licenses)
2022-03-27 14:50:44 +02:00
return render(
request,
"licenses/license_list.html",
{
"user_licenses": user_licenses,
"computer_licenses": computer_licenses,
},
)
2020-06-28 23:31:35 +02:00
class LicenseWithComputerCreateView(LoginRequiredMixin, CreateView):
model = LicenseWithComputer
2022-03-27 14:50:44 +02:00
template_name = "licenses/license_with_computer_create.html"
2023-07-10 16:47:01 +02:00
fields = "__all__" # type: ignore
2020-06-28 23:31:35 +02:00
def get_success_url(self):
2022-03-27 14:50:44 +02:00
return reverse("computer", args=(self.computer.pk,))
2020-06-28 23:31:35 +02:00
def get_initial(self):
"""
Set the device and customer dropdown to the device from the previous
view and the customer related to the device.
"""
2022-03-27 14:50:44 +02:00
self.computer = get_object_or_404(Computer, id=self.kwargs.get("pk"))
2020-06-28 23:31:35 +02:00
return {
2022-03-27 14:50:44 +02:00
"computer": self.computer,
2020-06-28 23:31:35 +02:00
}
2020-06-28 23:59:50 +02:00
class LicenseWithComputerDeleteView(LoginRequiredMixin, DeleteView): # type: ignore
2020-06-28 23:59:50 +02:00
model = LicenseWithComputer
2022-03-27 14:50:44 +02:00
template_name = "licenses/license_with_computer_confirm_delete.html"
2020-06-28 23:59:50 +02:00
def get_success_url(self):
2022-03-27 14:50:44 +02:00
return reverse("computer", args=(self.object.computer.pk,))
class UserLicenseDeleteView(LoginRequiredMixin, DeleteView): # type: ignore
model = UserLicense
2022-03-27 14:50:44 +02:00
template_name = "licenses/license_confirm_delete.html"
def get_success_url(self):
2022-03-27 14:50:44 +02:00
return reverse("licenses", args=(self.object.customer.pk,))
class ComputerLicenseDeleteView(LoginRequiredMixin, DeleteView): # type: ignore
model = ComputerLicense
2022-03-27 14:50:44 +02:00
template_name = "licenses/license_confirm_delete.html"
def get_success_url(self):
2022-03-27 14:50:44 +02:00
return reverse("licenses", args=(self.object.customer.pk,))