dnmgmt/whoisdb/generic.py

54 lines
1.8 KiB
Python
Raw Normal View History

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
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)
class MntGenericMixin(object):
2019-05-30 22:10:55 +02:00
def get_queryset(self):
mnts = self.request.user.maintainer_set.all()
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)
2019-05-30 22:10:55 +02:00
return self.model.objects.filter(q).distinct()
2019-05-30 22:10:55 +02:00
def get_form_kwargs(self, *args, **kwargs):
kwargs = super(MntGenericMixin, self).get_form_kwargs(*args, **kwargs)
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
2019-05-30 22:10:55 +02:00
return kwargs