dnmgmt/whoisdb/generic.py

49 lines
1.4 KiB
Python
Raw Normal View History

2017-02-28 19:44:15 +01:00
from django.views.generic import DeleteView
from django.http import HttpResponseRedirect
from django.contrib import messages
from django.db.models import Q
2017-02-28 19:44:15 +01:00
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" % self.object.handle)
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