]> git.ipfire.org Git - ipfire.org.git/blame - webapp/handlers_iuse.py
Move everything to the root of the repository.
[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):
12 @property
13 def iuse(self):
14 return backend.IUse()
15
16 @property
17 def stasy(self):
18 return backend.Stasy()
19
e8478c5e
MT
20 def get_error_html(self, status_code, **kwargs):
21 """
22 Select a random image from the errors directory
23 and return the content.
24 """
25 self.set_header("Content-Type", "image/png")
26
27 template_path = self.application.settings.get("template_path", "")
28 template_path = os.path.join(template_path, "i-use", "errors")
29
30 images = os.listdir(template_path)
31 if images:
32 image = random.choice(images)
33 image = os.path.join(template_path, image)
34
35 with open(image, "rb") as f:
36 return f.read()
37
c37ec602 38 def get(self, profile_id, image_id):
abc3bb31 39 image = None
b3250465
MT
40 # Try to get the image from memcache. If we have a cache miss we
41 # build a new one.
abc3bb31
MT
42 mem_id = "iuse-%s-%s-%s" % (profile_id, image_id, self.locale.code)
43
44 cache = self.get_argument("cache", "true")
45 if cache == "true":
46 image = self.memcached.get(mem_id)
47
48 if image:
49 logging.debug("Got image from cache for profile: %s" % profile_id)
50 else:
51 logging.info("Rendering new image for profile: %s" % profile_id)
c37ec602 52
b3250465
MT
53 image_cls = self.iuse.get_imagetype(image_id)
54 if not image_cls:
55 raise tornado.web.HTTPError(404, "Image class is unknown: %s" % image_id)
c37ec602 56
b3250465
MT
57 profile = self.stasy.get_profile(profile_id)
58 if not profile:
59 raise tornado.web.HTTPError(404, "Profile '%s' was not found." % profile_id)
60
61 # Render the image
abc3bb31 62 image = image_cls(self, profile).to_string()
c37ec602 63
b3250465
MT
64 # Save the image to the memcache for 15 minutes
65 self.memcached.set(mem_id, image, 15*60)
c37ec602 66
192f0bdf 67 self.set_header("Content-Type", "image/png")
b3250465 68 self.write(image)
c37ec602 69