rascunho

commit 56092728d0bc261ba59005c00cc42b2344312beb

Author: Pedro Lucas Porcellis <pedrolucasporcellis@gmail.com>

Save document on database

 core/blueprints/basic.py | 43 ++++++++++++++++++++++++++++++++++++++---


diff --git a/core/blueprints/basic.py b/core/blueprints/basic.py
index 4f6f100f5db0c4b7ea2fdc556643ae0667040796..4378bdf4ee9b4c2ad0b8936c0d2eeff7d21fce0b 100644
--- a/core/blueprints/basic.py
+++ b/core/blueprints/basic.py
@@ -1,5 +1,7 @@
-from flask import Blueprint, render_template, request
-import mistune
+from flask import Blueprint, render_template, request, abort, Response
+from core.database import db
+from core.types.document import Document
+from hashlib import sha1
 
 basic = Blueprint('basic', __name__)
 
@@ -10,7 +12,40 @@         return render_template('index.html')
     else:
         return create(request.form)
 
+@basic.route("/<sha>", methods = ['GET'])
+def show(sha):
+    doc = Document.query.filter(Document.sha == sha).one_or_none()
+
+    if not doc:
+        abort(404)
+
+    return render_template('preview.html', doc = doc)
+
+@basic.route("/<sha>/raw", methods = ['GET'])
+def raw(sha):
+    doc = Document.query.filter(Document.sha == sha).one_or_none()
+
+    if not doc:
+        abort(404)
+
+    return Response(doc.content, mimetype="text/plain")
+
 def create(params):
-    rendered = mistune.markdown(params['text'])
+    content = params['text']
+
+    if not content:
+        abort(422)
+
+    doc = Document()
+
+    sha = sha1()
+    sha.update(content.encode())
+
+    doc.sha = sha.hexdigest()
+    doc.content = content
 
-    return render_template('preview.html', rendered = rendered)
+    db.add(doc)
+    db.commit()
+    db.flush()
+
+    return render_template('preview.html', doc = doc)