[chronojump-server] players can have a photo



commit 84b78ed02f0f7a1cdb08f508b43695e63ad072d3
Author: Xavier de Blas <xaviblas gmail com>
Date:   Fri May 12 18:25:51 2017 +0200

    players can have a photo

 chronojump-flask/chronojump_server.py             |   34 +++++++++++++++++----
 chronojump-flask/templates/player_add.html        |    4 ++
 chronojump-flask/templates/player_add_result.html |   23 ++++++++++----
 chronojump-flask/templates/player_list.html       |   14 ++++++--
 tables.txt                                        |   12 ++++---
 5 files changed, 65 insertions(+), 22 deletions(-)
---
diff --git a/chronojump-flask/chronojump_server.py b/chronojump-flask/chronojump_server.py
index ed0ed5e..fd50f89 100644
--- a/chronojump-flask/chronojump_server.py
+++ b/chronojump-flask/chronojump_server.py
@@ -1,7 +1,8 @@
-from flask import Flask, render_template, request, jsonify
+from flask import Flask, render_template, request, jsonify, url_for
 from flaskext.mysql import MySQL
 import subprocess
 import os
+from werkzeug import secure_filename
 
 
 mysql = MySQL()
@@ -73,13 +74,14 @@ def list():
     if p is None or p == "" or p == "All":
         cursor.execute("SELECT * FROM person")
     elif p == "Task":
-        cursor.execute("SELECT person.*, task.id, task.comment FROM person, task WHERE task.id=person.id")
+        cursor.execute("SELECT person.*, task.id, task.comment FROM person, task WHERE 
task.personId=person.id")
     else:
         cursor.execute("SELECT * FROM person") #TODO: select non-task persons
 
     rows = cursor.fetchall()
     return render_template("player_list.html", header = getHeader("Llistat de jugadors"), rows = rows, p=p)
 
+
 @app.route('/player_add')
 def player_add():
         name = request.args.get('name')
@@ -149,15 +151,35 @@ def player_add_submit():
             return render_template("player_add_result.html", header = getHeader("Afegir jugador"),
                     added=False, msg = msg, name=name, weight=weight, height=height, rfid=rfid)
 
-
         #5 insert person and show success
-        cursor.execute("INSERT INTO person (name, weight, height, rfid) VALUES (" +
-                "'" + name + "', " + str(weight) + ", " + str(height) + ", '" + rfid + "')")
+        cursor.execute("INSERT INTO person (name, weight, height, rfid, imageName) VALUES (" +
+                "'" + name + "', " + str(weight) + ", " + str(height) + ", '" + rfid + "', '')")
         db.commit()
+
         msg = "Afegit " + name
 
         return render_template("player_add_result.html", header = getHeader("Afegir jugador"),
-                added=True, msg = msg, name=name, weight=weight, height=height,  rfid=rfid)
+                added=True, image=False, msg = msg, name=name, weight=weight, height=height, rfid=rfid, 
personId=cursor.lastrowid)
+
+app.config['UPLOAD_FOLDER'] = "static/images"
+
+@app.route('/uploader', methods = ['GET', 'POST'])
+def uploader():
+    if request.method == 'POST':
+        #copy file
+        f = request.files['file']
+        f.save(os.path.join(app.config['UPLOAD_FOLDER'], secure_filename(f.filename)))
+
+        #update SQL record
+        personId = request.form['personId']
+
+        db = mysql.connect()
+        cursor = db.cursor()
+        cursor.execute("UPDATE person SET imageName = '" + secure_filename(f.filename) + "' WHERE id = " + 
personId)
+        db.commit()
+
+        return render_template("player_add_result.html", header = getHeader("Afegida imatge"),
+                added=True, image=True, msg = "")
 
 
 if __name__ == "__main__":
diff --git a/chronojump-flask/static/images/.gitkeep b/chronojump-flask/static/images/.gitkeep
new file mode 100644
index 0000000..e69de29
diff --git a/chronojump-flask/templates/player_add.html b/chronojump-flask/templates/player_add.html
index af04467..1397328 100644
--- a/chronojump-flask/templates/player_add.html
+++ b/chronojump-flask/templates/player_add.html
@@ -55,6 +55,10 @@
                                  </td>
                          </tr>
                          <tr>
+                                 <td>&nbsp;
+                                 </td>
+                         </tr>
+                         <tr>
                                  <td>
                                          <input type = "submit" value = "Llegeix RFID" onClick="countDown()" 
/><br>
                                  </td>
diff --git a/chronojump-flask/templates/player_add_result.html 
b/chronojump-flask/templates/player_add_result.html
index 28185cb..ad08f74 100644
--- a/chronojump-flask/templates/player_add_result.html
+++ b/chronojump-flask/templates/player_add_result.html
@@ -9,16 +9,25 @@
   <body>
          <div class="main">
          {{ header|safe }}
-         {% if rfid %}
-         <br><br>RFID: {{rfid}}
-         {% endif %}
 
-         <br><br>{{ msg }}
+         {{ msg }}
 
          {% if not added %}
-         <br></br><br></br><a 
href="player_add?name={{name}}&weight={{weight}}&height={{height}}&rfid={{rfid}}">Tornar</a>
-         {% endif %}
-         </div>
+         <br></br><br></br><a 
href="player_add?name={{name}}&weight={{weight}}&height={{height}}&imageURL={{imageURL}}&rfid={{rfid}}">Tornar</a>
+         {% else %}
+               {% if rfid %}
+                       (RFID: {{rfid}})
+               {% endif %}
+               {% if not image %}
+               <br><br><br>Vols afegir una fotografia?
+               <form action = "{{ url_for('uploader') }}" method = "POST" enctype = "multipart/form-data">
+                       <input type = "hidden" name = "personId" value="{{personId}}"/>
+                       <br><input type="file" name="file">
+                       <br><br><input type = "submit" value = "Afegeix" /><br>
+               </form>
+               {% endif %}
+       {% endif %}
+       </div>
   </body>
 </html>
 
diff --git a/chronojump-flask/templates/player_list.html b/chronojump-flask/templates/player_list.html
index d09627f..3e35a8e 100644
--- a/chronojump-flask/templates/player_list.html
+++ b/chronojump-flask/templates/player_list.html
@@ -51,9 +51,10 @@
                                <th>Pes</th>
                                <th>Alçada</th>
                                <th>RFID</th>
+                               <th>Foto</th>
                                {% if p == "Task" %}
-                               <th>Tasca Id</th>
-                               <th>Tasca</th>
+                                       <th>Tasca Id</th>
+                                       <th>Tasca</th>
                                {% endif %}
                        </thead>
 
@@ -64,9 +65,14 @@
                                <td>{{row[2]}}</td>
                                <td>{{row[3]}}</td>     
                                <td>{{row[4]}}</td>
+                               {% if (row[5] and row[5] != None and row[5] != "") %}</td>
+                                       <td><a href="{{ url_for('static', filename='images/' + row[5]) 
}}">foto</a></td>
+                               {% else %}
+                                       <td>&nbsp;</td>
+                               {% endif %}
                                {% if p == "Task" %}
-                               <td>{{row[5]}}</td>
-                               <td>{{row[6]}}</td>
+                                       <td>{{row[6]}}</td>
+                                       <td>{{row[7]}}</td>
                                {% endif %}
                        </tr>
                        {% endfor %}
diff --git a/tables.txt b/tables.txt
index ee58d65..6e7afde 100644
--- a/tables.txt
+++ b/tables.txt
@@ -9,12 +9,14 @@ CREATE TABLE results(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, dt TIMESTAMP DE
 
 CREATE TABLE station(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, name CHAR(30));
 
-CREATE TABLE rfid(dt TIMESTAMP DEFAULT CURRENT_TIMESTAMP, personID INT NOT NULL, rfid CHAR(30));
+CREATE TABLE rfid(dt TIMESTAMP DEFAULT CURRENT_TIMESTAMP, personId INT NOT NULL, rfid CHAR(30));
 
 #rfid can be six numbers long (of three digits each), separated by commas
-CREATE TABLE person(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, name CHAR(30), weight FLOAT, height FLOAT, 
rfid CHAR(23));
+CREATE TABLE person(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, name CHAR(30), weight FLOAT, height FLOAT, 
rfid CHAR(23), imageName CHAR(50));
 
-CREATE TABLE task(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, dt TIMESTAMP DEFAULT CURRENT_TIMESTAMP, 
personID INT, comment CHAR(200));
+CREATE TABLE task(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, dt TIMESTAMP DEFAULT CURRENT_TIMESTAMP, 
personId INT, comment CHAR(200));
+
+#CREATE TABLE image(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, personId INT NOT NULL, image BLOB, FOREIGN 
KEY (personId) REFERENCES person(id));
 
 # 2.- insert sample data:
 
@@ -27,8 +29,8 @@ insert into station VALUES(6, "Inercial 2");
 insert into station VALUES(7, "Inercial 3");
 insert into station VALUES(8, "Dominades");
 insert into station VALUES(9, "Leg press");
-insert into person VALUES(NULL, "joan", 80, 175.7, "212,212,212,212");
-insert into person VALUES(NULL, "pep", 72, 164.4, "211,211,211,211");
+insert into person VALUES(NULL, "joan", 80, 175.7, "212,212,212,212", "");
+insert into person VALUES(NULL, "pep", 72, 164.4, "211,211,211,211", "");
 insert into results VALUES(NULL, NULL, 1, 2, 45.2, "press de banca", 480, 6, "");
 insert into results VALUES(NULL, NULL, 1, 1, 2, "sprint", NULL, NULL, "5 segons");
 insert into task VALUES(NULL, NULL, 1, "Mig squat amb 45 Kg, 3 series de 8 repeticions a 1,1 m/s. 
Desplaçaments laterals amb la inercial de 800 W");


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