Exemplo n.º 1
0
    def get_languages(self):
        """Returns available languages for current site

        .. note:: 
            A special method is required so that the fetch
            can be speeded up, by pushing the categories to the central cache
            which cannot be done directly on a browse node.
        """
        lang_obj = Pool().get('ir.lang')

        cache_key = key_from_list([
            Transaction().cursor.dbname,
            Transaction().user,
            'nereid.website.get_languages',
            ])
        # The website is automatically appended to the cache prefix
        rv = cache.get(cache_key)
        if rv is None:
            language_ids = lang_obj.search([('translatable', '=', True)])
            languages = lang_obj.browse(language_ids)
            rv = [{
                'id': l.id,
                'name': l.name,
                'code': l.code,
                } for l in languages]
            cache.set(cache_key, rv, 60*60)
        return rv
Exemplo n.º 2
0
 def get_categories(self):
     """Returns the IDS of the categories
     """
     cache_key = key_from_list([
         Transaction().cursor.dbname,
         Transaction().user,
         'nereid.website.get_categories',
         self.id,
     ])
     rv = cache.get(cache_key)
     if rv is None:
         rv = map(int, self.categories)
         cache.set(cache_key, rv, 60 * 60)
     return rv
Exemplo n.º 3
0
 def get_categories(self):
     """Returns the IDS of the categories
     """
     cache_key = key_from_list([
         Transaction().cursor.dbname,
         Transaction().user,
         'nereid.website.get_categories',
         request.nereid_website.id,
         ])
     rv = cache.get(cache_key)
     if rv is None:
         rv = [x.id for x in request.nereid_website.categories]
         cache.set(cache_key, rv, 60 * 60)
     return rv
Exemplo n.º 4
0
    def menu_for(cls, identifier, ident_field_value, objectified=False):
        """
        Returns a dictionary of menu tree

        :param identifier: The unique identifier from which the menu
                has to be chosen
        :param ident_field_value: The value of the field that has to be
                looked up on model with search on ident_field
        :param objectified: The value returned is the active record of
                the menu identified rather than a tree.
        """
        # First pick up the menu through identifier
        try:
            menu, = cls.search([
                ('unique_identifier', '=', identifier),
                ('website', '=', request.nereid_website.id),
            ])

        except ValueError:
            current_app.logger.error(
                "Menu %s could not be identified" % identifier)
            return NotFound()

        # Get the data from the model
        MenuItem = Pool().get(menu.model.model)
        try:
            root_menu_item, = MenuItem.search(
                [(menu.identifier_field.name, '=', ident_field_value)],
                limit=1)
        except ValueError:
            current_app.logger.error(
                "Menu %s could not be identified" % ident_field_value)
            return InternalServerError()

        if objectified:
            return root_menu_item

        cache_key = key_from_list([
            Transaction().cursor.dbname,
            Transaction().user,
            Transaction().language,
            identifier, ident_field_value,
            'nereid.cms.menu.menu_for',
        ])
        rv = cache.get(cache_key)
        if rv is None:
            rv = menu._generate_menu_tree(root_menu_item)
            cache.set(cache_key, rv, 60*60)
        return rv
Exemplo n.º 5
0
    def get_currencies(self):
        """Returns available currencies for current site

        .. note::
            A special method is required so that the fetch can be speeded up,
            by pushing the categories to the central cache which cannot be
            done directly on a browse node.
        """
        cache_key = key_from_list([Transaction().cursor.dbname, Transaction().user, "nereid.website.get_currencies"])
        # The website is automatically appended to the cache prefix
        rv = cache.get(cache_key)
        if rv is None:
            rv = [{"id": c.id, "name": c.name, "symbol": c.symbol} for c in self.currencies]
            cache.set(cache_key, rv, 60 * 60)
        return rv
Exemplo n.º 6
0
    def sale_price(self, quantity=0):
        """Return the Sales Price.
        A wrapper designed to work as a context variable in templating

        The price is calculated from the pricelist associated with the current
        user. The user in the case of guest user is logged in user. In the
        event that the logged in user does not have a pricelist set against
        the user, the guest user's pricelist is chosen.

        Finally if neither the guest user, nor the regsitered user has a
        pricelist set against them then the list price is displayed as the
        list price of the product

        :param quantity: Quantity
        """
        price_list = request.nereid_user.party.sale_price_list.id if request.nereid_user.party.sale_price_list else None

        # If the registered user does not have a pricelist try for
        # the pricelist of guest user
        if not request.is_guest_user and price_list is None:
            guest_user = request.nereid_website.guest_user
            price_list = guest_user.party.sale_price_list.id if guest_user.party.sale_price_list else None

        # Build a Cache key to store in cache
        cache_key = key_from_list(
            [
                Transaction().cursor.dbname,
                Transaction().user,
                request.nereid_user.party.id,
                price_list,
                self.id,
                quantity,
                request.nereid_currency.id,
                "product.product.sale_price",
            ]
        )
        price = cache.get(cache_key)
        if price is None:
            # There is a valid pricelist, now get the price
            with Transaction().set_context(
                customer=request.nereid_user.party.id, price_list=price_list, currency=request.nereid_currency.id
            ):
                price = self.get_sale_price([self], quantity)[self.id]

            # Now convert the price to the session currency
            cache.set(cache_key, price, 60 * 5)
        return price
Exemplo n.º 7
0
    def sale_price(self, quantity=0):
        """Return the Sales Price.
        A wrapper designed to work as a context variable in templating

        The price is calculated from the pricelist associated with the current
        user. The user in the case of guest user is logged in user. In the
        event that the logged in user does not have a pricelist set against
        the user, the guest user's pricelist is chosen.

        Finally if neither the guest user, nor the regsitered user has a
        pricelist set against them then the list price is displayed as the
        list price of the product

        :param quantity: Quantity
        """
        Sale = Pool().get('sale.sale')

        price_list = Sale.default_price_list()

        if current_user.is_anonymous:
            customer = current_website.guest_user.party
        else:
            customer = current_user.party

        # Build a Cache key to store in cache
        cache_key = key_from_list([
            Transaction().cursor.dbname,
            Transaction().user,
            customer.id,
            price_list, self.id, quantity,
            current_locale.currency.id,
            'product.product.sale_price',
        ])
        price = cache.get(cache_key)
        if price is None:
            # There is a valid pricelist, now get the price
            with Transaction().set_context(
                customer=customer.id,
                price_list=price_list,
                currency=current_locale.currency.id
            ):
                price = self.get_sale_price([self], quantity)[self.id]

            # Now convert the price to the session currency
            cache.set(cache_key, price, 60 * 5)
        return price