[chronojump-server] Photos can be inserted during the creation proccess of the player.



commit b681a4bacd37f2d858bca5b3e3ab86324f243930
Author: Marcos Venteo <mventeo gmail com>
Date:   Tue Jun 27 18:51:38 2017 +0200

    Photos can be inserted during the creation proccess of the player.

 chronojumpserver/views.py |   71 +++++++++++++++++++++++++++-----------------
 1 files changed, 43 insertions(+), 28 deletions(-)
---
diff --git a/chronojumpserver/views.py b/chronojumpserver/views.py
index a765869..e966344 100755
--- a/chronojumpserver/views.py
+++ b/chronojumpserver/views.py
@@ -54,6 +54,32 @@ def show_stations():
     return render_template('station_list.html', stations=stations)
 
 
+def _update_player_photo(player_id, photo, previous_imageName):
+    """Update the photo of the player, and return the path."""
+    # First remove the previous photo
+    if previous_imageName:
+        previous_path = os.path.join('chronojumpserver',
+                                app.config['UPLOAD_FOLDER'],
+                                previous_imageName)
+        # Remove if exists
+        if os.path.exists(previous_path):
+            os.unlink(previous_path)
+
+    # Set the new photo filename
+    new_photo = 'player_' + str(player_id) + '_' + str(int(time()))
+    full_path = os.path.join('chronojumpserver',
+                            app.config['UPLOAD_FOLDER'],
+                            new_photo)
+    # save the photo in the disk
+    photo.save(full_path)
+    # Update the photo in the database
+    db_session.query(Person).filter_by(id=player_id).update({
+        "imageName": new_photo
+    })
+    # Commit the changes
+    db_session.commit()
+
+
 @app.route('/player/<player_id>', methods=['GET', 'POST'])
 def player_detail(player_id):
     """Show players detail."""
@@ -74,38 +100,21 @@ def player_detail(player_id):
         # Save the image in photos folder
         if form.validate_on_submit():
             """Form Valid. Update the player."""
-            # Check if a photo has been passed too
-            f = form.photo.data
-            # compose filename with id to avoid multiple images
-            if f:
-                # Update the photo too. First we'll erase the previous photo to
-                # assure there is only one photo of the player and to force the
-                # browser to load the photo beacause of the cache
-                if player.imageName:
-                    fullpath = os.path.join('chronojumpserver',
-                                            app.config['UPLOAD_FOLDER'],
-                                            player.imageName)
-                    # Remove if exists
-                    if os.path.exists(fullpath):
-                        os.unlink(fullpath)
-                # Set the new photo
-                new_photo = 'player_' + player_id + \
-                    '_' + str(int(time()))
-                fullpath = os.path.join('chronojumpserver',
-                                        app.config['UPLOAD_FOLDER'],
-                                        new_photo)
-                # save the photo
-                f.save(fullpath)
-                player.imageName = new_photo
-            # Save the player
+            # Update the player
             db_session.query(Person).filter_by(id=player_id).update({
                 "name": form.fullname.data,
                 "height": form.height.data,
                 "weight": form.weight.data,
-                "rfid": form.rfid.data,
-                "imageName": player.imageName
+                "rfid": form.rfid.data
             })
+            # Commit the changes
             db_session.commit()
+            # If a new photo has passed, update it too
+            # Check if a photo has been passed too
+            if form.photo.data:
+                _update_player_photo(player_id, form.photo.data, player.imageName)
+
+
             # Update done
 
             msg = "Les dades del jugador %s s'han guardat correctament." % form.fullname.data
@@ -136,9 +145,15 @@ def add_player():
                 rfid=form.rfid.data
             )
             db_session.add(player)
+            # Commit the changes
             db_session.commit()
-            msg = "Ej jugador %s s'ha creat correctament." % form.fullname.data
-            return redirect('/player/%d' % player.id)
+            # If a photo has given, update after person creation
+            if form.photo.data:
+                _update_player_photo(player.id, form.photo.data, None)
+
+
+            msg = "Ej jugador %s s'ha creat correctament." % (form.fullname.data,)
+            return redirect('/player_list')
         else:
             # There are some errors in the form
             msg = 'Hi han hagut errors, revisa el formulari.'


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