rascunho

commit b9a3358a6bba6c730f011381ef2a7e522ab7d8b6

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

Use the user IP to generate the sha digest and update if found one

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


diff --git a/core/blueprints/basic.py b/core/blueprints/basic.py
index 4378bdf4ee9b4c2ad0b8936c0d2eeff7d21fce0b..047bac8980426225cb81b7ecffbefe8f27da8aa4 100644
--- a/core/blueprints/basic.py
+++ b/core/blueprints/basic.py
@@ -14,7 +14,7 @@         return create(request.form)
 
 @basic.route("/<sha>", methods = ['GET'])
 def show(sha):
-    doc = Document.query.filter(Document.sha == sha).one_or_none()
+    doc = document.query.filter(document.sha == sha).one_or_none()
 
     if not doc:
         abort(404)
@@ -31,15 +31,22 @@
     return Response(doc.content, mimetype="text/plain")
 
 def create(params):
+    doc = None
     content = params['text']
 
     if not content:
         abort(422)
 
-    doc = Document()
-
     sha = sha1()
     sha.update(content.encode())
+    sha.update(request.remote_addr.encode())
+
+    existing_doc = Document.query.filter(Document.sha == sha.hexdigest()).one_or_none()
+
+    if existing_doc:
+        doc = existing_doc
+    else:
+        doc = Document()
 
     doc.sha = sha.hexdigest()
     doc.content = content