54 lines
1.6 KiB
Python
54 lines
1.6 KiB
Python
# 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)
|
|
|
|
from django.views.generic import DeleteView
|
|
from django.http import HttpResponseRedirect
|
|
from django.contrib import messages
|
|
from django.db.models import Q
|
|
|
|
|
|
class DeleteCheckView(DeleteView):
|
|
""" 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)
|
|
|
|
|
|
class MntGenericMixin(object):
|
|
def get_queryset(self):
|
|
mnts = self.request.user.maintainer_set.all()
|
|
|
|
q = Q(mnt_by__in=mnts)
|
|
if hasattr(self.model, "mnt_lower"):
|
|
q |= Q(mnt_lower__in=mnts)
|
|
|
|
return self.model.objects.filter(q).distinct()
|
|
|
|
def get_form_kwargs(self, *args, **kwargs):
|
|
kwargs = super(MntGenericMixin, self).get_form_kwargs(*args, **kwargs)
|
|
|
|
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
|
|
|
|
return kwargs
|