No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

generic.py 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. # This file is part of dnmgmt, a number resource management system
  2. # Licensed under GNU General Public License v3 or later
  3. # Written by Sebastian Lohff (seba@someserver.de)
  4. from django.views.generic import DeleteView
  5. from django.http import HttpResponseRedirect
  6. from django.contrib import messages
  7. from django.db.models import Q
  8. class DeleteCheckView(DeleteView):
  9. """ Check if object actually can be deleted. Provide reasons to template
  10. if not.
  11. """
  12. def delete(self, request, *args, **kwargs):
  13. self.object = self.get_object()
  14. success_url = self.get_success_url()
  15. reasons = self.object.getNoDeleteReasons()
  16. if reasons:
  17. # do not delete, do what get does...
  18. return self.get(request, *args, **kwargs)
  19. else:
  20. self.object.delete()
  21. messages.info(request, "Object %s has been deleted" % str(self.object))
  22. return HttpResponseRedirect(success_url)
  23. def get_context_data(self, **kwargs):
  24. if 'reasons' not in kwargs:
  25. kwargs['reasons'] = self.object.getNoDeleteReasons()
  26. return super(DeleteCheckView, self).get_context_data(**kwargs)
  27. class MntGenericMixin(object):
  28. def get_queryset(self):
  29. mnts = self.request.user.maintainer_set.all()
  30. q = Q(mnt_by__in=mnts)
  31. if hasattr(self.model, "mnt_lower"):
  32. q |= Q(mnt_lower__in=mnts)
  33. return self.model.objects.filter(q).distinct()
  34. def get_form_kwargs(self, *args, **kwargs):
  35. kwargs = super(MntGenericMixin, self).get_form_kwargs(*args, **kwargs)
  36. if hasattr(self.model, "mnt_lower"):
  37. mnts = self.request.user.maintainer_set.all()
  38. if not any(mnt in self.object.mnt_by.all() for mnt in mnts):
  39. # we are in mnt_lower
  40. kwargs["lower"] = True
  41. return kwargs