예제 #1
0
    def _save_data(target, value, oldvalue, initiator):
        """ Refresh metadata and save the binary data to the data field.

        :param target: The File instance
        :type target: :class:`kotti.resources.File` or subclass

        :param value: The container for binary data
        :type value: A :class:`cgi.FieldStorage` instance
        """

        if isinstance(value, bytes):
            value = _to_fieldstorage(fp=StringIO(value),
                                     filename=target.filename,
                                     mimetype=target.mimetype,
                                     size=len(value))

        newvalue = _SQLAMutationTracker._field_set(target, value, oldvalue,
                                                   initiator)

        if newvalue is None:
            return

        target.filename = newvalue.filename
        target.mimetype = newvalue.content_type
        target.size = newvalue.file.content_length

        return newvalue
예제 #2
0
 def edit(self, **appstruct):
     title = appstruct["title"]
     self.context.title = title
     self.context.description = appstruct["description"]
     self.context.tags = appstruct["tags"]
     if appstruct["file"]:
         self.context.data = _to_fieldstorage(**appstruct["file"])
예제 #3
0
    def _save_data(cls, target, value, oldvalue, initiator):
        """ Refresh metadata and save the binary data to the data field.

        :param target: The File instance
        :type target: :class:`kotti.resources.File` or subclass
        :param value: The container for binary data
        :type value: A :class:`cgi.FieldStorage` instance
        """

        if isinstance(value, bytes):
            value = _to_fieldstorage(fp=StringIO(value),
                                     filename=target.filename,
                                     mimetype=target.mimetype,
                                     size=len(value))

        newvalue = _SQLAMutationTracker._field_set(
            target, value, oldvalue, initiator)

        if newvalue is None:
            return

        target.filename = newvalue.filename
        target.mimetype = newvalue.content_type
        target.size = newvalue.file.content_length

        return newvalue
예제 #4
0
 def edit(self, **appstruct):
     title = appstruct['title']
     self.context.title = title
     self.context.description = appstruct['description']
     self.context.tags = appstruct['tags']
     if appstruct['file']:
         self.context.data = _to_fieldstorage(**appstruct['file'])
예제 #5
0
파일: resources.py 프로젝트: castaf/Kotti
    def _save_data(target: 'File',
                   value: Optional[Union[FieldStorage, bytes, UploadedFile, BufferedReader]],  # noqa
                   oldvalue: Optional[Union[UploadedFile, _symbol]],
                   initiator: Event) -> Optional[UploadedFile]:
        """ Refresh metadata and save the binary data to the data field.

        :param target: The File instance
        :type target: :class:`kotti.resources.File` or subclass

        :param value: The container for binary data
        :type value: A :class:`cgi.FieldStorage` instance
        """

        if isinstance(value, bytes):
            fp = BytesIO(value)
            value = _to_fieldstorage(fp=fp,
                                     filename=target.filename,
                                     mimetype=target.mimetype,
                                     size=len(value))

        newvalue = _SQLAMutationTracker._field_set(
            target, value, oldvalue, initiator)

        if newvalue is None:
            return

        target.filename = newvalue.filename
        target.mimetype = newvalue.content_type
        target.size = newvalue.file.content_length

        return newvalue
예제 #6
0
파일: content.py 프로젝트: xiang12383/Kotti
 def edit(self, **appstruct):
     title = appstruct['title']
     self.context.title = title
     self.context.description = appstruct['description']
     self.context.tags = appstruct['tags']
     if appstruct['file'] and appstruct['file']['fp']:
         self.context.data = _to_fieldstorage(**appstruct['file'])
예제 #7
0
파일: filedepot.py 프로젝트: Kotti/Kotti
def migrate_storage(from_storage: str, to_storage: str) -> None:

    log = logging.getLogger(__name__)

    old_default = DepotManager._default_depot
    DepotManager._default_depot = to_storage

    for klass, props in _SQLAMutationTracker.mapped_entities.items():
        log.info("Migrating %r", klass)

        mapper = klass._sa_class_manager.mapper

        # use type column to avoid polymorphism issues, getting the same
        # Node item multiple times.
        type_ = camel_case_to_name(klass.__name__)
        for instance in DBSession.query(klass).filter_by(type=type_):
            for prop in props:
                uf = getattr(instance, prop)
                if not uf:
                    continue
                pk = mapper.primary_key_from_instance(instance)
                log.info("Migrating %s for %r with pk %r", prop, klass, pk)

                filename = uf["filename"]
                content_type = uf["content_type"]
                data = _to_fieldstorage(
                    fp=uf.file,
                    filename=filename,
                    mimetype=content_type,
                    size=uf.file.content_length,
                )
                setattr(instance, prop, data)

    DepotManager._default_depot = old_default
예제 #8
0
def migrate_storage(from_storage, to_storage):

    log = logging.getLogger(__name__)

    old_default = DepotManager._default_depot
    DepotManager._default_depot = to_storage

    for klass, props in _SQLAMutationTracker.mapped_entities.items():
        log.info("Migrating %r", klass)

        mapper = klass._sa_class_manager.mapper

        # use type column to avoid polymorphism issues, getting the same
        # Node item multiple times.
        type_ = camel_case_to_name(klass.__name__)
        for instance in DBSession.query(klass).filter_by(type=type_):
            for prop in props:
                uf = getattr(instance, prop)
                if not uf:
                    continue
                pk = mapper.primary_key_from_instance(instance)
                log.info("Migrating %s for %r with pk %r", prop, klass, pk)

                filename = uf['filename']
                content_type = uf['content_type']
                data = _to_fieldstorage(fp=uf.file,
                                        filename=filename,
                                        mimetype=content_type,
                                        size=uf.file.content_length)
                setattr(instance, prop, data)

    DepotManager._default_depot = old_default
예제 #9
0
 def edit(self, **appstruct):
     title = appstruct["title"]
     self.context.title = title
     self.context.description = appstruct["description"]
     self.context.tags = appstruct["tags"]
     if appstruct["file"] and appstruct["file"]["fp"]:
         self.context.data = _to_fieldstorage(**appstruct["file"])
예제 #10
0
    def _save_data(
            target: 'File',
            value: Optional[Union[FieldStorage, bytes, UploadedFile,
                                  BufferedReader]],  # noqa
            oldvalue: Optional[Union[UploadedFile, _symbol]],
            initiator: Event) -> Optional[UploadedFile]:
        """ Refresh metadata and save the binary data to the data field.

        :param target: The File instance
        :type target: :class:`kotti.resources.File` or subclass

        :param value: The container for binary data
        :type value: A :class:`cgi.FieldStorage` instance
        """

        if isinstance(value, bytes):
            fp = BytesIO(value)
            value = _to_fieldstorage(fp=fp,
                                     filename=target.filename,
                                     mimetype=target.mimetype,
                                     size=len(value))

        newvalue = _SQLAMutationTracker._field_set(target, value, oldvalue,
                                                   initiator)

        if newvalue is None:
            return

        target.filename = newvalue.filename
        target.mimetype = newvalue.content_type
        target.size = newvalue.file.content_length

        return newvalue
예제 #11
0
 def add(self, **appstruct):
     filename = appstruct["file"]["filename"]
     item = self.item_class(
         title=appstruct["title"] or filename,
         description=appstruct["description"],
         tags=appstruct["tags"],
         data=_to_fieldstorage(**appstruct["file"]),
     )
     return item
예제 #12
0
 def add(self, **appstruct):
     filename = appstruct['file']['filename']
     item = self.item_class(
         title=appstruct['title'] or filename,
         description=appstruct['description'],
         tags=appstruct['tags'],
         data=_to_fieldstorage(**appstruct['file']),
     )
     return item
예제 #13
0
파일: content.py 프로젝트: xiang12383/Kotti
 def add(self, **appstruct):
     filename = appstruct['file']['filename']
     item = self.item_class(
         title=appstruct['title'] or filename,
         description=appstruct['description'],
         tags=appstruct['tags'],
         data=_to_fieldstorage(**appstruct['file']),
     )
     return item
예제 #14
0
 def add(self, **appstruct):
     filename = appstruct["file"]["filename"]
     item = self.item_class(
         title=appstruct["title"] or filename,
         description=appstruct["description"],
         tags=appstruct["tags"],
         data=_to_fieldstorage(**appstruct["file"]),
     )
     return item