changeset 577ee57b2b57 in www.tryton.org:default
details: https://hg.tryton.org/www.tryton.org?cmd=changeset;node=577ee57b2b57
description:
        Avoid small cycles in success stories and show first those with story
diffstat:

 app.py |  18 +++++++++++++-----
 1 files changed, 13 insertions(+), 5 deletions(-)

diffs (49 lines):

diff -r 7a93fcd4c581 -r 577ee57b2b57 app.py
--- a/app.py    Thu Jul 18 22:48:55 2019 +0200
+++ b/app.py    Fri Jul 19 00:48:28 2019 +0200
@@ -9,7 +9,7 @@
 from functools import partial
 from http import HTTPStatus
 from logging.handlers import SMTPHandler
-from random import shuffle, choice
+from random import sample, shuffle
 from urllib.parse import urlparse
 
 import requests
@@ -239,6 +239,13 @@
     def logo(self):
         return 'images/success-stories/%s.jpg' % self.name
 
+    def __eq__(self, other):
+        if isinstance(other, str):
+            return self.name == other
+        elif isinstance(other, Case):
+            return self.name == other.name
+        return NotImplemented
+
 
 CASES = [
     Case(title="ALS Swiss",
@@ -303,8 +310,9 @@
 @app.route('/success-stories')
 @cache.cached()
 def success_stories():
-    shuffle(CASES)
-    return render_template('success_stories.html', cases=CASES)
+    cases = sorted(
+        sample(CASES, len(CASES)), key=lambda c: c.story, reverse=True)
+    return render_template('success_stories.html', cases=cases)
 
 
 @app.route('/business-cases.html', endpoint='success_stories-alt')
@@ -315,8 +323,8 @@
 @app.route('/success-stories/<story>')
 @cache.cached()
 def success_story(story):
-    next_case = choice(
-        [case for case in CASES if case.url and case.name != story])
+    cases = [c for c in CASES if c.story or c.name == story]
+    next_case = cases[(cases.index(story) + 1) % len(cases)]
     try:
         return render_template(
             'success_stories/%s.html' % story, next_case=next_case)

Reply via email to