[chronojump-server] Fixed result filters now working as expected



commit b36af55cfa5e02fbb644353c803c505d4e210ceb
Author: Max Ros i Morejon <mros33 gmail com>
Date:   Thu Jan 31 13:55:15 2019 +0100

    Fixed result filters now working as expected

 .../chronojump_networks/results/api/views.py       | 54 ++++++++++++++++++++--
 .../templates/results/results_list.html            | 39 ++++++++++++----
 2 files changed, 79 insertions(+), 14 deletions(-)
---
diff --git a/chronojumpserver-django/chronojump_networks/results/api/views.py 
b/chronojumpserver-django/chronojump_networks/results/api/views.py
index a730680..943278b 100644
--- a/chronojumpserver-django/chronojump_networks/results/api/views.py
+++ b/chronojumpserver-django/chronojump_networks/results/api/views.py
@@ -7,9 +7,11 @@ from rest_framework.response import Response
 from rest_framework.generics import ListAPIView, RetrieveAPIView, DestroyAPIView, 
RetrieveUpdateDestroyAPIView
 from rest_framework.permissions import IsAuthenticated
 
-from chronojump_networks.organizations.models import User, Group, Player, GroupPlayer
+from chronojump_networks.organizations.models import User, Group, Player, GroupPlayer, Station, Exercise, 
Organization, Gym
+
 from datetime import datetime, timedelta
 
+
 class ResultEncoderList(ListAPIView):
     permission_classes = (IsAuthenticated, )
     serializer_class = ResultEncoderSerializer
@@ -20,6 +22,11 @@ class ResultEncoderList(ListAPIView):
         coach_id = int(data['coach_id'][0])
         group_id = str(data['group_id'][0])
         timeInterval = int(data['timeInterval'][0])
+        player_id = str(data['player_id'][0])
+        station_id = str(data['station_id'][0])
+        exercise_id = str(data['exercise_id'][0])
+        
+        #GROUP SELECT
         if group_id.lower() == "all":
             # Get all results from all the groups of the coach
             coach = get_object_or_404(User, pk=coach_id)
@@ -30,16 +37,53 @@ class ResultEncoderList(ListAPIView):
         else:
             players = GroupPlayer.objects.filter(group_id = int(group_id))
             player_list = [p.player_id for p in players]
-
+            
+        #PLAYER SELECT
+        if player_id.lower() != "all":
+            players = Player.objects.filter(id = int(player_id))
+            player_list_aux = [p.id for p in players]
+            player_list = [value for value in player_list if value in player_list_aux]
+            
+        #STATION SELECT
+        if station_id.lower() == "all":
+            # Get all results from all the stations of the coach
+            coach = get_object_or_404(User, pk=coach_id)
+            group_list = [g.group.id for g in coach.groups_by_coach.all()]
+            group_organizations = Organization.objects.filter(groups__in = group_list)
+            organization_list = [gp.id for gp in group_organizations]
+            gym_organization = Gym.objects.filter(organization_id__in = organization_list)
+            gym_list = [gm.id for gm in gym_organization]
+            stations = Station.objects.filter(gym_id__in = gym_list)
+            station_list = [s.id for s in stations]
+        else:
+            stations = Station.objects.filter(id = int(station_id))
+            station_list = [s.id for s in stations]
+            
+        #EXERCISE SELECT
+        if exercise_id.lower() == "all":
+            coach = get_object_or_404(User, pk=coach_id)
+            group_list = [g.group.id for g in coach.groups_by_coach.all()]
+            group_organizations = Organization.objects.filter(groups__in = group_list)
+            organization_list = [gp.id for gp in group_organizations]
+            gym_organization = Gym.objects.filter(organization_id__in = organization_list)
+            gym_list = [gm.id for gm in gym_organization]
+            stations = Station.objects.filter(gym_id__in = gym_list)
+            stations_list = [s.id for s in stations]
+            exercises = Exercise.objects.filter(station_id__in = stations_list)
+            exercise_list = [e.id for e in exercises]
+        else:
+            exercises = Exercise.objects.filter(id = int(exercise_id))
+            exercise_list = [e.id for e in exercises]
+         
+            
         if timeInterval > 0 :
             print(timeInterval)
             d = datetime.now() - timedelta(timeInterval)
             print(d)
-            queryset = ResultEncoder.objects.filter(person__in = player_list,
-            dt__gte = d )
+            queryset = ResultEncoder.objects.filter(person__in = player_list, dt__gte = d, station__in = 
station_list, exercise__in = exercise_list)
         else:
             # No filter by time
-            queryset = ResultEncoder.objects.filter(person__in = player_list)
+            queryset = ResultEncoder.objects.filter(person__in = player_list, station__in = station_list, 
exercise__in = exercise_list)
         # Serialize the results and return
         serializer = ResultEncoderSerializer(queryset, many=True)
         return Response(serializer.data)
diff --git a/chronojumpserver-django/chronojump_networks/templates/results/results_list.html 
b/chronojumpserver-django/chronojump_networks/templates/results/results_list.html
index 8161f67..47ff685 100644
--- a/chronojumpserver-django/chronojump_networks/templates/results/results_list.html
+++ b/chronojumpserver-django/chronojump_networks/templates/results/results_list.html
@@ -46,8 +46,8 @@
       {% endfor %}
     </select>
   </div>
-  <div class="col">
-    <label for="filterByPlayer">{% trans 'Filter by player' %}</label>
+  <div class="col form-group">
+    <label for="playerSelect">{% trans 'Filter by player' %}</label>
     <select name="player" class="form-control" id="playerSelect">
       <option value="all">{% trans 'All players' %}
       {% for player in user.players_by_coach %}
@@ -55,8 +55,8 @@
       {% endfor %}
     </select>
   </div>
-  <div class="col">
-    <label for="filterByStation">{% trans 'Filter by station' %}</label>
+  <div class="col form-group">
+    <label for="stationSelect">{% trans 'Filter by station' %}</label>
     <select name="station" class="form-control" id="stationSelect">
       <option value="all">{% trans 'All stations' %}
       {% for station in user.stations_by_coach %}
@@ -64,8 +64,8 @@
       {% endfor %}
     </select>
   </div>
-  <div class="col">
-    <label for="filterByExercise">{% trans 'Filter by exercise' %}</label>
+  <div class="col form-group">
+    <label for="exerciseSelect">{% trans 'Filter by exercise' %}</label>
     <select name="exercise" class="form-control" id="exerciseSelect">
       <option value="all">{% trans 'All exercises' %}
       {% for exercise in user.exercises_by_coach %}
@@ -121,6 +121,9 @@
         "url" : "{% url 'api_results:results_list' %}",
         data: function(d){
                d.group_id = $('#groupSelect').val();
+               d.player_id = $('#playerSelect').val();
+               d.station_id = $('#stationSelect').val();
+               d.exercise_id = $('#exerciseSelect').val();
                d.org_id = "{{ user.organization.id }}"
                d.coach_id = "{{ user.id }}"
           d.timeInterval = $('#time-interval').val();
@@ -379,7 +382,7 @@
                var idx = column.index();
                if (idx == COLUMN_PLAYER) {
                        var select = $('<select class="form-control"><option value="">{% trans "All players" 
%}</option></select>')
-                       .appendTo($('#filterByPlayer'))
+                       .appendTo($('#playerSelect'))
                        .on('change', function() {
                                // Player selected
                                console.log("canvi player select");
@@ -401,7 +404,7 @@
                        });
                } else if (idx == COLUMN_STATION) {
                        var select = $('<select class="form-control"><option value="">{% trans "All stations" 
%}</option></select>')
-                       .appendTo($('#filterByStation'))
+                       .appendTo($('#stationSelect'))
                        .on('change', function() {
                                var val = $.fn.dataTable.util.escapeRegex(
                                        $(this).val()
@@ -419,7 +422,7 @@
                        });
                } else if (idx == COLUMN_EXERCISE) {
                        var select = $('<select class="form-control"><option value="">{% trans "All 
exercises" %}</option></select>')
-                       .appendTo($('#filterByExercise'))
+                       .appendTo($('#exerciseSelect'))
                        .on('change', function() {
                                var val = $.fn.dataTable.util.escapeRegex(
                                        $(this).val()
@@ -518,6 +521,24 @@
         console.log("Reload by group change");
     });
     
+    /* Reload results for the player selected */
+    $('#playerSelect').on('change', function() {
+       table_results.ajax.reload(enableDeletion, true);
+        console.log("Reload by player change");
+    });
+    
+    /* Reload results for the station selected */
+    $('#stationSelect').on('change', function() {
+       table_results.ajax.reload(enableDeletion, true);
+        console.log("Reload by station change");
+    });
+    
+    /* Reload results for the exercise selected */
+    $('#exerciseSelect').on('change', function() {
+       table_results.ajax.reload(enableDeletion, true);
+        console.log("Reload by exercise change");
+    });
+    
 
     $('.time-interval-btn').on('click', function() {
       $('.time-interval-btn').removeClass('active btn-primary').addClass('btn-secondary');


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