diff --git a/tailbone/templates/products/view.mako b/tailbone/templates/products/view.mako index b2821553..8522048a 100644 --- a/tailbone/templates/products/view.mako +++ b/tailbone/templates/products/view.mako @@ -110,7 +110,11 @@ ${self.render_main_fields(form)} % if image_url: - ${h.image(image_url, "Product Image", id='product-image', path=image_path, use_pil=False)} + % if image_path is not Undefined: + ${h.image(image_url, "Product Image", id='product-image', path=image_path, use_pil=False)} + % else: + ${h.image(image_url, "Product Image", id='product-image', width=150, height=150)} + % endif % endif diff --git a/tailbone/views/products.py b/tailbone/views/products.py index 0063aba8..1b853cb3 100644 --- a/tailbone/views/products.py +++ b/tailbone/views/products.py @@ -2,7 +2,7 @@ ################################################################################ # # Rattail -- Retail Software Framework -# Copyright © 2010-2016 Lance Edgar +# Copyright © 2010-2017 Lance Edgar # # This file is part of Rattail. # @@ -29,6 +29,7 @@ from __future__ import unicode_literals, absolute_import import os import re +import six import sqlalchemy as sa from sqlalchemy import orm @@ -345,6 +346,19 @@ class ProductsView(MasterView): 'instance_title': self.get_instance_title(instance), 'form': form}) + def image(self): + """ + View which renders the product's image as a response. + """ + product = self.get_instance() + if not product.image: + raise httpexceptions.HTTPNotFound() + # TODO: how to properly detect image type? + # self.request.response.content_type = six.binary_type('image/png') + self.request.response.content_type = six.binary_type('image/jpeg') + self.request.response.body = product.image.bytes + return self.request.response + def search(self): """ Locate a product(s) by UPC. @@ -562,6 +576,10 @@ class ProductsView(MasterView): cls._defaults(config) + # product image + config.add_route('products.image', '/products/{uuid}/image') + config.add_view(cls, attr='image', route_name='products.image') + class ProductVersionView(VersionView): """