Explorar el Código

Added option to sort last seums by score or recentness

Denis Merigoux hace 9 años
padre
commit
9db41a65e1

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

@@ -0,0 +1,20 @@
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,6 +15,8 @@ class Counter(models.Model):
15 15
                              verbose_name='utilisateur associé')
16 16
     email_notifications = models.BooleanField(
17 17
         'notifications par email', default=False)
18
+    sort_by_score = models.BooleanField(
19
+        'trier par SeumScore™', default=True)
18 20
 
19 21
     def __str__(self):
20 22
         return '%s (%s)' % (self.trigramme, self.name)

+ 7 - 0
counter/templates/homeTemplate.html

@@ -211,6 +211,13 @@
211 211
 		Activer les notifications par mail
212 212
 		{% endif %}
213 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 221
 </div>
215 222
 
216 223
 <script>

+ 2 - 0
counter/urls.py

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

+ 24 - 12
counter/views.py

@@ -123,18 +123,22 @@ def home(request):
123 123
             counter.lastReset.delta, locale='fr', threshold=1)
124 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 142
     if (len(lastResets) == 0):
139 143
         noGraph = True
140 144
         col_chart = None
@@ -466,6 +470,14 @@ def toggleEmailNotifications(request):
466 470
 
467 471
 
468 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 481
 def like(request):
470 482
     if (request.method == 'POST'):
471 483
         # create a form instance and populate it with data from the request: