import random
import re
import string
+import unicodedata
def parse_search_query(query):
q = []
r = (random.choice(input_chars) for i in range(length))
return "".join(r)
+
+def normalize(s):
+ # Remove any non-ASCII characters
+ try:
+ s = unicodedata.normalize("NFKD", s)
+ except TypeError:
+ pass
+
+ # Remove excessive whitespace
+ s = re.sub(r"[^\w]+", " ", s)
+
+ return "-".join(s.split())
return bytes(res.data)
def get_thumbnail(self, size):
+ cache_key = "-".join((self.path, util.normalize(self.filename), self.created_at.isoformat(), "%spx" % size))
+
+ # Try to fetch the data from the cache
+ thumbnail = self.memcache.get(cache_key)
+ if thumbnail:
+ return thumbnail
+
+ # Generate the thumbnail
+ thumbnail = self._generate_thumbnail(size)
+
+ # Put it into the cache for forever
+ self.memcache.set(cache_key, thumbnail)
+
+ return thumbnail
+
+ def _generate_thumbnail(self, size):
image = PIL.Image.open(io.BytesIO(self.blob))
# Resize the image to the desired resolution