Changeset - b80cb4af94e4
[Not reviewed]
0 2 0
Lance Edgar (lance) - 3 years ago 2021-11-03 19:13:42
lance@edbob.org
Add some product info fetchers to custorder batch handler

for easier customization
2 files changed with 65 insertions and 0 deletions:
0 comments (0 inline, 0 general)
rattail/autocomplete/products.py
Show inline comments
 
@@ -47,26 +47,29 @@ class ProductAutocompleter(Autocompleter):
 
    """
 
    autocompleter_key = 'products'
 
    model_class = model.Product
 
    autocomplete_fieldname = 'description'
 

	
 
    def make_base_query(self, session):
 
        model = self.model
 
        return session.query(model.Product)\
 
                      .outerjoin(model.Brand)\
 
                      .options(orm.joinedload(model.Product.brand))
 

	
 
    def restrict_autocomplete_query(self, session, query, **kwargs):
 
        model = self.model
 

	
 
        # do not show "deleted" items by default
 
        query = query.filter(model.Product.deleted == False)
 

	
 
        return query
 

	
 
    def filter_autocomplete_query(self, session, query, term):
 
        model = self.model
 

	
 
        # filter by user-provided term
 
        criteria = []
 
        for word in term.split():
 
            criteria.append(sa.or_(
 
                model.Brand.name.ilike('%{}%'.format(word)),
 
                model.Product.description.ilike('%{}%'.format(word))))
 
        query = query.filter(sa.and_(*criteria))
rattail/batch/custorder.py
Show inline comments
 
@@ -377,24 +377,86 @@ class CustomerOrderBatchHandler(BatchHandler):
 
        return info
 

	
 
    def custom_product_autocomplete(self, session, term, **kwargs):
 
        """
 
        For the given term, this should return a (possibly empty) list
 
        of products which "match" the term.  Each element in the list
 
        should be a dict with "label" and "value" keys.
 
        """
 
        raise NotImplementedError("Please define the "
 
                                  "{}.custom_product_autocomplete() "
 
                                  "method.".format(__class__.__name__))
 

	
 
    def get_product_info(self, batch, product, **kwargs):
 
        """
 
        Return a data dict containing misc. info pertaining to the
 
        given product, for the order batch.
 
        """
 
        products = self.app.get_products_handler()
 
        info = {
 
            'uuid': product.uuid,
 
            'upc': six.text_type(product.upc),
 
            'upc_pretty': product.upc.pretty(),
 
            'unit_price_display': products.render_price(product.regular_price),
 
            'full_description': product.full_description,
 
            'image_url': products.get_image_url(product),
 
            'uom_choices': self.uom_choices_for_product(product),
 
        }
 

	
 
        key = self.config.product_key()
 
        if key == 'upc':
 
            info['key'] = info['upc_pretty']
 
        else:
 
            info['key'] = getattr(product, key, info['upc_pretty'])
 

	
 
        return info
 

	
 
    def uom_choices_for_product(self, product):
 
        """
 
        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
 

	
 
    def why_not_add_product(self, product, batch):
 
        """
 
        This method can inspect the given product, and batch, to
 
        determine if the product may be added to the batch as a new
 
        row.  Useful to e.g. prevent one customer from ordering too
 
        many things, etc.
 

	
 
        :returns: If there is a reason not to add the product, should
 
           return that reason as a string; otherwise ``None``.
 
        """
 

	
 
    def add_product(self, batch, product, order_quantity, order_uom,
0 comments (0 inline, 0 general)