2018-01-17 00:23:00 +01:00
|
|
|
# This file is part of dnmgmt, a number resource management system
|
|
|
|
# Licensed under GNU General Public License v3 or later
|
|
|
|
# Written by Sebastian Lohff (seba@someserver.de)
|
|
|
|
|
2017-02-28 19:44:15 +01:00
|
|
|
from django.views.generic import DeleteView
|
|
|
|
from django.http import HttpResponseRedirect
|
|
|
|
from django.contrib import messages
|
2017-03-02 18:28:15 +01:00
|
|
|
from django.db.models import Q
|
2017-02-28 19:44:15 +01:00
|
|
|
|
2017-03-22 04:06:24 +01:00
|
|
|
|
2017-02-28 19:44:15 +01:00
|
|
|
class DeleteCheckView(DeleteView):
|
2019-05-30 22:10:55 +02:00
|
|
|
""" Check if object actually can be deleted. Provide reasons to template
|
|
|
|
if not.
|
|
|
|
"""
|
|
|
|
def delete(self, request, *args, **kwargs):
|
|
|
|
self.object = self.get_object()
|
|
|
|
success_url = self.get_success_url()
|
|
|
|
|
|
|
|
reasons = self.object.getNoDeleteReasons()
|
|
|
|
if reasons:
|
|
|
|
# do not delete, do what get does...
|
|
|
|
return self.get(request, *args, **kwargs)
|
|
|
|
else:
|
|
|
|
self.object.delete()
|
|
|
|
messages.info(request, "Object %s has been deleted" % str(self.object))
|
|
|
|
return HttpResponseRedirect(success_url)
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
if 'reasons' not in kwargs:
|
|
|
|
kwargs['reasons'] = self.object.getNoDeleteReasons()
|
|
|
|
return super(DeleteCheckView, self).get_context_data(**kwargs)
|
2017-03-02 18:28:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
class MntGenericMixin(object):
|
2019-05-30 22:10:55 +02:00
|
|
|
def get_queryset(self):
|
|
|
|
mnts = self.request.user.maintainer_set.all()
|
2017-03-02 18:28:15 +01:00
|
|
|
|
2019-05-30 22:10:55 +02:00
|
|
|
q = Q(mnt_by__in=mnts)
|
|
|
|
if hasattr(self.model, "mnt_lower"):
|
|
|
|
q |= Q(mnt_lower__in=mnts)
|
2017-03-02 18:28:15 +01:00
|
|
|
|
2019-05-30 22:10:55 +02:00
|
|
|
return self.model.objects.filter(q).distinct()
|
2017-03-02 18:28:15 +01:00
|
|
|
|
2019-05-30 22:10:55 +02:00
|
|
|
def get_form_kwargs(self, *args, **kwargs):
|
|
|
|
kwargs = super(MntGenericMixin, self).get_form_kwargs(*args, **kwargs)
|
2017-03-02 18:28:15 +01:00
|
|
|
|
2019-05-30 22:10:55 +02:00
|
|
|
if hasattr(self.model, "mnt_lower"):
|
|
|
|
mnts = self.request.user.maintainer_set.all()
|
|
|
|
if not any(mnt in self.object.mnt_by.all() for mnt in mnts):
|
|
|
|
# we are in mnt_lower
|
|
|
|
kwargs["lower"] = True
|
2017-03-02 18:28:15 +01:00
|
|
|
|
2019-05-30 22:10:55 +02:00
|
|
|
return kwargs
|