Exemplo n.º 1
0
    def articles(self):
        """Returns list of dictionary of articles in shop

        :rtype: list
        """
        res = []
        for item in IShoppingSite(self.context).get_content_listing(IArticle, sort_on="sku"):
            obj = item.getObject()
            article = IArticleAdapter(obj)
            sbehavior = IStockBehavior(obj)
            stock = sbehavior.stock()
            stocks = sbehavior.stocks()
            if stocks:
                price = stocks[-1].price
                subtotal = price * stock
                price = getUtility(IPriceUtility, name="string")(price)
                subtotal = getUtility(IPriceUtility, name="string")(subtotal)
            else:
                price = subtotal = "N/A"
            res.append(
                {
                    "price": price,
                    "sku": item.sku,
                    "stock": stock,
                    "subtotal": subtotal,
                    "title": article.title(),
                    "url": item.getURL(),
                }
            )
        return res
    def add(self):
        """Returns attributes: max and size for input: add

        :rtype: dict
        """
        stock = IStock(self.context)
        maximum = stock.initial_stock() - stock.stock()
        if maximum == 0:
            return None

        return {
            'max': maximum,
            'size': len(str(maximum)),
        }
    def get_schema(self, name):
        """Get schema by name.

        :param name: Name of schema.
        :type name: str
        """
        from collective.behavior.stock.interfaces import IStock

        return IStock.get(name)
    def update(self):
        form = self.request.form
        url = self.context.restrictedTraverse('@@plone_context_state').current_base_url()
        stock = IStock(self.context)

        if form.get('form.buttons.QuickAdd') is not None:
            value = form.get('quick-add')
            validate = validation.validatorFor('isInt')
            maximum = self.add()['max']
            if validate(value) != 1:
                message = _(u'add_less_than_number', default=u'Add less than ${number}.', mapping={'number': maximum})
                IStatusMessage(self.request).addStatusMessage(message, type='warn')
            else:
                value = int(value)
                message = _(u'successfully_added_number', default=u'Successfully added ${number} pc(s).', mapping={
                    'number': stock.add_stock(value)})
                IStatusMessage(self.request).addStatusMessage(message, type='info')
            return self.request.response.redirect(url)

        elif form.get('form.buttons.QuickSubtract') is not None:
            value = form.get('quick-subtract')
            validate = validation.validatorFor('isInt')
            maximum = self.subtract()['max']
            if validate(value) != 1:
                message = _(u'subtract_less_than_number', default=u'Subtract less than ${number}.', mapping={'number': maximum})
                IStatusMessage(self.request).addStatusMessage(message, type='warn')
            else:
                value = int(value)
                message = _(u'successfully_subtracted_number', default=u'Successfully subtracted ${number} pc(s).', mapping={
                    'number': stock.sub_stock(value)})
                IStatusMessage(self.request).addStatusMessage(message, type='info')
            return self.request.response.redirect(url)

        elif form.get('form.buttons.AddNewStock') is not None:
            url = '{}/++add++collective.cart.stock.Stock'.format(self.context.absolute_url())
            return self.request.response.redirect(url)
 def test_instance_provides_IStock(self):
     instance = self.create_instance()
     from collective.behavior.stock.interfaces import IStock
     self.assertTrue(IStock.providedBy(instance))