From 75b8fb63caaf19135f606bf8a1545e2fe432467c Mon Sep 17 00:00:00 2001
From: Robin Sonnabend <robin@fsmpi.rwth-aachen.de>
Date: Mon, 19 Mar 2018 17:47:56 +0100
Subject: [PATCH] Do not create protocols if the type already has one on that
 day

/close #97
/close #187
---
 models/database.py | 13 ++++++++++++-
 tasks.py           |  5 +++--
 2 files changed, 15 insertions(+), 3 deletions(-)

diff --git a/models/database.py b/models/database.py
index b9f64aa..11c68bf 100644
--- a/models/database.py
+++ b/models/database.py
@@ -105,6 +105,12 @@ class ProtocolType(DatabaseModel):
             return None
         return candidates[0]
 
+    def get_protocols_on_date(self, protocol_date):
+        return [
+            protocol for protocol in self.protocols
+            if protocol.date == protocol_date
+        ]
+
     def has_public_view_right(self, user, check_networks=True):
         return (
             self.has_public_anonymous_view_right(check_networks=check_networks)
@@ -423,7 +429,12 @@ class Protocol(DatabaseModel):
             tzinfo=tz.tzlocal())
 
     @staticmethod
-    def create_new_protocol(protocoltype, date, start_time=None):
+    def create_new_protocol(
+            protocoltype, date, start_time=None, allow_duplicate=False):
+        if not allow_duplicate:
+            duplicate_candidates = protocoltype.get_protocols_on_date(date)
+            if duplicate_candidates:
+                return duplicate_candidates[0]
         if start_time is None:
             start_time = protocoltype.usual_time
         protocol = Protocol(
diff --git a/tasks.py b/tasks.py
index 50d0476..1d142ed 100644
--- a/tasks.py
+++ b/tasks.py
@@ -492,8 +492,9 @@ def parse_protocol_async_inner(protocol):
         if len(protocol_tag.values) > 1:
             new_protocol_time = datetime.strptime(
                 protocol_tag.values[1], "%H:%M")
-        Protocol.create_new_protocol(
-            protocol.protocoltype, new_protocol_date, new_protocol_time)
+        if not protocol.protocoltype.get_protocols_on_date(new_protocol_date):
+            Protocol.create_new_protocol(
+                protocol.protocoltype, new_protocol_date, new_protocol_time)
 
     # TOPs
     old_tops = list(protocol.tops)
-- 
GitLab