From f6ed3d4df72c04410e706e419cf757f6afad31d2 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Thu, 11 Oct 2018 11:59:39 +0100 Subject: [PATCH] accounts: Fix generating avatars on Python 3 Signed-off-by: Michael Tremer --- src/backend/accounts.py | 22 +++++++-------- src/templates/talk/modules/channels.html | 5 +++- src/web/handlers_accounts.py | 35 +++++++++++------------- src/web/handlers_base.py | 8 ++++++ 4 files changed, 39 insertions(+), 31 deletions(-) diff --git a/src/backend/accounts.py b/src/backend/accounts.py index d322198b..493738f8 100644 --- a/src/backend/accounts.py +++ b/src/backend/accounts.py @@ -177,7 +177,7 @@ class Account(Object): res = self.attributes.get(attr, []) if res: - return res[0].decode() + return res[0] def get(self, key): try: @@ -221,15 +221,15 @@ class Account(Object): @property def uid(self): - return self._get_first_attribute("uid") + return self._get_first_attribute("uid").decode() @property def name(self): - return self._get_first_attribute("cn") + return self._get_first_attribute("cn").decode() @property def first_name(self): - return self._get_first_attribute("givenName") + return self._get_first_attribute("givenName").decode(9) @lazy_property def groups(self): @@ -247,7 +247,7 @@ class Account(Object): @property def address(self): - address = self._get_first_attribute("homePostalAddress", "") + address = self._get_first_attribute("homePostalAddress", "").decode() address = address.replace(", ", "\n") return address @@ -273,14 +273,14 @@ class Account(Object): @property def sip_id(self): if "sipUser" in self.classes: - return self._get_first_attribute("sipAuthenticationUser") + return self._get_first_attribute("sipAuthenticationUser").decode() if "sipRoutingObject" in self.classes: - return self._get_first_attribute("sipLocalAddress") + return self._get_first_attribute("sipLocalAddress").decode() @property def sip_password(self): - return self._get_first_attribute("sipPassword") + return self._get_first_attribute("sipPassword").decode() @property def sip_url(self): @@ -295,7 +295,7 @@ class Account(Object): @property def sip_routing_url(self): if "sipRoutingObject" in self.classes: - return self._get_first_attribute("sipRoutingAddress") + return self._get_first_attribute("sipRoutingAddress").decode() @lazy_property def sip_registrations(self): @@ -349,13 +349,13 @@ class Account(Object): return self._resize_avatar(avatar, size) def _resize_avatar(self, image, size): - image = io.StringIO(image) + image = io.BytesIO(image) image = PIL.Image.open(image) # Resize the image to the desired resolution image.thumbnail((size, size), PIL.Image.ANTIALIAS) - f = io.StringIO() + f = io.BytesIO() # If writing out the image does not work with optimization, # we try to write it out without any optimization. diff --git a/src/templates/talk/modules/channels.html b/src/templates/talk/modules/channels.html index c2a1959c..25ddd7ed 100644 --- a/src/templates/talk/modules/channels.html +++ b/src/templates/talk/modules/channels.html @@ -18,7 +18,7 @@ {% elif chan.application == "voicemail" %} {{ _("Voicemail") }} - {% else %} + {% elif chan.callee %} {% if chan.callee %} {{ chan.callee }} {% else %} @@ -26,6 +26,9 @@ {% end %} ({{ chan.callee_number }}) + + {% else %} + {{ chan.called_number }} {% end %} {% elif chan.direction == "outbound" %} {% if chan.caller %} diff --git a/src/web/handlers_accounts.py b/src/web/handlers_accounts.py index 2c740785..bbf113e2 100644 --- a/src/web/handlers_accounts.py +++ b/src/web/handlers_accounts.py @@ -3,9 +3,9 @@ import logging import tornado.web -from .handlers_base import * +from . import handlers_base as base -class AccountsAvatarHandler(BaseHandler): +class AccountsAvatarHandler(base.BaseHandler): def get(self, who): # Get the desired size of the avatar file size = self.get_argument("size", 0) @@ -15,29 +15,26 @@ class AccountsAvatarHandler(BaseHandler): except (TypeError, ValueError): size = None - # Cache handle - cache_handle = "accounts-avatar-%s-%s" % (who, size or 0) - avatar = self.memcached.get(cache_handle) + logging.debug("Querying for avatar of %s" % who) - if not avatar: - logging.debug("Querying for avatar of %s" % who) - - account = self.backend.accounts.get_by_uid(who) - if not account: - raise tornado.web.HTTPError(404) + # Fetch user account + account = self.backend.accounts.get_by_uid(who) + if not account: + raise tornado.web.HTTPError(404) - avatar = account.get_avatar(size) + # Allow downstream to cache this for 60 minutes + self.set_expires(3600) - # Save the avatar to cache for 6 hours - if avatar: - self.memcached.set(cache_handle, avatar, 6 * 3600) + # Resize avatar + avatar = account.get_avatar(size) - # Otherwise raise 404 (XXX should send a default image) - else: - raise tornado.web.HTTPError(404, "No avatar set for %s" % account) + # If there is no avatar, we serve a default image + if not avatar: + raise tornado.web.HTTPError(404, "No avatar set for %s" % account) - self.set_header("Cache-Control", "public,max-age=300") + # Set headers about content self.set_header("Content-Disposition", "inline; filename=\"%s.jpg\"" % who) self.set_header("Content-Type", "image/jpeg") + # Deliver payload self.finish(avatar) diff --git a/src/web/handlers_base.py b/src/web/handlers_base.py index 4dce9fd2..2731a915 100644 --- a/src/web/handlers_base.py +++ b/src/web/handlers_base.py @@ -12,6 +12,14 @@ import tornado.web from .. import util class BaseHandler(tornado.web.RequestHandler): + def set_expires(self, seconds): + # For HTTP/1.1 + self.set_header("Cache-Control", "max-age=%s, must-revalidate" % seconds) + + # For HTTP/1.0 + expires = datetime.datetime.utcnow() + datetime.timedelta(seconds=seconds) + self.set_header("Expires", expires) + def write_error(self, status_code, **kwargs): # Translate code into message try: -- 2.47.3