| # Copyright 2014 the Melange authors. |
| # |
| # Licensed under the Apache License, Version 2.0 (the "License"); |
| # you may not use this file except in compliance with the License. |
| # You may obtain a copy of the License at |
| # |
| # http://www.apache.org/licenses/LICENSE-2.0 |
| # |
| # Unless required by applicable law or agreed to in writing, software |
| # distributed under the License is distributed on an "AS IS" BASIS, |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| # See the License for the specific language governing permissions and |
| # limitations under the License. |
| |
| """Module containing a template to display images.""" |
| |
| from django.template import loader |
| |
| from soc.views import template |
| |
| class Image(template.Template): |
| """Template to display an image based on its URL.""" |
| |
| def __init__(self, data, image_url, template_path, width=None, height=None): |
| """Initializes a new instance of this template. |
| |
| Args: |
| data: request_data.RequestData for the current request. |
| image_url: URL which serves the image. |
| template_path: Path to the HTML template to use for rendering. |
| width: An int representing width of the image to display (in pixels). |
| If not specified, natural size of the image will be used. |
| height: An int representing height of the image to display (in pixels). |
| If not specified, natural size of the image will be used. |
| """ |
| super(Image, self).__init__(data) |
| self._image_url = image_url |
| self._template_path = template_path |
| self._width = width |
| self._height = height |
| |
| def render(self): |
| """Renders the template as HTML. |
| |
| Returns: |
| A string containing HTML form of the template. |
| """ |
| context = { |
| 'image_url': self._image_url, |
| 'height': self._height, |
| 'width': self._width, |
| } |
| return loader.render_to_string(self._template_path, dictionary=context) |