From 7a24f2a307229f70ddcee0e4d99df55ded89940c Mon Sep 17 00:00:00 2001 From: zsloan Date: Tue, 19 Jul 2016 19:26:51 +0000 Subject: Now correctly displays number of collections in top menu bar for non-logged in users --- wqflask/wqflask/templates/base.html | 2 ++ wqflask/wqflask/user_manager.py | 22 +++++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) (limited to 'wqflask') diff --git a/wqflask/wqflask/templates/base.html b/wqflask/wqflask/templates/base.html index 759c4a8d..6af07edc 100644 --- a/wqflask/wqflask/templates/base.html +++ b/wqflask/wqflask/templates/base.html @@ -43,6 +43,8 @@ Collections {% if g.user_session.user_ob %} {{ g.user_session.user_ob.display_num_collections() }} + {% else %} + {{ g.cookie_session.display_num_collections() }} {% endif %} diff --git a/wqflask/wqflask/user_manager.py b/wqflask/wqflask/user_manager.py index e91c7327..31754ff5 100644 --- a/wqflask/wqflask/user_manager.py +++ b/wqflask/wqflask/user_manager.py @@ -99,6 +99,25 @@ class AnonUser(object): return {} else: return json.loads(collections) + + def display_num_collections(self): + """ + Returns the number of collections or a blank string if there are zero. + + Because this is so unimportant...we wrap the whole thing in a try/expect...last thing we + want is a webpage not to be displayed because of an error here + + Importand TODO: use redis to cache this, don't want to be constantly computing it + """ + try: + num = len(self.get_collections().keys()) + if num > 0: + return num + else: + return "" + except Exception as why: + print("Couldn't display_num_collections:", why) + return "" def verify_cookie(cookie): the_uuid, separator, the_signature = cookie.partition(':') @@ -186,7 +205,8 @@ class UserSession(object): @app.before_request def before_request(): g.user_session = UserSession() - + g.cookie_session = AnonUser() + class UsersManager(object): def __init__(self): self.users = model.User.query.all() -- cgit v1.2.3