Skip to content

Commit

Permalink
Avoid TypeErrors when looking for outdated scales.
Browse files Browse the repository at this point in the history
Fixes #12
  • Loading branch information
mauritsvanrees committed Oct 23, 2016
1 parent 250beec commit 543a284
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ New:

Fixes:

- Avoid TypeErrors when looking for outdated scales.
Fixes `issue 12 <https://github.com/plone/plone.scale/issues/12>`_.
[maurits]

- Catch KeyError when deleting non existing scale. This can happen in corner cases.
Fixes `issue 15 <https://github.com/plone/plone.scale/issues/15>`_.
[maurits]
Expand Down
25 changes: 19 additions & 6 deletions plone/scale/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,21 @@ def __init__(self, context, modified=None):
self.context = context
self.modified = modified

def _modified_since(self, since):
def _modified_since(self, since, offset=0):
if since is None:
return False
elif self.modified_time is None:
modified_time = self.modified_time
if modified_time is None:
return False
else:
return self.modified_time > since
# We expect either a float or an int, but in corner cases it can be
# something else entirely.
# https://github.com/plone/plone.scale/issues/12
if not isinstance(modified_time, (float, int)):
return False
if not isinstance(since, (float, int)):
return False
since = since - offset
return modified_time > since

@property
def modified_time(self):
Expand Down Expand Up @@ -176,14 +184,19 @@ def scale(self, factory=None, **parameters):
def _cleanup(self):
storage = self.storage
modified_time = self.modified_time
if modified_time is None:
return
if not isinstance(modified_time, (float, int)):
# https://github.com/plone/plone.scale/issues/12
return
for key, value in storage.items():
# remove info stored by tuple keys
# before refactoring
if isinstance(key, tuple):
del self[key]
# clear cache from scales older than one day
elif (modified_time and
value['modified'] < modified_time - KEEP_SCALE_MILLIS):
elif self._modified_since(
value['modified'], offset=KEEP_SCALE_MILLIS):
del self[key]

def __getitem__(self, uid):
Expand Down

0 comments on commit 543a284

Please sign in to comment.