Exemple #1
0
class Image(Domain):
    """Class responsible for storing images and it's description

    See also:
    `schema <http://doc.stoq.com.br/schema/tables/image.html>`__

    """

    implements(IDescribable)

    __storm_table__ = 'image'

    (THUMBNAIL_SIZE_HEIGHT, THUMBNAIL_SIZE_WIDTH) = (64, 64)

    #: the image itself in a bin format
    image = BLOBCol(default=None)

    #: the image thumbnail in a bin format
    thumbnail = BLOBCol(default=None)

    #: the image description
    description = UnicodeCol(default=u'')

    #
    #  Public API
    #

    def get_base64_encoded(self):
        return base64.b64encode(self.image)

    #
    #  IDescribable implementation
    #

    def get_description(self):
        if self.description:
            return self.description
        # TODO: Dont use id here
        return _(u"Stoq image #%d") % (self.id, )

    #
    #  ORMObject
    #

    @classmethod
    def delete(cls, id, store):
        ImageRemoveEvent.emit(cls.get(id, store))
        super(Image, cls).delete(id, store)

    #
    # Domain
    #

    def on_create(self):
        ImageCreateEvent.emit(self)

    def on_update(self):
        ImageEditEvent.emit(self)
Exemple #2
0
class Certificate(Domain):
    __storm_table__ = 'certificate'

    TYPE_PKCS11 = u'pkcs11'
    TYPE_PKCS12 = u'pkcs12'

    types_str = collections.OrderedDict([
        (TYPE_PKCS11, _("A3: Smartcard")),
        (TYPE_PKCS12, _("A1: Digital certificate")),
    ])

    #: The type of the certificate
    type = EnumCol(allow_none=False, default=TYPE_PKCS12)

    #: If the certificate is active or not
    active = BoolCol(default=True)

    #: The name of the certificate/lib when it was uploaded to the dataabse
    name = UnicodeCol(default=u'')

    #: The content of the certificate. The library file for PKCS11
    #: or the certificate itself for PKCS12
    content = BLOBCol()

    #: The certificate password. If it is ``None`` it means that the user
    #: should be asked each time it is going to be used (for PKCS11 only)
    _password = BLOBCol(name='password', allow_none=True, default=None)

    #: The certificate expiration date.
    expiration_date = DateTimeCol(default=None)

    @property
    def password(self):
        po = PasswordObfuscator()
        po.hashed_password = self._password and self._password
        return po

    @password.setter
    def password(self, password):
        assert isinstance(password, PasswordObfuscator)
        hashed = password.hashed_password
        self._password = hashed

    @property
    def type_str(self):
        return self.types_str[self.type]

    @classmethod
    def get_active_certs(cls, store, exclude=None):
        """Get active certificates except the one given in exclude parameter"""
        except_id = exclude and exclude.id
        return store.find(cls, And(Eq(cls.active, True), cls.id != except_id))
Exemple #3
0
class PluginEgg(Domain):
    """A cache for plugins eggs"""

    __storm_table__ = 'plugin_egg'

    plugin_name = UnicodeCol()
    egg_content = BLOBCol(default=None)
    egg_md5sum = UnicodeCol(default=None)
Exemple #4
0
class Attachment(Domain):
    __storm_table__ = 'attachment'

    #: the attachment name
    name = UnicodeCol(default=u'')

    #: MIME for the filetype attached
    mimetype = UnicodeCol(default=u'')

    #: blob that contains the file
    blob = BLOBCol(default=None)

    #
    #  IDescribable implementation
    #

    def get_description(self):
        return self.name
Exemple #5
0
class DeviceConstant(Domain):
    """
    Describes a device constant

    The constant_value field is only used by custom tax codes,
    eg when constant_type is TYPE_TAX and constant_enum is TaxType.CUSTOM

    @cvar constant_type: the type of constant
    @cvar constant_name: name of the constant
    @cvar constant_enum: enum value of the constant
    @cvar constant_value: value of the constant, only for TAX constants for
      which it represents the tax percentage
    @cvar device_value: the device value
    @cvar printer: printer
    """

    __storm_table__ = 'device_constant'

    constant_type = EnumCol()
    constant_name = UnicodeCol()
    constant_value = DecimalCol(default=None)
    constant_enum = IntCol(default=None)
    device_value = BLOBCol()
    printer_id = IdCol()
    printer = Reference(printer_id, 'ECFPrinter.id')

    TYPE_UNIT = u'unit'
    TYPE_TAX = u'tax'
    TYPE_PAYMENT = u'payment'

    constant_types = {TYPE_UNIT: _(u'Unit'),
                      TYPE_TAX: _(u'Tax'),
                      TYPE_PAYMENT: _(u'Payment')}

    def get_constant_type_description(self):
        """
        Describe the type in a human readable form
        @returns: description of the constant type
        @rtype: str
        """
        return DeviceConstant.constant_types[self.constant_type]

    @classmethod
    def get_custom_tax_constant(cls, printer, constant_value, store):
        """
        Fetches a custom tax constant.

        @param printer: printer to fetch constants from
        @type printer: :class:`ECFPrinter`
        @param constant_enum: tax enum code
        @type constant_enum: int
        @param store: a store
        @returns: the constant
        @rtype: :class:`DeviceConstant`
        """
        return store.find(DeviceConstant,
                          printer=printer,
                          constant_type=DeviceConstant.TYPE_TAX,
                          constant_enum=int(TaxType.CUSTOM),
                          constant_value=constant_value).one()

    @classmethod
    def get_tax_constant(cls, printer, constant_enum, store):
        """
        Fetches a tax constant.
        Note that you need to use :class:`ECFPrinter.get_custom_tax_constant`
        for custom tax constants.

        @param printer: printer to fetch constants from
        @type printer: :class:`ECFPrinter`
        @param constant_enum: tax enum code
        @type constant_enum: int
        @param store: a store
        @returns: the constant
        @rtype: :class:`DeviceConstant`
        """
        if constant_enum == TaxType.CUSTOM:
            raise ValueError("Use get_custom_tax_constant for custom "
                             "tax codes")
        return store.find(DeviceConstant,
                          printer=printer,
                          constant_type=DeviceConstant.TYPE_TAX,
                          constant_enum=int(constant_enum)).one()

    def get_description(self):
        return self.constant_name
Exemple #6
0
class Image(Domain):
    """Class responsible for storing images and it's description

    See also:
    `schema <http://doc.stoq.com.br/schema/tables/image.html>`__

    """

    __storm_table__ = 'image'

    (THUMBNAIL_SIZE_HEIGHT, THUMBNAIL_SIZE_WIDTH) = (128, 128)

    #: the image itself in a bin format
    image = BLOBCol(default=None)

    #: the image thumbnail in a bin format
    thumbnail = BLOBCol(default=None)

    #: the image description
    description = UnicodeCol(default=u'')

    #: The image filename
    filename = UnicodeCol(default=u'')

    #: The date that this image was uploaded to the database
    create_date = DateTimeCol(default_factory=StatementTimestamp)

    #: Some keywords for this image
    keywords = UnicodeCol(default=u'')

    #: Some notes about the image
    notes = UnicodeCol(default=u'')

    #: If this is the main image. Only makes sense if :obj:`.sellable`
    #: is not `None`
    is_main = BoolCol(default=False)

    #: If this image is only for internal use (i.e. it won't be synchronized
    #: to any e-commerce website to be displayed publicly)
    internal_use = BoolCol(default=False)

    sellable_id = IdCol(default=None)
    #: The |sellable| that this image belongs to
    sellable = Reference(sellable_id, 'Sellable.id')

    category_id = IdCol(default=None)
    #: The |category| that this image belongs to
    category = Reference(category_id, 'SellableCategory.id')

    station_type_id = IdCol(default=None)
    #: The station type this image should be used instead of the main image.
    station_type = Reference(station_type_id, 'StationType.id')

    #
    #  Public API
    #

    def get_base64_encoded(self):
        return base64.b64encode(self.image).decode()

    #
    #  IDescribable implementation
    #

    def get_description(self):
        if self.description:
            return self.description
        return _(u"Stoq image")

    #
    #  ORMObject
    #

    @classmethod
    def delete(cls, id, store):
        image = store.get(cls, id)
        ImageRemoveEvent.emit(image)
        store.remove(image)

    #
    # Domain
    #

    def on_create(self):
        ImageCreateEvent.emit(self)

    def on_update(self):
        ImageEditEvent.emit(self)
Exemple #7
0
class Image(Domain):
    __storm_table__ = 'image'

    image = BLOBCol(default=None)
    thumbnail = BLOBCol(default=None)
    description = UnicodeCol(default=u'')