]> git.ipfire.org Git - ipfire.org.git/commitdiff
accounts: Fix generating avatars on Python 3
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 11 Oct 2018 10:59:39 +0000 (11:59 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 11 Oct 2018 10:59:39 +0000 (11:59 +0100)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/backend/accounts.py
src/templates/talk/modules/channels.html
src/web/handlers_accounts.py
src/web/handlers_base.py

index d322198bedadd52c774b82bc2187e2cd7c1bfc7f..493738f8313dc0fb51c21e1c460892f9e55441a2 100644 (file)
@@ -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.
index c2a1959c6449634220ad04c4c3971130661f479a..25ddd7ed56fcc0881a2adfbff17a2b3a0bf4fdef 100644 (file)
@@ -18,7 +18,7 @@
                                                                        {% elif chan.application == "voicemail" %}
                                                                                {{ _("Voicemail") }}
 
-                                                                       {% else %}
+                                                                       {% elif chan.callee %}
                                                                                {% if chan.callee %}
                                                                                        <a href="/users/{{ chan.callee.uid }}">{{ chan.callee }}</a>
                                                                                {% else %}
@@ -26,6 +26,9 @@
                                                                                {% end %}
                
                                                                                <span class="text-muted">({{ chan.callee_number }})</span>
+
+                                                                       {% else %}
+                                                                               {{ chan.called_number }}
                                                                        {% end %}
                                                                {% elif chan.direction == "outbound" %}
                                                                        {% if chan.caller %}
index 2c74078546594fc0426c368c8e639ead7b4d5fbf..bbf113e2a7f022921e8dfdfd5f7d1b21f252c09d 100644 (file)
@@ -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)
index 4dce9fd2d9a8fdf6a816eb48159e02755836ac7b..2731a915f1df2c6e685fb1c0d7ddfdd838137a62 100644 (file)
@@ -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: