Changeset - bd39609a3d1b
[Not reviewed]
0 1 0
Lance Edgar (lance) - 3 years ago 2021-09-25 14:26:20
lance@edbob.org
Add `render_price()` method for products handler

so that it can be invoked from multiple web views
1 file changed with 29 insertions and 0 deletions:
0 comments (0 inline, 0 general)
rattail/products.py
Show inline comments
 
@@ -14,96 +14,125 @@
 
#  Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
 
#  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
#  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 
#  details.
 
#
 
#  You should have received a copy of the GNU General Public License along with
 
#  Rattail.  If not, see <http://www.gnu.org/licenses/>.
 
#
 
################################################################################
 
"""
 
Products Handler
 
"""
 

	
 
from __future__ import unicode_literals, absolute_import
 

	
 
import sqlalchemy as sa
 
from sqlalchemy import orm
 

	
 
from rattail import pod
 
from rattail.util import load_object
 
from rattail.app import GenericHandler
 

	
 

	
 
class ProductsHandler(GenericHandler):
 
    """
 
    Base class and default implementation for product handlers.
 

	
 
    A products handler of course should get the final say in how products are
 
    handled.  This means everything from pricing, to whether or not a
 
    particular product can be deleted, etc.
 
    """
 

	
 
    def get_image_url(self, upc=None, product=None, **kwargs):
 
        """
 
        Return the preferred image URL for the given UPC or product.
 
        """
 
        if product and product.upc:
 
            upc = product.upc
 
        if upc:
 
            return self.get_pod_image_url(upc)
 

	
 
    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),
 
                price.pack_multiple)
 
        if price.price is not None:
 
            if price.multiple is not None and price.multiple > 1:
 
                return "{} / {}".format(
 
                    self.app.render_currency(price.price),
 
                    price.multiple)
 
            return self.app.render_currency(price.price)
 
        if price.pack_price is not None:
 
            return "{} / {}".format(
 
                self.app.render_currency(price.pack_price),
 
                price.pack_multiple)
 

	
 
    def get_uom_sil_codes(self, session, uppercase=False, **kwargs):
 
        """
 
        This should return a dict, keys of which are UOM abbreviation strings,
 
        and values of which are corresponding SIL code strings.
 

	
 
        :param session: Reference to current Rattail DB session.
 
        :param uppercase: Set to ``True`` to cause all UOM abbreviations to be
 
           upper-cased when adding to the map.
 
        :returns: Dictionary containing all known UOM / SIL code mappings.
 
        """
 
        model = self.model
 

	
 
        def normalize(uom):
 
            if uom.sil_code:
 
                return uom.sil_code
 

	
 
        def make_key(uom, normal):
 
            key = uom.abbreviation
 
            if uppercase:
 
                key = key.upper()
 
            return key
 

	
 
        return self.cache_model(session, model.UnitOfMeasure,
 
                                normalizer=normalize,
 
                                key=make_key)
 

	
 
    def get_uom_sil_code(self, session, uom, uppercase=False, **kwargs):
 
        """
 
        This should return a SIL code which corresponds to the given UOM
 
        abbreviation string.  Useful when you just need one out of the blue,
 
        but if you need multiple codes looked up then you're probably better
 
        off using :meth:`get_uom_sil_codes()` for efficiency.
 

	
 
        :param session: Reference to current Rattail DB session.
 
        :param uppercase: Set to ``True`` to cause the UOM abbreviation to be
 
           upper-cased before performing the lookup.  This effectively makes
 
           the search case-insensitive.
 
        :param uom:  Unit of measure as abbreviated string, e.g. ``'LB'``.
 
        :returns: SIL code for the UOM, as string (e.g. ``'49'``), or ``None``
 
           if no matching code was found.
 
        """
 
        model = self.model
 
        query = session.query(model.UnitOfMeasure)
 
        if uppercase:
 
            query = query.filter(sa.func.upper(model.UnitOfMeasure.abbreviation) == uom.upper())
 
        else:
 
            query = query.filter(model.UnitOfMeasure.abbreviation == uom)
 
        try:
0 comments (0 inline, 0 general)