[chronojump-server] Adding delete player functionality



commit 5aa7ba42f0413e7953b7e04e712762ea76173574
Author: Max Ros i Morejon <mros33 gmail com>
Date:   Fri Feb 22 12:45:12 2019 +0100

    Adding delete player functionality

 .../chronojump_networks/organizations/api/views.py | 13 ++++++
 .../organizations/players/players_list.html        | 46 +++++++++++++++++++++-
 2 files changed, 57 insertions(+), 2 deletions(-)
---
diff --git a/chronojumpserver-django/chronojump_networks/organizations/api/views.py 
b/chronojumpserver-django/chronojump_networks/organizations/api/views.py
index 586be7d..04b81a7 100644
--- a/chronojumpserver-django/chronojump_networks/organizations/api/views.py
+++ b/chronojumpserver-django/chronojump_networks/organizations/api/views.py
@@ -129,6 +129,19 @@ class PlayerListView(ListCreateAPIView):
         )
         s = PlayerSerializer(o)
         return Response(s.data)
+    
+    def delete(self, request, *args, **kwargs):
+        print("delete player")        
+        data = dict(request.data)
+        player_ids = data['player_ids[]']
+        print("delete player")
+        for player_id in player_ids:
+            print("deleting %d player" % (int(player_id))) 
+            o = Player.objects.get(id=int(player_id))
+            o.delete()
+            print("Removed player %d" % (int(player_id)))
+        return JsonResponse({}, status=204, safe=False)
+    
 
 def register_rfid(request):
     """Call an external program to read rfid and return the value read."""
diff --git a/chronojumpserver-django/chronojump_networks/templates/organizations/players/players_list.html 
b/chronojumpserver-django/chronojump_networks/templates/organizations/players/players_list.html
index a042040..bb8a561 100644
--- a/chronojumpserver-django/chronojump_networks/templates/organizations/players/players_list.html
+++ b/chronojumpserver-django/chronojump_networks/templates/organizations/players/players_list.html
@@ -118,11 +118,24 @@
             $('#addPlayerModalForm').modal('show');
 
           }
+        },
+        { /* Remove Players Button */
+          text: "{% trans 'Remove players' %}",
+          className: "btn btn-danger disabled removePlayerBtn",
+          action: function( e, dt, node, config ) {
+            var player_ids = [];
+            $.each($('.selectPlayerCheckbox:checked'), function(index, value) {
+              var v = $(value);
+                               player_ids.push(v.attr('data-player-id'));
+
+            });
+            removePlayersFromGroup(player_ids);
+          }
         }
       ],
       initComplete: function() {
         // Enable the remove player selection the first time data is loaded
-        //enablePlayersTableFunctionality(); TODO: CHECK if this is needed and implemented
+        enablePlayersTableFunctionality(); //TODO: CHECK if this is needed and implemented
       },
       "language": {
        "lengthMenu": "{% trans 'Showing _MENU_ players per page' %}",
@@ -142,6 +155,35 @@
       }
     });
     
+    /* Buttons operation */
+    function removePlayersFromGroup(player_ids) {
+        console.log("gonna remova player");
+        $.ajax({
+        'url': "{% url 'api_organizations:players_list' organization_id=user.organization.id %}",
+        'method': 'DELETE',
+        'data': {
+          'player_ids': player_ids
+        },
+        success:function(data) {
+          console.log("players removed");
+          // Hide AddPlayerModel dialog
+          var totalChecked = $('.selectPlayerCheckbox:checked').length;
+          if (totalChecked == 1) {
+            alert("{% trans 'The player has been removed' %}");
+          } else {
+            alert("{% trans 'The players have been removed' %}");
+          }
+          table_players.ajax.reload(enablePlayersTableFunctionality, false);
+        }
+      });
+    }
+    
+    function enablePlayersTableFunctionality() {
+      // Enanble checkboxes
+      $('.removePlayerBtn').removeClass('disabled').addClass('disabled');
+      enableCheckboxSelection('selectPlayerCheckbox', 'removePlayerBtn');
+    }
+    
     $('#btn_read_rfid').click(function() {
     console.log('crida script')
         $.ajax({
@@ -199,7 +241,7 @@
             'data': player,
             success: function(data) {
                 alert("The player has been added");
-                table_players.ajax.reload();
+                table_players.ajax.reload(enablePlayersTableFunctionality,false);
                 $('#addPlayerModalForm').modal('hide');
             }
 


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