From df1ee680c8cb32a89acbd22386a63bafaa0f27fb Mon Sep 17 00:00:00 2001
From: Markus Scheller <scheller_m@live.de>
Date: Mon, 19 Mar 2018 18:52:09 +0100
Subject: [PATCH] Added invitation storage

protokollsystem/proto3#179
---
 migrations/versions/45c336464a07_.py | 35 ++++++++++++++++++++++++++++
 models/database.py                   | 33 +++++++++++++++++++++++++-
 2 files changed, 67 insertions(+), 1 deletion(-)
 create mode 100644 migrations/versions/45c336464a07_.py

diff --git a/migrations/versions/45c336464a07_.py b/migrations/versions/45c336464a07_.py
new file mode 100644
index 0000000..7e79aff
--- /dev/null
+++ b/migrations/versions/45c336464a07_.py
@@ -0,0 +1,35 @@
+"""empty message
+
+Revision ID: 45c336464a07
+Revises: 3b2e9b2b7579
+Create Date: 2018-03-19 18:50:45.428045
+
+"""
+from alembic import op
+import sqlalchemy as sa
+
+
+# revision identifiers, used by Alembic.
+revision = '45c336464a07'
+down_revision = '3b2e9b2b7579'
+branch_labels = None
+depends_on = None
+
+
+def upgrade():
+    # ### commands auto generated by Alembic - please adjust! ###
+    op.create_table('invitationdocuments',
+    sa.Column('id', sa.Integer(), nullable=False),
+    sa.Column('protocol_id', sa.Integer(), nullable=True),
+    sa.Column('name', sa.String(), nullable=True),
+    sa.Column('filename', sa.String(), nullable=True),
+    sa.ForeignKeyConstraint(['protocol_id'], ['protocols.id'], ),
+    sa.PrimaryKeyConstraint('id')
+    )
+    # ### end Alembic commands ###
+
+
+def downgrade():
+    # ### commands auto generated by Alembic - please adjust! ###
+    op.drop_table('invitationdocuments')
+    # ### end Alembic commands ###
diff --git a/models/database.py b/models/database.py
index fdbb976..e4b2ce3 100644
--- a/models/database.py
+++ b/models/database.py
@@ -207,6 +207,9 @@ class Protocol(DatabaseModel):
     documents = relationship(
         "Document", backref=backref("protocol"),
         cascade="all, delete-orphan", order_by="Document.is_compiled")
+    invitations = relationship(
+        "Invitation", backref=backref("protocol"),
+        cascade="all, delete-orphan", order_by="Invitation.id")
     errors = relationship(
         "Error", backref=backref("protocol"), cascade="all, delete-orphan",
         order_by="Error.id")
@@ -584,6 +587,33 @@ def on_decisions_document_delete(mapper, connection, document):
             os.remove(document_path)
 
 
+class InvitationDocument(DatabaseModel):
+    __tablename__ = "invitationdocuments"
+    __model_name__ = "invitationdocument"
+    id = db.Column(db.Integer, primary_key=True)
+    protocol_id = db.Column(db.Integer, db.ForeignKey("protocols.id"))
+    name = db.Column(db.String)
+    filename = db.Column(db.String)
+
+    def get_parent(self):
+        return self.decision
+
+    def get_filename(self):
+        return os.path.join(config.DOCUMENTS_PATH, self.filename)
+
+    def as_file_like(self):
+        with open(self.get_filename(), "rb") as file:
+            return BytesIO(file.read())
+
+
+@event.listens_for(InvitationDocument, "before_delete")
+def on_decisions_document_delete(mapper, connection, document):
+    if document.filename is not None:
+        document_path = document.get_filename()
+        if os.path.isfile(document_path):
+            os.remove(document_path)
+
+
 class TodoState(Enum):
     open = 0
     waiting = 1
@@ -933,5 +963,6 @@ class LikeTOPAssociation(DatabaseModel):
 
 ALL_MODELS = [
     ProtocolType, Protocol, DefaultTOP, TOP, Document, DecisionDocument,
-    Todo, Decision, MeetingReminder, Error, DefaultMeta, Meta, DecisionCategory
+    InvitationDocument, Todo, Decision, MeetingReminder, Error, DefaultMeta,
+    Meta, DecisionCategory
 ]
-- 
GitLab