Exemplo n.º 1
0
def initialize_sql(engine, drop_all=False):
    DBSession.registry.clear()
    DBSession.configure(bind=engine)
    metadata.bind = engine

    if drop_all or os.environ.get('KOTTI_TEST_DB_STRING'):
        metadata.reflect()
        metadata.drop_all(engine)

    # Allow users of Kotti to cherry pick the tables that they want to use:
    settings = get_current_registry().settings
    tables = settings['kotti.use_tables'].strip() or None
    if tables:
        tables = [metadata.tables[name] for name in tables.split()]

    if engine.dialect.name == 'mysql':  # pragma: no cover
        from sqlalchemy.dialects.mysql.base import LONGBLOB
        File.__table__.c.data.type = LONGBLOB()

    # Allow migrations to set the 'head' stamp in case the database is
    # initialized freshly:
    if not engine.table_names():
        stamp_heads()

    metadata.create_all(engine, tables=tables)
    if os.environ.get('KOTTI_DISABLE_POPULATORS', '0') not in ('1', 'y'):
        for populate in get_settings()['kotti.populators']:
            populate()
    commit()

    return DBSession
Exemplo n.º 2
0
class File(Node):
    """
        A file model
    """
    __tablename__ = 'file'
    __table_args__ = default_table_args
    __mapper_args__ = {'polymorphic_identity': 'file'}
    id = Column(Integer, ForeignKey('node.id'), primary_key=True)
    description = Column(String(100), default="")
    data = deferred(Column(LONGBLOB()))
    mimetype = Column(String(100))
    size = Column(Integer)

    def getvalue(self):
        """
        Method making our file object compatible with the common file rendering
        utility
        """
        return self.data

    @property
    def label(self):
        """
        Simple shortcut for getting a label for this file
        """
        return self.description or self.name

    @property
    def data_obj(self):
        res = StringIO()
        res.write(self.data)
        return res
Exemplo n.º 3
0
class ConfigFiles(DBBASE):
    """
        A file model
    """
    __tablename__ = 'config_files'
    __table_args__ = default_table_args
    id = Column(Integer, primary_key=True)
    key = Column(String(100), unique=True)
    name = Column(String(100))
    data = deferred(Column(LONGBLOB()))
    mimetype = Column(String(100))
    size = Column(Integer)

    def getvalue(self):
        """
        Method making our file object compatible with the common file rendering
        utility
        """
        return self.data

    @property
    def label(self):
        """
        Simple shortcut for getting a label for this file
        """
        return self.name

    @classmethod
    def get(cls, key):
        """
        Override the default get method to get by key and not by id
        """
        return cls.query().filter(cls.key == key).first()

    @classmethod
    def set(cls, key, appstruct):
        """
        Set a file for the given key, if the key isn't field yet, add a new
        instance
        """
        instance = cls.get(key)
        if instance is None:
            instance = cls(key=key)
        for attr_name, attr_value in appstruct.items():
            setattr(instance, attr_name, attr_value)
        if instance.id is not None:
            DBSESSION().merge(instance)
        else:
            DBSESSION().add(instance)
Exemplo n.º 4
0
def adjust_for_engine(conn, branch):
    # adjust for engine type

    if conn.engine.dialect.name == 'mysql':  # pragma: no cover
        from sqlalchemy.dialects.mysql.base import LONGBLOB
        DBStoredFile.__table__.c.data.type = LONGBLOB()

    # sqlite's Unicode columns return a buffer which can't be encoded by
    # a json encoder. We have to convert to a unicode string so that the value
    # can be saved corectly by
    # :class:`depot.fields.sqlalchemy.upload.UploadedFile`

    def patched_processed_result_value(self, value, dialect):
        if not value:
            return None
        return self._upload_type.decode(unicode(value))

    if conn.engine.dialect.name == 'sqlite':  # pragma: no cover
        from depot.fields.sqlalchemy import UploadedFileField
        UploadedFileField.process_result_value = patched_processed_result_value