[chronojump-server] getPersonByRFID returns a JSON object instead of returning a list of arrays.



commit 3bfa76fa9a813e68170fac9f9cfefbb161286719
Author: Carles Pina i Estany <carles pina cat>
Date:   Mon Jun 5 15:34:31 2017 -0400

    getPersonByRFID returns a JSON object instead of returning a list of arrays.
    
    Before was returning:
    [[7, "S. Roberto", 68.0, 178.0, "999,999,999,007", "jugadors/roberto.jpg"]]
    
    Now something like (with the names used in the database):
    {
            "id": "7",
            "name": "S. Roberto",
            "weight": 78.0,
            "height": 178.0,
            "rfid": "999,999,999,007",
            "photoURL": "jugadors/roberto.jpg"
    }

 main.py |   17 ++++++++++++-----
 1 files changed, 12 insertions(+), 5 deletions(-)
---
diff --git a/main.py b/main.py
index 5fa760d..6dbee7c 100755
--- a/main.py
+++ b/main.py
@@ -7,6 +7,7 @@ import json
 import os
 import string
 import MySQLdb
+import MySQLdb.cursors
 from werkzeug.contrib.fixers import ProxyFix
 import email_error_log
 
@@ -38,12 +39,15 @@ def create_directory(backtraces_directory):
     if not os.path.exists(backtraces_directory):
         os.mkdir(backtraces_directory)
 
-def connect_db():
+def connect_db(cursorclass=MySQLdb.cursors.Cursor):
+    """ Pass cursorclass=MySQLdb.cursors.DictCursor to have selects with Dictionaries. """
+
     db = MySQLdb.connect(
         host=config.get("db","server"),
         user=config.get("db","user"),
         passwd=config.get("db","password"),
-        db=config.get("db","name")
+        db=config.get("db","name"),
+        cursorclass=cursorclass
         )
 
     cur = db.cursor()
@@ -104,14 +108,17 @@ def getPersonByRFID():
 
     rfid  = content.get('rfid', "")
 
-    (con, cur) = connect_db()
+    (con, cur) = connect_db(cursorclass=MySQLdb.cursors.DictCursor)
 
     selectStr = "SELECT * FROM person where rfid = \"" + rfid + "\""
     print(selectStr)
     cur.execute(selectStr)
-    person = cur.fetchall()
+    people = cur.fetchall()
+
+    assert len(people) >= 1
 
-    return Response(json.dumps(person))
+    # TODO: handle if len(people) == 0 (rfid not found)
+    return Response(json.dumps(people[0]))
 
 @app.route("/getTasks", methods=['POST'])
 @auto.doc()


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]