def _update_database_after_move(self, src_file_web_path, dst_file_web_path): try: media_file = File.objects.get( name=(os.path.basename(src_file_web_path)), directory__path=os.path.dirname(src_file_web_path)) # create parent directory objects dst_dir_web_path = os.path.dirname(dst_file_web_path) self._create_directories_in_chain(dst_dir_web_path) # if media_file is moved to trash save current timestamp as trash_time # otherwise unset trash_time move_to_trash = locations.web_path_in_trash(dst_file_web_path) if move_to_trash: now = localized_time(datetime.now()) trash_time = now else: trash_time = None # parent directory should exist new_directory = Directory.objects.get(path=dst_dir_web_path) # update directory modification times self._update_directory_mtime(new_directory) self._update_directory_mtime(media_file.directory) media_file.directory = new_directory media_file.trash_time = trash_time media_file.save() except (File.DoesNotExist, Directory.DoesNotExist): raise BadRequestException("Database object doesn't exist: (name={0})".format(src_file_web_path))
def remove_old_trash_files(): month_ago = localized_time(datetime.now() - timedelta(days=30)) # select images from trash that were moved month before or earlier old_images_in_trash = Image.objects.filter( Q(directory__path__startswith="Trash/") | Q(directory__path__exact="Trash") ).filter(trash_time__lte=month_ago) # remove file in collection # thumbnails and database object will be removed by Thumbnailer and Indexer respectively for image in old_images_in_trash: image_phys_path = locations.collection_phys_path(image.path) all_files_with_prefix = set(glob.glob(os.path.splitext(image_phys_path)[0] + ".*")) logging.info("Removing outdated files in trash: " + " ".join(all_files_with_prefix)) for file_name in all_files_with_prefix: os.unlink(file_name) # remove directory in trash if empty trash_dir_phys_path = locations.collection_phys_path(locations.TRASH_DIR_NAME) for (root, dirs, files) in os.walk(trash_dir_phys_path, topdown=False): for directory in dirs: try: dir_path = os.path.join(root, directory) os.rmdir(dir_path) logging.info("Removing empty directory in trash: " + dir_path) except IOError as e: # directory isn't empty - skipping pass
def _get_miniature_timestamp(cls, miniature_phys_path): timestamp_match = re.search(TIMESTAMP_PATTERN, miniature_phys_path) if timestamp_match: timestamp_strig = timestamp_match.group(0)[1:] miniature_mtime = datetime.strptime(timestamp_strig, TIMESTAMP_FORMAT) return localized_time(miniature_mtime) return None
def _fix_image_properties(cls): # assign any non assigned images to image group for image in Image.objects.filter(image_group__isnull=True): cls._assign_image_to_image_group(image) # for all images in Trash with empty trash_time, set it to current timestamp count = Image.objects \ .filter(Q(directory__path__startswith='Trash/') | Q(directory__path__exact='Trash')) \ .filter(trash_time__isnull=True) \ .update(trash_time=(localized_time(datetime.now()))) if count: logging.info("updated trash_time of {} trashed images.".format(count))
def get_mtime_datetime(path): mtime = datetime.datetime.fromtimestamp(os.path.getmtime(path)) mtime_with_second_resolution = mtime.replace(microsecond=0) return localized_time(mtime_with_second_resolution)