|
@@ -1,10 +1,15 @@
|
|
|
-from django.shortcuts import render
|
|
|
-from django.views.generic import ListView, CreateView, DetailView, UpdateView
|
|
|
+from django.shortcuts import render, get_object_or_404
|
|
|
+from django.views.generic import ListView, CreateView, DetailView, UpdateView, RedirectView
|
|
|
+from django.views.generic.detail import SingleObjectMixin
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
|
|
from django.contrib.auth.mixins import PermissionRequiredMixin
|
|
|
+from django.http import HttpResponseGone
|
|
|
+from django.core.exceptions import PermissionDenied
|
|
|
+from django.utils import timezone
|
|
|
|
|
|
-from .models import Service, ServiceType, IPResource
|
|
|
-from .forms import ServiceForm
|
|
|
+from djadhere.utils import get_active_filter
|
|
|
+from .models import Service, ServiceType, IPResource, ResourceAllocation
|
|
|
+from .forms import ServiceForm, ResourceAllocationForm
|
|
|
|
|
|
|
|
|
class ServiceMixin(PermissionRequiredMixin):
|
|
@@ -17,7 +22,6 @@ class ServiceList(ServiceMixin, ListView):
|
|
|
|
|
|
|
|
|
class ServiceCreate(ServiceMixin, CreateView):
|
|
|
- #fields = ('adhesion', 'service_type', 'label', 'notes', 'active',)
|
|
|
form_class = ServiceForm
|
|
|
|
|
|
|
|
@@ -34,6 +38,20 @@ class ServiceUpdate(ServiceMixin, UpdateView):
|
|
|
fields = ('label', 'notes', 'active',)
|
|
|
|
|
|
|
|
|
+class ServiceAllocate(PermissionRequiredMixin, CreateView):
|
|
|
+ model = ResourceAllocation
|
|
|
+ permission_required = 'services.change_resourceallocation'
|
|
|
+
|
|
|
+ def get_form(self):
|
|
|
+ service = get_object_or_404(Service, pk=self.kwargs['pk'])
|
|
|
+ kwargs = super().get_form_kwargs()
|
|
|
+ kwargs['initial'].update({'service': service.pk})
|
|
|
+ form = ResourceAllocationForm(**kwargs)
|
|
|
+ form.fields['service'].disabled = True
|
|
|
+ form.fields['resource'].queryset = IPResource.objects.exclude(get_active_filter('allocation'))
|
|
|
+ return form
|
|
|
+
|
|
|
+
|
|
|
class ServiceTypeMixin(PermissionRequiredMixin):
|
|
|
model = ServiceType
|
|
|
permission_required = 'services.change_servicetype'
|
|
@@ -66,3 +84,40 @@ class IPResourceList(IPResourceMixin, ListView):
|
|
|
|
|
|
class IPResourceDetail(IPResourceMixin, DetailView):
|
|
|
pass
|
|
|
+
|
|
|
+
|
|
|
+class IPResourceAllocate(PermissionRequiredMixin, CreateView):
|
|
|
+ model = ResourceAllocation
|
|
|
+ permission_required = 'services.change_resourceallocation'
|
|
|
+
|
|
|
+ def get(self, request, *args, **kwargs):
|
|
|
+ resource = get_object_or_404(IPResource, pk=self.kwargs['pk'])
|
|
|
+ if resource.in_use:
|
|
|
+ return HttpResponseGone("Cette IP est déjà alloué !")
|
|
|
+ return super().get(request, *args, **kwargs)
|
|
|
+
|
|
|
+ def get_form(self):
|
|
|
+ resource = get_object_or_404(IPResource, pk=self.kwargs['pk'])
|
|
|
+ kwargs = super().get_form_kwargs()
|
|
|
+ kwargs['initial'].update({'resource': resource.pk})
|
|
|
+ form = ResourceAllocationForm(**kwargs)
|
|
|
+ form.fields['resource'].disabled = True
|
|
|
+ return form
|
|
|
+
|
|
|
+
|
|
|
+class ResourceAllocationMixin(PermissionRequiredMixin):
|
|
|
+ model = ResourceAllocation
|
|
|
+ permission_required = 'services.change_resourceallocation'
|
|
|
+
|
|
|
+
|
|
|
+class ResourceAllocationDeallocate(ResourceAllocationMixin, SingleObjectMixin, RedirectView):
|
|
|
+ def get_object(self, queryset=None):
|
|
|
+ obj = super().get_object(queryset)
|
|
|
+ if not obj.active:
|
|
|
+ raise PermissionDenied
|
|
|
+ obj.end = timezone.now()
|
|
|
+ obj.save()
|
|
|
+ return obj
|
|
|
+
|
|
|
+ def get_redirect_url(self, *args, **kwargs):
|
|
|
+ return self.get_object().get_absolute_url()
|