Kaynağa Gözat

Added option to sort last seums by score or recentness

Denis Merigoux 9 yıl önce
ebeveyn
işleme
9db41a65e1

+ 20 - 0
counter/migrations/0006_counter_sort_by_score.py

1
+# -*- coding: utf-8 -*-
2
+# Generated by Django 1.10.4 on 2017-01-07 16:49
3
+from __future__ import unicode_literals
4
+
5
+from django.db import migrations, models
6
+
7
+
8
+class Migration(migrations.Migration):
9
+
10
+    dependencies = [
11
+        ('counter', '0005_auto_20170103_1628'),
12
+    ]
13
+
14
+    operations = [
15
+        migrations.AddField(
16
+            model_name='counter',
17
+            name='sort_by_score',
18
+            field=models.BooleanField(default=True, verbose_name='trier par SeumScore™'),
19
+        ),
20
+    ]

+ 2 - 0
counter/models.py

15
                              verbose_name='utilisateur associé')
15
                              verbose_name='utilisateur associé')
16
     email_notifications = models.BooleanField(
16
     email_notifications = models.BooleanField(
17
         'notifications par email', default=False)
17
         'notifications par email', default=False)
18
+    sort_by_score = models.BooleanField(
19
+        'trier par SeumScore™', default=True)
18
 
20
 
19
     def __str__(self):
21
     def __str__(self):
20
         return '%s (%s)' % (self.trigramme, self.name)
22
         return '%s (%s)' % (self.trigramme, self.name)

+ 7 - 0
counter/templates/homeTemplate.html

211
 		Activer les notifications par mail
211
 		Activer les notifications par mail
212
 		{% endif %}
212
 		{% endif %}
213
 	</a>
213
 	</a>
214
+	<a href="{% url 'toggle_sort_score' %}" class="btn btn-success">
215
+		{% if myCounter.sort_by_score %}
216
+		Trier les seums par ancienneté
217
+		{% else %}
218
+		Trier les seums par score
219
+		{% endif %}
220
+	</a>
214
 </div>
221
 </div>
215
 
222
 
216
 <script>
223
 <script>

+ 2 - 0
counter/urls.py

14
     url(r'^like/$', views.like, name='like'),
14
     url(r'^like/$', views.like, name='like'),
15
     url(r'^toggle-notif/$', views.toggleEmailNotifications,
15
     url(r'^toggle-notif/$', views.toggleEmailNotifications,
16
         name='toggle_email_notifications'),
16
         name='toggle_email_notifications'),
17
+    url(r'^toggle-sort-score/$', views.toggleScoreSorting,
18
+        name='toggle_sort_score'),
17
     url(r'^login/$', auth_views.login,
19
     url(r'^login/$', auth_views.login,
18
         {'template_name': 'login.html'},
20
         {'template_name': 'login.html'},
19
         name='login'),
21
         name='login'),

+ 24 - 12
counter/views.py

123
             counter.lastReset.delta, locale='fr', threshold=1)
123
             counter.lastReset.delta, locale='fr', threshold=1)
124
         counter.isHidden = 'hidden'
124
         counter.isHidden = 'hidden'
125
 
125
 
126
-    # Now we sort the counters according to a reddit-like ranking formula
127
-    # We take into account the number of likes of a reset and its recentness
128
-    # The log on the score will give increased value to the first likes
129
-    # The negative exp for the time with a characteristic time of 1 day will
130
-    # cause that after 1 day the « recentness score drops from 1 to 0.36
131
-    # The counters with no seum have a like count of -1 by convention
132
-    counters = sorted(counters, key=lambda t: - (
133
-                      math.log(t.likeCount + 2) /
134
-                      (1 + (t.lastReset.delta.total_seconds()) /
135
-                       (24 * 3600))))
136
-
137
-    # Column graph
126
+    if myCounter.sort_by_score:
127
+        # Now we sort the counters according to a reddit-like ranking formula
128
+        # We take into account the number of likes of a reset and its recentness
129
+        # The log on the score will give increased value to the first likes
130
+        # The negative exp for the time with a characteristic time of 1 day will
131
+        # cause that after 1 day the « recentness score drops from 1 to 0.36
132
+        # The counters with no seum have a like count of -1 by convention
133
+        counters = sorted(counters, key=lambda t: - (
134
+            math.log(t.likeCount + 2) /
135
+            (1 + (t.lastReset.delta.total_seconds()) /
136
+             (24 * 3600))))
137
+    else:
138
+        counters = sorted(counters, key=lambda t: +
139
+                          t.lastReset.delta.total_seconds())
140
+
141
+        # Column graph
138
     if (len(lastResets) == 0):
142
     if (len(lastResets) == 0):
139
         noGraph = True
143
         noGraph = True
140
         col_chart = None
144
         col_chart = None
466
 
470
 
467
 
471
 
468
 @login_required
472
 @login_required
473
+def toggleScoreSorting(request):
474
+    counter = Counter.objects.get(user=request.user)
475
+    counter.sort_by_score = not counter.sort_by_score
476
+    counter.save()
477
+    return HttpResponseRedirect(reverse('home'))
478
+
479
+
480
+@login_required
469
 def like(request):
481
 def like(request):
470
     if (request.method == 'POST'):
482
     if (request.method == 'POST'):
471
         # create a form instance and populate it with data from the request:
483
         # create a form instance and populate it with data from the request: