示例#1
0
文件: data.py 项目: Easyshop/Easyshop
    def asDict(self):
        """
        """
        pvm = IProductVariantsManagement(self.context)
        
        if pvm.hasVariants() == True:
            variant = pvm.getSelectedVariant() or pvm.getDefaultVariant()
            return IData(variant).asDict()
        else:
            # price
            cm    = ICurrencyManagement(self.context)
            price = IPrices(self.context).getPriceForCustomer()
            price = cm.priceToString(price)

            # image
            image = IImageManagement(self.context).getMainImage()
            if image is not None:
                image = "%s/image_%s" % (image.absolute_url(), "preview")

            images = []
            for temp in IImageManagement(self.context).getImages():
                images.append("%s/image_tile" % temp.absolute_url())
            
            return {
                "article_id"  : self.context.getArticleId(),                
                "title"       : self.context.Title(),
                "short_title" : self.context.getShortTitle() or self.context.Title(),
                "description" : self.context.Description(),
                "url"         : self.context.absolute_url(),
                "price"       : price,
                "image"       : image,
                "images"      : images,
                "text"        : self.context.getText(),
                "short_text"  : self.context.getShortText(),
            }        
示例#2
0
    def getStockInformation(self):
        """
        """
        shop = self._getShop()
        sm = IStockManagement(shop)

        pvm = IProductVariantsManagement(self.context)

        if pvm.hasVariants() == False:
            stock_information = sm.getStockInformationFor(self.context)
        else:
            product_variant = pvm.getSelectedVariant()

            # First, we try to get information for the selected product variant
            stock_information = sm.getStockInformationFor(product_variant)

            # If nothing is found, we try to get information for parent product
            # variants object.
            if stock_information is None:
                stock_information = sm.getStockInformationFor(self.context)

        if stock_information is None:
            return None

        return IData(stock_information).asDict()
示例#3
0
 def getProperties(self):
     """
     """
     pvm = IProductVariantsManagement(self.context)
     if pvm.hasVariants():
         return self._getPropertiesForVariants()
     else:
         return self._getPropertiesForConfiguration()
示例#4
0
    def __init__(self, context):
        """
        """
        pvm  = IProductVariantsManagement(context)
        shop = IShopManagement(context).getShop()
        
        self.context = context

        self.gross_prices = shop.getGrossPrices()
        self.has_variants = pvm.hasVariants()
        self.taxes = ITaxes(context)

        if self.has_variants:
            self.product_variant = \
                pvm.getSelectedVariant() or pvm.getDefaultVariant()
示例#5
0
文件: prices.py 项目: viona/Easyshop
    def __init__(self, context):
        """
        """
        pvm = IProductVariantsManagement(context)
        shop = IShopManagement(context).getShop()

        self.context = context

        self.gross_prices = shop.getGrossPrices()
        self.has_variants = pvm.hasVariants()
        self.taxes = ITaxes(context)

        if self.has_variants:
            self.product_variant = \
                pvm.getSelectedVariant() or pvm.getDefaultVariant()
示例#6
0
def deleteProperty(property, event):
    """Removes property from all existing product variants.
    """
    product = property.aq_inner.aq_parent
    pvm = IProductVariantsManagement(product)

    if pvm.hasVariants() == False:
        return

    to_delete_property_id = property.getId()
    for variant in pvm.getVariants():
        new_properties = []
        for variant_property in variant.getForProperties():
            variant_property_id = variant_property.split(":")[0]
            if variant_property_id != to_delete_property_id:
                new_properties.append(variant_property)
        new_properties.sort()

        variant.setForProperties(new_properties)
示例#7
0
def deleteProperty(property, event):
    """Removes property from all existing product variants.
    """
    product = property.aq_inner.aq_parent
    pvm = IProductVariantsManagement(product)
    
    if pvm.hasVariants() == False:
        return 
        
    to_delete_property_id = property.getId()
    for variant in pvm.getVariants():
        new_properties = []
        for variant_property in variant.getForProperties():
            variant_property_id = variant_property.split(":")[0]
            if variant_property_id != to_delete_property_id:
                new_properties.append(variant_property)
        new_properties.sort()

        variant.setForProperties(new_properties)
示例#8
0
    def _information(self):
        """
        """
        shop = IShopManagement(self.context).getShop()
        im = IInformationManagement(shop)

        pvm = IProductVariantsManagement(self.context)

        if pvm.hasVariants() == False:
            information = im.getInformationPagesFor(self.context)
        else:
            product_variant = pvm.getSelectedVariant()

            # First, we try to get information for the selected product variant
            information = im.getInformationPagesFor(product_variant)

            # If nothing is found, we try to get information for parent product
            # variants object.
            if information is None:
                information = im.getInformationPagesFor(self.context)

        return information
示例#9
0
    def _information(self):
        """
        """
        shop = IShopManagement(self.context).getShop()
        im = IInformationManagement(shop)
        
        pvm = IProductVariantsManagement(self.context)
        
        if pvm.hasVariants() == False:
            information = im.getInformationPagesFor(self.context)
        else:
            product_variant = pvm.getSelectedVariant()

            # First, we try to get information for the selected product variant
            information = im.getInformationPagesFor(product_variant)

            # If nothing is found, we try to get information for parent product 
            # variants object.
            if information is None:
                information = im.getInformationPagesFor(self.context)
            
        return information
示例#10
0
    def __call__(self):
        """
        """
        # Set last seen products
        title = self.context.Title()
        short_title = title[:10] + "..."

        url = self.context.absolute_url()
            
        last_products = self.request.SESSION.get("last_products", [])
        if len(last_products) > 5:
            last_products = last_products[0:-1]

        urls = [p["url"] for p in last_products]

        if self.context.absolute_url() not in urls:
            last_products.insert(
                0, {
                    "title" : title,
                    "short_title" : short_title,
                    "url" : url
                    }
            )

        self.request.SESSION.set("last_products", last_products)

        pvm = IProductVariantsManagement(self.context)
        if pvm.hasVariants() == True and \
           self.request.get("variant_selected", None) is not None:

            selected_variant = pvm.getSelectedVariant()

            if selected_variant is None:
                putils = getToolByName(self.context, "plone_utils")
                putils.addPortalMessage(MESSAGES["VARIANT_DONT_EXIST"])

        return super(ProductView, self).__call__()
示例#11
0
    def asDict(self):
        """
        """
        pvm = IProductVariantsManagement(self.context)

        if pvm.hasVariants() == True:
            variant = pvm.getSelectedVariant() or pvm.getDefaultVariant()
            return IData(variant).asDict()
        else:
            # price
            cm = ICurrencyManagement(self.context)
            price = IPrices(self.context).getPriceForCustomer()
            price = cm.priceToString(price)

            # image
            image = IImageManagement(self.context).getMainImage()
            if image is not None:
                image = "%s/image_%s" % (image.absolute_url(), "preview")

            images = []
            for temp in IImageManagement(self.context).getImages():
                images.append("%s/image_tile" % temp.absolute_url())

            return {
                "article_id": self.context.getArticleId(),
                "title": self.context.Title(),
                "short_title": self.context.getShortTitle()
                or self.context.Title(),
                "description": self.context.Description(),
                "url": self.context.absolute_url(),
                "price": price,
                "image": image,
                "images": images,
                "text": self.context.getText(),
                "short_text": self.context.getShortText(),
            }
示例#12
0
    def addToCart(self, redirect=True, add_accessories=True):
        """
        """
        shop = IShopManagement(self.context).getShop()
        cm = ICartManagement(shop)

        cart = cm.getCart()
        if cart is None:
            cart = cm.createCart()

        pvm = IProductVariantsManagement(self.context)
        if pvm.hasVariants():

            # Get the actual "product"
            product = pvm.getSelectedVariant() or pvm.getDefaultVariant()

            # Using here the selected product ensures that we save the right
            # properties. This is important when the selected variant doesn't
            # exist.
            properties = []
            for property in product.getForProperties():
                property_id, selected_option = property.split(":")
                properties.append({
                    "id": property_id,
                    "selected_option": selected_option,
                })
        else:

            # The product is the context
            product = self.context

            # Unlike above we take the properties out of the request, because
            # there is no object wich stores the different properties.
            properties = []
            for property in IPropertyManagement(product).getProperties():
                selected_option_id = self.request.get(
                    "property_%s_%s" % (product.UID(), property.getId()))

                # If nothing is selected we take the first option of the
                # property
                if (selected_option_id is None) or (selected_option_id
                                                    == "select"):
                    property = IPropertyManagement(product).getProperty(
                        property.getId())
                    property_options = property.getOptions()

                    if property_options:
                        selected_option = property.getOptions()[0]
                        selected_option_id = selected_option["id"]
                    else:
                        selected_option_id = ""

                properties.append({
                    "id": property.getId(),
                    "selected_option": selected_option_id
                })

        # get quantity
        quantity = int(
            self.context.request.get("%s_quantity" % self.context.UID(), 1))

        # returns true if the product was already within the cart
        result, item_id = IItemManagement(cart).addItem(
            product, tuple(properties), quantity)

        # Add product to session (for display on add to cart view)
        if self.request.SESSION.get("added-to-cart") is None:
            self.request.SESSION["added-to-cart"] = []
        self.request.SESSION["added-to-cart"].append(item_id)

        # Add the accessories
        if add_accessories == True:
            catalog = getToolByName(self.context, "portal_catalog")
            accessories = tuplize(self.request.get("accessories", []))
            for uid in accessories:
                try:
                    brain = catalog.searchResults(UID=uid)[0]
                except IndexError:
                    continue

                # We reuse the same view with an other context. The context are
                # the accessories
                product = brain.getObject()
                view = getMultiAdapter((product, self.request),
                                       name="addToCart")
                view.addToCart(redirect=False, add_accessories=False)

        if redirect == True:
            # Set portal message
            # putils = getToolByName(self.context, "plone_utils")
            # if result == True:
            #     putils.addPortalMessage(MESSAGES["CART_INCREASED_AMOUNT"])
            # else:
            #     putils.addPortalMessage(MESSAGES["CART_ADDED_PRODUCT"])

            url = "%s/added-to-cart" % shop.absolute_url()
            self.context.request.response.redirect(url)