def upload_import(request, task_id): try: upload_rec = get_pending_upload(request.session['username'], task_id) except: return HttpResponseNotFound() res = AsyncResult(task_id) if res.state != "SUCCESS": return HttpResponseForbidden() filename = upload_rec['filename'] filename_base = filename.split(".")[0] if request.method == 'POST': form = UploadImport(request.POST) if form.is_valid(): title = form.cleaned_data['title'] path = "/%s/%s" % (request.session['username'], form.cleaned_data['path']) description = form.cleaned_data['description'] labels = form.cleaned_data['labels'].split(',') labels = [label.strip() for label in labels if len(label.strip()) > 0] task = place_upload.delay(upload_rec['main_rowkey'], upload_rec['subfiles'], title, path, description, selected_dae=upload_rec['dae_choice'], extra_metadata={'labels': labels}) save_upload_task(username=request.session['username'], task_id=task.task_id, row_key=upload_rec['main_rowkey'], filename=upload_rec['filename'], subfiles=upload_rec['subfiles'], dae_choice=upload_rec['dae_choice'], task_name="place_upload") try: remove_pending_upload(request.session['username'], task_id) except: return HttpResponseServerError("There was an error removing your old upload record.") return redirect('content.views.upload_processing', task_id=task.task_id) else: if res.result is None: default_filename = filename else: default_filename = res.result form = UploadImport(initial={'path':default_filename, 'title':filename_base.capitalize()}) view_params = {'form': form, 'task_id': task_id, 'filename': filename} return render_to_response('content/import.html', view_params, context_instance = RequestContext(request))
def upload_choice(request, task_id): try: upload_rec = get_pending_upload(request.session['username'], task_id) except: return HttpResponseForbidden() res = AsyncResult(task_id) if res.state == "FAILURE" and type(res.result).__name__ == "TooManyDaeFound": names = res.result.names else: return HttpResponseForbidden() if request.method == 'POST': form = UploadChoiceForm(task_id, names, request.POST) if form.is_valid(): selected_dae = form.cleaned_data['file'] for id,value in form.fields['file'].choices: if id == selected_dae: selected_name = value task = import_upload.delay(upload_rec['main_rowkey'], upload_rec['subfiles'], selected_name) save_upload_task(username=request.session['username'], task_id=task.task_id, row_key=upload_rec['main_rowkey'], filename=upload_rec['filename'], subfiles=upload_rec['subfiles'], dae_choice=selected_name, task_name="import_upload") try: remove_pending_upload(request.session['username'], task_id) except: return HttpResponseServerError("There was an error removing your old upload record.") return redirect('content.views.upload_processing', task_id=task.task_id) else: form = UploadChoiceForm(task_id, names) view_params = {'task_id':task_id, 'form':form} return render_to_response('content/upload_choice.html', view_params, context_instance = RequestContext(request))
def api_upload(request): result = {} if request.method != 'POST': result['success'] = False result['error'] = 'Invalid request' else: oauth_request = oauth_server.request_from_django(request) verified = oauth_server.verify_access_request(oauth_request) if oauth_request else False if not verified: result['success'] = False result['error'] = 'OAuth Authentication Error' else: params = ['path', 'main_filename', 'title', 'description', 'subfiles', 'ephemeral', 'ttl_time', 'labels'] upload_data = dict((param, oauth_request.get(param)) for param in params) form = APIUpload(data=upload_data, files=request.FILES, file_names=request.FILES.keys()) if not form.is_valid(): result['success'] = False # Only log these errors if we haven't encountered some other error first. if 'error' not in result: errors = [] for field in form: if field.errors: errors.append("%s:%s" % (field.name, field.errors)) result['error'] = "Invalid form fields.\n" + form.errors.as_text() else: title = form.cleaned_data['title'] username = oauth_request.get_parameter('username') path = "/%s/%s" % (username, form.cleaned_data['path']) description = form.cleaned_data['description'] labels = form.cleaned_data['labels'].split(',') labels = [label.strip() for label in labels if len(label.strip()) > 0] main_filename = form.cleaned_data['main_filename'] ephemeral = form.cleaned_data['ephemeral'] ttl_time = form.cleaned_data['ttl_time'] ephemeral_subfiles = form.get_subfiles() main_rowkey = request.FILES[main_filename].row_key subfiles = {} for fname, fobj in request.FILES.iteritems(): if fname != main_filename: subfiles[fname] = fobj.row_key dae_choice = "" filename = main_filename if ephemeral: create_index = False else: ttl_time = None create_index = True ephemeral_subfiles = None task = place_upload.delay(main_rowkey, subfiles, title, path, description, create_index=create_index, ephemeral_ttl=ttl_time, ephemeral_subfiles=ephemeral_subfiles) save_upload_task(username=username, task_id=task.task_id, row_key=main_rowkey, filename=filename, subfiles=subfiles, dae_choice="", task_name="place_upload", ephemeral=ephemeral) result['success'] = True result['task_id'] = task.task_id response = HttpResponse(simplejson.dumps(result, default=json_handler, indent=4), mimetype='application/json') return response
def upload(request, task_id=None): if task_id: try: upload_rec = get_pending_upload(request.session['username'], task_id) except: return HttpResponseForbidden() orig_name = upload_rec['filename'] subfiles_uploaded = upload_rec['subfiles'] existing_files = subfiles_uploaded.keys() existing_files.append(orig_name) res = AsyncResult(task_id) if res.state == "FAILURE" and type(res.result).__name__ == "SubFilesNotFound": names = res.result.names else: return HttpResponseForbidden() if request.method == 'POST': if task_id: form = UploadForm(names, request.POST, request.FILES) if form.is_valid(): subfiles = subfiles_uploaded for n in names: subfiles[n] = request.FILES[n].row_key task = import_upload.delay(upload_rec['main_rowkey'], subfiles, upload_rec['dae_choice']) save_upload_task(username=request.session['username'], task_id=task.task_id, row_key=upload_rec['main_rowkey'], filename=upload_rec['filename'], subfiles=subfiles, dae_choice=upload_rec['dae_choice'], task_name="import_upload") try: remove_pending_upload(request.session['username'], task_id) except: return HttpResponseServerError("There was an error removing your old upload record.") return redirect('content.views.upload_processing', task_id=task.task_id) else: view_params = {'form':form, 'existing_files':existing_files} else: form = UploadForm(['File'], request.POST, request.FILES) if form.is_valid(): upfile = request.FILES['File'] task = import_upload.delay(upfile.row_key, subfiles={}) save_upload_task(username=request.session['username'], task_id=task.task_id, row_key=upfile.row_key, filename=upfile.name, subfiles={}, dae_choice="", task_name="import_upload") return redirect('content.views.upload_processing', task_id=task.task_id) else: view_params = {'form':form} else: if task_id: form = UploadForm(names) view_params = {'form':form, 'existing_files':existing_files} else: form = UploadForm(['File']) view_params = {'form':form} view_params['task_id'] = task_id return render_to_response('content/upload.html', view_params, context_instance = RequestContext(request))