Django+Bootstrap圧縮パッケージをアップロードして解析
url.py中
html内
form.py中
view.py中
img_util.py中
url(r'^blind/(\w+)/project_type/$', 'encode.views.blind_project_type', name="blind_project_type"),
html内
{{ message }}
form.py中
class UploadComPackageForm(forms.Form):
files_field = forms.FileField(label=u" ", required=True, widget=forms.ClearableFileInput(attrs={'multiple': True}))
view.py中
from utils.img_util import *
from utils.definition import RESOLUTION_DEFINITION, RESOLUTION_DEFINITION_CONVERT, VIDEO_EXTENSION_LIST, RESOLUTION_LIST
# COMPRESS_PACKAGE_PREFIX = "/mnt/competitor/new-comparison-platform/"
@login_required
def blind_project_type(request, blind_task_id=None):
blind_task = BlindTask.objects.get(id=ObjectId(blind_task_id))
if request.method == "POST":
form = UploadComPackageForm(request.POST, request.FILES)
if form.is_valid():
try:
files = request.FILES.getlist('files_field')
for f in files:
file_name = os.path.splitext(f.name)[0]
project = Project(name=file_name, user=request.user.username)
try:
project.save()
blind_task.projects.append(project)
blind_task.save()
except NotUniqueError:
message = " , !"
return render(request, 'blind_task_type.html', locals())
is_or_not, file_extension = is_or_not_compress(f.name)
if is_or_not == "compress":
compress_data = f.read()
path = config.COMPRESS_PACKAGE_PREFIX
filepath = os.path.join(path, f.name)
with open(filepath, 'wb+')as destination:
for chunk in f.chunks():
destination.write(chunk)
if file_extension == "zip":
uncompress_dir = un_zip(filepath)
if file_extension == "tar":
uncompress_dir = un_tar(filepath)
# if file_extension == "gz":
# un_gz(filepath)
project_list = os.listdir(uncompress_dir)
for project_index in range(0, len(project_list)):
project_dir = os.path.join(uncompress_dir, project_list[project_index])
if os.path.isdir(project_dir):
entry_list = os.listdir(project_dir)
for entry_index in range(0, len(entry_list)):
entry_dir = os.path.join(project_dir, entry_list[entry_index])
entry = Entry(desc=entry_list[entry_index])
entry.save()
project.entries.append(entry)
project.save()
if os.path.isdir(entry_dir):
image_list = os.listdir(entry_dir)
display_name = 0
master_info = []
other_info = {}
for image_index in range(0, len(image_list)):
image_dir = os.path.join(entry_dir, image_list[image_index])
if os.path.isdir(image_dir):
message = " ! !"
else:
image_ture_name = image_list[image_index]
real_name = image_ture_name
real_com = real_name.split(".")[0]
display_name = display_name + 1
myimage = open(os.path.join(entry_dir, image_ture_name), 'rb')
image_data = myimage.read()
tool = ImageMetaTool(image_data)
image_doc = ImageDoc.create(
real_name,
real_com,
" %d" % (display_name),
tool.get_resolution(),
image_data,
real_name,
tool.get_content_type(),
)
entry.images.append(image_doc)
entry.save()
redirect_url = reverse('blind_add_task_self', args=[blind_task.id])
return HttpResponseRedirect(redirect_url)
except ResolutionException as e:
error = str(e)
except Exception as e:
print traceback.format_exc(e)
error = str(e)
elif request.method == "GET":
form = UploadComPackageForm()
return render(request, 'blind_task_type.html', locals())
img_util.py中
def un_zip(filename):
zip_file = zipfile.ZipFile(filename)
compress_dir = os.path.splitext(filename)[0]
if os.path.isdir(compress_dir):
pass
else:
os.mkdir(compress_dir)
for names in zip_file.namelist():
zip_file.extract(names, compress_dir)
zip_file.close()
return compress_dir
def un_gz(filename):
print "this is tar.gz "
g_file = gzip.GzipFile(filename)
print g_file
if os.path.isdir(os.path.splitext(filename)[0]):
pass
else:
os.mkdir(os.path.splitext(filename)[0])
print os.path.splitext(filename)[0]
un_tar(os.path.splitext(filename)[0])
g_file.close()
def is_or_not_compress(filename):
file_extension = os.path.splitext(filename)[1]
file_extension = file_extension.replace('.', '')
if file_extension in COMPRESS_EXTENSION_LIST:
return "compress", file_extension