def get_folder_content(request): search_tag = request.GET.get('search_tag', '') search_text = request.GET.get('search_text', '') ppath = request.GET.get('dir', '') ppath = urllib.unquote(ppath.rstrip('/')) path = clean_path(ppath) if path != ppath: path = '' default_text = _(u'Recent files') bdelete = False if path == '': files = get_home_page_files(request, search_tag, search_text) if search_tag or search_text: default_text = _(u'Search results') bmanager = False folder_item_template = 'fileman/fm_folder_item_home.html' else: buser, bmanager = get_user_access_to_path(request.user, path) if not buser: return HttpResponse('') else: files, dirs = get_files(request, path, search_tag, search_text) folder_item_template = 'fileman/fm_folder_item.html' if dirs or files: bdelete = False else: bdelete = True parts = path.split('/') if len(parts) == 1: brename = False bdelete = False else: brename = True cdir_components = get_path_components(path, default_text) extra_context = {'cdir_components': cdir_components, 'files': files, 'search_text': search_text, 'search_tag': search_tag, 'manager': bmanager, 'brename': brename, 'bdelete': bdelete } extra_context['folder_item_template'] = folder_item_template context = RequestContext(request) return render_to_response('fileman/fm_folder_content.html', extra_context, context_instance=context)
def get_folder_content(request): search_tag = request.GET.get('search_tag', '') search_text = request.GET.get('search_text', '') ppath = request.GET.get('dir', '') ppath = urllib.unquote(ppath.rstrip('/')) path = clean_path(ppath) if path != ppath: path = '' bdelete = False if path == '': files = get_home_page_files(request, search_tag, search_text) bmanager = False folder_item_template = 'fileman/fm_folder_item_home.html' else: buser, bmanager = get_user_access_to_path(request.user, path) if not buser: return HttpResponse('') else: files, dirs = get_files(request, path, search_tag, search_text) folder_item_template = 'fileman/fm_folder_item.html' if dirs or files: bdelete = False else: bdelete = True parts = path.split('/') if len(parts) == 1: brename = False bdelete = False else: brename = True cdir_components = get_path_components(path) extra_context = {'cdir_components': cdir_components, 'files': files, 'search_text': search_text, 'search_tag': search_tag, 'manager': bmanager, 'brename': brename, 'bdelete': bdelete } extra_context['folder_item_template'] = folder_item_template context = RequestContext(request) return render_to_response('fileman/fm_folder_content.html', extra_context, context_instance=context)
def fm_properties_view(request): pathfile = request.REQUEST.get('dir', None) file_name = request.REQUEST.get('file_name', None) file_id = request.REQUEST.get('file_id', 0) ret = {} if pathfile: ppath = urllib.unquote(pathfile.rstrip('/')) path_dest = clean_path(ppath) if path_dest != ppath: ret['message'] = gettext(u"Incorrect path") ret['status'] = 'error' else: buser, bmanager = get_user_access_to_path(request.user, path_dest) if bmanager is False: ret['message'] = gettext(u"Access denied") ret['status'] = 'error' else: folder = get_shared_folder(path_dest) try: fo = ZornaFile.objects.get(pk=int(file_id)) except: ret['message'] = gettext(u"Access denied") ret['status'] = 'error' else: cdir_components = get_path_components(path_dest) if request.method == 'POST': root_path = get_upload_library() form = ZornaFileForm(request.POST, instance=fo) if form.is_valid(): fo = form.save(commit=False) fo.modifier = request.user fo.save() if request.FILES: f = request.FILES['file'] s = os.path.splitext(f.name) fname = u"%s%s" % (slugify(s[0]), s[1]) os.remove(u"%s/%s/%s,%s" % ( root_path, path_dest, file_id, file_name)) destination = open(u"%s/%s/%s,%s" % ( root_path, path_dest, file_id, fname), 'wb+') for chunk in f.chunks(): destination.write(chunk) destination.close() # notify users bnotify = request.POST.get('notify_users', 0) if folder and folder.email_notification and (folder.email_notification == 1 or bnotify): notify_users(request, folder, [{ 'name': fname, 'description': fo.description}], False) ret['message'] = gettext( u"File updated successfully") ret['status'] = 'success' json_data = simplejson.dumps(ret) return HttpResponse('<textarea>' + json_data + '</textarea>') else: ret['message'] = gettext(u"Invalid form") ret['status'] = 'error' else: form = ZornaFileForm(instance=fo) ret['status'] = 'success' t = loader.get_template('fileman/file_properties.html') ec = { 'file': fo, 'form': form, 'file_path': pathfile, 'file_name': file_name, 'file_id': file_id, 'cdir_components': cdir_components, 'folder_content_url': get_url_folder_content(pathfile), 'folder': folder} c = RequestContext(request, ec) ret['data'] = t.render(c) else: ret['message'] = gettext(u"Invalid file path") ret['status'] = 'error' json_data = simplejson.dumps(ret) return HttpResponse(json_data)
def fm_multiple_upload_view(request): pathfile = request.REQUEST.get('dir', None) ret = {} if pathfile: ppath = urllib.unquote(pathfile.rstrip('/')) path_dest = clean_path(ppath) if path_dest != ppath: ret['message'] = gettext(u"Incorrect path") ret['status'] = 'error' else: buser, bmanager = get_user_access_to_path(request.user, path_dest) folder = get_shared_folder(path_dest) if bmanager is False: ret['message'] = gettext(u"Access denied") ret['status'] = 'error' else: cdir_components = get_path_components(path_dest) form = ZornaFileUploadForm(request.POST, request.FILES) if request.method == 'POST': root_path = get_upload_library() if form.is_valid(): path = u"%s/%s" % ( root_path, urllib.unquote(path_dest)) uploaded_file = request.FILES['file'] chunk = request.REQUEST.get('chunk', '0') chunks = request.REQUEST.get('chunks', '0') name = uploaded_file.name temp_file = '%s/%s.part' % (path, name) with open(temp_file, ('wb' if chunk == '0' else 'ab')) as f: for content in uploaded_file.chunks(): f.write(content) if int(chunk) + 1 >= int(chunks): s = os.path.splitext(name) file = ZornaFile( owner=request.user, modifier=request.user, description='', folder=path_dest.split('/')[0]) file.save() destination = u"%s/%s,%s" % (path, file.pk, name) shutil.move(temp_file, destination) if request.is_ajax(): response = HttpResponse( '{"jsonrpc" : "2.0", "result" : null, "id" : "id"}') response[ 'Expires'] = 'Mon, 1 Jan 2000 01:00:00 GMT' response[ 'Cache-Control'] = 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0' response['Pragma'] = 'no-cache' return response else: ret['status'] = 'success' else: ret['status'] = 'success' t = loader.get_template( 'fileman/upload_multiple_documents.html') c = RequestContext(request, { 'form': form, 'cdir_components': cdir_components, 'folder_dest': pathfile, 'folder_content_url': get_url_folder_content(pathfile), 'folder': folder, 'language': settings.LANGUAGE_CODE[0:2]}) ret['data'] = t.render(c) else: ret['message'] = gettext(u"Invalid file path") ret['status'] = 'error' json_data = simplejson.dumps(ret) return HttpResponse(json_data)
def fm_upload_view(request): pathfile = request.REQUEST.get('dir', None) ret = {} if pathfile: ppath = urllib.unquote(pathfile.rstrip('/')) path_dest = clean_path(ppath) if path_dest != ppath: ret['message'] = gettext(u"Incorrect path") ret['status'] = 'error' else: buser, bmanager = get_user_access_to_path(request.user, path_dest) folder = get_shared_folder(path_dest) if bmanager is False: ret['message'] = gettext(u"Access denied") ret['status'] = 'error' else: cdir_components = get_path_components(path_dest) if request.method == 'POST': root_path = get_upload_library() fa_set = formset_factory(ZornaFileAddForm, extra=2) form_set = fa_set(request.POST, request.FILES) if form_set.is_valid(): upload_files = {} ifiles = [] for i in range(0, form_set.total_form_count()): form = form_set.forms[i] try: f = request.FILES['form-' + str(i) + '-file'] s = os.path.splitext(f.name) fname = u"%s%s" % (slugify(s[0]), s[1]) upload_files[fname] = {'pk': '', 'file': f, 'description': form.cleaned_data['description'], 'tags': form.cleaned_data['tags']} except: continue path = u"%s/%s" % ( root_path, urllib.unquote(path_dest)) bupload = False for f, info in upload_files.iteritems(): file = ZornaFile(owner=request.user, modifier=request.user, description=info[ 'description'], tags=info['tags'], folder=path_dest.split('/')[0]) file.save() destination = open(u"%s/%s,%s" % ( path, file.pk, f), 'wb+') bupload = True for chunk in info['file'].chunks(): destination.write(chunk) destination.close() ifiles.append({'name': f, 'description': info[ 'description']}) if bupload: ret['message'] = gettext( u"Files uploaded successfully") # notify users bnotify = request.POST.get('notify_users', 0) if folder and folder.email_notification and (folder.email_notification == 1 or bnotify): notify_users(request, folder, ifiles, True) else: ret['message'] = gettext(u"No file uploaded") ret['status'] = 'success' json_data = simplejson.dumps(ret) return HttpResponse('<textarea>' + json_data + '</textarea>') else: ret['message'] = gettext(u"Invalid form") ret['status'] = 'error' t = loader.get_template( 'fileman/upload_documents.html') c = RequestContext(request, {'form_set': form_set, 'cdir_components': cdir_components, 'folder_dest': pathfile, 'folder_content_url': get_url_folder_content(pathfile)}) ret['data'] = t.render(c) json_data = simplejson.dumps(ret) return HttpResponse('<textarea>' + json_data + '</textarea>') else: fa_set = formset_factory(ZornaFileAddForm, extra=2) form_set = fa_set() ret['status'] = 'success' t = loader.get_template('fileman/upload_documents.html') c = RequestContext(request, {'form_set': form_set, 'cdir_components': cdir_components, 'folder_dest': pathfile, 'folder_content_url': get_url_folder_content(pathfile), 'folder': folder, 'manager': bmanager, }) ret['data'] = t.render(c) else: ret['message'] = gettext(u"Invalid file path") ret['status'] = 'error' json_data = simplejson.dumps(ret) return HttpResponse(json_data)