コード例 #1
0
ファイル: views.py プロジェクト: hnkien/sirikata-cdn
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))
コード例 #2
0
ファイル: views.py プロジェクト: hnkien/sirikata-cdn
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