def export_folder(self, folder, directory_settings=None, filter=None): # get the directory content # relative_path = os.path.join( # self._request.registry.settings['root_dir'], # self._request.matchdict['dir']) self._folder = folder relative_path = folder listing = os.listdir(relative_path) relative_path = str(os.path.abspath(relative_path)).encode('string-escape') relative_path = relative_path.decode('string-escape') # filter the folder content itemgrouper = ItemGrouper() visible_items_by_extension, visibleitems, invisibleitems = itemgrouper.group_folder(listing, directory_settings) # filter the specific file extension if filter is not None: if filter in visible_items_by_extension: visible_items_by_extension = visible_items_by_extension[filter] output = '' if os.path.exists(relative_path + '/.intro.md'): with open(relative_path + '/.intro.md') as file: output += file.read() # iterate through the file output += self._iterate_folder('#', visible_items_by_extension) if os.path.exists(relative_path + '/.outro.md'): with open(relative_path + '/.outro.md') as file: output += file.read() return output
def handle_request(request, relative_path, directory_settings): depth = 0 listing = os.listdir(relative_path) relative_path = str(os.path.abspath(relative_path)).encode('string-escape') relative_path = relative_path.decode('string-escape') returnfilename = os.path.basename(relative_path) # filter the folder content itemgrouper = ItemGrouper() items_dict, visibleitems, invisibleitems = itemgrouper.group_folder(listing, directory_settings) if 'specific' in dict(request.params): specific_filetype = request.params['specific'] if specific_filetype in items_dict: items_dict = items_dict[specific_filetype] returnfilename += '_' + specific_filetype depth = 1 # TODO: Sometimes zip file creation doesnt halt. It seems to be dependent on the fact whether another request.zip file already exists nosubfolderallowed = 'key_not_as_folder_separator' in dict(request.params) tuples = DirectoryZipHandler._file_tuples(depth, '', items_dict) tempdirpath = tempfile.mkdtemp() zippath = tempdirpath + '/request.zip' with ZipFile(zippath, 'w') as zip: for tuple in tuples: localfilepath = relative_path + '/' + tuple[0] if tuple[0].startswith('.'): continue if os.path.isdir(localfilepath): continue if nosubfolderallowed: zip.write(localfilepath, tuple[0]) else: zip.write(localfilepath, tuple[1]) with open(zippath, 'rb') as zip: return Response(body=zip.read(), content_type='application/zip', content_disposition='attachment; filename="{0}.zip"'.format(returnfilename))
def directory(self): # TODO: load the description files relative_path = DirectoryRequestHandler.requestfolderpath(self.request) listing = os.listdir(relative_path) relative_path = str(os.path.abspath(relative_path)).encode('string-escape') relative_path = relative_path.decode('string-escape') # load settings, and reload if necessary directory_settings = self.request.registry.settings['directory_settings'] directory_settings = DirectoryLoadSettings.handle_request(self.request, relative_path, directory_settings) # load custom description description = self._get_custom_directory_description() # TODO: Check whether there is a more 'clean' way to handle these specific requests custom_response = self._custom_request_handler(relative_path, directory_settings) if custom_response is not None: return custom_response visible_items_by_extension, vi, invitems = ItemGrouper().group_folder(listing, directory_settings) # get the folders and files folders = visible_items_by_extension[''] if '' in visible_items_by_extension else [] files = dict(visible_items_by_extension) if '' in files: del files[''] # get labels element_labels = self._retrieve_labels(self.request.matchdict['dir'], vi) # get all folde descriptions folder_descriptions = dict() for f in folders: folder_descriptions[f] = self._get_custom_directory_description(f) if 'specific_filetemplates' in directory_settings: for (key, value) in visible_items_by_extension.items(): if key not in directory_settings['specific_filetemplates']: continue # could crash in one level cases visible_items_by_extension[key] = ItemGrouper().convert_leafs_to_dicts(visible_items_by_extension[key], filespecific_updates=element_labels) # apply specific to the items # TODO: if there is a specific key template, iterate till 'filename' is found then apply specific template # for the whole tree apply then the specific key template key_path = [key] visible_items_by_extension[key] = TemplateHandler().\ apply_templates(visible_items_by_extension[key], directory_settings, folder_descriptions, overwrite_key=key, keypath=key_path) custom_directory_template_path = TemplateHandler.loadCustomTemplate(self.request, directory_settings, 'directory_template_path', 'template/directory.pt') # send it to the general directory view directory_entry = render(custom_directory_template_path, dict(dir=self.request.matchdict['dir'], visible_items_by_extension=visible_items_by_extension, description=description, request=self.request)) custom_index_path = TemplateHandler.loadCustomTemplate(self.request, directory_settings, 'custom_index_path', 'template/index.pt') localsettingsfileexists = '.settings.json' in invitems index_parameter = dict(request=self.request, html=directory_entry, folders=folders, files=files, localsettingsfile=localsettingsfileexists, logged_in=self.request.authenticated_userid) return Response(render(custom_index_path, index_parameter))