def on_startup(self): """Called once on startup to invoke the creation of all necessary thumbnails and copying all the images into the folder that serves static content""" print ('Preparing Pyntrest...') global current_subdir # declare global counter current_subdir = 1 mkdirs (self.static_images_path) mkdirs (self.static_athumbs_path) mkdirs (self.static_ithumbs_path) mkdirs (self.static_fullsize_path) # Obtain number of sub directories number_of_subdirs = 0 for dirname, _, _ in walk (self.main_images_path): if not '\.' in str(dirname) and not '/.' in str(dirname): number_of_subdirs += 1 print ('Found {0} non-hidden directories...'.format(number_of_subdirs)) # Call recursive method self.on_startup_prepare_folder( self.main_images_path, '/', number_of_subdirs) print ('Preparation of Pyntrest done...')
def process_subalbum (self, subalbums, subalbum_name, local_albumpath_rel, local_albumpath_abs): """Uses the given sub-album name and creates a Album model for it, including the search for optional descriptions and album covers and the creation of all necessary thumbnails. Finally it appends the Album to the provided sub-album list.""" local_subalbumpath_abs = path.join(local_albumpath_abs, subalbum_name) modified, lastmodified = is_modified( local_subalbumpath_abs, False, pyntrest_config.MAX_AGE_OF_NEW_IMAGES_H, pyntrest_config.HIGHLIGHT_NEW_IMAGES, pyntrest_config.IMAGE_FILE_PATTERN) meta_title, meta_description, meta_cover, _, _, _, _ = ( read_optional_album_metadata (local_subalbumpath_abs, pyntrest_config.META_INI_FILE_PATTERN)) local_subalbumcover_abs = None # find and set cover image # If sub album cover manually set.. if meta_cover: # Check for existence.. cover_candidate = path.join(local_subalbumpath_abs, meta_cover) # on windows we also allow uncased matches (or .. lower()) if path.exists(cover_candidate) or path.exists(cover_candidate.lower()): local_subalbumcover_abs = cover_candidate # if no album cover was set manually or if it did not exist... if not local_subalbumcover_abs: # ... get first in folder for cover_candidate in listdir(local_subalbumpath_abs): if pyntrest_config.IMAGE_FILE_PATTERN.match( cover_candidate.lower()): local_subalbumcover_abs = path.join( local_subalbumpath_abs, cover_candidate) break subalbum_webpath = path.join(local_albumpath_rel, subalbum_name) subalbum_webpath = '/' + sub('\\\\', '/', subalbum_webpath) # If still no album cover was found then this is an empty album hence # we need to let the template use a default image if not local_subalbumcover_abs: # setup template context subalbum = Album(title=meta_title, description=meta_description, path=subalbum_webpath, width=pyntrest_config.IMAGE_THUMB_WIDTH, height=pyntrest_config.IMAGE_THUMB_HEIGHT, cover=None, modified=modified, last_modified=lastmodified) else: # otherwise prepare it for static serving image_basename = path.basename(local_subalbumcover_abs) local_thumbnail_folder_abs = path.join ( self.static_athumbs_path, local_albumpath_rel, subalbum_name) thumbnail_webpath = path.join (local_albumpath_rel, subalbum_name, image_basename) target_file = path.join(local_thumbnail_folder_abs, image_basename) mkdirs(local_thumbnail_folder_abs) self.pil_handler.create_album_thumbnail_if_not_present( local_subalbumcover_abs, target_file) # setup template context subalbum = Album( title=meta_title, description=meta_description, path=subalbum_webpath, width=pyntrest_config.IMAGE_THUMB_WIDTH, height=pyntrest_config.IMAGE_THUMB_HEIGHT, cover=thumbnail_webpath, modified=modified, last_modified=lastmodified) # append subalbum to context subalbums.append(subalbum)
def generate_view_context(self, request_path): """The core "magic" method that reads the requested album filesystem path related to the virtual album path from the request and obtains all necessary information to create a neat web photo album""" local_albumpath_abs = convert_url_path_to_local_filesystem_path( self.main_images_path, request_path) local_albumpath_rel = convert_url_path_to_local_filesystem_path( '', request_path) mkdirs(path.join (self.static_fullsize_path, local_albumpath_rel)) mkdirs(path.join (self.static_ithumbs_path, local_albumpath_rel)) (album_title, album_description, album_cover, reversed_sorting, hide_cover, mods_on_top, _) = read_optional_album_metadata ( local_albumpath_abs, pyntrest_config.META_INI_FILE_PATTERN) # setup sub albums subalbums = [] for subalbum_name in get_immediate_subdirectories(local_albumpath_abs): self.process_subalbum (subalbums, subalbum_name, local_albumpath_rel, local_albumpath_abs) # sort subalbums by path if (mods_on_top): subalbums = sorted(subalbums, key=lambda subalbum: subalbum.last_modified, reverse=True) else: subalbums = sorted(subalbums, key=lambda subalbum: subalbum.path, reverse=False) # setup images images = [] for image_name in listdir(local_albumpath_abs): subimage = self.process_subimage(image_name, local_albumpath_rel, local_albumpath_abs) if subimage is None: continue if (album_cover is None or hide_cover is False): images.append(subimage) else: if album_cover == image_name: pass # ignore else: images.append(subimage) # update image descriptions image_descriptions = read_optional_image_metadata( local_albumpath_abs, pyntrest_config.META_INI_FILE_PATTERN) for image in images: basename = path.basename(image.location).lower() try: image_description = image_descriptions[basename] image.description = image_description except KeyError: pass # Ignore # sort images by path images = sorted(images, key=lambda albumimage: albumimage.location, reverse=reversed_sorting) # setup breadcrumb breadcrumbs = [] breadcrumb_paths = get_absolute_breadcrumb_filesystem_paths ( request_path) local_albumpath_abs = self.main_images_path path_string = '' for breadcrumb_path in breadcrumb_paths: local_albumpath_abs = path.join(local_albumpath_abs, breadcrumb_path) path_string = path_string + '/' + breadcrumb_path path_string = sub ('[/]+' , '/', path_string) album_title, album_description, _, _, _, _, _= ( read_optional_album_metadata (local_albumpath_abs, pyntrest_config.META_INI_FILE_PATTERN)) url_path = path_string if pyntrest_config.EXTERNAL_BASE_URL is not None: url_path = pyntrest_config.EXTERNAL_BASE_URL + url_path web_path = WebPath(title=album_title, path=url_path) breadcrumbs.append(web_path) page_title = breadcrumbs[0].title breadcrumbs[0].title = pyntrest_config.WORDING_HOME # check for intro text intro_content = None intro_file = None subfiles = get_immediate_subfiles(local_albumpath_abs) for subfile in subfiles: matching = match(pyntrest_config.INTRO_MD_FILE_PATTERN, subfile.lower()) if matching != None: intro_file = path.join(local_albumpath_abs, subfile) if intro_file and path.isfile(intro_file): intro_content, _ = get_html_content(intro_file) # show header? show_breadcrumb = True first_page = len(breadcrumbs) == 1 try: if first_page and pyntrest_config.SUPPRESS_BREADCRUMB_ON_HOME: show_breadcrumb = False except AttributeError: pass # just show it. context = { 'page_title': page_title, 'col_width': pyntrest_config.IMAGE_THUMB_WIDTH, 'col_height' : pyntrest_config.IMAGE_THUMB_HEIGHT, 'images': images, 'show_breadcrum': False, 'first_page': first_page, 'subalbums': subalbums, 'album_title' : album_title, 'album_description' : album_description, 'lang_images' : pyntrest_config.WORDING_IMAGES, 'lang_albums' : pyntrest_config.WORDING_ALBUM, 'breadcrumbs' : breadcrumbs, 'show_breadcrumb': show_breadcrumb, 'show_headings' : pyntrest_config.SHOW_ALBUM_IMAGES_WORDINGS, 'intro_content' : intro_content} return context