From 208a47b41d2d81f95a47c2e59c69310aa3486426 Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Thu, 16 Jul 2020 13:52:18 +0100 Subject: [PATCH] Fix tag views redis cache --- app/blueprints/packages/packages.py | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/app/blueprints/packages/packages.py b/app/blueprints/packages/packages.py index 8cb9b62d..bacbbe1f 100644 --- a/app/blueprints/packages/packages.py +++ b/app/blueprints/packages/packages.py @@ -52,18 +52,20 @@ def list_all(): joinedload(Package.media_license), \ subqueryload(Package.tags)) - edited = False - for tag in qb.tags: - edited = True - key = "tag-" + tag.name - if not has_key(key): - set_key(key, "true") - Tag.query.filter_by(id=tag.id).update({ - "views": Tag.views + 1 - }) + ip = request.headers.get("X-Forwarded-For") or request.remote_addr + if ip is not None: + edited = False + for tag in qb.tags: + edited = True + key = "tag/{}/{}".format(ip, tag.name) + if not has_key(key): + set_key(key, "true") + Tag.query.filter_by(id=tag.id).update({ + "views": Tag.views + 1 + }) - if edited: - db.session.commit() + if edited: + db.session.commit() if qb.lucky: package = query.first()