Changeset - c3487917ceba
[Not reviewed]
0 2 0
Lance Edgar (lance) - 3 years ago 2021-11-06 17:35:15
lance@edbob.org
Only show POD image if so configured; use "image not found" fallback

also update a random docstring
2 files changed with 22 insertions and 6 deletions:
0 comments (0 inline, 0 general)
rattail/config.py
Show inline comments
 
@@ -559,48 +559,52 @@ class RattailConfig(object):
 
        """
 
        spec = self.get('rattail', 'model', default='rattail.db.model', usedb=False)
 
        return import_module_path(spec)
 

	
 
    def get_trainwreck_model(self):
 
        """
 
        Returns a reference to the configured data 'model' module for
 
        Trainwreck.  Note that there is *not* a default value for this; it must
 
        be configured.
 
        """
 
        spec = self.require('rattail.trainwreck', 'model', usedb=False)
 
        return import_module_path(spec)
 

	
 
    def product_key(self, default='upc'):
 
        """
 
        Returns the name of the attribute which should be treated as the
 
        canonical product key field, e.g. 'upc' or 'item_id' etc.
 
        """
 
        return self.get('rattail', 'product.key', default=default) or default
 

	
 
    def product_key_title(self, key=None):
 
        """
 
        Returns the title string to be used when displaying product key field,
 
        e.g. "UPC" or "Part No." etc.
 

	
 
        :param key: The product key for which to return a label.  This
 
           is optional; if not specified then :meth:`product_key()`
 
           will be called to determine the key.
 
        """
 
        title = self.get('rattail', 'product.key_title')
 
        if title:
 
            return title
 
        if not key:
 
            key = self.product_key()
 
        if key == 'upc':
 
            return "UPC"
 
        if key == 'item_id':
 
            return "Item ID"
 
        return prettify(key)
 

	
 
    def single_store(self):
 
        """
 
        Returns boolean indicating whether the system is configured to behave
 
        as if it belongs to a single Store.
 
        """
 
        return self.getbool('rattail', 'single_store', default=False)
 

	
 
    def get_store(self, session):
 
        """
 
        Returns a :class:`rattail.db.model.Store` instance corresponding to app
 
        config, or ``None``.
 
        """
rattail/products.py
Show inline comments
 
@@ -163,60 +163,72 @@ class ProductsHandler(GenericHandler):
 

	
 
        # maybe look for 'altcode' match
 
        if not only or 'altcode' in only:
 
            product = get_product_by_code(session, value)
 
            if product:
 
                products.append(('altcode', product))
 

	
 
        # maybe look for 'sku' match
 
        if not only or 'sku' in only:
 
            product = get_product_by_vendor_code(session, value,
 
                                                 vendor=vendor)
 
            if product:
 
                products.append(('sku', product))
 

	
 
        # maybe strip keys out of the result
 
        if not include_keys:
 
            products = [tup[1] for tup in products]
 

	
 
        return products
 

	
 
    def get_image_url(self, product=None, upc=None, **kwargs):
 
        """
 
        Return the preferred image URL for the given UPC or product.
 
        """
 
        # we prefer the "image on file" if available
 
        if product and product.image:
 
            url = self.config.base_url()
 
            if url:
 
                return '{}/products/{}/image'.format(url, product.uuid)
 
        base_url = self.config.base_url()
 

	
 
        # fallback to the POD image, if available
 
        # we prefer the "image on file" if available
 
        if base_url and product and product.image:
 
            return '{}/products/{}/image'.format(base_url, product.uuid)
 

	
 
        # and if this product is a pack item, then we prefer the unit
 
        # item image as fallback, if available
 
        if base_url and product and product.is_pack_item():
 
            unit = product.unit
 
            if unit and unit.image:
 
                return '{}/products/{}/image'.format(base_url, unit.uuid)
 

	
 
        # fallback to the POD image, if available and so configured
 
        if self.config.getbool('tailbone', 'products.show_pod_image',
 
                               default=False):
 
            if product and not upc:
 
                upc = product.upc
 
            if upc:
 
                return self.get_pod_image_url(upc)
 

	
 
        if base_url:
 
            return '{}/tailbone/img/product.png'.format(base_url)
 

	
 
    def get_pod_image_url(self, upc, **kwargs):
 
        """
 
        Return the POD image URL for the given UPC.
 
        """
 
        if upc:
 
            return pod.get_image_url(self.config, upc)
 

	
 
    def render_price(self, price, html=False, **kwargs):
 
        """
 
        Render the given ``price`` object as text.
 

	
 
        :returns: String containing the rendered price, or ``None`` if
 
           nothing was applicable.
 
        """
 
        if price.price is not None and price.pack_price is not None:
 
            if price.multiple > 1:
 
                return "{} / {}  ({} / {})".format(
 
                    self.app.render_currency(price.price),
 
                    price.multiple,
 
                    self.app.render_currency(price.pack_price),
 
                    price.pack_multiple)
 
            return "{}  ({} / {})".format(
 
                self.app.render_currency(price.price),
 
                self.app.render_currency(price.pack_price),
0 comments (0 inline, 0 general)