def ajax_upload(self, 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. """ try: # 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') request.session = engine.SessionStore(session_key) request.user = User.objects.get( id=request.session['_auth_user_id']) # 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') # Get clipboad 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 = '' if iext in ['.jpg', '.jpeg', '.png', '.gif']: uploadform = UploadImageFileForm( { 'original_filename': iname, 'owner': request.user.pk }, {'file': ifile}) else: uploadform = UploadFileForm( { 'original_filename': iname, 'owner': request.user.pk }, {'file': ifile}) if uploadform.is_valid(): try: file = uploadform.save(commit=False) # Enforce the FILER_IS_PUBLIC_DEFAULT file.is_public = filer_settings.FILER_IS_PUBLIC_DEFAULT file.save() file_items.append(file) clipboard_item = ClipboardItem(clipboard=clipboard, file=file) clipboard_item.save() except Exception, e: #print e pass else: pass #print uploadform.errors except Exception, e: #print e pass
def ajax_upload(self, 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. """ file_items = [] try: # 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') request.session = engine.SessionStore(session_key) request.user = User.objects.get(id=request.session['_auth_user_id']) # 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') # Get clipboad clipboard, was_clipboard_created = Clipboard.objects.get_or_create(user=request.user) files = generic_handle_file(file, original_filename) for ifile, iname in files: try: iext = os.path.splitext(iname)[1].lower() except: iext = '' if iext in ['.jpg', '.jpeg', '.png', '.gif']: uploadform = UploadImageFileForm({'original_filename':iname, 'owner': request.user.pk}, {'file':ifile}) else: uploadform = UploadFileForm({'original_filename':iname, 'owner': request.user.pk}, {'file':ifile}) if uploadform.is_valid(): try: file = uploadform.save(commit=False) # Enforce the FILER_IS_PUBLIC_DEFAULT file.is_public = filer_settings.FILER_IS_PUBLIC_DEFAULT file.save() file_items.append(file) clipboard_item = ClipboardItem(clipboard=clipboard, file=file) clipboard_item.save() except Exception: logger.exception("Failed to save clipboard item.") else: logger.warn("Upload form not valid. The errors are: %s", uploadform.errors) except Exception: logger.exception("Failed to process upload:") return render_to_response('admin/filer/tools/clipboard/clipboard_item_rows.html', {'items': file_items }, context_instance=RequestContext(request))
def ajax_upload(self, request, folder_id=None): """ Receives an upload from the uploader. Receives only one file at a time. """ mimetype = "application/json" if request.is_ajax() else "text/html" content_type_key = 'mimetype' if DJANGO_1_4 else 'content_type' response_params = {content_type_key: mimetype} try: upload, filename, is_raw = handle_upload(request) # Get clipboad clipboard = Clipboard.objects.get_or_create(user=request.user)[0] # find the file type for filer_class in filer_settings.FILER_FILE_MODELS: FileSubClass = load_object(filer_class) # TODO: What if there are more than one that qualify? if FileSubClass.matches_file_type(filename, upload, request): FileForm = modelform_factory(model=FileSubClass, fields=('original_filename', 'owner', 'file')) break uploadform = FileForm( { 'original_filename': filename, 'owner': request.user.pk }, {'file': upload}) if uploadform.is_valid(): file_obj = uploadform.save(commit=False) # Enforce the FILER_IS_PUBLIC_DEFAULT file_obj.is_public = filer_settings.FILER_IS_PUBLIC_DEFAULT file_obj.save() clipboard_item = ClipboardItem(clipboard=clipboard, file=file_obj) clipboard_item.save() json_response = { 'thumbnail': file_obj.icons['32'], 'alt_text': '', 'label': str(file_obj), } return HttpResponse(json.dumps(json_response), **response_params) else: form_errors = '; '.join([ '%s: %s' % (field, ', '.join(errors)) for field, errors in list(uploadform.errors.items()) ]) raise UploadException( "AJAX request not valid: form invalid '%s'" % (form_errors, )) except UploadException as e: return HttpResponse(json.dumps({'error': str(e)}), **response_params)
def ajax_upload(self, request, folder_id=None): """ Receives an upload from the uploader. Receives only one file at a time. """ mimetype = "application/json" if request.is_ajax() else "text/html" content_type_key = 'mimetype' if DJANGO_1_4 else 'content_type' response_params = {content_type_key: mimetype} try: upload, filename, is_raw = handle_upload(request) # Get clipboad clipboard = Clipboard.objects.get_or_create(user=request.user)[0] # find the file type for filer_class in filer_settings.FILER_FILE_MODELS: FileSubClass = load_object(filer_class) # TODO: What if there are more than one that qualify? if FileSubClass.matches_file_type(filename, upload, request): FileForm = modelform_factory( model=FileSubClass, fields=('original_filename', 'owner', 'file') ) break uploadform = FileForm({'original_filename': filename, 'owner': request.user.pk}, {'file': upload}) if uploadform.is_valid(): file_obj = uploadform.save(commit=False) # Enforce the FILER_IS_PUBLIC_DEFAULT file_obj.is_public = filer_settings.FILER_IS_PUBLIC_DEFAULT file_obj.save() clipboard_item = ClipboardItem( clipboard=clipboard, file=file_obj) clipboard_item.save() json_response = { 'thumbnail': file_obj.icons['32'], 'alt_text': '', 'label': str(file_obj), } return HttpResponse(json.dumps(json_response), **response_params) else: form_errors = '; '.join(['%s: %s' % ( field, ', '.join(errors)) for field, errors in list( uploadform.errors.items()) ]) raise UploadException( "AJAX request not valid: form invalid '%s'" % ( form_errors,)) except UploadException as e: return HttpResponse(json.dumps({'error': str(e)}), **response_params)
def ajax_upload(self, request, folder_id=None): """ receives an upload from the uploader. Receives only one file at the time. """ mimetype = "application/json" if request.is_ajax() else "text/html" try: upload, filename, is_raw = handle_upload(request) # Get clipboad clipboard = Clipboard.objects.get_or_create(user=request.user)[0] # find the file type for filer_class in filer_settings.FILER_FILE_MODELS: FileSubClass = load_object(filer_class) #TODO: What if there are more than one that qualify? if FileSubClass.matches_file_type(filename, upload, request): FileForm = modelform_factory(model=FileSubClass, fields=('original_filename', 'owner', 'file')) break uploadform = FileForm( { 'original_filename': filename, 'owner': request.user.pk }, {'file': upload}) if uploadform.is_valid(): file_obj = uploadform.save(commit=False) # Enforce the FILER_IS_PUBLIC_DEFAULT file_obj.is_public = filer_settings.FILER_IS_PUBLIC_DEFAULT file_obj.save() clipboard_item = ClipboardItem(clipboard=clipboard, file=file_obj) clipboard_item.save() try: json_response = { 'thumbnail': file_obj.icons['32'], 'alt_text': '', 'label': unicode(file_obj), } except Exception, e: json_response = { 'thumbnail': None, 'alt_text': '', 'label': unicode(file_obj), } return HttpResponse(simplejson.dumps(json_response), mimetype=mimetype) else:
def ajax_upload(self, request, folder_id=None): """ receives an upload from the uploader. Receives only one file at the time. """ mimetype = "application/json" if request.is_ajax() else "text/html" try: upload, filename, is_raw = handle_upload(request) # Get clipboad clipboard = Clipboard.objects.get_or_create(user=request.user)[0] # find the file type for filer_class in filer_settings.FILER_FILE_MODELS: FileSubClass = load_object(filer_class) #TODO: What if there are more than one that qualify? if FileSubClass.matches_file_type(filename, upload, request): FileForm = modelform_factory( model = FileSubClass, fields = ('original_filename', 'owner', 'file') ) break uploadform = FileForm({'original_filename': filename, 'owner': request.user.pk}, {'file': upload}) if uploadform.is_valid(): file_obj = uploadform.save(commit=False) # Enforce the FILER_IS_PUBLIC_DEFAULT file_obj.is_public = filer_settings.FILER_IS_PUBLIC_DEFAULT file_obj.save() clipboard_item = ClipboardItem( clipboard=clipboard, file=file_obj) clipboard_item.save() try: json_response = { 'thumbnail': file_obj.icons['32'], 'alt_text': '', 'label': unicode(file_obj), } except Exception, e: json_response = { 'thumbnail': None, 'alt_text': '', 'label': unicode(file_obj), } return HttpResponse(simplejson.dumps(json_response), mimetype=mimetype) else:
def ajax_upload(self, 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. """ try: #print request.POST # flashcookie-hack (flash does not submit the cookie, so we send the # django sessionid over regular post #print "ajax upload view" engine = __import__(settings.SESSION_ENGINE, {}, {}, ['']) #print "imported session store" #session_key = request.POST.get('jsessionid') session_key = request.POST.get('jsessionid') #print "session_key: %s" % session_key request.session = engine.SessionStore(session_key) #print "got session object" request.user = User.objects.get(id=request.session['_auth_user_id']) #print "got user" #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 #print "get clipboad" clipboard, was_clipboard_created = Clipboard.objects.get_or_create(user=request.user) #print "handle files" files = generic_handle_file(file, original_filename) file_items = [] #print "loop files" for ifile, iname in files: try: iext = os.path.splitext(iname)[1].lower() except: iext = '' #print "inaem: %s iext: %s" % (iname, iext) #print "extension: ", iext if iext in ['.jpg','.jpeg','.png','.gif']: uploadform = UploadImageFileForm({'original_filename':iname,'owner': request.user.pk}, {'_file':ifile}) else: uploadform = UploadFileForm({'original_filename':iname,'owner': request.user.pk}, {'_file':ifile}) if uploadform.is_valid(): #print 'uploadform is valid' try: file = uploadform.save(commit=False) file.save() file_items.append(file) bi = ClipboardItem(clipboard=clipboard, file=file) bi.save() except Exception, e: #print "some exception" #print e pass #print "save %s" % image #sprint image else: pass#print uploadform.errors #print "what up?" except Exception, e: #print e pass