Reformat using black

This commit is contained in:
cuom1999 2022-05-14 12:57:27 -05:00
parent efee4ad081
commit a87fb49918
221 changed files with 19127 additions and 7310 deletions

View file

@ -8,27 +8,25 @@ import os, os.path
import tempfile
import shutil
__all__ = (
'handle_upload', 'save_upload', 'FineUploadForm', 'FineUploadFileInput'
)
__all__ = ("handle_upload", "save_upload", "FineUploadForm", "FineUploadFileInput")
def combine_chunks(total_parts, total_size, source_folder, dest):
if not os.path.exists(os.path.dirname(dest)):
os.makedirs(os.path.dirname(dest))
with open(dest, 'wb+') as destination:
with open(dest, "wb+") as destination:
for i in range(total_parts):
part = os.path.join(source_folder, str(i))
with open(part, 'rb') as source:
with open(part, "rb") as source:
destination.write(source.read())
def save_upload(f, path):
if not os.path.exists(os.path.dirname(path)):
os.makedirs(os.path.dirname(path))
with open(path, 'wb+') as destination:
if hasattr(f, 'multiple_chunks') and f.multiple_chunks():
with open(path, "wb+") as destination:
if hasattr(f, "multiple_chunks") and f.multiple_chunks():
for chunk in f.chunks():
destination.write(chunk)
else:
@ -37,29 +35,35 @@ def save_upload(f, path):
# pass callback function to post_upload
def handle_upload(f, fileattrs, upload_dir, post_upload=None):
chunks_dir = os.path.join(tempfile.gettempdir(), 'chunk_upload_tmp')
chunks_dir = os.path.join(tempfile.gettempdir(), "chunk_upload_tmp")
if not os.path.exists(os.path.dirname(chunks_dir)):
os.makedirs(os.path.dirname(chunks_dir))
chunked = False
dest_folder = upload_dir
dest = os.path.join(dest_folder, fileattrs['qqfilename'])
dest = os.path.join(dest_folder, fileattrs["qqfilename"])
# Chunked
if fileattrs.get('qqtotalparts') and int(fileattrs['qqtotalparts']) > 1:
if fileattrs.get("qqtotalparts") and int(fileattrs["qqtotalparts"]) > 1:
chunked = True
dest_folder = os.path.join(chunks_dir, fileattrs['qquuid'])
dest = os.path.join(dest_folder, fileattrs['qqfilename'], str(fileattrs['qqpartindex']))
dest_folder = os.path.join(chunks_dir, fileattrs["qquuid"])
dest = os.path.join(
dest_folder, fileattrs["qqfilename"], str(fileattrs["qqpartindex"])
)
save_upload(f, dest)
# If the last chunk has been sent, combine the parts.
if chunked and (fileattrs['qqtotalparts'] - 1 == fileattrs['qqpartindex']):
combine_chunks(fileattrs['qqtotalparts'],
fileattrs['qqtotalfilesize'],
if chunked and (fileattrs["qqtotalparts"] - 1 == fileattrs["qqpartindex"]):
combine_chunks(
fileattrs["qqtotalparts"],
fileattrs["qqtotalfilesize"],
source_folder=os.path.dirname(dest),
dest=os.path.join(upload_dir, fileattrs['qqfilename']))
dest=os.path.join(upload_dir, fileattrs["qqfilename"]),
)
shutil.rmtree(os.path.dirname(os.path.dirname(dest)))
if post_upload and (not chunked or fileattrs['qqtotalparts'] - 1 == fileattrs['qqpartindex']):
if post_upload and (
not chunked or fileattrs["qqtotalparts"] - 1 == fileattrs["qqpartindex"]
):
post_upload()
@ -75,13 +79,16 @@ class FineUploadForm(forms.Form):
class FineUploadFileInput(ClearableFileInput):
template_name = 'widgets/fine_uploader.html'
template_name = "widgets/fine_uploader.html"
def fine_uploader_id(self, name):
return name + '_' + 'fine_uploader'
return name + "_" + "fine_uploader"
def get_context(self, name, value, attrs):
context = super().get_context(name, value, attrs)
context['widget'].update({
'fine_uploader_id': self.fine_uploader_id(name),
})
return context
context["widget"].update(
{
"fine_uploader_id": self.fine_uploader_id(name),
}
)
return context