Ejemplo n.º 1
0
def ajax_upload(request, folder_id=None):
    """
    receives an upload from the flash uploader and fixes the session
    because of the missing cookie. Receives only one file at the time, 
    althow it may be a zip file, that will be unpacked.
    """
    # print request.POST
    # flashcookie-hack (flash does not submit the cookie, so we send the
    # django sessionid over regular post
    try:
        # import pdb
        # pdb.set_trace()
        engine = __import__(settings.SESSION_ENGINE, {}, {}, [""])
        # session_key = request.POST.get('jsessionid')
        session_key = request.POST.get("jsessionid")
        request.session = engine.SessionStore(session_key)
        request.user = User.objects.get(id=request.session["_auth_user_id"])
        # print request.session['_auth_user_id']
        # print session_key
        # print engine
        # print request.user
        # print request.session
        # upload and save the file
        if not request.method == "POST":
            return HttpResponse("must be POST")
        original_filename = request.POST.get("Filename")
        file = request.FILES.get("Filedata")
        # print request.FILES
        # print original_filename, file
        clipboard, was_clipboard_created = Clipboard.objects.get_or_create(user=request.user)
        files = generic_handle_file(file, original_filename)
        file_items = []
        for ifile, iname in files:
            try:
                iext = os.path.splitext(iname)[1].lower()
            except:
                iext = ""
            # print "extension: ", iext
            if iext in [".jpg", ".jpeg", ".png", ".gif"]:
                imageform = UploadFileForm({"original_filename": iname, "owner": request.user.pk}, {"file": ifile})
                if imageform.is_valid():
                    # print 'imageform is valid'
                    try:
                        image = imageform.save(commit=False)
                        image.save()
                        file_items.append(image)
                    except Exception, e:
                        print e
                    # print "save %s" % image
                    bi = ClipboardItem(clipboard=clipboard, file=image)
                    bi.save()
                    # sprint image
                else:
                    pass  # print imageform.errors
    except Exception, e:
        print e
        raise e
Ejemplo n.º 2
0
def ajax_upload(request, folder_id=None):
    """
    receives an upload from the flash uploader and fixes the session
    because of the missing cookie. Receives only one file at the time, 
    althow it may be a zip file, that will be unpacked.
    """
    print request.POST
    # flashcookie-hack (flash does not submit the cookie, so we send the
    # django sessionid over regular post
    engine = __import__(settings.SESSION_ENGINE, {}, {}, [''])
    #session_key = request.POST.get('jsessionid')
    # this sucks... session key in get!
    session_key = request.GET.get('jsessionid')
    request.session = engine.SessionStore(session_key)
    print request.session.session_key, request.user
    if folder_id:
        folder = Folder.objects.get(id=folder_id)
    else:
        folder = None
    
    # check permissions
    if request.user.is_superuser:
        pass
    elif folder == None:
        # regular users may not add root folders
        return HttpResponseForbidden()
    elif not folder.has_add_children_permission(request):
        # the user does not have the permission to images to this folder
        return HttpResponseForbidden()
    
    # upload and save the file
    if not request.method == 'POST':
        return HttpResponse("must be POST")
    original_filename = request.POST.get('Filename')
    file = request.FILES.get('Filedata')
    #print request.FILES
    #print original_filename, file
    clipboard, was_bucket_created = Clipboard.objects.get_or_create(user=request.user)
    files = generic_handle_file(file, original_filename)
    for ifile, iname in files:
        iext = os.path.splitext(iname)[1].lower()
        #print "extension: ", iext
        if iext in ['.jpg','.jpeg','.png','.gif']:
            imageform = UploadFileForm({'original_filename':iname,'owner': request.user.pk}, {'file':ifile})
            if imageform.is_valid():
                print 'imageform is valid'
                image = imageform.save(commit=False)
                image.save()
                bi = ClipboardItem(clipboard=clipboard, file=image)
                bi.save()
                print image
            else:
                pass#print imageform.errors
    return HttpResponse("ok")