Skip to content

Instantly share code, notes, and snippets.

@groovecoder
Created December 7, 2012 18:07
Show Gist options
  • Save groovecoder/4235149 to your computer and use it in GitHub Desktop.
Save groovecoder/4235149 to your computer and use it in GitHub Desktop.
def _document_last_modified(request, document_slug, document_locale):
"""Utility function to derive the last modified timestamp of a document.
Mainly for the @condition decorator."""
nk = u'/'.join((document_locale, document_slug))
nk_hash = hashlib.md5(nk.encode('utf8')).hexdigest()
cache_key = DOCUMENT_LAST_MODIFIED_CACHE_KEY_TMPL % nk_hash
try:
last_mod = cache.get(cache_key)
if not last_mod:
doc = Document.objects.get(locale=document_locale,
slug=document_slug)
# Convert python datetime to Unix epoch seconds. This is more
# easily digested by the cache, and is more compatible with other
# services that might spy on Kuma's cache entries (eg. KumaScript)
last_mod = doc.modified.strftime('%s')
cache.set(cache_key, last_mod)
# Convert the cached Unix epoch seconds back to Python datetime
return datetime.fromtimestamp(float(last_mod))
except Document.DoesNotExist:
return None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment