Просмотр исходного кода

Improved handling of no-seum cases and fixed a limit for JSS

Denis Merigoux лет назад: 9
Родитель
Сommit
4f07724516
4 измененных файлов с 88 добавлено и 28 удалено
  1. 7 1
      counter/templates/counterTemplate.html
  2. 13 1
      counter/templates/homeTemplate.html
  3. 0 1
      counter/urls.py
  4. 68 25
      counter/views.py

+ 7 - 1
counter/templates/counterTemplate.html

@@ -41,7 +41,13 @@
41 41
           <h2 class="panel-title">Timeline du seum</h2>
42 42
         </div>
43 43
         <div class="graphs timeline panel-body">
44
-          {{chart.as_html}}
44
+            {% if counter.lastReset.noSeum %}
45
+            <div class="text-center text-muted">
46
+                <p>Pas encore de timeline du seum...</p>
47
+            </div>
48
+            {% else %}
49
+            {{chart.as_html}}
50
+            {% endif %}
45 51
         </div>
46 52
       </div>
47 53
     </div>

+ 13 - 1
counter/templates/homeTemplate.html

@@ -6,7 +6,7 @@
6 6
 	<div class="row">
7 7
 		{% for counter in counters %}
8 8
 		<div class="col-md-4 col-sm-6 col-lg-3">
9
-			<div class="panel panel-primary">
9
+			<div class="panel panel-{{counter.CSSclass}}">
10 10
 				<div class="panel-heading">
11 11
 					<a class="counter-link" href="{% url 'counter' id_counter=counter.id %}"><h2 class="panel-title">{{ counter.trigramme }} <small>{{ counter.name }}</small></h2></a>
12 12
 				</div>
@@ -45,7 +45,13 @@
45 45
 					<h2 class="panel-title">Graphe du seum</h2>
46 46
 				</div>
47 47
 				<div class="panel-body graphs">
48
+					{% if noGraph %}
49
+					<div class="text-center text-muted">
50
+						<p>Personne n'a le seum...</p>
51
+					</div>
52
+					{% else %}
48 53
 					{{ col_chart.as_html }}
54
+					{% endif %}
49 55
 				</div>
50 56
 			</div>
51 57
 		</div>
@@ -57,7 +63,13 @@
57 63
 					<h2 class="panel-title">Timeline du seum</h2>
58 64
 				</div>
59 65
 				<div class="panel-body timeline graphs">
66
+					{% if noTimeline %}
67
+					<div class="text-center text-muted">
68
+						<p>Pas de seum dans les dernières 24 heures...</p>
69
+					</div>
70
+					{% else %}
60 71
 					{{ line_chart.as_html }}
72
+					{% endif %}
61 73
 				</div>
62 74
 			</div>
63 75
 		</div>

+ 0 - 1
counter/urls.py

@@ -4,7 +4,6 @@ from . import views
4 4
 
5 5
 urlpatterns = [
6 6
     url(r'^$', views.home,name="home"),
7
-    url(r'^/', views.home),
8 7
     url(r'^reset-counter/',views.resetCounter,name="reset-counter"),
9 8
     url(r'^counter/(?P<id_counter>\d+)$', views.counter, name="counter"),
10 9
 ]

+ 68 - 25
counter/views.py

@@ -16,8 +16,9 @@ class resetCounterForm(forms.ModelForm):
16 16
         model = Reset
17 17
         fields = ['reason','counter']
18 18
 
19
-# Create your views here.
20 19
 def home(request):
20
+    #JSS above this limit will not be displayed on the col graph
21
+    JSS_limit = 7
21 22
     #Display counters
22 23
     counters = Counter.objects.all()
23 24
     lastResets = []
@@ -30,41 +31,83 @@ def home(request):
30 31
             counter.lastReset = Reset()
31 32
             counter.lastReset.delta = timezero
32 33
             counter.lastReset.noSeum = True
34
+            counter.CSSclass = "warning"
33 35
         else:
34 36
             counter.lastReset = lastReset[0]
35 37
             counter.lastReset.noSeum = False
36 38
             counter.lastReset.delta = datetime.now()-counter.lastReset.timestamp.replace(tzinfo=None)
37
-            lastResets.append([counter.trigramme,{'v' : (counter.lastReset.delta.total_seconds())/(24*3600), 'f' : str(round((counter.lastReset.delta.total_seconds())/(24*3600),1))} ])
38
-            if (counter.lastReset.delta.total_seconds())/(24*3600) > maxJSS:
39
-                maxJSS = (counter.lastReset.delta.total_seconds())/(24*3600)
40
-            counter.lastReset.formatted_delta = format_timedelta(counter.lastReset.delta,locale='fr',threshold=1)
39
+            if ((counter.lastReset.delta.total_seconds())/(24*3600)<JSS_limit):
40
+                #If more thant 7 JSS do not display on graph
41
+                lastResets.append([counter.trigramme,{'v' : (counter.lastReset.delta.total_seconds())/(24*3600), 'f' : str(round((counter.lastReset.delta.total_seconds())/(24*3600),1))} ])
42
+                counter.CSSclass = "primary"
43
+                if (counter.lastReset.delta.total_seconds())/(24*3600) > maxJSS:
44
+                    maxJSS = (counter.lastReset.delta.total_seconds())/(24*3600)
45
+                counter.lastReset.formatted_delta = format_timedelta(counter.lastReset.delta,locale='fr',threshold=1)
46
+            else:
47
+                counter.CSSclass = "danger"
41 48
         counter.isHidden = "hidden"
42
-    counters = sorted(counters,key=lambda t: -t.lastReset.delta)
49
+    counters = sorted(counters,key=lambda t: t.lastReset.delta)
43 50
     #Column graph
44
-    lastResets.sort(key=lambda x: x[1]['v'])
45
-    lastResets.insert(0,['Trigramme','Jours sans seum'])
46
-    col_data = SimpleDataSource(lastResets)
47
-    col_chart = gchart.ColumnChart(col_data,options={'title' : '', 'legend' : 'none','vAxis' : { 'viewWindow' : { 'max' : max(maxJSS,1) , 'min' : 0} , 'ticks' : [1,2,3,4,5,6,7,8,9,10,11,12,13,14],'title' : 'Jours sans seum' }, 'hAxis' : {'title' : 'Trigramme' }})
51
+    if (len(lastResets) ==0):
52
+        noGraph = True
53
+        col_chart = None
54
+    else:
55
+        noGraph = False
56
+        lastResets.sort(key=lambda x: x[1]['v'])
57
+        lastResets.insert(0,['Trigramme','Jours sans seum'])
58
+        col_data = SimpleDataSource(lastResets)
59
+        col_chart = gchart.ColumnChart(col_data,options={
60
+            'title' : '',
61
+            'legend' : 'none',
62
+            'vAxis' : {
63
+                'viewWindow' : {
64
+                    'max' : max(maxJSS,1) ,
65
+                    'min' : 0
66
+                    },
67
+                'ticks' : [1,2,3,4,5,6,7],
68
+                'title' : 'Jours sans seum'
69
+                },
70
+            'hAxis' : {'title' : 'Trigramme' },
71
+        })
48 72
 
49 73
     ###Timeline graph
50 74
     #Data pre-processing
51 75
     resets = Reset.objects.filter(timestamp__gte=timezone.now() - timedelta(days=1))
52
-    for reset in resets:
53
-        reset.timestamp={'v' : reset.timestamp.timestamp(), 'f' : "Il y a "+format_timedelta(datetime.now()-reset.timestamp.replace(tzinfo=None),locale='fr',threshold=1) }
54
-        reset.Seum={'v' : 0, 'f' : reset.counter.trigramme+" : "+reset.reason}
55
-    #Drawing the graph
56
-    line_data = ModelDataSource(resets,fields=['timestamp','Seum'])
57
-    line_chart = gchart.LineChart(line_data, options={
58
-        'lineWidth' : 0,
59
-        'pointSize' : 10,
60
-        'title' : '',
61
-        'vAxis' : { 'ticks' : []},
62
-        'hAxis' : {'ticks' : [{'v' : (datetime.now() - timedelta(days=1)).timestamp(), 'f' : 'Il y a 24 h' }, { 'v' :datetime.now().timestamp(), 'f' : 'Présent'}]},
63
-        'legend' : 'none',
64
-        'height' : 90
65
-    })
76
+    if (resets.count() == 0):
77
+        noTimeline = True
78
+        line_chart = None
79
+    else:
80
+        noTimeline = False
81
+        for reset in resets:
82
+            reset.timestamp={
83
+                'v' : reset.timestamp.timestamp(),
84
+                'f' : "Il y a "+format_timedelta(datetime.now()-reset.timestamp.replace(tzinfo=None),locale='fr',threshold=1)
85
+                }
86
+            reset.Seum={'v' : 0, 'f' : reset.counter.trigramme+" : "+reset.reason}
87
+            #Drawing the graph
88
+            line_data = ModelDataSource(resets,fields=['timestamp','Seum'])
89
+            line_chart = gchart.LineChart(line_data, options={
90
+            'lineWidth' : 0,
91
+            'pointSize' : 10,
92
+            'title' : '',
93
+            'vAxis' : { 'ticks' : []},
94
+            'hAxis' : {
95
+                'ticks' : [
96
+                        {'v' : (datetime.now() - timedelta(days=1)).timestamp(), 'f' : 'Il y a 24 h' },
97
+                        { 'v' :datetime.now().timestamp(), 'f' : 'Présent'}
98
+                    ]
99
+                },
100
+            'legend' : 'none',
101
+            'height' : 90
102
+            })
66 103
 
67
-    return render(request,'homeTemplate.html', {'counters' : counters, 'col_chart' : col_chart, 'line_chart' : line_chart})
104
+    return render(request,'homeTemplate.html', {
105
+        'counters' : counters,
106
+        'col_chart' : col_chart,
107
+        'line_chart' : line_chart,
108
+        'noTimeline' : noTimeline,
109
+        'noGraph' : noGraph
110
+        })
68 111
 
69 112
 def resetCounter(request):
70 113
     #Update Form counter