def test_stripquotes_get_args(self): args, kwargs = get_filter_args('"test",one="test",two=2', stripquotes=True) self.assertEqual(args[0], "test") self.assertEqual(kwargs["one"], "test") self.assertEqual(kwargs["two"], "2") args, kwargs = get_filter_args('"test",one="test",two=2', stripquotes=False) self.assertEqual(args[0], '"test"') self.assertEqual(kwargs["one"], '"test"')
def test_stripquotes_get_args(self): args, kwargs = get_filter_args('"test",one="test",two=2', stripquotes=True) self.assertEqual(args[0], 'test') self.assertEqual(kwargs['one'], 'test') self.assertEqual(kwargs['two'], '2') args, kwargs = get_filter_args('"test",one="test",two=2', stripquotes=False) self.assertEqual(args[0], '"test"') self.assertEqual(kwargs['one'], '"test"')
def test_stripquotes_get_args(self): args, kwargs = get_filter_args('"test",one="test",two=2', stripquotes=True) self.assertEqual(args[0], 'test') self.assertEqual(kwargs['one'], 'test') self.assertEqual(kwargs['two'], '2') args, kwargs = get_filter_args('"test",one="test",two=2', stripquotes=False) self.assertEqual(args[0], '"test"') self.assertEqual(kwargs['one'], '"test"')
def currency(value, args=""): """Convert a value to a money formatted string. places: required number of places after the decimal point curr: optional currency symbol before the sign (may be blank) wrapcents:tag to wrap the part after the decimal point Usage: val|currency val|currency:'places=2' val|currency:'places=2:wrapcents=sup' """ if value == '' or value is None: return value args, kwargs = get_filter_args(args, keywords=('places','curr', 'wrapcents'), intargs=('places',), stripquotes=True) try: value = Decimal(str(value)) except InvalidOperation: log.error("Could not convert value '%s' to decimal", value) raise if not 'places' in kwargs: kwargs['places'] = 2 return mark_safe(moneyfmt(value, **kwargs))
def test_numerical_get_args(self): args, kwargs = get_filter_args("test,one=1,two=2", (), ("one", "two")) self.assertEqual(args[0], "test") self.assertEqual(kwargs["one"], 1) self.assertEqual(kwargs["two"], 2)
def test_extended_get_args(self): args, kwargs = get_filter_args("test,one=1,two=2") self.assertEqual(args[0], "test") self.assertEqual(kwargs["one"], "1") self.assertEqual(kwargs["two"], "2")
def test_simple_get_args(self): args, kwargs = get_filter_args("one=1,two=2") self.assertEqual(len(args), 0) self.assertEqual(kwargs["one"], "1") self.assertEqual(kwargs["two"], "2")
def random_testimonials(tg, args): args, kwargs = get_filter_args(args, intargs=('length',)) ct = kwargs.get('length', 4) if tg: return tg.random_testimonies(ct) else: return []
def test_keystrip_get_args(self): args, kwargs = get_filter_args("test,one=1,two=2", ("one"), ("one")) self.assertEqual(args[0], "test") self.assertEqual(kwargs["one"], 1) self.assertFalse("two" in kwargs)
def currency(value, args=""): """Convert a value to a money formatted string. places: required number of places after the decimal point curr: optional currency symbol before the sign (may be blank) wrapcents:tag to wrap the part after the decimal point Usage: val|currency val|currency:'places=2' val|currency:'places=2:wrapcents=sup' """ if value == '' or value is None: return value args, kwargs = get_filter_args(args, keywords=('places', 'curr', 'wrapcents'), intargs=('places', ), stripquotes=True) try: value = Decimal(str(value)) except InvalidOperation: log.error("Could not convert value '%s' to decimal", value) raise return mark_safe(moneyfmt(value, **kwargs))
def test_simple_get_args(self): args, kwargs = get_filter_args('one=1,two=2') self.assertEqual(len(args), 0) self.assertEqual(kwargs['one'], '1') self.assertEqual(kwargs['two'], '2')
def test_numerical_get_args(self): args, kwargs = get_filter_args('test,one=1,two=2', (), ('one', 'two')) self.assertEqual(args[0], 'test') self.assertEqual(kwargs['one'], 1) self.assertEqual(kwargs['two'], 2)
def test_keystrip_get_args(self): args, kwargs = get_filter_args('test,one=1,two=2', ('one'), ('one')) self.assertEqual(args[0], 'test') self.assertEqual(kwargs['one'], 1) self.assertFalse('two' in kwargs)
def test_simple_get_args(self): args, kwargs = get_filter_args('one=1,two=2') self.assertEqual(len(args), 0) self.assertEqual(kwargs['one'], '1') self.assertEqual(kwargs['two'], '2')
def test_extended_get_args(self): args, kwargs = get_filter_args('test,one=1,two=2') self.assertEqual(args[0], 'test') self.assertEqual(kwargs['one'], '1') self.assertEqual(kwargs['two'], '2')
def test_extended_get_args(self): args, kwargs = get_filter_args('test,one=1,two=2') self.assertEqual(args[0], 'test') self.assertEqual(kwargs['one'], '1') self.assertEqual(kwargs['two'], '2')
def test_numerical_get_args(self): args, kwargs = get_filter_args('test,one=1,two=2', (), ('one','two')) self.assertEqual(args[0], 'test') self.assertEqual(kwargs['one'], 1) self.assertEqual(kwargs['two'], 2)
def test_keystrip_get_args(self): args, kwargs = get_filter_args('test,one=1,two=2', ('one'), ('one')) self.assertEqual(args[0], 'test') self.assertEqual(kwargs['one'], 1) self.assertFalse('two' in kwargs)
def normalize_decimal(value, args=""): """ PARTIAL UNIT ROUNDING DECIMAL Converts a valid float, integer, or string to a decimal number with a specified number of decimal places, performs "partial unit rounding", and decimal normalization. Usage: val|normalize_decimal val|normalize_decimal:'places=2' val|normalize_decimal:'places=2:roundfactor=.5' val|normalize_decimal:'places=2:roundfactor=.5:normalize=False' val The value to be converted and optionally formated to decimal. places The decimal place precision is defined by integer "places" and must be <= the precision defined in the decimal.Decimal context. roundfactor represents the maximum number of decimal places to display if normalize is False. roundfactor (partial unit rounding factor) If roundfactor is between 0 and 1, roundfactor rounds up (positive roundfactor value) or down (negative roundfactor value) in factional "roundfactor" increments. normalize If normalize is True (any value other than False), then rightmost zeros are truncated. General Filter/Template Usage. normalize_decimal is generally used without parameters in the template. Defaults are: places=2, roundfactor=None, normalize=True If normalize_decimal is not used as a template filter, the value (quantity) will display the full decimal value in the template field. """ if value == '' or value is None: return value args, kwargs = get_filter_args(args, keywords=('places', 'roundfactor', 'normalize'), intargs=('places', ), boolargs=('normalize', ), stripquotes=True) if not 'places' in kwargs: kwargs['places'] = 2 try: return mark_safe(str(round_decimal(val=value, **kwargs))) except RoundedDecimalError as e: log.error("normalize_decimal error val=%s, id-%s, msg=%s", (e.val, e.id, e.msg)) return value
def order_variable(order, args): """ Get a variable from an order Sample usage:: {{ order|order_variable:'variable' }} """ args, kwargs = get_filter_args(args) if len(args) != 1: raise template.TemplateSyntaxError("%r filter expected only a variable, got: %s" % (args[0], args)) return order.get_variable(args[0])
def product_category_siblings(product, args=""): args, kwargs = get_filter_args(args, keywords=('variations', 'include_self'), boolargs=('variations', 'include_self'), stripquotes=True) sibs = product.get_category.product_set.all().order_by('ordering', 'name') if not kwargs.get('variations', True): sibs = [sib for sib in sibs if not sib.has_variants] if not kwargs.get('include_self', True): sibs = [sib for sib in sibs if not sib == product] return sibs
def product_category_siblings(product, args=""): args, kwargs = get_filter_args(args, keywords=('variations', 'include_self'), boolargs=('variations', 'include_self'), stripquotes=True) sibs = product.get_category.product_set.all().order_by('ordering', 'name') if not kwargs.get('variations', True): sibs = [sib for sib in sibs if not sib.has_variants] if not kwargs.get('include_self', True): sibs = [sib for sib in sibs if not sib == product] return sibs
def normalize_decimal(value, args=""): """ PARTIAL UNIT ROUNDING DECIMAL Converts a valid float, integer, or string to a decimal number with a specified number of decimal places, performs "partial unit rounding", and decimal normalization. Usage: val|normalize_decimal val|normalize_decimal:'places=2' val|normalize_decimal:'places=2:roundfactor=.5' val|normalize_decimal:'places=2:roundfactor=.5:normalize=False' val The value to be converted and optionally formated to decimal. places The decimal place precision is defined by integer "places" and must be <= the precision defined in the decimal.Decimal context. roundfactor represents the maximum number of decimal places to display if normalize is False. roundfactor (partial unit rounding factor) If roundfactor is between 0 and 1, roundfactor rounds up (positive roundfactor value) or down (negative roundfactor value) in factional "roundfactor" increments. normalize If normalize is True (any value other than False), then rightmost zeros are truncated. General Filter/Template Usage. normalize_decimal is generally used without parameters in the template. Defaults are: places=2, roundfactor=None, normalize=True If normalize_decimal is not used as a template filter, the value (quantity) will display the full decimal value in the template field. """ if value == '' or value is None: return value args, kwargs = get_filter_args(args, keywords=('places','roundfactor', 'normalize'), intargs=('places',), boolargs=('normalize',), stripquotes=True) if not 'places' in kwargs: kwargs['places'] = 2 try: return mark_safe(str(round_decimal(val=value, **kwargs))) except RoundedDecimalError, e: log.error("normalize_decimal error val=%s, id-%s, msg=%s", (e.val, e.id, e.msg)) return value
def order_variable(order, args): """ Get a variable from an order Sample usage:: {{ order|order_variable:'variable' }} """ args, kwargs = get_filter_args(args) args = token.split_contents() if not len(args == 1): raise template.TemplateSyntaxError("%r filter expected variable, got: %s" % (args[0], args)) return order.get_variable(args[0])
def order_variable(order, args): """ Get a variable from an order Sample usage:: {{ order|order_variable:'variable' }} """ args, kwargs = get_filter_args(args) if len(args) != 1: raise template.TemplateSyntaxError( "%r filter expected only a variable, got: %s" % (args[0], args)) return order.get_variable(args[0])
def product_count(category, args=''): """Get a count of products for the base object. If `category` is None, then count everything. If it is a `Category` object then count everything in the category and subcategories. """ args, kwargs = get_filter_args(args, boolargs=('variations')) variations = kwargs.get('variations', False) try: ct = keyedcache.cache_get('product_count', category, variations) except keyedcache.NotCachedError: if not category: ct = Product.objects.active_by_site(variations=variations).count() else: ct = category.active_products(include_children=True, variations=variations).count() keyedcache.cache_set('product_count', category, args, value=ct) return ct
def product_count(category, args=''): """Get a count of products for the base object. If `category` is None, then count everything. If it is a `Category` object then count everything in the category and subcategories. """ args, kwargs = get_filter_args(args, boolargs=('variations')) variations = kwargs.get('variations', False) try: ct = keyedcache.cache_get('product_count', category, variations) except keyedcache.NotCachedError: if not category: ct = Product.objects.active_by_site(variations=variations).count() else: ct = category.active_products(include_children=True, variations=variations).count() keyedcache.cache_set('product_count', category, args, value=ct) return ct
def product_images(product, args=""): args, kwargs = get_filter_args(args, keywords=('include_main', 'maximum'), boolargs=('include_main'), intargs=('maximum'), stripquotes=True) q = product.productimage_set if kwargs.get('include_main', True): q = q.all() else: main = product.main_image q = q.exclude(id=main.id) maximum = kwargs.get('maximum', -1) if maximum > -1: q = list(q)[:maximum] return q
def product_images(product, args=""): args, kwargs = get_filter_args(args, keywords=('include_main', 'maximum'), boolargs=('include_main'), intargs=('maximum'), stripquotes=True) q = product.productimage_set if kwargs.get('include_main', True): q = q.all() else: main = product.main_image q = q.exclude(id = main.id) maximum = kwargs.get('maximum', -1) if maximum>-1: q = list(q)[:maximum] return q