本文整理汇总了Python中soil.util.expose_cached_download函数的典型用法代码示例。如果您正苦于以下问题:Python expose_cached_download函数的具体用法?Python expose_cached_download怎么用?Python expose_cached_download使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了expose_cached_download函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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)
示例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,
}
示例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)
示例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)
示例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]
)
)
示例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]
)
)
示例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]
)
)
示例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()
示例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()
示例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]))
示例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)
示例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)
示例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]
)
)
示例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,
)
示例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
}
)