]> git.ipfire.org Git - people/shoehn/ipfire.org.git/blobdiff - webapp/handlers_iuse.py
Donation page responsivness
[people/shoehn/ipfire.org.git] / webapp / handlers_iuse.py
index 5d3c83c0cfbea5f3d913fe5308634ba3af914bc0..4fce56d8926a760b792ceb64a9d35a0cf5d36900 100644 (file)
@@ -1,5 +1,6 @@
 #!/usr/bin/python
 
+import datetime
 import logging
 import os
 import random
@@ -9,15 +10,9 @@ from handlers_base import *
 import backend
 
 class IUseImage(BaseHandler):
-       @property
-       def iuse(self):
-               return backend.IUse()
+       expires = 3600 * 3
 
-       @property
-       def stasy(self):
-               return backend.Stasy()
-
-       def get_error_html(self, status_code, **kwargs):
+       def write_error(self, status_code, **kwargs):
                """
                        Select a random image from the errors directory
                        and return the content.
@@ -32,14 +27,22 @@ class IUseImage(BaseHandler):
                        image = random.choice(images)
                        image = os.path.join(template_path, image)
 
+                       imgdata = None
                        with open(image, "rb") as f:
-                               return f.read()
+                               imgdata = f.read()
+
+                       self.finish(imgdata)
 
        def get(self, profile_id, image_id):
                image = None
+
+               when = self.get_argument_date("when", None)
+
                # Try to get the image from memcache. If we have a cache miss we
                # build a new one.
                mem_id = "iuse-%s-%s-%s" % (profile_id, image_id, self.locale.code)
+               if when:
+                       mem_id += "-%s" % when.isoformat()
 
                cache = self.get_argument("cache", "true")
                if cache == "true":
@@ -50,20 +53,25 @@ class IUseImage(BaseHandler):
                else:
                        logging.info("Rendering new image for profile: %s" % profile_id)
 
+                       profile = self.fireinfo.get_profile_with_data(profile_id, when=when)
+                       if not profile:
+                               raise tornado.web.HTTPError(404, "Profile '%s' was not found." % profile_id)
+
                        image_cls = self.iuse.get_imagetype(image_id)
                        if not image_cls:
                                raise tornado.web.HTTPError(404, "Image class is unknown: %s" % image_id)
 
-                       profile = self.stasy.get_profile(profile_id)
-                       if not profile:
-                               raise tornado.web.HTTPError(404, "Profile '%s' was not found." % profile_id)
-
                        # Render the image
-                       image = image_cls(self, profile).to_string()
+                       image = image_cls(self.backend, self, profile).to_string()
 
-                       # Save the image to the memcache for 15 minutes
-                       self.memcached.set(mem_id, image, 15*60)
+                       # Save the image to the memcache
+                       self.memcached.set(mem_id, image, self.expires)
 
                self.set_header("Content-Type", "image/png")
-               self.write(image)
 
+               # Set expiry headers
+               self.set_header("Expires",
+                       datetime.datetime.utcnow() + datetime.timedelta(seconds=self.expires))
+               self.set_header("Cache-Control", "public,max-age=%d" % self.expires)
+
+               self.write(image)