load dump
This commit is contained in:
parent
41e55a74a6
commit
d68a5c6dd0
@ -1,5 +1,12 @@
|
||||
from Main.models import Task
|
||||
import io
|
||||
import json
|
||||
from zipfile import ZipFile
|
||||
|
||||
from django.db import transaction
|
||||
|
||||
from Main.models import Task, ExtraFile
|
||||
from SprintLib.BaseView import BaseView
|
||||
from SprintLib.utils import write_bytes, delete_file
|
||||
|
||||
|
||||
class TasksView(BaseView):
|
||||
@ -11,3 +18,55 @@ class TasksView(BaseView):
|
||||
task_name = self.request.POST["name"]
|
||||
task = Task.objects.create(name=task_name, creator=self.request.user)
|
||||
return f"/admin/task?task_id={task.id}"
|
||||
|
||||
def post_upload_file(self):
|
||||
archive = ZipFile(io.BytesIO(self.request.FILES["file"].read()))
|
||||
fs_ids = {}
|
||||
created = True
|
||||
try:
|
||||
with transaction.atomic():
|
||||
task = Task(name='новый таск', creator=self.request.user)
|
||||
for file in archive.infolist():
|
||||
if file.filename == 'meta.json':
|
||||
continue
|
||||
else:
|
||||
bts = archive.read(file.filename)
|
||||
fs_id = write_bytes(bts)
|
||||
readable = True
|
||||
try:
|
||||
bts.decode('utf-8')
|
||||
except UnicodeDecodeError:
|
||||
readable = False
|
||||
fs_ids[file.filename] = fs_id, readable
|
||||
meta = json.loads(archive.read('meta.json').decode('utf-8'))
|
||||
task_fields = [
|
||||
'name',
|
||||
'public',
|
||||
'legend',
|
||||
'input_format',
|
||||
'output_format',
|
||||
'specifications',
|
||||
'time_limit',
|
||||
'time_estimation',
|
||||
]
|
||||
for key in task_fields:
|
||||
setattr(task, key, meta[key])
|
||||
task.save()
|
||||
for file in meta['files']:
|
||||
fs_id, readable = fs_ids[str(file['id'])]
|
||||
ExtraFile.objects.create(
|
||||
filename=file['filename'],
|
||||
is_test=file['is_test'],
|
||||
is_sample=file['is_sample'],
|
||||
fs_id=fs_id,
|
||||
readable=readable,
|
||||
task=task,
|
||||
)
|
||||
except Exception as e:
|
||||
for fs_id in fs_ids.values():
|
||||
delete_file(fs_id[0])
|
||||
created = False
|
||||
raise e
|
||||
if created:
|
||||
return f"/admin/task?task_id={task.id}"
|
||||
return '/tasks'
|
||||
|
@ -30,8 +30,6 @@ class Command(MessagingSupport):
|
||||
'filename',
|
||||
'is_test',
|
||||
'is_sample',
|
||||
'readable',
|
||||
'test_number'
|
||||
]
|
||||
task_data['files'] = [
|
||||
{
|
||||
|
@ -34,7 +34,10 @@
|
||||
</div>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-primary"><i class="fa fa-arrow-up"></i> Загрузить</button>
|
||||
<label for="file-upload" class="btn btn-primary">
|
||||
<i class="fa fa-arrow-up"></i> Загрузить архив
|
||||
</label>
|
||||
<input type="file" form="fileform" style="display: none;" accept=".zip" class="btn form-control-file" id="file-upload" value="Выбрать файл" name="file" onchange="this.form.submit();">
|
||||
<button type="button" class="btn btn-danger" data-dismiss="modal"><i class="fa fa-times-circle"></i> Закрыть</button>
|
||||
<button type="submit" class="btn btn-success"><i class="fa fa-plus-circle"></i> Создать</button>
|
||||
</div>
|
||||
@ -43,6 +46,10 @@
|
||||
</div>
|
||||
</div>
|
||||
</td>
|
||||
<form method="POST" enctype="multipart/form-data" id="fileform">
|
||||
<input type="hidden" name="action" value="upload_file">
|
||||
{% csrf_token %}
|
||||
</form>
|
||||
{% endif %}
|
||||
</tr>
|
||||
</table>
|
||||
|
Loading…
Reference in New Issue
Block a user