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