edit_handlers.py 1.1 KB

123456789101112131415161718192021222324252627282930
  1. from django.template.loader import render_to_string
  2. from django.utils.safestring import mark_safe
  3. from django.utils.translation import gettext as _
  4. from wagtail.admin.panels import EditHandler
  5. class FormSubmissionsPanel(EditHandler):
  6. template = "wagtailforms/edit_handlers/form_responses_panel.html"
  7. def bind_to(self, model=None, instance=None, request=None, form=None):
  8. new = super().bind_to(model=model)
  9. if self.heading is None:
  10. new.heading = _('{} submissions').format(model.get_verbose_name())
  11. return new
  12. def render(self):
  13. Submission = self.model.get_submission_class()
  14. submissions = Submission.objects.filter(page=self.instance)
  15. submission_count = submissions.count()
  16. if not submission_count:
  17. return ''
  18. return mark_safe(render_to_string(self.template, {
  19. 'self': self,
  20. 'submission_count': submission_count,
  21. 'last_submit_time': (submissions.order_by('submit_time')
  22. .last().submit_time),
  23. }))