]>
Commit | Line | Data |
---|---|---|
c37ec602 MT |
1 | #!/usr/bin/python |
2 | ||
abc3bb31 | 3 | import logging |
c37ec602 MT |
4 | import tornado.web |
5 | ||
6 | from handlers_base import * | |
7 | import backend | |
8 | ||
9 | class IUseImage(BaseHandler): | |
10 | @property | |
11 | def iuse(self): | |
12 | return backend.IUse() | |
13 | ||
14 | @property | |
15 | def stasy(self): | |
16 | return backend.Stasy() | |
17 | ||
18 | def get(self, profile_id, image_id): | |
abc3bb31 | 19 | image = None |
b3250465 MT |
20 | # Try to get the image from memcache. If we have a cache miss we |
21 | # build a new one. | |
abc3bb31 MT |
22 | mem_id = "iuse-%s-%s-%s" % (profile_id, image_id, self.locale.code) |
23 | ||
24 | cache = self.get_argument("cache", "true") | |
25 | if cache == "true": | |
26 | image = self.memcached.get(mem_id) | |
27 | ||
28 | if image: | |
29 | logging.debug("Got image from cache for profile: %s" % profile_id) | |
30 | else: | |
31 | logging.info("Rendering new image for profile: %s" % profile_id) | |
c37ec602 | 32 | |
b3250465 MT |
33 | image_cls = self.iuse.get_imagetype(image_id) |
34 | if not image_cls: | |
35 | raise tornado.web.HTTPError(404, "Image class is unknown: %s" % image_id) | |
c37ec602 | 36 | |
b3250465 MT |
37 | profile = self.stasy.get_profile(profile_id) |
38 | if not profile: | |
39 | raise tornado.web.HTTPError(404, "Profile '%s' was not found." % profile_id) | |
40 | ||
41 | # Render the image | |
abc3bb31 | 42 | image = image_cls(self, profile).to_string() |
c37ec602 | 43 | |
b3250465 MT |
44 | # Save the image to the memcache for 15 minutes |
45 | self.memcached.set(mem_id, image, 15*60) | |
c37ec602 | 46 | |
192f0bdf | 47 | self.set_header("Content-Type", "image/png") |
b3250465 | 48 | self.write(image) |
c37ec602 | 49 |