]>
git.ipfire.org Git - ipfire.org.git/blob - src/web/handlers_iuse.py
9 from .handlers_base
import *
11 class IUseImage(BaseHandler
):
14 def write_error(self
, status_code
, **kwargs
):
16 Select a random image from the errors directory
17 and return the content.
19 self
.set_header("Content-Type", "image/png")
21 template_path
= self
.application
.settings
.get("template_path", "")
22 template_path
= os
.path
.join(template_path
, "i-use", "errors")
24 images
= os
.listdir(template_path
)
26 image
= random
.choice(images
)
27 image
= os
.path
.join(template_path
, image
)
30 with
open(image
, "rb") as f
:
35 def get(self
, profile_id
, image_id
):
38 when
= self
.get_argument_date("when", None)
40 # Try to get the image from memcache. If we have a cache miss we
42 mem_id
= "iuse-%s-%s-%s" % (profile_id
, image_id
, self
.locale
.code
)
44 mem_id
+= "-%s" % when
.isoformat()
46 cache
= self
.get_argument("cache", "true")
48 image
= self
.memcached
.get(mem_id
)
51 logging
.debug("Got image from cache for profile: %s" % profile_id
)
53 logging
.info("Rendering new image for profile: %s" % profile_id
)
55 profile
= self
.fireinfo
.get_profile_with_data(profile_id
, when
=when
)
57 raise tornado
.web
.HTTPError(404, "Profile '%s' was not found." % profile_id
)
59 image_cls
= self
.iuse
.get_imagetype(image_id
)
61 raise tornado
.web
.HTTPError(404, "Image class is unknown: %s" % image_id
)
64 image
= image_cls(self
.backend
, self
, profile
).to_string()
66 # Save the image to the memcache
67 self
.memcached
.set(mem_id
, image
, self
.expires
)
69 self
.set_header("Content-Type", "image/png")
72 self
.set_header("Expires",
73 datetime
.datetime
.utcnow() + datetime
.timedelta(seconds
=self
.expires
))
74 self
.set_header("Cache-Control", "public,max-age=%d" % self
.expires
)