def test_basics(self): app = flask.Flask(__name__) b = babel.Babel(app) n = 1099 with app.test_request_context(): assert babel.format_number(n) == u'1,099' assert babel.format_decimal(Decimal('1010.99')) == u'1,010.99' assert babel.format_currency(n, 'USD') == '$1,099.00' assert babel.format_percent(0.19) == '19%' assert babel.format_scientific(10000) == u'1E4'
def test_basics(self): app = flask.Flask(__name__) b = babel.Babel(app) n = 1099 with app.test_request_context(): assert babel.format_number(n) == u"1,099" assert babel.format_decimal(Decimal("1010.99")) == u"1,010.99" assert babel.format_currency(n, "USD") == "$1,099.00" assert babel.format_percent(0.19) == "19%" assert babel.format_scientific(10000) == u"1E4"
def recent_products(self): """ GET --- Return a list of recently visited products in JSON POST ---- Add the product to the recent list manually. This method is required if the product page is cached, or is served by a Caching Middleware like Varnish which may clear the session before sending the request to Nereid. Just as with GET the response is the AJAX of recent products """ if request.method == 'POST': self._add_to_recent_list(request.form.get('product_id', type=int)) fields = request.args.getlist('fields') if fields: allowed_fields = [ f for f in fields if f in self.json_allowed_fields ] else: allowed_fields = self.json_allowed_fields[:] products = [] if 'sale_price' in allowed_fields: allowed_fields.remove('sale_price') if hasattr(session, 'sid'): product_ids = session.get('recent-products', []) products = self.read(product_ids, allowed_fields) for product in products: product['sale_price'] = format_currency( self.sale_price(product['id']), request.nereid_currency.code ) return jsonify( products = products )
def recent_products(cls): """ GET --- Return a list of recently visited products in JSON POST ---- Add the product to the recent list manually. This method is required if the product page is cached, or is served by a Caching Middleware like Varnish which may clear the session before sending the request to Nereid. Just as with GET the response is the AJAX of recent products """ if request.method == 'POST': cls._add_to_recent_list(request.form.get('product_id', type=int)) fields = set(request.args.getlist('fields')) or cls.json_allowed_fields fields = fields & cls.json_allowed_fields if 'sale_price' in fields: fields.remove('sale_price') response = [] if hasattr(session, 'sid'): products = cls.browse(session.get('recent-products', [])) for product in products: product_val = {} for field in fields: product_val[field] = getattr(product, field) product_val['sale_price'] = format_currency( product.sale_price(), request.nereid_currency.code ) response.append(product_val) return jsonify(products=response)