]> git.ipfire.org Git - ipfire.org.git/blame - src/web/handlers_iuse.py
web: Rename base module
[ipfire.org.git] / src / web / handlers_iuse.py
CommitLineData
c37ec602
MT
1#!/usr/bin/python
2
d7c4018b 3import datetime
abc3bb31 4import logging
e8478c5e
MT
5import os
6import random
c37ec602
MT
7import tornado.web
8
124a8404 9from . import base
c37ec602 10
124a8404 11class IUseImage(base.BaseHandler):
d7c4018b
MT
12 expires = 3600 * 3
13
30bb04e2 14 def write_error(self, status_code, **kwargs):
e8478c5e
MT
15 """
16 Select a random image from the errors directory
17 and return the content.
18 """
5eac92f1
MT
19 self.set_expires(3600)
20
e8478c5e
MT
21 self.set_header("Content-Type", "image/png")
22
23 template_path = self.application.settings.get("template_path", "")
24 template_path = os.path.join(template_path, "i-use", "errors")
25
26 images = os.listdir(template_path)
27 if images:
28 image = random.choice(images)
29 image = os.path.join(template_path, image)
30
30bb04e2 31 imgdata = None
e8478c5e 32 with open(image, "rb") as f:
30bb04e2
MT
33 imgdata = f.read()
34
35 self.finish(imgdata)
e8478c5e 36
c37ec602 37 def get(self, profile_id, image_id):
abc3bb31 38 image = None
6ec867f2
MT
39
40 when = self.get_argument_date("when", None)
41
5eac92f1 42 logging.info("Rendering new image for profile: %s" % profile_id)
c37ec602 43
5eac92f1
MT
44 profile = self.fireinfo.get_profile_with_data(profile_id, when=when)
45 if not profile:
46 raise tornado.web.HTTPError(404, "Profile '%s' was not found." % profile_id)
d7bebd28 47
5eac92f1
MT
48 image_cls = self.iuse.get_imagetype(image_id)
49 if not image_cls:
50 raise tornado.web.HTTPError(404, "Image class is unknown: %s" % image_id)
c37ec602 51
5eac92f1
MT
52 # Render the image
53 image = image_cls(self.backend, self, profile).to_string()
c37ec602 54
5eac92f1
MT
55 # Cache generate images for 3 hours
56 self.set_expires(3600 * 3)
c37ec602 57
192f0bdf 58 self.set_header("Content-Type", "image/png")
b3250465 59 self.write(image)