From 30f6ab8d675d7645286ecebc9f4d2f45495ecfb7 Mon Sep 17 00:00:00 2001
From: FSMPI Admin-Team <admin@fsmpi.rwth-aachen.de>
Date: Thu, 2 Mar 2017 06:14:05 +0100
Subject: [PATCH] Import todomails, hotfix since send_file currently does not
 work with uwsgi

---
 legacy.py | 21 ++++++++++++++++++---
 server.py | 16 +++++++++++++---
 2 files changed, 31 insertions(+), 6 deletions(-)

diff --git a/legacy.py b/legacy.py
index 3764780..9127450 100644
--- a/legacy.py
+++ b/legacy.py
@@ -2,14 +2,14 @@ from datetime import datetime
 from fuzzywuzzy import fuzz, process
 import tempfile
 
-from models.database import Todo, OldTodo, Protocol, ProtocolType
+from models.database import Todo, OldTodo, Protocol, ProtocolType, TodoMail
 from shared import db
 
 import config
 
 def log_fuzzy(text):
-    with tempfile.NamedTemporaryFile(delete=False, mode="w") as tmpfile:
-        tmpfile.write(text + "\n\n")
+    #with tempfile.NamedTemporaryFile(delete=False, mode="w") as tmpfile:
+    #    tmpfile.write(text + "\n\n")
     print(text)
 
 def lookup_todo_id(old_candidates, new_who, new_description):
@@ -40,6 +40,7 @@ def lookup_todo_id(old_candidates, new_who, new_description):
 INSERT_PROTOCOLTYPE = "INSERT INTO `protocolManager_protocoltype`"
 INSERT_PROTOCOL = "INSERT INTO `protocolManager_protocol`"
 INSERT_TODO = "INSERT INTO `protocolManager_todo`"
+INSERT_TODOMAIL = "INSERT INTO `protocolManager_todonamemailassignment`"
 
 def import_old_protocols(sql_text):
     protocoltype_lines = []
@@ -73,6 +74,20 @@ def import_old_protocols(sql_text):
     for protocol in sorted(protocols, key=lambda p: p.date):
         print(protocol.date)
         tasks.parse_protocol(protocol)
+
+def import_old_todomails(sql_text):
+    todomail_lines = []
+    for line in sql_text.splitlines():
+        if line.startswith(INSERT_TODOMAIL):
+            todomail_lines.append(line)
+    if len(todomail_lines) == 0:
+        raise ValueError("Necessary lines not found.")
+    for line in todomail_lines:
+        for assignment_id, name, mail in _split_insert_line(line):
+            todomail = TodoMail(name, mail)
+            print(todomail)
+            db.session.add(todomail)
+            db.session.commit()
     print("done importing")
 
 
diff --git a/server.py b/server.py
index 844bc79..ebc58a3 100755
--- a/server.py
+++ b/server.py
@@ -2,7 +2,7 @@
 import locale
 locale.setlocale(locale.LC_TIME, "de_DE.utf8")
 
-from flask import Flask, g, current_app, request, session, flash, redirect, url_for, abort, render_template, Response, send_file
+from flask import Flask, g, current_app, request, session, flash, redirect, url_for, abort, render_template, Response#, send_file
 from werkzeug.utils import secure_filename
 from flask_script import Manager, prompt
 from flask_migrate import Migrate, MigrateCommand
@@ -17,6 +17,7 @@ from io import StringIO, BytesIO
 import os
 from datetime import datetime
 import math
+import mimetypes
 
 import config
 from shared import db, date_filter, datetime_filter, date_filter_long, date_filter_short, time_filter, ldap_manager, security_manager, current_user, check_login, login_required, group_required, class_filter, needs_date_test, todostate_name_filter, code_filter, indent_tab_filter
@@ -24,7 +25,7 @@ from utils import is_past, mail_manager, url_manager, get_first_unused_int, set_
 from models.database import ProtocolType, Protocol, DefaultTOP, TOP, Document, Todo, Decision, MeetingReminder, Error, TodoMail, DecisionDocument, TodoState, Meta, DefaultMeta
 from views.forms import LoginForm, ProtocolTypeForm, DefaultTopForm, MeetingReminderForm, NewProtocolForm, DocumentUploadForm, KnownProtocolSourceUploadForm, NewProtocolSourceUploadForm, ProtocolForm, TopForm, SearchForm, NewProtocolFileUploadForm, NewTodoForm, TodoForm, TodoMailForm, DefaultMetaForm, MetaForm
 from views.tables import ProtocolsTable, ProtocolTypesTable, ProtocolTypeTable, DefaultTOPsTable, MeetingRemindersTable, ErrorsTable, TodosTable, DocumentsTable, DecisionsTable, TodoTable, ErrorTable, TodoMailsTable, DefaultMetasTable
-from legacy import import_old_todos, import_old_protocols
+from legacy import import_old_todos, import_old_protocols, import_old_todomails
 
 app = Flask(__name__)
 app.config.from_object(config)
@@ -90,8 +91,14 @@ def import_legacy():
         content = sqlfile.read().decode("utf-8")
         import_old_todos(content)
         import_old_protocols(content)
+        import_old_todomails(content)
     
-
+# cause uwsgi currently has a bug
+def send_file(file_like, cache_timeout, as_attachment, attachment_filename):
+    mimetype, _ = mimetypes.guess_type(attachment_filename)
+    response = Response(file_like.read(), mimetype)
+    response.headers["Content-Disposition"] = 'attachment; filename="{}"'.format(attachment_filename)
+    return response
 
 @app.route("/")
 def index():
@@ -979,6 +986,9 @@ def download_document(document_id):
             and not document.protocol.has_public_view_right(user))):
         flash("Keine Berechtigung.", "alert-error")
         return redirect(request.args.get("next") or url_for("index"))
+    #response = Response(document.as_file_like().read(), mimetype="application/pdf")
+    #response.headers["Content-Disposition"] = 'attachment; filename="{}"'.format(document.name)
+    #return response
     return send_file(document.as_file_like(), cache_timeout=1, as_attachment=True, attachment_filename=document.name)
 
 @app.route("/document/upload/<int:protocol_id>", methods=["POST"])
-- 
GitLab