From 12b8d52ef42d25e541076d77baac67a761efef78 Mon Sep 17 00:00:00 2001 From: Stephen Martinis Date: Tue, 16 Sep 2014 16:18:16 -0700 Subject: [PATCH] Fixed Anon user singleton. --- server/app/auth.py | 6 ++++-- server/app/models.py | 10 ++++++++-- server/tests/test_permissions.py | 2 +- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/server/app/auth.py b/server/app/auth.py index a9c2ff5ac..38ad0b3fd 100644 --- a/server/app/auth.py +++ b/server/app/auth.py @@ -1,8 +1,10 @@ """Convert access tokens to user records.""" +# Because pylint doesn't understand memcache for some reason +# pylint: disable=no-member + from app import models from app import app -from app.utils import create_api_response from app.authenticator import AuthenticationException from google.appengine.api import memcache as mc @@ -21,7 +23,7 @@ def authenticate(): return models.User.get_or_insert(user.email()) if 'access_token' not in request.args: - return models.AnonymousUser + return models.AnonymousUser() else: access_token = request.args['access_token'] user = mc.get("%s-%s" % (MC_NAMESPACE, access_token)) diff --git a/server/app/models.py b/server/app/models.py index 1e4981b3a..d8fa28638 100644 --- a/server/app/models.py +++ b/server/app/models.py @@ -190,10 +190,16 @@ def put(self, *args, **kwds): @classmethod def get_or_insert(cls, *args, **kwds): - return super(AnonymousUser, cls).get_or_insert(*args, **kwds) + return super(_AnonUserClass, cls).get_or_insert(*args, **kwds) +_AnonUserClass = AnonymousUser +_AnonUser = None -AnonymousUser = AnonymousUser.get_or_insert("anon_user") +def AnonymousUser(): + global _AnonUser + if not _AnonUser: + _AnonUser = _AnonUserClass.get_or_insert("anon_user") + return _AnonUser class Assignment(Base): diff --git a/server/tests/test_permissions.py b/server/tests/test_permissions.py index 526db95fc..2c9f64d35 100755 --- a/server/tests/test_permissions.py +++ b/server/tests/test_permissions.py @@ -60,7 +60,7 @@ def get_accounts(self): login="albert", role=ADMIN_ROLE ), - "anon": models.AnonymousUser, + "anon": models.AnonymousUser(), } def enroll(self, student, course):