]> git.ipfire.org Git - people/shoehn/ipfire.org.git/blame - webapp/handlers_iuse.py
Major update of the webapp.
[people/shoehn/ipfire.org.git] / webapp / handlers_iuse.py
CommitLineData
c37ec602
MT
1#!/usr/bin/python
2
abc3bb31 3import logging
e8478c5e
MT
4import os
5import random
c37ec602
MT
6import tornado.web
7
8from handlers_base import *
9import backend
10
11class IUseImage(BaseHandler):
e8478c5e
MT
12 def get_error_html(self, status_code, **kwargs):
13 """
14 Select a random image from the errors directory
15 and return the content.
16 """
17 self.set_header("Content-Type", "image/png")
18
19 template_path = self.application.settings.get("template_path", "")
20 template_path = os.path.join(template_path, "i-use", "errors")
21
22 images = os.listdir(template_path)
23 if images:
24 image = random.choice(images)
25 image = os.path.join(template_path, image)
26
27 with open(image, "rb") as f:
28 return f.read()
29
c37ec602 30 def get(self, profile_id, image_id):
abc3bb31 31 image = None
b3250465
MT
32 # Try to get the image from memcache. If we have a cache miss we
33 # build a new one.
abc3bb31
MT
34 mem_id = "iuse-%s-%s-%s" % (profile_id, image_id, self.locale.code)
35
36 cache = self.get_argument("cache", "true")
37 if cache == "true":
38 image = self.memcached.get(mem_id)
39
40 if image:
41 logging.debug("Got image from cache for profile: %s" % profile_id)
42 else:
43 logging.info("Rendering new image for profile: %s" % profile_id)
c37ec602 44
b3250465
MT
45 image_cls = self.iuse.get_imagetype(image_id)
46 if not image_cls:
47 raise tornado.web.HTTPError(404, "Image class is unknown: %s" % image_id)
c37ec602 48
b3250465
MT
49 profile = self.stasy.get_profile(profile_id)
50 if not profile:
51 raise tornado.web.HTTPError(404, "Profile '%s' was not found." % profile_id)
52
53 # Render the image
9068dba1 54 image = image_cls(self.backend, self, profile).to_string()
c37ec602 55
b3250465
MT
56 # Save the image to the memcache for 15 minutes
57 self.memcached.set(mem_id, image, 15*60)
c37ec602 58
192f0bdf 59 self.set_header("Content-Type", "image/png")
b3250465 60 self.write(image)
c37ec602 61