[chronojump-server] Now we can modify and delete the tasks



commit 8f880f985756f0de58d83be979b5abe9669bf738
Author: Marcos Venteo <mventeo gmail com>
Date:   Sun May 28 18:34:35 2017 +0200

    Now we can modify and delete the tasks

 chronojump-flask/chronojump_server.py       |   46 ++++++++++++++-------
 chronojump-flask/static/css/app.css         |    4 ++
 chronojump-flask/templates/player_list.html |   58 ++++++++++++++++++++------
 tables.txt                                  |    2 +-
 4 files changed, 79 insertions(+), 31 deletions(-)
---
diff --git a/chronojump-flask/chronojump_server.py b/chronojump-flask/chronojump_server.py
index 1415f09..72e8ca5 100644
--- a/chronojump-flask/chronojump_server.py
+++ b/chronojump-flask/chronojump_server.py
@@ -202,12 +202,10 @@ def get_all_players():
                                         'tasks': []
                                       }
             # Avoid null tasks
-            if row[7]:
+        if row[7]:
+            player_tasks[player_id]['tasks'].append({ 'id': row[6],
+                                                      'description' : row[7]})
 
-                player_tasks[player_id]['tasks'].append(row[7])
-        else:
-            # Add the task only
-            player_tasks[player_id]['tasks'].append(row[7])
     _rows =  [ val for key,val in player_tasks.iteritems()]
     return _rows
 
@@ -233,28 +231,44 @@ class Player(Resource):
 
 class TaskList(Resource):
 
-    def get(self):
-        pass
     def put(self):
         """ Add a new task to player."""
-        print "Adding new tasks to players"
+
         player_id = request.form['player_id']
-        new_task = request.form['new_task']
+        description = request.form['description']
+        task_id = request.form['task_id']
         dt = datetime.now()
-        sql = "insert into task values(NULL,"
-        sql += "'" + dt.strftime('%y-%m-%d %H:%M:%S') + "',"
-        sql += str(player_id) + ","
-        sql += "'" + new_task + "')"
-
 
+        if task_id == '':
+            sql = "insert into task values(NULL,"
+            sql += "'" + dt.strftime('%y-%m-%d %H:%M:%S') + "',"
+            sql += str(player_id) + ","
+            sql += "'" + description + "', 0)"
+        else:
+            sql = "update task set "
+            sql += "comment='" + description + "' "
+            sql += "where id="+task_id
+        print sql
         db = mysql.connect()
         cursor = db.cursor()
         cursor.execute(sql)
         db.commit()
 
-        print sql
-        return jsonify(msg="Task has been done succesfully.")
 
+        return jsonify(msg="Success")
+
+    def delete(self):
+        task_id = request.form['task_id']
+
+        sql = "delete from task "
+        sql += "where id="+task_id
+
+        db = mysql.connect()
+        cursor = db.cursor()
+        cursor.execute(sql)
+        db.commit()
+
+        return jsonify(msg="Success")
 
 api.add_resource(ResultsAPI, '/api/v1/results')
 api.add_resource(PlayerList, '/api/v1/players')
diff --git a/chronojump-flask/static/css/app.css b/chronojump-flask/static/css/app.css
index 1b03b4b..2d5776c 100644
--- a/chronojump-flask/static/css/app.css
+++ b/chronojump-flask/static/css/app.css
@@ -48,3 +48,7 @@ body.home {
     background-color: #2b2b2b;
     color: #ffffff;
 }
+
+.task-link {
+  cursor: pointer;
+}
diff --git a/chronojump-flask/templates/player_list.html b/chronojump-flask/templates/player_list.html
index 969f2e4..34ac822 100644
--- a/chronojump-flask/templates/player_list.html
+++ b/chronojump-flask/templates/player_list.html
@@ -6,29 +6,35 @@
 <script src="{{ url_for('static', filename='DataTables/media/js/dataTables.bootstrap.min.js') }}"></script>
 <script type="text/javascript">
 
-function addTaskToPlayer(){
-       // Add the task to the player through a ajax call
+function addModifyDeleteTask(action) {
        var player_id = $('#player-id').val();
-       var new_task = $('#task-comment').val();
-       console.log('add a new task ' + new_task + ' for player ' + player_id );
+       var description = $('#task-comment').val();
+       var task_id = $('#task-id').val();
+
+       // Set the method
+       if (action == 2) {
+               method = 'DELETE';
+       } else {
+               // For both Add or Modify we'll use PUT method
+               method = 'PUT';
+       }
        $('#myModal').modal('hide');
        $.ajax({
-               url: '/api/v1/tasks',
-               method: 'PUT',
+               url: '/api/v1/tasks',
+               method: method,
                data: {
                        player_id : player_id,
-                       new_task: new_task
+                       description: description,
+                       task_id: task_id
                }
        }).done(function() {
                var table = $('#players').DataTable();
                table.ajax.reload(null, false);
+
        });
-       // Reload the table to reflect the changes
 
 }
 
-
-
        $(document).ready(function() {
                var table = $('#players').DataTable({
                        "columns": [{
@@ -79,8 +85,9 @@ function addTaskToPlayer(){
                                        render: function(value) {
                                                if (value.length > 0) {
                                                        var html = "<ol>";
-                                                       $.each( value, function( index, value ) {
-                                                         html += "<li>"+ value + "</li>";
+                                                       $.each( value, function( index, task ) {
+                                                               console.log(value);
+                                                         html += "<li><a class='task-link' data-task-id='"+ 
task.id + "'>"+ task.description + "</a></li>";
                                                        });
                                                        html += "</ol>";
                                                        return html;
@@ -129,9 +136,28 @@ function addTaskToPlayer(){
                                $('#modal-title').text('Afegir nova tasca per ' + player.name);
                                $('#myModal').modal();
                                $('#player-id').val(player.id);
+                               $('#task-id').val('');
                                $('#task-comment').focus();
+                               $('#btnDeleteTask').removeClass('show').addClass('hidden');
+                               $('#btnUpdateTask').removeClass('show').addClass('hidden');
+                               $('#btnAddTask').removeClass('hidden').addClass('show');
            } );
 
+               $('#players').on('click', 'a', function() {
+                       var player = table.row( $(this).parents('tr') ).data();
+                       console.log('Modify task '+ $(this).text());
+                       $('#task-comment').val($(this).text());
+                       $('#modal-title').text('Modificar tasca #' + $(this).attr('data-task-id') + ' per ' + 
player.name);
+                       $('#myModal').modal();
+                       $('#player-id').val(player.id);
+                       $('#task-id').val($(this).attr('data-task-id'));
+                       $('#task-comment').focus();
+                       // Hide and show the buttons
+                       $('#btnDeleteTask').removeClass('hidden').addClass('show');
+                       $('#btnUpdateTask').removeClass('hidden').addClass('show');
+                       $('#btnAddTask').removeClass('show').addClass('hidden');
+               })
+
 
 
 });
@@ -155,6 +181,7 @@ function addTaskToPlayer(){
                      <div class="modal-body">
                                                <form>
                                                        <input type="hidden" id="player-id">
+                                                       <input type="hidden" id="task-id">
                          <div class="form-group">
                            <label for="recipient-name" class="control-label">DescripciĆ³ de la tasca:</label>
                            <textarea rows=2 class="form-control" id="task-comment"></textarea>
@@ -162,8 +189,11 @@ function addTaskToPlayer(){
                        </form>
                      </div>
                      <div class="modal-footer">
-                       <button type="button" class="btn btn-dager" data-dismiss="modal">Cancelar</button>
-                       <button type="button" class="btn btn-primary" onclick="addTaskToPlayer()">Afegeix la 
tasca</button>
+                       <button type="button" class="pull-left btn btn-default" 
data-dismiss="modal">Cancelar</button>
+
+                       <button id="btnAddTask" type="button" class="pull-right btn btn-primary hidden" 
onclick="addModifyDeleteTask(0)">Afegeix la tasca</button>
+                                               <button id="btnUpdateTask" type="button" class="pull-right 
btn btn-primary" onclick="addModifyDeleteTask(1)">Modificar la tasca</button>
+                                               <button id="btnDeleteTask" type="button" class="pull-right 
btn btn-danger hidden" onclick="addModifyDeleteTask(2)">Eliminar</button>
                      </div>
                    </div><!-- /.modal-content -->
                  </div><!-- /.modal-dialog -->
diff --git a/tables.txt b/tables.txt
index 88080e1..74bb9e0 100644
--- a/tables.txt
+++ b/tables.txt
@@ -16,7 +16,7 @@ CREATE TABLE rfid(dt TIMESTAMP DEFAULT CURRENT_TIMESTAMP, personId INT NOT NULL,
 #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), 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), bool done default 0);
 
 #CREATE TABLE image(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, personId INT NOT NULL, image BLOB, FOREIGN 
KEY (personId) REFERENCES person(id));
 


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