Changeset - 2a726760e552
[Not reviewed]
0 2 0
Lance Edgar (lance) - 3 years ago 2022-02-08 12:11:02
lance@edbob.org
Add common normalize logic to ProductsHandler

about dang time..now still need to enhance and use it everywhere
2 files changed with 138 insertions and 33 deletions:
0 comments (0 inline, 0 general)
rattail/batch/custorder.py
Show inline comments
 
@@ -546,39 +546,8 @@ class CustomerOrderBatchHandler(BatchHandler):
 
        """
 
        Return a list of UOM choices for the given product.
 
        """
 
        choices = []
 

	
 
        # Each
 
        if not product or not product.weighed:
 
            unit_name = self.enum.UNIT_OF_MEASURE[self.enum.UNIT_OF_MEASURE_EACH]
 
            choices.append({'key': self.enum.UNIT_OF_MEASURE_EACH,
 
                            'value': unit_name})
 

	
 
        # Pound
 
        if not product or product.weighed:
 
            unit_name = self.enum.UNIT_OF_MEASURE[self.enum.UNIT_OF_MEASURE_POUND]
 
            choices.append({
 
                'key': self.enum.UNIT_OF_MEASURE_POUND,
 
                'value': unit_name,
 
            })
 

	
 
        # Case
 
        case_text = None
 
        case_size = self.get_case_size_for_product(product)
 
        if case_size is None:
 
            case_text = "{} (× ?? {})".format(
 
                self.enum.UNIT_OF_MEASURE[self.enum.UNIT_OF_MEASURE_CASE],
 
                unit_name)
 
        elif case_size > 1:
 
            case_text = "{} (× {} {})".format(
 
                self.enum.UNIT_OF_MEASURE[self.enum.UNIT_OF_MEASURE_CASE],
 
                self.app.render_quantity(case_size),
 
                unit_name)
 
        if case_text:
 
            choices.append({'key': self.enum.UNIT_OF_MEASURE_CASE,
 
                            'value': case_text})
 

	
 
        return choices
 
        products_handler = self.app.get_products_handler()
 
        return products_handler.get_uom_choices(product)
 

	
 
    def why_not_add_product(self, product, batch):
 
        """
rattail/products.py
Show inline comments
 
@@ -26,6 +26,7 @@ Products Handler
 

	
 
from __future__ import unicode_literals, absolute_import
 

	
 
import decimal
 
import warnings
 
import logging
 

	
 
@@ -572,6 +573,131 @@ class ProductsHandler(GenericHandler):
 

	
 
        return get_product_by_code(session, entry)
 

	
 
    def normalize_product(self, product, fields=None, **kwargs):
 
        """
 
        Normalize the given product to a JSON-serializable dict.
 
        """
 
        data = {
 
            'uuid': product.uuid,
 
            'product_key': self.render_product_key(product),
 
            'description': product.description,
 
            'size': product.size,
 
        }
 

	
 
        if not fields:
 
            fields = [
 
                'brand_name',
 
                'full_description',
 
                'department_name',
 
            ]
 

	
 
        if 'url' in fields:
 
            data['url'] = self.get_url(product)
 

	
 
        if 'image_url' in fields:
 
            data['image_url'] = self.get_image_url(product)
 

	
 
        if 'brand_name' in fields:
 
            data['brand_name'] = product.brand.name if product.brand else None
 

	
 
        if 'full_description' in fields:
 
            data['full_description'] = self.make_full_description(product)
 

	
 
        if 'department_name' in fields:
 
            data['department_name'] = product.department.name if product.department else None
 

	
 
        if 'unit_price_display' in fields:
 
            data['unit_price_display'] = self.render_price(product.regular_price)
 

	
 
        if 'vendor_name' in fields:
 
            vendor = product.cost.vendor if product.cost else None
 
            data['vendor_name'] = vendor.name if vendor else None
 

	
 
        sale_fields = [
 
            'sale_price',
 
            'sale_price_display',
 
            'sale_ends',
 
            'sale_ends_display',
 
            'case_price',
 
            'case_price_display',
 
        ]
 
        if any([f in fields for f in sale_fields]):
 
            sale_price = None
 
            if not product.not_for_sale:
 
                sale_price = product.current_price
 
                if sale_price:
 
                    if sale_price.price:
 
                        data['sale_price'] = float(sale_price.price)
 
                    data['sale_price_display'] = self.render_price(sale_price)
 
                    sale_ends = sale_price.ends
 
                    if sale_ends:
 
                        sale_ends = self.app.localtime(sale_ends, from_utc=True).date()
 
                        data['sale_ends'] = six.text_type(sale_ends)
 
                        data['sale_ends_display'] = self.app.render_date(sale_ends)
 

	
 
        if 'case_quantity' in fields:
 
            data['case_quantity'] = self.app.render_quantity(
 
                self.get_case_size(product))
 

	
 
        if 'case_price' in fields or 'case_price_display' in fields:
 
            case_price = None
 
            if product.regular_price and product.regular_price is not None:
 
                case_size = self.get_case_size(product)
 
                # use sale price if there is one, else normal unit price
 
                unit_price = product.regular_price.price
 
                if sale_price:
 
                    unit_price = sale_price.price
 
                case_price = (case_size or 1) * unit_price
 
                case_price = case_price.quantize(decimal.Decimal('0.01'))
 
            data['case_price'] = six.text_type(case_price) if case_price is not None else None
 
            data['case_price_display'] = self.app.render_currency(case_price)
 

	
 
        if 'uom_choices' in fields:
 
            data['uom_choices'] = self.get_uom_choices(product)
 

	
 
        return data
 

	
 
    def get_uom_choices(self, product=None, **kwargs):
 
        """
 
        Return a list of UOM choices for the given product, or if no
 
        product specified then should return the default choices.
 
        """
 
        choices = []
 

	
 
        # Each
 
        if not product or not product.weighed:
 
            unit_name = self.enum.UNIT_OF_MEASURE[self.enum.UNIT_OF_MEASURE_EACH]
 
            choices.append({'key': self.enum.UNIT_OF_MEASURE_EACH,
 
                            'value': unit_name})
 

	
 
        # Pound
 
        if not product or product.weighed:
 
            unit_name = self.enum.UNIT_OF_MEASURE[self.enum.UNIT_OF_MEASURE_POUND]
 
            choices.append({
 
                'key': self.enum.UNIT_OF_MEASURE_POUND,
 
                'value': unit_name,
 
            })
 

	
 
        # Case
 
        case_text = None
 
        case_size = None
 
        if product:
 
            case_size = self.get_case_size(product)
 
        if case_size is None:
 
            case_text = "{} (× ?? {})".format(
 
                self.enum.UNIT_OF_MEASURE[self.enum.UNIT_OF_MEASURE_CASE],
 
                unit_name)
 
        elif case_size > 1:
 
            case_text = "{} (× {} {})".format(
 
                self.enum.UNIT_OF_MEASURE[self.enum.UNIT_OF_MEASURE_CASE],
 
                self.app.render_quantity(case_size),
 
                unit_name)
 
        if case_text:
 
            choices.append({'key': self.enum.UNIT_OF_MEASURE_CASE,
 
                            'value': case_text})
 

	
 
        return choices
 

	
 
    def get_case_size(self, product):
 
        """
 
        Return the effective case size for the given product.
 
@@ -626,6 +752,16 @@ class ProductsHandler(GenericHandler):
 
        if upc:
 
            return pod.get_image_url(self.config, upc)
 

	
 
    def render_product_key(self, product, **kwargs):
 
        """
 
        Render the key value for the given product, as human-readable
 
        text.
 
        """
 
        product_key = self.config.product_key()
 
        if product_key == 'upc':
 
            return self.app.render_gpc(product.upc)
 
        return six.text_type(getattr(product, product_key))
 

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