Compare commits

...

2 commits

2 changed files with 24 additions and 5 deletions

View file

@ -1,11 +1,24 @@
import msgpack import msgpack
import pickle
from django_redis.serializers.base import BaseSerializer from django_redis.serializers.base import BaseSerializer
def default(obj):
# Pickle anything that MessagePack can't handle itself.
return msgpack.ExtType(69, pickle.dumps(obj, protocol=4))
def ext_hook(code, data):
# Unpickle if we pickled - otherwise do nothing.
if code == 69:
return pickle.loads(data)
return msgpack.ExtType(code, data)
class MSGPackModernSerializer(BaseSerializer): class MSGPackModernSerializer(BaseSerializer):
def dumps(self, value): def dumps(self, value):
return msgpack.packb(value, use_bin_type=True) return msgpack.packb(value, default=default, use_bin_type=True)
def loads(self, value): def loads(self, value):
return msgpack.unpackb(value, raw=False) return msgpack.unpackb(value, ext_hook=ext_hook, raw=False)

View file

@ -1,4 +1,5 @@
from django.http import HttpResponse, HttpResponseRedirect from django.http import HttpResponse, HttpResponseRedirect
from django.views.decorators.cache import cache_page
from PIL import Image from PIL import Image
from lemoncurry import utils from lemoncurry import utils
@ -12,6 +13,7 @@ def try_libravatar_org(hash, get):
return HttpResponseRedirect(url) return HttpResponseRedirect(url)
@cache_page(60 * 15)
def libravatar(request, hash): def libravatar(request, hash):
g = request.GET g = request.GET
size = g.get('s', g.get('size', 80)) size = g.get('s', g.get('size', 80))
@ -42,8 +44,12 @@ def libravatar(request, hash):
return try_libravatar_org(hash, g) return try_libravatar_org(hash, g)
im = Image.open(user.avatar) im = Image.open(user.avatar)
im_resized = im.resize((size, size)) image_type = im.format
natural_size = min(im.size)
response = HttpResponse(content_type='image/'+im.format.lower()) im = im.crop((0, 0, natural_size, natural_size))
im_resized.save(response, im.format) im = im.resize((size, size), resample=Image.HAMMING)
response = HttpResponse(content_type='image/'+image_type.lower())
im.save(response, image_type)
return response return response