def test_upload(request): if request.method == 'GET': form = UploadForm() msg = 'Please upload a txt file' elif request.method == 'POST': form = UploadForm(request.POST, request.FILES) if form.is_valid(): file = request.FILES['file'] msg = file.read() return make_response(request, 'test_upload.html', { 'form': form, 'msg': msg, })
def upload(request): # upload/make a submission if request.method == 'POST': sleep(1) form = UploadForm(request.POST, request.FILES) if form.is_valid(): file = request.FILES['file'] assID = request.POST['assID'] user = request.user submission = None try: submission = _upload(user, assID, file) check_submission(submission) submission.retcode = 0 submission.message = 'success' submission.save() return json.dumps({ 'code': 0, 'message': submission.message, 'sid': str(submission.id), }) except UploadError as err: html = err.html() if submission: submission.code = err.code submission.message = html submission.save() result = json.dumps({ 'code': err.code, 'message': html, }) logger.debug('code:{}\nhtml:{}'.format(err.code, html.encode('utf8'))) return result return json.dumps({ 'code': 1, 'message': form.errors, }) return json.dumps({ 'code': 1, 'message': 'method is not POST', })