Exemplo n.º 1
0
    def as_string(self, value, format='png'):
        if value is ValueUnset:
            return ''

        success, buffer_ = value.save_to_bufferv(format, [], [])
        assert success
        return buffer_

    def from_string(self, value, format='png'):
        try:
            return pixbuf_from_string(value, format)
        except GObject.GError as e:
            raise ValidationError(_("Could not load image: %s") % e)

converter.add(_PixbufConverter)


@implementer(IProxyWidget)
class ProxyWidgetMixin(object):
    """This class is a mixin that provide a common interface for KiwiWidgets.

    Usually the Proxy class need to set and get data from the widgets. It also
    need a validation framework.

    :cvar allowed_data_types: A list of types which we are allowed to use
      in this class.
    """

    allowed_data_types = ()
Exemplo n.º 2
0
            symbol = self.symbol

        if precision is None:
            precision = self.precision

        return value.format(symbol, precision)

    def from_string(self, value):
        if value == '':
            return ValueUnset
        try:
            return currency(value)
        except (ValueError, decimal.InvalidOperation):
            raise ValidationError(
                _("%s can not be converted to a currency") % value)


converter.add(_CurrencyConverter)


def format_price(value, symbol=True, precision=None):
    """
    Formats a price according to the current locales monetary
    settings

    :param value: number
    :param symbol: whether to include the currency symbol
    """

    return currency(value).format(symbol, precision)
Exemplo n.º 3
0
 def testFake(self):
     self.assertRaises(KeyError, converter.remove, FakeConverter)
     converter.add(FakeConverter)
     self.assertRaises(ValueError, converter.add, FakeConverter)
     converter.remove(FakeConverter)
     self.assertRaises(KeyError, converter.remove, FakeConverter)
Exemplo n.º 4
0
 def testFake(self):
     self.assertRaises(KeyError, converter.remove, FakeConverter)
     converter.add(FakeConverter)
     self.assertRaises(ValueError, converter.add, FakeConverter)
     converter.remove(FakeConverter)
     self.assertRaises(KeyError, converter.remove, FakeConverter)
Exemplo n.º 5
0
        if symbol is None:
            symbol = self.symbol

        if precision is None:
            precision = self.precision

        return value.format(symbol, precision)

    def from_string(self, value):
        if value == '':
            return ValueUnset
        try:
            return currency(value)
        except (ValueError, decimal.InvalidOperation):
            raise ValidationError(
                _("%s can not be converted to a currency") % value)

converter.add(_CurrencyConverter)


def format_price(value, symbol=True, precision=None):
    """
    Formats a price according to the current locales monetary
    settings

    :param value: number
    :param symbol: whether to include the currency symbol
    """

    return currency(value).format(symbol, precision)