]>
git.ipfire.org Git - ipfire.org.git/blob - webapp/handlers_iuse.py
4fce56d8926a760b792ceb64a9d35a0cf5d36900
9 from handlers_base
import *
12 class IUseImage(BaseHandler
):
15 def write_error(self
, status_code
, **kwargs
):
17 Select a random image from the errors directory
18 and return the content.
20 self
.set_header("Content-Type", "image/png")
22 template_path
= self
.application
.settings
.get("template_path", "")
23 template_path
= os
.path
.join(template_path
, "i-use", "errors")
25 images
= os
.listdir(template_path
)
27 image
= random
.choice(images
)
28 image
= os
.path
.join(template_path
, image
)
31 with
open(image
, "rb") as f
:
36 def get(self
, profile_id
, image_id
):
39 when
= self
.get_argument_date("when", None)
41 # Try to get the image from memcache. If we have a cache miss we
43 mem_id
= "iuse-%s-%s-%s" % (profile_id
, image_id
, self
.locale
.code
)
45 mem_id
+= "-%s" % when
.isoformat()
47 cache
= self
.get_argument("cache", "true")
49 image
= self
.memcached
.get(mem_id
)
52 logging
.debug("Got image from cache for profile: %s" % profile_id
)
54 logging
.info("Rendering new image for profile: %s" % profile_id
)
56 profile
= self
.fireinfo
.get_profile_with_data(profile_id
, when
=when
)
58 raise tornado
.web
.HTTPError(404, "Profile '%s' was not found." % profile_id
)
60 image_cls
= self
.iuse
.get_imagetype(image_id
)
62 raise tornado
.web
.HTTPError(404, "Image class is unknown: %s" % image_id
)
65 image
= image_cls(self
.backend
, self
, profile
).to_string()
67 # Save the image to the memcache
68 self
.memcached
.set(mem_id
, image
, self
.expires
)
70 self
.set_header("Content-Type", "image/png")
73 self
.set_header("Expires",
74 datetime
.datetime
.utcnow() + datetime
.timedelta(seconds
=self
.expires
))
75 self
.set_header("Cache-Control", "public,max-age=%d" % self
.expires
)