Commit d1fae17c by Calen Pennington

Move the logic from `find` into the methods that use it

parent 3bcd5ceb
...@@ -584,7 +584,16 @@ class FieldDataCache(object): ...@@ -584,7 +584,16 @@ class FieldDataCache(object):
Returns: The found value Returns: The found value
Raises: KeyError if key isn't found in the cache Raises: KeyError if key isn't found in the cache
''' '''
field_object = self.find(key)
if key.scope.user == UserScope.ONE and not self.user.is_anonymous():
# If we're getting user data, we expect that the key matches the
# user we were constructed for.
assert key.user_id == self.user.id
if key.scope not in self.cache:
raise KeyError(key.field_name)
field_object = self.cache[key.scope].get(key)
if field_object is None: if field_object is None:
raise KeyError(key.field_name) raise KeyError(key.field_name)
...@@ -603,6 +612,7 @@ class FieldDataCache(object): ...@@ -603,6 +612,7 @@ class FieldDataCache(object):
kv_dict (dict): dict mapping from `DjangoKeyValueStore.Key`s to field values kv_dict (dict): dict mapping from `DjangoKeyValueStore.Key`s to field values
Raises: DatabaseError if any fields fail to save Raises: DatabaseError if any fields fail to save
""" """
saved_fields = [] saved_fields = []
# field_objects maps a field_object to a list of associated fields # field_objects maps a field_object to a list of associated fields
field_objects = dict() field_objects = dict()
...@@ -645,7 +655,16 @@ class FieldDataCache(object): ...@@ -645,7 +655,16 @@ class FieldDataCache(object):
Raises: KeyError if key isn't found in the cache Raises: KeyError if key isn't found in the cache
""" """
field_object = self.find(key)
if key.scope.user == UserScope.ONE and not self.user.is_anonymous():
# If we're getting user data, we expect that the key matches the
# user we were constructed for.
assert key.user_id == self.user.id
if key.scope not in self.cache:
raise KeyError(key.field_name)
field_object = self.cache[key.scope].get(key)
if field_object is None: if field_object is None:
raise KeyError(key.field_name) raise KeyError(key.field_name)
...@@ -667,7 +686,16 @@ class FieldDataCache(object): ...@@ -667,7 +686,16 @@ class FieldDataCache(object):
Returns: bool Returns: bool
""" """
field_object = self.find(key)
if key.scope.user == UserScope.ONE and not self.user.is_anonymous():
# If we're getting user data, we expect that the key matches the
# user we were constructed for.
assert key.user_id == self.user.id
if key.scope not in self.cache:
return False
field_object = self.cache[key.scope].get(key)
if field_object is None: if field_object is None:
return False return False
...@@ -676,31 +704,21 @@ class FieldDataCache(object): ...@@ -676,31 +704,21 @@ class FieldDataCache(object):
else: else:
return True return True
def find_or_create(self, key):
def find(self, key):
''' '''
Look for a model data object using an DjangoKeyValueStore.Key object Find a model data object in this cache, or create a new one if it doesn't
exist
key: An `DjangoKeyValueStore.Key` object selecting the object to find
returns the found object, or None if the object doesn't exist
''' '''
if key.scope.user == UserScope.ONE and not self.user.is_anonymous(): if key.scope.user == UserScope.ONE and not self.user.is_anonymous():
# If we're getting user data, we expect that the key matches the # If we're getting user data, we expect that the key matches the
# user we were constructed for. # user we were constructed for.
assert key.user_id == self.user.id assert key.user_id == self.user.id
if key.scope not in self.cache: if key.scope not in self.cache:
return None return
return self.cache[key.scope].get(key)
def find_or_create(self, key): field_object = self.cache[key.scope].get(key)
'''
Find a model data object in this cache, or create it if it doesn't
exist
'''
field_object = self.find(key)
if field_object is not None: if field_object is not None:
return field_object return field_object
...@@ -732,8 +750,5 @@ class FieldDataCache(object): ...@@ -732,8 +750,5 @@ class FieldDataCache(object):
student_id=key.user_id, student_id=key.user_id,
) )
if key.scope not in self.cache:
return
self.cache[key.scope].set(key, field_object) self.cache[key.scope].set(key, field_object)
return field_object return field_object
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment