]>
git.ipfire.org Git - people/shoehn/ipfire.org.git/blob - webapp/handlers_iuse.py
8 from handlers_base
import *
11 class IUseImage(BaseHandler
):
12 def write_error(self
, status_code
, **kwargs
):
14 Select a random image from the errors directory
15 and return the content.
17 self
.set_header("Content-Type", "image/png")
19 template_path
= self
.application
.settings
.get("template_path", "")
20 template_path
= os
.path
.join(template_path
, "i-use", "errors")
22 images
= os
.listdir(template_path
)
24 image
= random
.choice(images
)
25 image
= os
.path
.join(template_path
, image
)
28 with
open(image
, "rb") as f
:
33 def get(self
, profile_id
, image_id
):
36 when
= self
.get_argument_date("when", None)
38 # Try to get the image from memcache. If we have a cache miss we
40 mem_id
= "iuse-%s-%s-%s" % (profile_id
, image_id
, self
.locale
.code
)
42 mem_id
+= "-%s" % when
.isoformat()
44 cache
= self
.get_argument("cache", "true")
46 image
= self
.memcached
.get(mem_id
)
49 logging
.debug("Got image from cache for profile: %s" % profile_id
)
51 logging
.info("Rendering new image for profile: %s" % profile_id
)
53 image_cls
= self
.iuse
.get_imagetype(image_id
)
55 raise tornado
.web
.HTTPError(404, "Image class is unknown: %s" % image_id
)
57 profile
= self
.fireinfo
.get_profile(profile_id
, when
=when
)
59 raise tornado
.web
.HTTPError(404, "Profile '%s' was not found." % profile_id
)
62 image
= image_cls(self
.backend
, self
, profile
).to_string()
64 # Save the image to the memcache for 60 minutes
65 self
.memcached
.set(mem_id
, image
, 3600)
67 self
.set_header("Content-Type", "image/png")