diff --git a/atlas/models.py b/atlas/models.py index 2649812e..4767cad3 100644 --- a/atlas/models.py +++ b/atlas/models.py @@ -1307,6 +1307,27 @@ class CaseDetail(models.Model): return json.dumps(results) + def render_example_form(self, request=None): + """Build and return a rendered HTML snippet for the example answers form. + + If `request` is provided it will be passed to the template renderer so + csrf tokens and other context processors work correctly. The returned + HTML contains the json-editor widget (the `json_answer` field) and a + submit button for saving example answers. + """ + from django.template.loader import render_to_string + from django.utils.safestring import mark_safe + from .forms import JsonAnswerForm + + post_data = {} + # Ensure we pass the stored answers into the form so the widget is populated + post_data["json_answer"] = json.dumps(self.question_answers) if self.question_answers is not None else json.dumps({}) + + form = JsonAnswerForm(post_data, question_schema=self.question_schema) + + html = render_to_string("atlas/_rendered_example_form.html", {"form": form}, request=request) + return mark_safe(html) + class CasePrior(models.Model): case_detail = models.ForeignKey(CaseDetail, on_delete=models.CASCADE) prior_case = models.ForeignKey(Case, on_delete=models.CASCADE, related_name="prior_case") diff --git a/atlas/templates/atlas/_rendered_example_form.html b/atlas/templates/atlas/_rendered_example_form.html new file mode 100644 index 00000000..69bde385 --- /dev/null +++ b/atlas/templates/atlas/_rendered_example_form.html @@ -0,0 +1,17 @@ +{% load static %} +{# Render a small example answers form for a given CaseDetail - expects a context variable `form` (JsonAnswerForm) #} +
+ {% csrf_token %} + {# Include any media the field needs (json-editor) #} + {% if form.media %} + {{ form.media }} + {% endif %} + +
+ {{ form.json_answer }} +
+ +
+ +
+
diff --git a/atlas/templates/atlas/collection_viva.html b/atlas/templates/atlas/collection_viva.html index ddf2885b..41447f10 100644 --- a/atlas/templates/atlas/collection_viva.html +++ b/atlas/templates/atlas/collection_viva.html @@ -119,7 +119,7 @@ - {% comment %} {% endcomment %} + {% endblock %}