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):
Returns: The found value
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:
raise KeyError(key.field_name)
......@@ -603,6 +612,7 @@ class FieldDataCache(object):
kv_dict (dict): dict mapping from `DjangoKeyValueStore.Key`s to field values
Raises: DatabaseError if any fields fail to save
"""
saved_fields = []
# field_objects maps a field_object to a list of associated fields
field_objects = dict()
......@@ -645,7 +655,16 @@ class FieldDataCache(object):
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:
raise KeyError(key.field_name)
......@@ -667,7 +686,16 @@ class FieldDataCache(object):
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:
return False
......@@ -676,31 +704,21 @@ class FieldDataCache(object):
else:
return True
def find(self, key):
def find_or_create(self, key):
'''
Look for a model data object using an DjangoKeyValueStore.Key object
key: An `DjangoKeyValueStore.Key` object selecting the object to find
returns the found object, or None if the object doesn't exist
Find a model data object in this cache, or create a new one if it doesn't
exist
'''
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 None
return self.cache[key.scope].get(key)
return
def find_or_create(self, key):
'''
Find a model data object in this cache, or create it if it doesn't
exist
'''
field_object = self.find(key)
field_object = self.cache[key.scope].get(key)
if field_object is not None:
return field_object
......@@ -732,8 +750,5 @@ class FieldDataCache(object):
student_id=key.user_id,
)
if key.scope not in self.cache:
return
self.cache[key.scope].set(key, 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