当前位置: 首页>>代码示例>>Python>>正文


Python DefaultStorage.set_contents_from_file方法代码示例

本文整理汇总了Python中django.core.files.storage.DefaultStorage.set_contents_from_file方法的典型用法代码示例。如果您正苦于以下问题:Python DefaultStorage.set_contents_from_file方法的具体用法?Python DefaultStorage.set_contents_from_file怎么用?Python DefaultStorage.set_contents_from_file使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在django.core.files.storage.DefaultStorage的用法示例。


在下文中一共展示了DefaultStorage.set_contents_from_file方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: export_buildings

# 需要导入模块: from django.core.files.storage import DefaultStorage [as 别名]
# 或者: from django.core.files.storage.DefaultStorage import set_contents_from_file [as 别名]
def export_buildings(export_id, export_name, export_type,
                     building_ids, export_model='seed.BuildingSnapshot',
                     selected_fields=None):
    model = get_model(*export_model.split("."))

    selected_buildings = model.objects.filter(pk__in=building_ids)

    def _row_cb(i):
        cache.set("export_buildings__%s" % export_id, i)

    my_exporter = getattr(exporter, "export_%s" % export_type, None)
    if not my_exporter:
        _row_cb(-1)  # this means there was an error
        return

    exported_filename = my_exporter(selected_buildings,
                                    selected_fields,
                                    _row_cb)
    exported_file = open(exported_filename)

    s3_keyname = exporter._make_export_filename(export_id,
                                                export_name,
                                                export_type)
    s3_key = DefaultStorage().bucket.new_key(s3_keyname)
    s3_key.set_contents_from_file(exported_file)

    exported_file.close()
    os.remove(exported_filename)

    _row_cb(selected_buildings.count())  # means we're done!
开发者ID:gunduru,项目名称:seed,代码行数:32,代码来源:tasks.py

示例2: export

# 需要导入模块: from django.core.files.storage import DefaultStorage [as 别名]
# 或者: from django.core.files.storage.DefaultStorage import set_contents_from_file [as 别名]
    def export(self, buildings, fields, row_cb):
        """
        The main method of export. Uses the export type defined by the initializer

        :param buildings: Array of building ids to export
        :param fields: Array of fields to export
        :param row_cb: ID for row cache
        :return:
        """

        export_method = getattr(self, "export_%s" % self.export_type, None)
        if export_method:
            export_method(buildings, fields, row_cb)
        else:
            return None

        # save the tempfile to the file storage location (s3 or local)
        if not self.tempfile == None:
            if 'FileSystemStorage' in settings.DEFAULT_FILE_STORAGE:
                # This is non-ideal. We should just save the file in the right location to start with
                # or return the file from the "export". This was done to avoid changing the exporter code 'too much'.
                file_storage = DefaultStorage()
                f = open(self.tempfile, 'r')
                file_storage.save(self.filename(), f)
                f.close()
            else:
                s3_key = DefaultStorage().bucket.new_key(self.filename())
                f = open(self.tempfile)
                s3_key.set_contents_from_file(f)
                f.close()
                os.remove(self.tempfile)

        return self.filename
开发者ID:Maalka,项目名称:seed,代码行数:35,代码来源:exporter.py


注:本文中的django.core.files.storage.DefaultStorage.set_contents_from_file方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。