views.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. from __future__ import unicode_literals
  2. from django.contrib.auth.mixins import PermissionRequiredMixin
  3. from django.contrib import messages
  4. from django.http import Http404
  5. from django.shortcuts import get_object_or_404, redirect, render
  6. from django.utils.safestring import mark_safe
  7. from django.views.generic import View
  8. from utilities.forms import ConfirmationForm
  9. from utilities.views import ObjectDeleteView, ObjectEditView
  10. from .forms import ImageAttachmentForm
  11. from .models import ImageAttachment, ReportResult, UserAction
  12. from .reports import get_report, get_reports
  13. #
  14. # Image attachments
  15. #
  16. class ImageAttachmentEditView(PermissionRequiredMixin, ObjectEditView):
  17. permission_required = 'extras.change_imageattachment'
  18. model = ImageAttachment
  19. model_form = ImageAttachmentForm
  20. def alter_obj(self, imageattachment, request, args, kwargs):
  21. if not imageattachment.pk:
  22. # Assign the parent object based on URL kwargs
  23. model = kwargs.get('model')
  24. imageattachment.parent = get_object_or_404(model, pk=kwargs['object_id'])
  25. return imageattachment
  26. def get_return_url(self, request, imageattachment):
  27. return imageattachment.parent.get_absolute_url()
  28. class ImageAttachmentDeleteView(PermissionRequiredMixin, ObjectDeleteView):
  29. permission_required = 'extras.delete_imageattachment'
  30. model = ImageAttachment
  31. def get_return_url(self, request, imageattachment):
  32. return imageattachment.parent.get_absolute_url()
  33. #
  34. # Reports
  35. #
  36. class ReportListView(View):
  37. """
  38. Retrieve all of the available reports from disk and the recorded ReportResult (if any) for each.
  39. """
  40. def get(self, request):
  41. reports = get_reports()
  42. results = {r.report: r for r in ReportResult.objects.all()}
  43. ret = []
  44. for module, report_list in reports:
  45. module_reports = []
  46. for report in report_list:
  47. report.result = results.get(report.full_name, None)
  48. module_reports.append(report)
  49. ret.append((module, module_reports))
  50. return render(request, 'extras/report_list.html', {
  51. 'reports': ret,
  52. })
  53. class ReportView(View):
  54. """
  55. Display a single Report and its associated ReportResult (if any).
  56. """
  57. def get(self, request, name):
  58. # Retrieve the Report by "<module>.<report>"
  59. module_name, report_name = name.split('.')
  60. report = get_report(module_name, report_name)
  61. if report is None:
  62. raise Http404
  63. # Attach the ReportResult (if any)
  64. report.result = ReportResult.objects.filter(report=report.full_name).first()
  65. return render(request, 'extras/report.html', {
  66. 'report': report,
  67. 'run_form': ConfirmationForm(),
  68. })
  69. class ReportRunView(PermissionRequiredMixin, View):
  70. """
  71. Run a Report and record a new ReportResult.
  72. """
  73. permission_required = 'extras.add_reportresult'
  74. def post(self, request, name):
  75. # Retrieve the Report by "<module>.<report>"
  76. module_name, report_name = name.split('.')
  77. report = get_report(module_name, report_name)
  78. if report is None:
  79. raise Http404
  80. form = ConfirmationForm(request.POST)
  81. if form.is_valid():
  82. # Run the Report. A new ReportResult is created.
  83. report.run()
  84. result = 'failed' if report.failed else 'passed'
  85. msg = "Ran report {} ({})".format(report.full_name, result)
  86. messages.success(request, mark_safe(msg))
  87. UserAction.objects.log_create(request.user, report.result, msg)
  88. return redirect('extras:report', name=report.full_name)