views.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. from __future__ import unicode_literals
  2. from collections import OrderedDict
  3. from django.contrib.auth.mixins import PermissionRequiredMixin
  4. from django.shortcuts import get_object_or_404, render
  5. from django.urls import reverse
  6. from django.views.generic import View
  7. from . import reports
  8. from utilities.views import ObjectDeleteView, ObjectEditView
  9. from .forms import ImageAttachmentForm
  10. from .models import ImageAttachment, ReportResult
  11. from .reports import get_reports
  12. #
  13. # Image attachments
  14. #
  15. class ImageAttachmentEditView(PermissionRequiredMixin, ObjectEditView):
  16. permission_required = 'extras.change_imageattachment'
  17. model = ImageAttachment
  18. model_form = ImageAttachmentForm
  19. def alter_obj(self, imageattachment, request, args, kwargs):
  20. if not imageattachment.pk:
  21. # Assign the parent object based on URL kwargs
  22. model = kwargs.get('model')
  23. imageattachment.parent = get_object_or_404(model, pk=kwargs['object_id'])
  24. return imageattachment
  25. def get_return_url(self, request, imageattachment):
  26. return imageattachment.parent.get_absolute_url()
  27. class ImageAttachmentDeleteView(PermissionRequiredMixin, ObjectDeleteView):
  28. permission_required = 'extras.delete_imageattachment'
  29. model = ImageAttachment
  30. def get_return_url(self, request, imageattachment):
  31. return imageattachment.parent.get_absolute_url()
  32. #
  33. # Reports
  34. #
  35. class ReportListView(View):
  36. """
  37. Retrieve all of the available reports from disk and the recorded ReportResult (if any) for each.
  38. """
  39. def get(self, request):
  40. reports = get_reports()
  41. results = {r.report: r for r in ReportResult.objects.all()}
  42. foo = []
  43. for module, report_list in reports:
  44. module_reports = []
  45. for report_name, report_class in report_list:
  46. module_reports.append({
  47. 'name': report_name,
  48. 'description': report_class.description,
  49. 'results': results.get('{}.{}'.format(module, report_name), None)
  50. })
  51. foo.append((module, module_reports))
  52. return render(request, 'extras/report_list.html', {
  53. 'reports': foo,
  54. })