Changeset - 0c4e6776b142
[Not reviewed]
0 1 0
Lance Edgar (lance) - 17 months ago 2023-05-16 17:31:24
lance@edbob.org
Add way to prevent error in `pod.get_image_url()`
1 file changed with 11 insertions and 13 deletions:
0 comments (0 inline, 0 general)
rattail/pod.py
Show inline comments
 
@@ -2,7 +2,7 @@
 
################################################################################
 
#
 
#  Rattail -- Retail Software Framework
 
#  Copyright © 2010-2018 Lance Edgar
 
#  Copyright © 2010-2023 Lance Edgar
 
#
 
#  This file is part of Rattail.
 
#
 
@@ -26,12 +26,8 @@
 
.. _`Product Open Data`: http://www.product-open-data.com/
 
"""
 

	
 
from __future__ import unicode_literals, absolute_import
 

	
 
import os
 

	
 
import six
 

	
 

	
 
def get_image_path(config, gpc):
 
    """
 
@@ -39,18 +35,20 @@ def get_image_path(config, gpc):
 
    """
 
    root_path = config.get('rattail.pod', 'pictures.gtin.root_path')
 
    if root_path:
 
        gtin = six.text_type(gpc)[1:]
 
        gtin = str(gpc)[1:]
 
        return os.path.join(root_path, 'gtin-{}'.format(gtin[:3]), '{}.jpg'.format(gtin))
 

	
 

	
 
def get_image_url(config, gpc):
 
def get_image_url(config, gpc, require=True):
 
    """
 
    Get an image URL from a product GPC.
 
    """
 
    if gpc:
 
        root_url = config.require('rattail.pod', 'pictures.gtin.root_url')
 
        root_url = root_url.rstrip('/')
 
        gtin = six.text_type(gpc)[1:]
 
        return '{}/gtin-{}/{}.jpg'.format(root_url, gtin[:3], gtin)
 
    else:
 
        return config.get('rattail.pod', 'pictures.gtin.not_found_url')
 
        getter = config.require if require else config.get
 
        root_url = getter('rattail.pod', 'pictures.gtin.root_url')
 
        if root_url:
 
            root_url = root_url.rstrip('/')
 
            gtin = str(gpc)[1:]
 
            return '{}/gtin-{}/{}.jpg'.format(root_url, gtin[:3], gtin)
 

	
 
    return config.get('rattail.pod', 'pictures.gtin.not_found_url')
0 comments (0 inline, 0 general)