From c5901a6a1071db75d53c57ff39b7e3815db64118 Mon Sep 17 00:00:00 2001
From: Hinrikus Wolf <hinrikus.wolf@rwth-aachen.de>
Date: Sun, 8 Nov 2015 14:56:08 +0100
Subject: [PATCH] add current speaker to statements list

---
 migrations/versions/1b175b82cee_.py | 26 ++++++++++++++++++++++++++
 models/database.py                  | 16 +++++++++++-----
 modules/admin.py                    | 12 +++++++++---
 3 files changed, 46 insertions(+), 8 deletions(-)
 create mode 100644 migrations/versions/1b175b82cee_.py

diff --git a/migrations/versions/1b175b82cee_.py b/migrations/versions/1b175b82cee_.py
new file mode 100644
index 0000000..71ef064
--- /dev/null
+++ b/migrations/versions/1b175b82cee_.py
@@ -0,0 +1,26 @@
+"""empty message
+
+Revision ID: 1b175b82cee
+Revises: 3c75c74bb94
+Create Date: 2015-11-08 14:06:28.235808
+
+"""
+
+# revision identifiers, used by Alembic.
+revision = '1b175b82cee'
+down_revision = '3c75c74bb94'
+
+from alembic import op
+import sqlalchemy as sa
+
+
+def upgrade():
+    ### commands auto generated by Alembic - please adjust! ###
+    op.add_column('statements', sa.Column('is_current', sa.Boolean(), nullable=True))
+    ### end Alembic commands ###
+
+
+def downgrade():
+    ### commands auto generated by Alembic - please adjust! ###
+    op.drop_column('statements', 'is_current')
+    ### end Alembic commands ###
diff --git a/models/database.py b/models/database.py
index 453e893..937dd44 100644
--- a/models/database.py
+++ b/models/database.py
@@ -94,9 +94,9 @@ class Topic(db.Model):
     def sorted_statements(self):
         statements = [statement for statement in self.statements if not statement.executed]
         if self.mode == "fifo":
-            return sorted(statements, key=lambda st: -1 if st.is_meta else st.id)
+            return sorted(statements, key=lambda st:-2 if st.is_current else -1 if st.is_meta else st.id)
         elif self.mode == "balanced":
-            return sorted(statements, key=lambda st: -1 if st.is_meta else st.speaker.count(self))
+            return sorted(statements, key=lambda st:-2 if st.is_current else -1 if st.is_meta else st.speaker.count(self))
         else:
             return statements
     
@@ -169,34 +169,40 @@ class Statement(db.Model):
     executed = db.Column(db.Boolean)
     execution_time = db.Column(db.DateTime)
     is_meta = db.Column(db.Boolean, default=False)
+    is_current = db.Column(db.Boolean, default=False)
 
     speaker = relationship("Speaker", backref=backref("statements",order_by=id))
     topic = relationship("Topic", backref=backref("statements",order_by=id))
     
-    def __init__(self, speaker_id, topic_id, insertion_time=None, executed=False, execution_time=None, is_meta=False):
+    def __init__(self, speaker_id, topic_id, insertion_time=None, executed=False, execution_time=None, is_meta=False, is_current=False):
         self.speaker_id = speaker_id
         self.topic_id = topic_id
         self.insertion_time = insertion_time or datetime.now()
         self.executed = executed
         self.execution_time = execution_time or datetime.now()
         self.is_meta = is_meta
+        self.is_current = is_current
     
     def __repr__(self):
-        return "<Statement(id={}, speaker={}, topic_id={}, insertion_time={}, executed={}, execution_time={}, is_meta={})>".format(
+        return "<Statement(id={}, speaker={}, topic_id={}, insertion_time={}, executed={}, execution_time={}, is_meta={}, is_current={})>".format(
             self.id, 
             self.speaker,
             self.topic_id,
             self.insertion_time,
             self.executed,
             self.execution_time,
-            self.is_meta
+            self.is_meta,
+            self.is_current
         )
     
     def done(self):
         if self.executed:
             return False
         self.executed = True
+        self.is_current = False
         self.execution_time = datetime.now()
+        if self.topic.sorted_statements() is not None and self.topic.sorted_statements():
+            self.topic.sorted_statements()[0].is_current = True
         return True
     
     def undo(self):
diff --git a/modules/admin.py b/modules/admin.py
index b9a0875..662a8e9 100644
--- a/modules/admin.py
+++ b/modules/admin.py
@@ -282,8 +282,11 @@ def statement_new():
         topic = Topic.query.filter_by(id=form.topic.data).first()
         speaker = speaker_by_name_or_number(form.speaker_name.data, topic.event.id)
         if topic is not None and speaker is not None:
-            if speaker.count_active(topic) == 0 or (statement == "add_meta_statement" and speaker.count_active_meta(topic) == 0) :
-                statement = Statement(speaker.id, topic.id, is_meta=(statement == "add_meta_statement"))
+            if speaker.count_active(topic) == 0 or (statement == "add_meta_statement" 
+                and speaker.count_active_meta(topic) == 0) :
+                statement = Statement(speaker.id, topic.id,
+                                      is_meta=(statement == "add_meta_statement"),
+                                      is_current=(not topic.sorted_statements()))
                 db.session.add(statement)
                 db.session.commit()
             return redirect(url_for(".topic_show", id=topic.id))
@@ -309,12 +312,15 @@ def statement_done():
 @admin_permission.require()
 def statement_delete():
     statement_id = request.args.get("id", None)
+    topic_id = request.args.get("topic_id", None)
     if statement_id is not None:
         statement = Statement.query.filter_by(id=statement_id).first()
         if statement is not None:
+            topic = Topic.query.filter_by(id=topic_id).first()
+            if len(topic.sorted_statements()) > 1: 
+                topic.sorted_statements()[1].is_current = True
             db.session.delete(statement)
             db.session.commit()
-    topic_id = request.args.get("topic_id", None)
     if topic_id is not None:
         return redirect(url_for(".topic_show", id=topic_id))
     return redirect(url_for(".index"))
-- 
GitLab