From 2bdcada9a7ad07b475767a59bac9ba3de2fadb77 Mon Sep 17 00:00:00 2001 From: Clemens Wolff Date: Fri, 12 Apr 2019 22:41:30 -0400 Subject: [PATCH] Collapse Django annotation templates --- app/server/models.py | 14 ++++++------ .../annotation_base.html => annotation.html} | 4 ++++ .../annotation/document_classification.html | 6 ----- app/server/templates/annotation/seq2seq.html | 6 ----- .../annotation/sequence_labeling.html | 6 ----- .../templates/demo/demo_named_entity.html | 6 ----- .../demo/demo_text_classification.html | 6 ----- .../templates/demo/demo_translation.html | 6 ----- app/server/tests/test_models.py | 18 +++++++-------- app/server/views.py | 22 ++++++++++++++----- 10 files changed, 37 insertions(+), 57 deletions(-) rename app/server/templates/{annotation/annotation_base.html => annotation.html} (80%) delete mode 100644 app/server/templates/annotation/document_classification.html delete mode 100644 app/server/templates/annotation/seq2seq.html delete mode 100644 app/server/templates/annotation/sequence_labeling.html delete mode 100644 app/server/templates/demo/demo_named_entity.html delete mode 100644 app/server/templates/demo/demo_text_classification.html delete mode 100644 app/server/templates/demo/demo_translation.html diff --git a/app/server/models.py b/app/server/models.py index ce57dc77..a3c379f0 100644 --- a/app/server/models.py +++ b/app/server/models.py @@ -33,7 +33,7 @@ class Project(PolymorphicModel): def image(self): raise NotImplementedError() - def get_template_name(self): + def get_bundle_name(self): raise NotImplementedError() def get_upload_template(self): @@ -61,8 +61,8 @@ class TextClassificationProject(Project): def image(self): return staticfiles_storage.url('images/cats/text_classification.jpg') - def get_template_name(self): - return 'annotation/document_classification.html' + def get_bundle_name(self): + return 'document_classification' def get_upload_template(self): return 'admin/upload/text_classification.html' @@ -88,8 +88,8 @@ class SequenceLabelingProject(Project): def image(self): return staticfiles_storage.url('images/cats/sequence_labeling.jpg') - def get_template_name(self): - return 'annotation/sequence_labeling.html' + def get_bundle_name(self): + return 'sequence_labeling' def get_upload_template(self): return 'admin/upload/sequence_labeling.html' @@ -115,8 +115,8 @@ class Seq2seqProject(Project): def image(self): return staticfiles_storage.url('images/cats/seq2seq.jpg') - def get_template_name(self): - return 'annotation/seq2seq.html' + def get_bundle_name(self): + return 'seq2seq' def get_upload_template(self): return 'admin/upload/seq2seq.html' diff --git a/app/server/templates/annotation/annotation_base.html b/app/server/templates/annotation.html similarity index 80% rename from app/server/templates/annotation/annotation_base.html rename to app/server/templates/annotation.html index a9d763fc..32748a94 100644 --- a/app/server/templates/annotation/annotation_base.html +++ b/app/server/templates/annotation.html @@ -1,5 +1,6 @@ {% extends "base.html" %} {% load static %} +{% load render_bundle from webpack_loader %} {% block header %} {% endblock %} @@ -16,3 +17,6 @@ {% block content %}
{% endblock %} +{% block footer %} +{% render_bundle bundle_name 'js' %} +{% endblock %} diff --git a/app/server/templates/annotation/document_classification.html b/app/server/templates/annotation/document_classification.html deleted file mode 100644 index 177c620e..00000000 --- a/app/server/templates/annotation/document_classification.html +++ /dev/null @@ -1,6 +0,0 @@ -{% extends "annotation/annotation_base.html" %} -{% load static %} -{% load render_bundle from webpack_loader %} -{% block footer %} -{% render_bundle 'document_classification' 'js' %} -{% endblock %} diff --git a/app/server/templates/annotation/seq2seq.html b/app/server/templates/annotation/seq2seq.html deleted file mode 100644 index 741a1ce5..00000000 --- a/app/server/templates/annotation/seq2seq.html +++ /dev/null @@ -1,6 +0,0 @@ -{% extends "annotation/annotation_base.html" %} -{% load static %} -{% load render_bundle from webpack_loader %} -{% block footer %} -{% render_bundle 'seq2seq' 'js' %} -{% endblock %} diff --git a/app/server/templates/annotation/sequence_labeling.html b/app/server/templates/annotation/sequence_labeling.html deleted file mode 100644 index 26635c70..00000000 --- a/app/server/templates/annotation/sequence_labeling.html +++ /dev/null @@ -1,6 +0,0 @@ -{% extends "annotation/annotation_base.html" %} -{% load static %} -{% load render_bundle from webpack_loader %} -{% block footer %} -{% render_bundle 'sequence_labeling' 'js' %} -{% endblock %} diff --git a/app/server/templates/demo/demo_named_entity.html b/app/server/templates/demo/demo_named_entity.html deleted file mode 100644 index e02e5e72..00000000 --- a/app/server/templates/demo/demo_named_entity.html +++ /dev/null @@ -1,6 +0,0 @@ -{% extends "annotation/annotation_base.html" %} -{% load static %} -{% load render_bundle from webpack_loader %} -{% block footer %} -{% render_bundle 'demo_named_entity' 'js' %} -{% endblock %} diff --git a/app/server/templates/demo/demo_text_classification.html b/app/server/templates/demo/demo_text_classification.html deleted file mode 100644 index 9008b132..00000000 --- a/app/server/templates/demo/demo_text_classification.html +++ /dev/null @@ -1,6 +0,0 @@ -{% extends "annotation/annotation_base.html" %} -{% load static %} -{% load render_bundle from webpack_loader %} -{% block footer %} -{% render_bundle 'demo_text_classification' 'js' %} -{% endblock %} diff --git a/app/server/templates/demo/demo_translation.html b/app/server/templates/demo/demo_translation.html deleted file mode 100644 index b2a998bd..00000000 --- a/app/server/templates/demo/demo_translation.html +++ /dev/null @@ -1,6 +0,0 @@ -{% extends "annotation/annotation_base.html" %} -{% load static %} -{% load render_bundle from webpack_loader %} -{% block footer %} -{% render_bundle 'demo_translation' 'js' %} -{% endblock %} diff --git a/app/server/tests/test_models.py b/app/server/tests/test_models.py index 79fb9fa6..aee2daa7 100644 --- a/app/server/tests/test_models.py +++ b/app/server/tests/test_models.py @@ -19,9 +19,9 @@ class TestTextClassificationProject(TestCase): image_url = self.project.image self.assertTrue(image_url.endswith('.jpg')) - def test_get_template_name(self): - template = self.project.get_template_name() - self.assertEqual(template, 'annotation/document_classification.html') + def test_get_bundle_name(self): + template = self.project.get_bundle_name() + self.assertEqual(template, 'document_classification') def test_get_annotation_serializer(self): serializer = self.project.get_annotation_serializer() @@ -42,9 +42,9 @@ class TestSequenceLabelingProject(TestCase): image_url = self.project.image self.assertTrue(image_url.endswith('.jpg')) - def test_get_template_name(self): - template = self.project.get_template_name() - self.assertEqual(template, 'annotation/sequence_labeling.html') + def test_get_bundle_name(self): + template = self.project.get_bundle_name() + self.assertEqual(template, 'sequence_labeling') def test_get_annotation_serializer(self): serializer = self.project.get_annotation_serializer() @@ -65,9 +65,9 @@ class TestSeq2seqProject(TestCase): image_url = self.project.image self.assertTrue(image_url.endswith('.jpg')) - def test_get_template_name(self): - template = self.project.get_template_name() - self.assertEqual(template, 'annotation/seq2seq.html') + def test_get_bundle_name(self): + template = self.project.get_bundle_name() + self.assertEqual(template, 'seq2seq') def test_get_annotation_serializer(self): serializer = self.project.get_annotation_serializer() diff --git a/app/server/views.py b/app/server/views.py index e6a54509..bf37600b 100644 --- a/app/server/views.py +++ b/app/server/views.py @@ -18,10 +18,13 @@ class IndexView(TemplateView): class ProjectView(LoginRequiredMixin, TemplateView): + template_name = 'annotation.html' - def get_template_names(self): + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) project = get_object_or_404(Project, pk=self.kwargs['project_id']) - return [project.get_template_name()] + context['bundle_name'] = project.get_bundle_name() + return context class ProjectsView(LoginRequiredMixin, TemplateView): @@ -79,12 +82,21 @@ class LoginView(BaseLoginView): class DemoTextClassification(TemplateView): - template_name = 'demo/demo_text_classification.html' + template_name = 'annotation.html' + extra_context = { + 'bundle_name': 'demo_text_classification', + } class DemoNamedEntityRecognition(TemplateView): - template_name = 'demo/demo_named_entity.html' + template_name = 'annotation.html' + extra_context = { + 'bundle_name': 'demo_named_entity', + } class DemoTranslation(TemplateView): - template_name = 'demo/demo_translation.html' + template_name = 'annotation.html' + extra_context = { + 'bundle_name': 'demo_translation', + }