def test_instance_attachment_collection_service(test_id):
resp = flask.Response(json.dumps({'status': 'failed'}))
if request.method == 'POST':
if request.is_xhr:
file = request.files['attachments']
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
dirpath = os.path.join(UPLOAD_FOLDER, str(test_id), 'attachments')
os.makedirs(dirpath, exist_ok=True)
filepath = os.path.join(dirpath, filename)
file.save(filepath)
# db: update the attachment for the test where test.id = test_id
test = Test.query.get(test_id)
test.test_attachment.append(TestAttachment(name=filename, attachment_url=filepath))
db.session.commit()
resp = flask.Response(json.dumps({'status': 'success', 'url': filepath}))
elif request.method == 'DELETE':
if request.is_json:
filename = secure_filename(request.json['removedFile'])
dirpath = os.path.join(UPLOAD_FOLDER, str(test_id), 'attachments')
filepath = os.path.join(dirpath, filename)
try:
os.remove(filepath)
# db: delete the attachment for the test where test.id = test_id
TestAttachment.query.filter(
(TestAttachment.test_id == test_id) &
(TestAttachment.name == filename)
).delete()
db.session.commit()
resp = flask.Response(json.dumps({'status': 'success', 'url': filepath}))
except FileNotFoundError:
print('FileNotFound: ', filepath)
resp = flask.Response(json.dumps({'status': 'failed', 'url': filepath}))
return set_debug_response_header(resp)
评论列表
文章目录