def run(): for model in models: for obj in model.objects.all(): for field in image_fields: image = getattr(obj, field, None) # обрабатываем картинки if field != 'gallery' and image: path = models_name_dir_map[obj.__class__.__name__] new_path = os.path.join( path, slugify.slugify_filename(image.filename) ) try: site.storage.move(image.path, new_path) image.path = new_path obj.save() except IOError: # файл не существует print "can't find this file - %s" % image.path continue except OSError, e: # директория не создана site.storage.makedirs(path) print '--- create dir - %s' % path site.storage.move(image.path, new_path) image.path = new_path obj.save() # обрабатываем папки с галереями elif image and image.is_folder: # for galleries path = models_name_dir_map['Gallery'] % { 'model': obj.__class__.__name__.lower(), 'name': slugify.slugify_filename(obj.name.lower()) } gallery = FileListing(image.path) for image_name in gallery.listing(): try: new_path = os.path.join( path, slugify.slugify_filename(image_name) ) site.storage.move( os.path.join(image.path, image_name), new_path ) except IOError, e: # файл не существует print "can't find this file - %s" % image_name continue except OSError, e: # директория не создана site.storage.makedirs(path) print '--- create dir - %s' % path site.storage.move( os.path.join(image.path, image_name), new_path ) # save new gallery path print 'save new gallery path' image.path = path obj.save()
def userrepView(request): repha = models.repository.objects.filter(usern=request.user) filelisting = FileListing('git', sorting_by='date', sorting_order='desc') context = { 'rep': repha, 'folder': filelisting.listing(), } template = loader.get_template('myrep.html') return HttpResponse(template.render(context, request))
def treeview_parser(root='', abspath='', relpath='', flag='C'): """ According to the given root, traverse its file tree and return a json object. :param root: :param abspath: :param flag: 'C'-> Complete file tree, 'O'-> file tree used in open project :return: """ dataList = [] path = os.path.join(DIRECTORY, root) filelisting = FileListing(path, sorting_by='date', sorting_order='desc') for item in filelisting.listing(): fileobject = FileObject(os.path.join(path, item)) newabspath = os.path.join(abspath, item) # print(newabspath) if flag == 'O': dataList.append({ "text": item, "icon": "glyphicon glyphicon-folder-close", # "selectedIcon": "glyphicon glyphicon-folder-open", "nodes": treeview_parser(fileobject.path_relative_directory, newabspath, flag=flag), "href": reverse('maintest:index') + "?path=" + newabspath }) elif fileobject.is_folder: # and not fileobject.is_empty: dataList.append({ "text": item, "icon": "glyphicon glyphicon-folder-close", # "selectedIcon": "glyphicon glyphicon-folder-open", "nodes": treeview_parser(fileobject.path_relative_directory, newabspath, flag=flag) }) elif flag == 'C': dataList.append({ "text": item, "icon": "glyphicon glyphicon-file", "href": reverse('maintest:index') + "?file=" + newabspath + "&path=" + relpath # "href": "#edit-text" }) return dataList
def repview(request, id): obj = get_object_or_404(models.repository, pk=id) rep = models.repository.objects.get(id=id) pathh = rep.name filelisting = FileListing('repository/' + pathh, sorting_by=None, sorting_order=None) template = loader.get_template('repview.html') context = { 'obj': obj, 'rep': rep, 'folder': filelisting.listing(), } return HttpResponse(template.render(context, request))
def file_browse(self, request): query = request.GET query_dir = query.get('dir', '') path = u'%s' % os.path.join(site.directory, query_dir) # print(site.directory) # Return a file list:hehe filelisting = FileListing(path, sorting_by='date', sorting_order='desc') fileobjects = [] for filepath in filelisting.listing(): fileobject = FileObject(os.path.join(site.directory, query_dir, filepath)) # print(fileobject.path_relative_directory) fileobjects.append(fileobject) return render(request, 'files/index.html', { 'query': query, 'query_dir': query_dir, 'filelisting': filelisting, 'breadcrumbs': get_Breadcrumbs(query_dir), 'fileobjects': fileobjects })
def treeview_parser(root=''): """ According to the given root, traverse its file tree and return a json object. :param root: :return dict: """ dataList = [] path = os.path.join(DIRECTORY, root) filelisting = FileListing(path, sorting_by='date', sorting_order='desc') for item in filelisting.listing(): fileobject = FileObject(os.path.join(path, item)) if fileobject.is_folder and not fileobject.is_empty: dataList.append({ "text": item, "nodes": treeview_parser(fileobject.path_relative_directory) }) else: dataList.append({"text": item}) return dataList
class FileListingTests(TestCase): """ /_test/uploads/testimage.jpg /_test/uploads/folder/ /_test/uploads/folder/subfolder/ /_test/uploads/folder/subfolder/testimage.jpg """ def setUp(self): super(FileListingTests, self).setUp() self.F_LISTING_FOLDER = FileListing(self.DIRECTORY, sorting_by='date', sorting_order='desc') self.F_LISTING_IMAGE = FileListing(os.path.join(self.DIRECTORY, 'folder', 'subfolder', "testimage.jpg")) shutil.copy(self.STATIC_IMG_PATH, self.SUBFOLDER_PATH) shutil.copy(self.STATIC_IMG_PATH, self.DIRECTORY_PATH) def test_init_attributes(self): """ FileListing init attributes # path # filter_func # sorting_by # sorting_order """ self.assertEqual(self.F_LISTING_FOLDER.path, '_test/uploads/') self.assertEqual(self.F_LISTING_FOLDER.filter_func, None) self.assertEqual(self.F_LISTING_FOLDER.sorting_by, 'date') self.assertEqual(self.F_LISTING_FOLDER.sorting_order, 'desc') def test_listing(self): """ FileObject listing # listing # files_listing_total # files_listing_filtered # results_listing_total # results_listing_filtered """ self.assertEqual(self.F_LISTING_IMAGE.listing(), []) self.assertEqual(list(self.F_LISTING_FOLDER.listing()), [u'folder', u'testimage.jpg']) self.assertEqual(list(f.path for f in self.F_LISTING_FOLDER.files_listing_total()), [u'_test/uploads/testimage.jpg', u'_test/uploads/folder']) self.assertEqual(list(f.path for f in self.F_LISTING_FOLDER.files_listing_filtered()), [u'_test/uploads/testimage.jpg', u'_test/uploads/folder']) self.assertEqual(self.F_LISTING_FOLDER.results_listing_total(), 2) self.assertEqual(self.F_LISTING_FOLDER.results_listing_filtered(), 2) def test_listing_filtered(self): """ FileObject listing # listing # files_listing_total # files_listing_filtered # results_listing_total # results_listing_filtered """ self.assertEqual(self.F_LISTING_IMAGE.listing(), []) self.assertEqual(list(self.F_LISTING_FOLDER.listing()), [u'folder', u'testimage.jpg']) self.assertEqual(list(f.path for f in self.F_LISTING_FOLDER.files_listing_total()), [u'_test/uploads/testimage.jpg', u'_test/uploads/folder']) self.assertEqual(list(f.path for f in self.F_LISTING_FOLDER.files_listing_filtered()), [u'_test/uploads/testimage.jpg', u'_test/uploads/folder']) self.assertEqual(self.F_LISTING_FOLDER.results_listing_total(), 2) self.assertEqual(self.F_LISTING_FOLDER.results_listing_filtered(), 2) def test_walk(self): """ FileObject walk # walk # files_walk_total # files_walk_filtered # results_walk_total # results_walk_filtered """ self.assertEqual(self.F_LISTING_IMAGE.walk(), []) self.assertEqual(list(self.F_LISTING_FOLDER.walk()), [u'folder/subfolder/testimage.jpg', u'folder/subfolder', u'folder', u'testimage.jpg']) self.assertEqual(list(f.path for f in self.F_LISTING_FOLDER.files_walk_total()), [u'_test/uploads/testimage.jpg', u'_test/uploads/folder', u'_test/uploads/folder/subfolder', u'_test/uploads/folder/subfolder/testimage.jpg']) self.assertEqual(list(f.path for f in self.F_LISTING_FOLDER.files_walk_filtered()), [u'_test/uploads/testimage.jpg', u'_test/uploads/folder', u'_test/uploads/folder/subfolder', u'_test/uploads/folder/subfolder/testimage.jpg']) self.assertEqual(self.F_LISTING_FOLDER.results_walk_total(), 4) self.assertEqual(self.F_LISTING_FOLDER.results_walk_filtered(), 4)
class FileListingTests(TestCase): def setUp(self): """ Save original values/functions so they can be restored in tearDown our temporary file structure looks like this: /fb_test_directory/ /fb_test_directory/testimage.jpg /fb_test_directory/fb_tmp_dir/ /fb_test_directory/fb_tmp_dir/fb_tmp_dir_sub/ /fb_test_directory/fb_tmp_dir/fb_tmp_dir_sub/testimage.jpg """ self.original_path = filebrowser.base.os.path self.original_directory = site.directory self.original_versions_basedir = filebrowser.base.VERSIONS_BASEDIR self.original_versions = filebrowser.base.VERSIONS self.original_admin_versions = filebrowser.base.ADMIN_VERSIONS # DIRECTORY # custom directory because this could be set with sites # and we cannot rely on filebrowser.settings # FIXME: find better directory name self.directory = "fb_test_directory/" self.directory_path = os.path.join(site.storage.location, self.directory) if os.path.exists(self.directory_path): self.fail("Test directory already exists.") else: os.makedirs(self.directory_path) # set site directory site.directory = self.directory # create temporary test folder and move testimage # FIXME: find better path names self.tmpdir_name = os.path.join("fb_tmp_dir", "fb_tmp_dir_sub") self.tmpdir_path = os.path.join(site.storage.location, self.directory, self.tmpdir_name) if os.path.exists(self.tmpdir_path): self.fail("Temporary testfolder already exists.") else: os.makedirs(self.tmpdir_path) # copy test image to temporary test folder self.image_path = os.path.join(FILEBROWSER_PATH, "static", "filebrowser", "img", "testimage.jpg") if not os.path.exists(self.image_path): self.fail("Testimage not found.") shutil.copy(self.image_path, self.directory_path) shutil.copy(self.image_path, self.tmpdir_path) # set posixpath filebrowser.base.os.path = posixpath # filelisting/fileobject self.f_listing = FileListing(self.directory, sorting_by='date', sorting_order='desc') self.f_listing_file = FileListing(os.path.join(self.directory, self.tmpdir_name, "testimage.jpg")) def test_init_attributes(self): """ FileListing init attributes # path # filter_func # sorting_by # sorting_order """ self.assertEqual(self.f_listing.path, 'fb_test_directory/') self.assertEqual(self.f_listing.filter_func, None) self.assertEqual(self.f_listing.sorting_by, 'date') self.assertEqual(self.f_listing.sorting_order, 'desc') def test_listing(self): """ FileObject listing # listing # files_listing_total # files_listing_filtered # results_listing_total # results_listing_filtered """ self.assertEqual(self.f_listing_file.listing(), []) self.assertEqual(list(self.f_listing.listing()), [u'fb_tmp_dir', u'testimage.jpg']) self.assertEqual(list(f.path for f in self.f_listing.files_listing_total()), [u'fb_test_directory/testimage.jpg', u'fb_test_directory/fb_tmp_dir']) self.assertEqual(list(f.path for f in self.f_listing.files_listing_filtered()), [u'fb_test_directory/testimage.jpg', u'fb_test_directory/fb_tmp_dir']) self.assertEqual(self.f_listing.results_listing_total(), 2) self.assertEqual(self.f_listing.results_listing_filtered(), 2) def test_listing_filtered(self): """ FileObject listing # listing # files_listing_total # files_listing_filtered # results_listing_total # results_listing_filtered """ self.assertEqual(self.f_listing_file.listing(), []) self.assertEqual(list(self.f_listing.listing()), [u'fb_tmp_dir', u'testimage.jpg']) self.assertEqual(list(f.path for f in self.f_listing.files_listing_total()), [u'fb_test_directory/testimage.jpg', u'fb_test_directory/fb_tmp_dir']) self.assertEqual(list(f.path for f in self.f_listing.files_listing_filtered()), [u'fb_test_directory/testimage.jpg', u'fb_test_directory/fb_tmp_dir']) self.assertEqual(self.f_listing.results_listing_total(), 2) self.assertEqual(self.f_listing.results_listing_filtered(), 2) def test_walk(self): """ FileObject walk # walk # files_walk_total # files_walk_filtered # results_walk_total # results_walk_filtered """ self.assertEqual(self.f_listing_file.walk(), []) self.assertEqual(list(self.f_listing.walk()), [u'fb_tmp_dir/fb_tmp_dir_sub/testimage.jpg', u'fb_tmp_dir/fb_tmp_dir_sub', u'fb_tmp_dir', u'testimage.jpg']) self.assertEqual(list(f.path for f in self.f_listing.files_walk_total()), [u'fb_test_directory/testimage.jpg', u'fb_test_directory/fb_tmp_dir', u'fb_test_directory/fb_tmp_dir/fb_tmp_dir_sub', u'fb_test_directory/fb_tmp_dir/fb_tmp_dir_sub/testimage.jpg']) self.assertEqual(list(f.path for f in self.f_listing.files_walk_filtered()), [u'fb_test_directory/testimage.jpg', u'fb_test_directory/fb_tmp_dir', u'fb_test_directory/fb_tmp_dir/fb_tmp_dir_sub', u'fb_test_directory/fb_tmp_dir/fb_tmp_dir_sub/testimage.jpg']) self.assertEqual(self.f_listing.results_walk_total(), 4) self.assertEqual(self.f_listing.results_walk_filtered(), 4) def tearDown(self): """ Restore original values/functions """ filebrowser.base.os.path = self.original_path site.directory = self.original_directory filebrowser.base.VERSIONS_BASEDIR = self.original_versions_basedir filebrowser.base.VERSIONS = self.original_versions filebrowser.base.ADMIN_VERSIONS = self.original_admin_versions # remove temporary directory and test folder shutil.rmtree(self.directory_path)
def home_view(request): filelisting = FileListing(settings.MEDIA_ROOT, sorting_by='date', sorting_order='desc') files = filelisting.listing() return render(request, 'index.html', {'files': files})
def browse(self, request): """ Browse Files/Directories. """ filter_re = [] for exp in EXCLUDE: filter_re.append(re.compile(exp)) for k,v in VERSIONS.iteritems(): exp = (r'_%s(%s)') % (k, '|'.join(EXTENSION_LIST)) filter_re.append(re.compile(exp)) def filter_browse(item): filtered = item.filename.startswith('.') for re_prefix in filter_re: if re_prefix.search(item.filename): filtered = True if filtered: return False return True query = request.GET.copy() print query path = u'%s' % os.path.join(self.directory, query.get('dir', '')) print path filelisting = FileListing(path, filter_func=filter_browse, sorting_by=query.get('o', DEFAULT_SORTING_BY), sorting_order=query.get('ot', DEFAULT_SORTING_ORDER), site=self) print filelisting print filelisting.walk() files = [] if SEARCH_TRAVERSE and query.get("q"): listing = filelisting.files_walk_filtered() else: listing = filelisting.files_listing_filtered() # If we do a search, precompile the search pattern now do_search = query.get("q") if do_search: re_q = re.compile(query.get("q").lower(), re.M) filter_type = query.get('filter_type') filter_date = query.get('filter_date') for fileobject in listing: # date/type filter append = False if (not filter_type or fileobject.filetype == filter_type) and (not filter_date or get_filterdate(filter_date, fileobject.date or 0)): append = True # search if do_search and not re_q.search(fileobject.filename.lower()): append = False # append if append: files.append(fileobject) filelisting.results_total = len(listing) filelisting.results_current = len(files) p = Paginator(files, LIST_PER_PAGE) page_nr = request.GET.get('p', '1') try: page = p.page(page_nr) except (EmptyPage, InvalidPage): page = p.page(p.num_pages) store_tests = [] print 'pk HERE' filelisting.listing() tests = filelisting.walk() for test in tests: if '.py' in test: test = str.replace( str(test),'.py','') store_tests.append(test) print store_tests # PASSING new template field called tests return render_to_response('filebrowser/index.html', { 'p': p, 'page': page, 'filelisting': filelisting, 'tests': store_tests, 'query': query, 'title': _(u'Testology'), 'settings_var': get_settings_var(directory=self.directory), 'breadcrumbs': get_breadcrumbs(query, query.get('dir', '')), 'breadcrumbs_title': "", 'filebrowser_site': 'test' }, context_instance=Context(request, current_app=self.name))