From 18bb996993db2699a2ca5b9922bed7ccfbd616c8 Mon Sep 17 00:00:00 2001
From: Robin Sonnabend <robin@fsmpi.rwth-aachen.de>
Date: Tue, 28 Feb 2017 13:36:51 +0100
Subject: [PATCH] Generate protocol template correctly

---
 models/database.py              | 13 +++++++++++++
 templates/protocol-template.txt |  7 ++++---
 2 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/models/database.py b/models/database.py
index 897ae88..2a6d373 100644
--- a/models/database.py
+++ b/models/database.py
@@ -218,6 +218,12 @@ class Protocol(db.Model):
     def get_originating_todos(self):
         return [todo for todo in self.todos if self == todo.get_first_protocol()]
 
+    def get_open_todos(self):
+        return [
+            todo for todo in self.protocoltype.todos
+            if not todo.is_done()
+        ]
+
     def has_compiled_document(self):
         candidates = [
             document for document in self.documents
@@ -472,6 +478,13 @@ class Todo(db.Model):
             self.get_state_plain()
         )
 
+    def render_template(self):
+        parts = ["todo", self.who, self.description, self.state.get_name()]
+        if self.state.needs_date():
+            parts.append(date_filter(self.state))
+        parts.append("id {}".format(self.get_id()))
+        return "[{}]".format(";".join(parts))
+
 
 class TodoProtocolAssociation(db.Model):
     __tablename__ = "todoprotocolassociations"
diff --git a/templates/protocol-template.txt b/templates/protocol-template.txt
index 4f1516f..13b2faf 100644
--- a/templates/protocol-template.txt
+++ b/templates/protocol-template.txt
@@ -8,9 +8,10 @@
 {% macro render_top(top) %}
 {TOP {{top.name}}
             {% if top.name == "Todos" %}
-                {% if protocol.todos|length > 0 %}
-                    {% for todo in protocol.todos %}
-    [todo;{{todo.who}};{{todo.description}};id {{todo.get_id()}}];
+                {% set todos=protocol.get_open_todos() %}
+                {% if todos|length > 0 %}
+                    {% for todo in todos %}
+    {{todo.render_template()}};
                     {% endfor %}
                 {% else %}
 
-- 
GitLab