def preview(self): """ Presents a preview of the files to be imported, giving the user an opportunity to view and change dates for images, highlighting images which may already be in the system, and the like. """ rel_import_dir = os.path.relpath(self._env.get("PATH_INFO", "").lstrip("/"), "import/preview") import_dir = os.path.realpath(os.path.join(S.IMPORT_DIR, rel_import_dir)) file_listing = [] import_identifier = hashlib.sha1() hashes = [] session_file_struct = {} for base_dir, _, files in os.walk(import_dir): for f in files: if not util.is_image_file(f): continue fc = util.FileContainer(os.path.join(import_dir, f), S.IMPORT_DIR) ts = util.get_time(fc, allow_date_from_path=False) if ts["time"] != None: fc.time = time.strftime("%Y-%m-%d %H:%M:%S", ts["time"]) hashes.append(fc.hash) import_identifier.update(fc.hash) file_listing.append(fc) session_file_struct[fc.hash] = { "file_data": fc.__dict__(), "conflicts": None } break file_listing = sorted(file_listing, key=itemgetter('name')) conflicts = Photo.get_by_hash(hashes) for conflict_hash in conflicts.keys(): conflicts_for_json = [c.id for c in conflicts[conflict_hash]] session_file_struct[conflict_hash]["conflicts"] = conflicts_for_json session_file_struct[conflict_hash]["file_data"]["marked"] = True Logger.debug(session_file_struct) session_id = import_identifier.hexdigest() session_data = { "file_listing": session_file_struct, "rel_dir": rel_import_dir, "session_id": session_id } with open(os.path.join("/tmp", "%s.txt" % session_id), "w+") as f: f.write(json.dumps(session_data)) return self.construct_response( Template.render( "import/preview.html", { "files": file_listing, "import_id": session_id, "import_dir": rel_import_dir, "conflicts": conflicts } ), self._route_types.HTML_CONTENT_TYPE )
def preview(self): """ Presents a preview of the files to be imported, giving the user an opportunity to view and change dates for images, highlighting images which may already be in the system, and the like. """ rel_import_dir = os.path.relpath( self._env.get("PATH_INFO", "").lstrip("/"), "import/preview") import_dir = os.path.realpath( os.path.join(S.IMPORT_DIR, rel_import_dir)) file_listing = [] import_identifier = hashlib.sha1() hashes = [] session_file_struct = {} for base_dir, _, files in os.walk(import_dir): for f in files: if not util.is_image_file(f): continue fc = util.FileContainer(os.path.join(import_dir, f), S.IMPORT_DIR) ts = util.get_time(fc, allow_date_from_path=False) if ts["time"] != None: fc.time = time.strftime("%Y-%m-%d %H:%M:%S", ts["time"]) hashes.append(fc.hash) import_identifier.update(fc.hash) file_listing.append(fc) session_file_struct[fc.hash] = { "file_data": fc.__dict__(), "conflicts": None } break file_listing = sorted(file_listing, key=itemgetter('name')) conflicts = Photo.get_by_hash(hashes) for conflict_hash in conflicts.keys(): conflicts_for_json = [c.id for c in conflicts[conflict_hash]] session_file_struct[conflict_hash][ "conflicts"] = conflicts_for_json session_file_struct[conflict_hash]["file_data"]["marked"] = True Logger.debug(session_file_struct) session_id = import_identifier.hexdigest() session_data = { "file_listing": session_file_struct, "rel_dir": rel_import_dir, "session_id": session_id } with open(os.path.join("/tmp", "%s.txt" % session_id), "w+") as f: f.write(json.dumps(session_data)) return self.construct_response( Template.render( "import/preview.html", { "files": file_listing, "import_id": session_id, "import_dir": rel_import_dir, "conflicts": conflicts }), self._route_types.HTML_CONTENT_TYPE)