本文整理汇总了Python中django.core.files.storage.default_storage.save方法的典型用法代码示例。如果您正苦于以下问题:Python default_storage.save方法的具体用法?Python default_storage.save怎么用?Python default_storage.save使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.core.files.storage.default_storage
的用法示例。
在下文中一共展示了default_storage.save方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: download_media_files
# 需要导入模块: from django.core.files.storage import default_storage [as 别名]
# 或者: from django.core.files.storage.default_storage import save [as 别名]
def download_media_files(self, xml_doc, media_path):
for media_node in xml_doc.getElementsByTagName('mediaFile'):
filename_node = media_node.getElementsByTagName('filename')
url_node = media_node.getElementsByTagName('downloadUrl')
if filename_node and url_node:
filename = filename_node[0].childNodes[0].nodeValue
path = os.path.join(media_path, filename)
if default_storage.exists(path):
continue
download_url = url_node[0].childNodes[0].nodeValue
if self._get_media_response(download_url):
download_res = self._current_response
media_content = ContentFile(download_res.content)
default_storage.save(path, media_content)
self.logger.debug("Fetched %s." % filename)
else:
self.logger.error("Failed to fetch %s." % filename)
示例2: profile_settings
# 需要导入模块: from django.core.files.storage import default_storage [as 别名]
# 或者: from django.core.files.storage.default_storage import save [as 别名]
def profile_settings(request, username):
content_user = check_and_set_user(request, username)
profile, created = UserProfile.objects.get_or_create(user=content_user)
if request.method == 'POST':
form = UserProfileForm(request.POST, instance=profile)
if form.is_valid():
# get user
# user.email = cleaned_email
form.instance.user.email = form.cleaned_data['email']
form.instance.user.save()
form.save()
# todo: add string rep. of settings to see what changed
audit = {}
audit_log(
Actions.PROFILE_SETTINGS_UPDATED, request.user, content_user,
_("Profile settings updated."), audit, request)
return HttpResponseRedirect(reverse(
public_profile, kwargs={'username': request.user.username}
))
else:
form = UserProfileForm(
instance=profile, initial={"email": content_user.email})
return render(request, "settings.html",
{'content_user': content_user, 'form': form})
示例3: create_from_as2mdn
# 需要导入模块: from django.core.files.storage import default_storage [as 别名]
# 或者: from django.core.files.storage.default_storage import save [as 别名]
def create_from_as2mdn(self, as2mdn, message, status, return_url=None):
"""Create the MDN from the pyas2lib's MDN object"""
signed = True if as2mdn.digest_alg else False
mdn, _ = self.update_or_create(
message=message,
defaults=dict(
mdn_id=as2mdn.message_id,
status=status,
signed=signed,
return_url=return_url,
),
)
filename = f"{uuid4()}.mdn"
mdn.headers.save(
name=f"{filename}.header", content=ContentFile(as2mdn.headers_str)
)
mdn.payload.save(filename, content=ContentFile(as2mdn.content))
return mdn
示例4: send_async_mdn
# 需要导入模块: from django.core.files.storage import default_storage [as 别名]
# 或者: from django.core.files.storage.default_storage import save [as 别名]
def send_async_mdn(self):
""" Send the asynchronous MDN to the partner"""
# convert the mdn headers to dictionary
headers = HeaderParser().parsestr(self.headers.read().decode())
# Send the mdn to the partner
try:
response = requests.post(
self.return_url, headers=dict(headers.items()), data=self.payload.read()
)
response.raise_for_status()
except requests.exceptions.RequestException:
return
# Update the status of the MDN
self.status = "S"
self.save()
示例5: handle
# 需要导入模块: from django.core.files.storage import default_storage [as 别名]
# 或者: from django.core.files.storage.default_storage import save [as 别名]
def handle(self, *args, **options):
script = options.get('script')
if not script:
if len(args):
script = args[0]
else:
raise CommandError('You must provide a script path or directory containing scripts.')
if not os.path.exists(script):
raise CommandError('{0} does not exist.'.format(script))
group = options.get('group', 'Djangui Scripts')
scripts = [os.path.join(script, i) for i in os.listdir(script)] if os.path.isdir(script) else [script]
converted = 0
for script in scripts:
if script.endswith('.pyc') or '__init__' in script:
continue
if script.endswith('.py'):
sys.stdout.write('Converting {}\n'.format(script))
# copy the script to our storage
with open(script, 'r') as f:
script = default_storage.save(os.path.join(djangui_settings.DJANGUI_SCRIPT_DIR, os.path.split(script)[1]), File(f))
added, error = add_djangui_script(script=os.path.abspath(os.path.join(settings.MEDIA_ROOT, script)), group=group)
if added:
converted += 1
sys.stdout.write('Converted {} scripts\n'.format(converted))
示例6: test_open_read
# 需要导入模块: from django.core.files.storage import default_storage [as 别名]
# 或者: from django.core.files.storage.default_storage import save [as 别名]
def test_open_read(self):
self.storage.location = 'root'
stream = io.BytesIO(b'Im a stream')
name = self.storage.save('path/some file.txt', stream)
fh = self.storage.open(name, 'r+b')
try:
self.assertEqual(fh.read(), b'Im a stream')
finally:
fh.close()
stream = io.BytesIO()
self.storage.service.get_blob_to_stream(
container_name=self.storage.azure_container,
blob_name='root/path/some file.txt',
stream=stream,
max_connections=1,
timeout=10)
stream.seek(0)
self.assertEqual(stream.read(), b'Im a stream')
示例7: test_open_read_write
# 需要导入模块: from django.core.files.storage import default_storage [as 别名]
# 或者: from django.core.files.storage.default_storage import save [as 别名]
def test_open_read_write(self):
self.storage.location = 'root'
stream = io.BytesIO(b'Im a stream')
name = self.storage.save('file.txt', stream)
fh = self.storage.open(name, 'r+b')
try:
self.assertEqual(fh.read(), b'Im a stream')
fh.write(b' foo')
fh.seek(0)
self.assertEqual(fh.read(), b'Im a stream foo')
finally:
fh.close()
stream = io.BytesIO()
self.storage.service.get_blob_to_stream(
container_name=self.storage.azure_container,
blob_name='root/file.txt',
stream=stream,
max_connections=1,
timeout=10)
stream.seek(0)
self.assertEqual(stream.read(), b'Im a stream foo')
示例8: test_model_form
# 需要导入模块: from django.core.files.storage import default_storage [as 别名]
# 或者: from django.core.files.storage.default_storage import save [as 别名]
def test_model_form(self):
files = {
'foo_file': SimpleUploadedFile(
name='foo.pdf',
content=b'foo content',
content_type='application/pdf')}
form = FooFileModelForm(data={}, files=files)
self.assertTrue(form.is_valid())
form.save()
self.assertTrue(default_storage.exists('foo_uploads/foo.pdf'))
# check file content was saved
fh = default_storage.open('foo_uploads/foo.pdf', 'r+b')
try:
self.assertEqual(fh.read(), b'foo content')
finally:
fh.close()
示例9: add_dataset
# 需要导入模块: from django.core.files.storage import default_storage [as 别名]
# 或者: from django.core.files.storage.default_storage import save [as 别名]
def add_dataset(request):
daterange = ""
dataset = Dataset(author=request.user, index=request.POST['index'],
mapping=request.POST['mapping'], daterange=daterange, access=(request.POST['access']))
dataset.save()
create_dataset_access_permission_and_propagate(dataset, request.POST['access'])
indices = ES_Manager.get_indices()
ds_out = dataset.__dict__
for index in indices:
if index['index'] == ds_out['index']:
ds_out['status'] = index['status']
ds_out['docs_count'] = index['docs_count']
ds_out['store_size'] = index['store_size']
break
elif '*' in ds_out['index']:
ds_out['status'] = 'open'
ds_out['docs_count'] = 'multiindex'
ds_out['store_size'] = 'multiindex'
ds_out['_state'] = ''
ds_out['author'] = request.user.get_username()
return JsonResponse(ds_out)
示例10: silence_streams
# 需要导入模块: from django.core.files.storage import default_storage [as 别名]
# 或者: from django.core.files.storage.default_storage import save [as 别名]
def silence_streams(func):
def wrapper(*args, **kwargs):
# save stderr
save_streams = sys.__stderr__
save_streams.flush()
# file descriptor for stderr is 2
save_stderr = os.dup(2)
# silence
null_fd = os.open(os.devnull, os.O_RDWR)
os.dup2(null_fd, 2)
# uncomment the line to test if stderr is silenced
# 1/0
try:
bak_file_path = func(*args, **kwargs)
sys.stderr = save_streams
os.dup2(save_stderr, 2)
return bak_file_path
except:
sys.stderr = save_streams
os.dup2(save_stderr, 2)
return wrapper
示例11: download_xforms
# 需要导入模块: from django.core.files.storage import default_storage [as 别名]
# 或者: from django.core.files.storage.default_storage import save [as 别名]
def download_xforms(self, include_instances=False):
# fetch formList
if not self._get_response(self.form_list_url):
response = self._current_response.content \
if self._current_response else "Unknown Error"
self.logger.error("Failed to download xforms %s." % response)
return
response = self._current_response
forms = self._get_form_list(response.content)
self.logger.debug('Successfull fetched %s.' % self.form_list_url)
for id_string, download_url, manifest_url in forms:
form_path = os.path.join(
self.forms_path, id_string, '%s.xml' % id_string)
if not default_storage.exists(form_path):
if not self._get_response(download_url):
self.logger.error("Failed to download xform %s."
% download_url)
continue
form_res = self._current_response
content = ContentFile(form_res.content.strip())
default_storage.save(form_path, content)
else:
form_res = default_storage.open(form_path)
content = form_res.read()
self.logger.debug("Fetched %s." % download_url)
self.download_manifest(manifest_url, id_string)
if include_instances:
self.download_instances(id_string)
self.logger.debug("Done downloading submissions for %s" %
id_string)
示例12: save
# 需要导入模块: from django.core.files.storage import default_storage [as 别名]
# 或者: from django.core.files.storage.default_storage import save [as 别名]
def save(self, **kwargs):
user = User.objects.get(username=self.username)
xls_file_path = upload_to(None, '%s%s.xls' % (
self.xform.id_string,
XForm.CLONED_SUFFIX),
self.username)
xls_data = default_storage.open(self.xform.xls.name)
xls_file = default_storage.save(xls_file_path, xls_data)
self.cloned_form = DataDictionary.objects.create(
user=user,
xls=xls_file
)
示例13: save
# 需要导入模块: from django.core.files.storage import default_storage [as 别名]
# 或者: from django.core.files.storage.default_storage import save [as 别名]
def save(self, new_user):
new_profile = \
UserProfile(user=new_user, name=self.cleaned_data['name'],
city=self.cleaned_data['city'],
country=self.cleaned_data['country'],
organization=self.cleaned_data['organization'],
home_page=self.cleaned_data['home_page'],
twitter=self.cleaned_data['twitter'])
new_profile.save()
return new_profile
# order of inheritance control order of form display
示例14: update_status
# 需要导入模块: from django.core.files.storage import default_storage [as 别名]
# 或者: from django.core.files.storage.default_storage import save [as 别名]
def update_status(self):
try:
status = self.site_instances.order_by('-date').first().form_status
print status, identifier
except:
status = 0
self.current_status = status
self.save()
示例15: update_current_progress
# 需要导入模块: from django.core.files.storage import default_storage [as 别名]
# 或者: from django.core.files.storage.default_storage import save [as 别名]
def update_current_progress(self):
self.current_progress = self.progress()
try:
status = self.site_instances.order_by('-date').first().form_status
except:
status = 0
self.current_status = status
self.save()