本文整理汇总了Python中soil.util.expose_cached_download函数的典型用法代码示例。如果您正苦于以下问题:Python expose_cached_download函数的具体用法?Python expose_cached_download怎么用?Python expose_cached_download使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了expose_cached_download函数的19个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: cache_file_to_be_served
def cache_file_to_be_served(tmp, checkpoint, download_id, format=None, filename=None, expiry=10*60*60):
"""
tmp can be either either a path to a tempfile or a StringIO
(the APIs for tempfiles vs StringIO are unfortunately... not similar)
"""
if checkpoint:
format = Format.from_format(format)
try:
filename = unidecode(filename)
except Exception:
pass
escaped_filename = escape_quotes('%s.%s' % (filename, format.extension))
payload = tmp.payload
expose_cached_download(payload, expiry, ".{}".format(format.extension),
mimetype=format.mimetype,
content_disposition='attachment; filename="%s"' % escaped_filename,
extras={'X-CommCareHQ-Export-Token': checkpoint.get_id},
download_id=download_id)
tmp.delete()
else:
# this just gives you a link saying there wasn't anything there
expose_cached_download("Sorry, there wasn't any data.", expiry, None,
content_disposition="",
mimetype="text/html",
download_id=download_id).save(expiry)
开发者ID:dimagi,项目名称:commcare-hq,代码行数:27,代码来源:tasks.py
示例2: build_application_zip
def build_application_zip(include_multimedia_files, include_index_files, app,
download_id, build_profile_id=None, compress_zip=False, filename="commcare.zip"):
from corehq.apps.hqmedia.views import iter_app_files
DownloadBase.set_progress(build_application_zip, 0, 100)
errors = []
compression = zipfile.ZIP_DEFLATED if compress_zip else zipfile.ZIP_STORED
use_transfer = settings.SHARED_DRIVE_CONF.transfer_enabled
if use_transfer:
fpath = os.path.join(settings.SHARED_DRIVE_CONF.transfer_dir, "{}{}{}{}{}".format(
app._id,
'mm' if include_multimedia_files else '',
'ccz' if include_index_files else '',
app.version,
build_profile_id
))
else:
_, fpath = tempfile.mkstemp()
if not (os.path.isfile(fpath) and use_transfer): # Don't rebuild the file if it is already there
files, errors = iter_app_files(app, include_multimedia_files, include_index_files, build_profile_id)
with open(fpath, 'wb') as tmp:
with zipfile.ZipFile(tmp, "w") as z:
for path, data in files:
# don't compress multimedia files
extension = os.path.splitext(path)[1]
file_compression = zipfile.ZIP_STORED if extension in MULTIMEDIA_EXTENSIONS else compression
z.writestr(path, data, file_compression)
common_kwargs = dict(
mimetype='application/zip' if compress_zip else 'application/x-zip-compressed',
content_disposition='attachment; filename="{fname}"'.format(fname=filename),
download_id=download_id,
)
if use_transfer:
expose_file_download(
fpath,
use_transfer=use_transfer,
**common_kwargs
)
else:
expose_cached_download(
FileWrapper(open(fpath)),
expiry=(1 * 60 * 60),
file_extension=file_extention_from_filename(filename),
**common_kwargs
)
DownloadBase.set_progress(build_application_zip, 100, 100)
return {
"errors": errors,
}
开发者ID:saketkanth,项目名称:commcare-hq,代码行数:54,代码来源:tasks.py
示例3: prepare_download
def prepare_download(download_id, payload_func, content_disposition,
content_type, expiry=10*60*60):
"""
payload_func should be an instance of SerializableFunction, and can return
either a string or a FileWrapper object
"""
try:
payload = payload_func(process=prepare_download)
except TypeError:
payload = payload_func()
expose_cached_download(payload, expiry, None, mimetype=content_type,
content_disposition=content_disposition,
download_id=download_id)
开发者ID:saketkanth,项目名称:commcare-hq,代码行数:13,代码来源:tasks.py
示例4: export_async
def export_async(custom_export, download_id, format=None, filename=None, **kwargs):
try:
export_files = custom_export.get_export_files(format=format, process=export_async, **kwargs)
except SchemaMismatchException, e:
# fire off a delayed force update to prevent this from happening again
rebuild_schemas.delay(custom_export.index)
expiry = 10 * 60 * 60
expose_cached_download(
"Sorry, the export failed for %s, please try again later" % custom_export._id,
expiry,
None,
content_disposition="",
mimetype="text/html",
download_id=download_id,
).save(expiry)
开发者ID:philipkaare,项目名称:commcare-hq,代码行数:15,代码来源:tasks.py
示例5: post
def post(self, request):
replace = 'replace' in request.POST
file_ref = expose_cached_download(
request.file.read(),
file_extension=file_extention_from_filename(request.file.name),
expiry=1*60*60,
)
# catch basic validation in the synchronous UI
try:
validate_fixture_file_format(file_ref.get_filename())
except FixtureUploadError as e:
messages.error(
request, _('Please fix the following formatting issues in your Excel file: %s') %
'<ul><li>{}</li></ul>'.format('</li><li>'.join(e.errors)),
extra_tags='html'
)
return HttpResponseRedirect(fixtures_home(self.domain))
# hand off to async
task = fixture_upload_async.delay(
self.domain,
file_ref.download_id,
replace,
)
file_ref.set_task(task)
return HttpResponseRedirect(
reverse(
FixtureUploadStatusView.urlname,
args=[self.domain, file_ref.download_id]
)
)
开发者ID:kkrampa,项目名称:commcare-hq,代码行数:33,代码来源:views.py
示例6: post
def post(self, request, *args, **kwargs):
upload = request.FILES.get('bulk_upload_file')
if not upload:
messages.error(request, _('no file uploaded'))
return self.get(request, *args, **kwargs)
if not args:
messages.error(request, _('no domain specified'))
return self.get(request, *args, **kwargs)
domain = args[0]
# stash this in soil to make it easier to pass to celery
file_ref = expose_cached_download(upload.read(),
expiry=1*60*60)
task = import_locations_async.delay(
domain,
file_ref.download_id,
)
file_ref.set_task(task)
return HttpResponseRedirect(
reverse(
LocationImportStatusView.urlname,
args=[domain, file_ref.download_id]
)
)
开发者ID:ekush,项目名称:commcare-hq,代码行数:25,代码来源:views.py
示例7: post
def post(self, request, *args, **kwargs):
form_ids = self.get_xform_ids(request)
if not self.request.can_access_all_locations:
inaccessible_forms_accessed = self.inaccessible_forms_accessed(
form_ids, self.domain, request.couch_user)
if inaccessible_forms_accessed:
return HttpResponseBadRequest(
"Inaccessible forms accessed. Id(s): %s " % ','.join(inaccessible_forms_accessed))
mode = self.request.POST.get('mode')
task_ref = expose_cached_download(payload=None, expiry=1*60*60, file_extension=None)
task = bulk_form_management_async.delay(
mode,
self.domain,
self.request.couch_user,
form_ids
)
task_ref.set_task(task)
return HttpResponseRedirect(
reverse(
XFormManagementStatusView.urlname,
args=[self.domain, mode, task_ref.download_id]
)
)
开发者ID:dimagi,项目名称:commcare-hq,代码行数:25,代码来源:views.py
示例8: process_upload
def process_upload(self):
if hasattr(self.uploaded_file, 'temporary_file_path') and settings.SHARED_DRIVE_CONF.temp_dir:
processing_id = uuid.uuid4().hex
path = settings.SHARED_DRIVE_CONF.get_temp_file(suffix='.upload')
shutil.move(self.uploaded_file.temporary_file_path(), path)
status = BulkMultimediaStatusCacheNfs(processing_id, path)
status.save()
else:
self.uploaded_file.file.seek(0)
saved_file = expose_cached_download(
self.uploaded_file.file.read(),
expiry=BulkMultimediaStatusCache.cache_expiry,
file_extension=file_extention_from_filename(self.uploaded_file.name),
)
processing_id = saved_file.download_id
status = BulkMultimediaStatusCache(processing_id)
status.save()
process_bulk_upload_zip.delay(processing_id, self.domain, self.app_id,
username=self.username,
share_media=self.share_media,
license_name=self.license_used,
author=self.author,
attribution_notes=self.attribution_notes)
return status.get_response()
开发者ID:johan--,项目名称:commcare-hq,代码行数:25,代码来源:views.py
示例9: populate_export_download_task
def populate_export_download_task(export_instances, filters, download_id, filename=None, expiry=10 * 60 * 60):
export_file = get_export_file(export_instances, filters)
file_format = Format.from_format(export_file.format)
filename = filename or export_instances[0].name
escaped_filename = escape_quotes('%s.%s' % (filename, file_format.extension))
payload = export_file.file.payload
expose_cached_download(
payload,
expiry,
".{}".format(file_format.extension),
mimetype=file_format.mimetype,
content_disposition='attachment; filename="%s"' % escaped_filename,
download_id=download_id,
)
export_file.file.delete()
开发者ID:ansarbek,项目名称:commcare-hq,代码行数:17,代码来源:tasks.py
示例10: post
def post(self, request, *args, **kwargs):
upload = request.FILES.get("bulk_upload_file")
"""View's dispatch method automatically calls this"""
try:
self.workbook = WorkbookJSONReader(upload)
except InvalidFileException:
try:
csv.DictReader(io.StringIO(upload.read().decode("ascii"), newline=None))
return HttpResponseBadRequest(
"CommCare HQ no longer supports CSV upload. "
"Please convert to Excel 2007 or higher (.xlsx) "
"and try again."
)
except UnicodeDecodeError:
return HttpResponseBadRequest("Unrecognized format")
except JSONReaderError as e:
messages.error(request, "Your upload was unsuccessful. %s" % e.message)
return self.get(request, *args, **kwargs)
except HeaderValueError as e:
return HttpResponseBadRequest("Upload encountered a data type error: %s" % e.message)
try:
self.user_specs = self.workbook.get_worksheet(title="users")
except WorksheetNotFound:
try:
self.user_specs = self.workbook.get_worksheet()
except WorksheetNotFound:
return HttpResponseBadRequest("Workbook has no worksheets")
try:
self.group_specs = self.workbook.get_worksheet(title="groups")
except WorksheetNotFound:
self.group_specs = []
self.location_specs = []
if Domain.get_by_name(self.domain).commtrack_enabled:
try:
self.location_specs = self.workbook.get_worksheet(title="locations")
except WorksheetNotFound:
# if there is no sheet for locations (since this was added
# later and is optional) we don't error
pass
try:
check_headers(self.user_specs)
except UserUploadError as e:
return HttpResponseBadRequest(e)
task_ref = expose_cached_download(None, expiry=1 * 60 * 60)
task = bulk_upload_async.delay(
self.domain, list(self.user_specs), list(self.group_specs), list(self.location_specs)
)
task_ref.set_task(task)
return HttpResponseRedirect(reverse(UserUploadStatusView.urlname, args=[self.domain, task_ref.download_id]))
开发者ID:idiene,项目名称:commcare-hq,代码行数:54,代码来源:users.py
示例11: export_async
def export_async(custom_export, download_id, format=None, filename=None, **kwargs):
try:
export_files = custom_export.get_export_files(format=format, process=export_async, **kwargs)
except SchemaMismatchException as e:
# fire off a delayed force update to prevent this from happening again
rebuild_schemas.delay(custom_export.index)
expiry = 10*60*60
expose_cached_download(
"Sorry, the export failed for %s, please try again later" % custom_export._id,
expiry,
None,
content_disposition="",
mimetype="text/html",
download_id=download_id
).save(expiry)
else:
if export_files:
if export_files.format is not None:
format = export_files.format
if not filename:
filename = custom_export.name
return cache_file_to_be_served(export_files.file, export_files.checkpoint, download_id, format, filename)
else:
return cache_file_to_be_served(None, None, download_id, format, filename)
开发者ID:kkrampa,项目名称:commcare-hq,代码行数:24,代码来源:tasks.py
示例12: _cache_file
def _cache_file(request, domain, upload):
"""Stash in soil for ten hours to make it easier to pass to celery
:returns: `LocationImportView.Ref` object that can be identified
with `isinstance(rval, LocationImportView.Ref)` or an HTTP
response generated by `lock_locations` (and guaranteed not to be
`LocationImportView.Ref`) if the lock could not be acquired.
"""
TEN_HOURS = 10 * 60 * 60
file_ref = expose_cached_download(
upload.read(),
expiry=TEN_HOURS,
file_extension=file_extention_from_filename(upload.name),
)
# put the file_ref.download_id in cache to lookup from elsewhere
cache.set(import_locations_task_key(domain), file_ref.download_id, TEN_HOURS)
return LocationImportView.Ref(file_ref)
开发者ID:kkrampa,项目名称:commcare-hq,代码行数:17,代码来源:views.py
示例13: post
def post(self, request, *args, **kwargs):
form_ids_or_query_string = self.get_form_ids_or_query_string(request)
mode = self.request.POST.get('mode')
task_ref = expose_cached_download(payload=None, expiry=1*60*60, file_extension=None)
task = bulk_form_management_async.delay(
mode,
self.domain,
self.request.couch_user,
form_ids_or_query_string
)
task_ref.set_task(task)
return HttpResponseRedirect(
reverse(
XFormManagementStatusView.urlname,
args=[self.domain, mode, task_ref.download_id]
)
)
开发者ID:ansarbek,项目名称:commcare-hq,代码行数:18,代码来源:views.py
示例14: prepare_fixture_download
def prepare_fixture_download(table_ids, domain, task, download_id):
"""Prepare fixture data for Excel download
"""
data_types_book, excel_sheets = _prepare_fixture(table_ids, domain, task=task)
header_groups = [("types", excel_sheets["types"]["headers"])]
value_groups = [("types", excel_sheets["types"]["rows"])]
for data_type in data_types_book:
header_groups.append((data_type.tag, excel_sheets[data_type.tag]["headers"]))
value_groups.append((data_type.tag, excel_sheets[data_type.tag]["rows"]))
file = StringIO()
format = Format.XLS_2007
export_raw(tuple(header_groups), tuple(value_groups), file, format)
return expose_cached_download(
file.getvalue(),
60 * 60 * 2,
mimetype=Format.from_format(format).mimetype,
content_disposition='attachment; filename="%s_fixtures.xlsx"' % domain,
download_id=download_id,
)
开发者ID:LifeCoaching,项目名称:commcare-hq,代码行数:21,代码来源:download.py
示例15: excel_config
def excel_config(request, domain):
"""
Step one of three.
This is the initial post when the user uploads the excel file
named_columns:
Whether or not the first row of the excel sheet contains
header strings for the columns. This defaults to True and
should potentially not be an option as it is always used
due to how important it is to see column headers
in the rest of the importer.
"""
if request.method != 'POST':
return HttpResponseRedirect(base.ImportCases.get_url(domain=domain))
if not request.FILES:
return render_error(request, domain, 'Please choose an Excel file to import.')
named_columns = request.POST.get('named_columns') == "on"
uploaded_file_handle = request.FILES['file']
extension = os.path.splitext(uploaded_file_handle.name)[1][1:].strip().lower()
# NOTE: We may not always be able to reference files from subsequent
# views if your worker changes, so we have to store it elsewhere
# using the soil framework.
if extension not in importer_util.ExcelFile.ALLOWED_EXTENSIONS:
return render_error(request, domain,
'The Excel file you chose could not be processed. '
'Please check that it is saved as a Microsoft '
'Excel 97/2000 .xls file.')
# stash content in the default storage for subsequent views
file_ref = expose_cached_download(
uploaded_file_handle.read(),
expiry=1*60*60,
file_extension=file_extention_from_filename(uploaded_file_handle.name),
)
request.session[EXCEL_SESSION_ID] = file_ref.download_id
spreadsheet = importer_util.get_spreadsheet(file_ref, named_columns)
if not spreadsheet:
return _spreadsheet_expired(request, domain)
columns = spreadsheet.get_header_columns()
row_count = spreadsheet.get_num_rows()
if row_count == 0:
return render_error(request, domain,
'Your spreadsheet is empty. '
'Please try again with a different spreadsheet.')
case_types_from_apps = []
# load types from all modules
for row in ApplicationBase.view(
'app_manager/types_by_module',
reduce=True,
group=True,
startkey=[domain],
endkey=[domain, {}]
).all():
if not row['key'][1] in case_types_from_apps:
case_types_from_apps.append(row['key'][1])
case_types_from_cases = get_case_types_for_domain(domain)
# for this we just want cases that have data but aren't being used anymore
case_types_from_cases = filter(lambda x: x not in case_types_from_apps, case_types_from_cases)
if len(case_types_from_apps) == 0 and len(case_types_from_cases) == 0:
return render_error(
request,
domain,
'No cases have been submitted to this domain and there are no '
'applications yet. You cannot import case details from an Excel '
'file until you have existing cases or applications.'
)
return render(
request,
"importer/excel_config.html", {
'named_columns': named_columns,
'columns': columns,
'case_types_from_cases': case_types_from_cases,
'case_types_from_apps': case_types_from_apps,
'domain': domain,
'report': {
'name': 'Import: Configuration'
},
'slug': base.ImportCases.slug
}
)
开发者ID:johan--,项目名称:commcare-hq,代码行数:93,代码来源:views.py
示例16: build_form_multimedia_zip
#.........这里部分代码省略.........
form_info = {
"form": form,
"attachments": list(),
"name": form["form"].get("@name", "unknown form"),
"user": meta.get("username", "unknown_user"),
"cases": cases,
"id": form["_id"],
}
for k, v in form["_attachments"].iteritems():
if v["content_type"] == "text/xml":
continue
try:
question_id = unicode(u"-".join(find_question_id(form["form"], k)))
except TypeError:
question_id = unicode(u"unknown" + unicode(unknown_number))
unknown_number += 1
if not properties or question_id in properties:
extension = unicode(os.path.splitext(k)[1])
form_info["attachments"].append(
{
"size": v["length"],
"name": k,
"question_id": question_id,
"extension": extension,
"timestamp": parse(form["received_on"]).timetuple(),
}
)
return form_info
key = [domain, app_id, xmlns]
form_ids = {
f["id"]
for f in XFormInstance.get_db().view(
"attachments/attachments", start_key=key + [startdate], end_key=key + [enddate, {}], reduce=False
)
}
properties = set()
if export_id:
schema = FormExportSchema.get(export_id)
for table in schema.tables:
# - in question id is replaced by . in excel exports
properties |= {c.display.replace(".", "-") for c in table.columns}
if not app_id:
zip_name = "Unrelated Form"
forms_info = list()
for form in iter_docs(XFormInstance.get_db(), form_ids):
if not zip_name:
zip_name = unidecode(form["form"].get("@name", "unknown form"))
forms_info.append(extract_form_info(form, properties))
num_forms = len(forms_info)
DownloadBase.set_progress(build_form_multimedia_zip, 0, num_forms)
# get case names
case_id_to_name = {c: c for c in case_ids}
for case in iter_docs(CommCareCase.get_db(), case_ids):
if case["name"]:
case_id_to_name[case["_id"]] = case["name"]
use_transfer = settings.SHARED_DRIVE_CONF.transfer_enabled
if use_transfer:
params = "_".join(map(str, [xmlns, startdate, enddate, export_id, num_forms]))
fname = "{}-{}".format(app_id, hashlib.md5(params).hexdigest())
fpath = os.path.join(settings.SHARED_DRIVE_CONF.transfer_dir, fname)
else:
_, fpath = tempfile.mkstemp()
if not (os.path.isfile(fpath) and use_transfer): # Don't rebuild the file if it is already there
with open(fpath, "wb") as zfile:
with zipfile.ZipFile(zfile, "w") as z:
for form_number, form_info in enumerate(forms_info):
f = XFormInstance.wrap(form_info["form"])
form_info["cases"] = {case_id_to_name[case_id] for case_id in form_info["cases"]}
for a in form_info["attachments"]:
fname = filename(form_info, a["question_id"], a["extension"])
zi = zipfile.ZipInfo(fname, a["timestamp"])
z.writestr(zi, f.fetch_attachment(a["name"], stream=True).read(), zipfile.ZIP_STORED)
DownloadBase.set_progress(build_form_multimedia_zip, form_number + 1, num_forms)
common_kwargs = dict(
mimetype="application/zip",
content_disposition='attachment; filename="{fname}.zip"'.format(fname=zip_name),
download_id=download_id,
)
if use_transfer:
expose_file_download(fpath, use_transfer=use_transfer, **common_kwargs)
else:
expose_cached_download(
FileWrapper(open(fpath)),
expiry=(1 * 60 * 60),
file_extension=file_extention_from_filename(fpath),
**common_kwargs
)
DownloadBase.set_progress(build_form_multimedia_zip, num_forms, num_forms)
开发者ID:johan--,项目名称:commcare-hq,代码行数:101,代码来源:tasks.py
示例17: build_application_zip
def build_application_zip(include_multimedia_files, include_index_files, app,
download_id, build_profile_id=None, compress_zip=False, filename="commcare.zip",
download_targeted_version=False):
from corehq.apps.hqmedia.views import iter_app_files
DownloadBase.set_progress(build_application_zip, 0, 100)
initial_progress = 10 # early on indicate something is happening
file_progress = 50.0 # arbitrarily say building files takes half the total time
errors = []
compression = zipfile.ZIP_DEFLATED if compress_zip else zipfile.ZIP_STORED
use_transfer = settings.SHARED_DRIVE_CONF.transfer_enabled
if use_transfer:
fpath = os.path.join(settings.SHARED_DRIVE_CONF.transfer_dir, "{}{}{}{}{}".format(
app._id,
'mm' if include_multimedia_files else '',
'ccz' if include_index_files else '',
app.version,
build_profile_id
))
if download_targeted_version:
fpath += '-targeted'
else:
dummy, fpath = tempfile.mkstemp()
DownloadBase.set_progress(build_application_zip, initial_progress, 100)
if not (os.path.isfile(fpath) and use_transfer): # Don't rebuild the file if it is already there
files, errors, file_count = iter_app_files(
app, include_multimedia_files, include_index_files, build_profile_id,
download_targeted_version=download_targeted_version,
)
if toggles.CAUTIOUS_MULTIMEDIA.enabled(app.domain):
manifest = json.dumps({
'include_multimedia_files': include_multimedia_files,
'include_index_files': include_index_files,
'download_id': download_id,
'build_profile_id': build_profile_id,
'compress_zip': compress_zip,
'filename': filename,
'download_targeted_version': download_targeted_version,
'app': app.to_json(),
}, indent=4)
files = itertools.chain(files, [('manifest.json', manifest)])
with open(fpath, 'wb') as tmp:
with zipfile.ZipFile(tmp, "w") as z:
progress = initial_progress
for path, data in files:
# don't compress multimedia files
extension = os.path.splitext(path)[1]
file_compression = zipfile.ZIP_STORED if extension in MULTIMEDIA_EXTENSIONS else compression
z.writestr(path, data, file_compression)
progress += file_progress / file_count
DownloadBase.set_progress(build_application_zip, progress, 100)
# Integrity check that all media files present in media_suite.xml were added to the zip
if include_multimedia_files and include_index_files and toggles.CAUTIOUS_MULTIMEDIA.enabled(app.domain):
with open(fpath, 'rb') as tmp:
with zipfile.ZipFile(tmp, "r") as z:
media_suites = [f for f in z.namelist() if re.search(r'\bmedia_suite.xml\b', f)]
if len(media_suites) != 1:
message = _('Could not identify media_suite.xml in CCZ')
errors.append(message)
else:
with z.open(media_suites[0]) as media_suite:
from corehq.apps.app_manager.xform import parse_xml
parsed = parse_xml(media_suite.read())
resources = {node.text for node in
parsed.findall("media/resource/location[@authority='local']")}
names = z.namelist()
missing = [r for r in resources if re.sub(r'^\.\/', '', r) not in names]
errors += [_('Media file missing from CCZ: {}').format(r) for r in missing]
if errors:
os.remove(fpath)
raise Exception('\t' + '\t'.join(errors))
else:
DownloadBase.set_progress(build_application_zip, initial_progress + file_progress, 100)
common_kwargs = {
'mimetype': 'application/zip' if compress_zip else 'application/x-zip-compressed',
'content_disposition': 'attachment; filename="{fname}"'.format(fname=filename),
'download_id': download_id,
'expiry': (1 * 60 * 60),
}
if use_transfer:
expose_file_download(
fpath,
use_transfer=use_transfer,
**common_kwargs
)
else:
expose_cached_download(
FileWrapper(open(fpath, 'rb')),
file_extension=file_extention_from_filename(filename),
**common_kwargs
)
#.........这里部分代码省略.........
开发者ID:dimagi,项目名称:commcare-hq,代码行数:101,代码来源:tasks.py
示例18: build_application_zip
def build_application_zip(include_multimedia_files, include_index_files, app,
download_id, build_profile_id=None, compress_zip=False, filename="commcare.zip",
download_targeted_version=False):
from corehq.apps.hqmedia.views import iter_app_files
DownloadBase.set_progress(build_application_zip, 0, 100)
initial_progress = 10 # early on indicate something is happening
file_progress = 50.0 # arbitrarily say building files takes half the total time
errors = []
compression = zipfile.ZIP_DEFLATED if compress_zip else zipfile.ZIP_STORED
use_transfer = settings.SHARED_DRIVE_CONF.transfer_enabled
if use_transfer:
fpath = os.path.join(settings.SHARED_DRIVE_CONF.transfer_dir, "{}{}{}{}{}".format(
app._id,
'mm' if include_multimedia_files else '',
'ccz' if include_index_files else '',
app.version,
build_profile_id
))
if download_targeted_version:
fpath += '-targeted'
else:
_, fpath = tempfile.mkstemp()
DownloadBase.set_progress(build_application_zip, initial_progress, 100)
if not (os.path.isfile(fpath) and use_transfer): # Don't rebuild the file if it is already there
files, errors, file_count = iter_app_files(
app, include_multimedia_files, include_index_files, build_profile_id,
download_targeted_version=download_targeted_version,
)
with open(fpath, 'wb') as tmp:
with zipfile.ZipFile(tmp, "w") as z:
progress = initial_progress
for path, data in files:
# don't compress multimedia files
extension = os.path.splitext(path)[1]
file_compression = zipfile.ZIP_STORED if extension in MULTIMEDIA_EXTENSIONS else compression
z.writestr(path, data, file_compression)
progress += file_progress / file_count
DownloadBase.set_progress(build_application_zip, progress, 100)
else:
DownloadBase.set_progress(build_application_zip, initial_progress + file_progress, 100)
common_kwargs = {
'mimetype': 'application/zip' if compress_zip else 'application/x-zip-compressed',
'content_disposition': 'attachment; filename="{fname}"'.format(fname=filename),
'download_id': download_id,
'expiry': (1 * 60 * 60),
}
if use_transfer:
expose_file_download(
fpath,
use_transfer=use_transfer,
**common_kwargs
)
else:
expose_cached_download(
FileWrapper(open(fpath, 'rb')),
file_extension=file_extention_from_filename(filename),
**common_kwargs
)
DownloadBase.set_progress(build_application_zip, 100, 100)
return {
"errors": errors,
}
开发者ID:kkrampa,项目名称:commcare-hq,代码行数:69,代码来源:tasks.py
示例19: build_form_multimedia_zip
#.........这里部分代码省略.........
form_info = {
'form': form,
'attachments': list(),
'name': form['form'].get('@name', 'unknown form'),
'user': meta.get('username', 'unknown_user'),
'cases': cases,
'id': form['_id']
}
for k, v in form['_attachments'].iteritems():
if v['content_type'] == 'text/xml':
continue
try:
question_id = unicode(u'-'.join(find_question_id(form['form'], k)))
except TypeError:
question_id = unicode(u'unknown' + unicode(unknown_number))
unknown_number += 1
if not properties or question_id in properties:
extension = unicode(os.path.splitext(k)[1])
form_info['attachments'].append({
'size': v['length'],
'name': k,
'question_id': question_id,
'extension': extension,
'timestamp': parse(form['received_on']).timetuple(),
})
return form_info
key = [domain, app_id, xmlns]
form_ids = {f['id'] for f in XFormInstance.get_db().view("attachments/attachments",
start_key=key + [startdate],
end_key=key + [enddate, {}],
reduce=False)}
properties = set()
if export_id:
schema = FormExportSchema.get(export_id)
for table in schema.tables:
# - in question id is replaced by . in excel exports
properties |= {c.display.replace('.', '-') for c in table.columns}
if not app_id:
zip_name = 'Unrelated Form'
forms_info = list()
for form in iter_docs(XFormInstance.get_db(), form_ids):
if not zip_name:
zip_name = unidecode(form['form'].get('@name', 'unknown form'))
forms_info.append(extract_form_info(form, properties))
num_forms = len(forms_info)
DownloadBase.set_progress(build_form_multimedia_zip, 0, num_forms)
# get case names
case_id_to_name = {c: c for c in case_ids}
for case in iter_docs(CommCareCase.get_db(), case_ids):
if case['name']:
case_id_to_name[case['_id']] = case['name']
use_transfer = settings.SHARED_DRIVE_CONF.transfer_enabled
if use_transfer:
params = '_'.join(map(str, [xmlns, startdate, enddate, export_id, num_forms]))
fname = '{}-{}'.format(app_id, hashlib.md5(params).hexdigest())
fpath = os.path.join(settings.SHARED_DRIVE_CONF.transfer_dir, fname)
else:
_, fpath = tempfile.mkstemp()
if not (os.path.isfile(fpath) and use_transfer): # Don't rebuild the file if it is already there
with open(fpath, 'wb') as zfile:
with zipfile.ZipFile(zfile, 'w') as z:
for form_number, form_info in enumerate(forms_info):
f = XFormInstance.wrap(form_info['form'])
form_info['cases'] = {case_id_to_name[case_id] for case_id in form_info['cases']}
for a in form_info['attachments']:
fname = filename(form_info, a['question_id'], a['extension'])
zi = zipfile.ZipInfo(fname, a['timestamp'])
z.writestr(zi, f.fetch_attachment(a['name'], stream=True).read(), zipfile.ZIP_STORED)
DownloadBase.set_progress(build_form_multimedia_zip, form_number + 1, num_forms)
common_kwargs = dict(
mimetype='application/zip',
content_disposition='attachment; filename="{fname}.zip"'.format(fname=zip_name),
download_id=download_id,
)
if use_transfer:
expose_file_download(
fpath,
use_transfer=use_transfer,
**common_kwargs
)
else:
expose_cached_download(
FileWrapper(open(fpath)),
expiry=(1 * 60 * 60),
file_extension=file_extention_from_filename(fpath),
**common_kwargs
)
DownloadBase.set_progress(build_form_multimedia_zip, num_forms, num_forms)
开发者ID:tlwakwella,项目名称:commcare-hq,代码行数:101,代码来源:tasks.py
注:本文中的soil.util.expose_cached_download函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论