diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2019-01-27 13:21:12 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2019-01-27 13:21:12 +0100 |
commit | 45180c1f4f2e75b38b57d581f454b170ca68cfe1 (patch) | |
tree | 62b31735a99e8e60655e3bbe1bc0956294a4652c /ishtar_common/management/commands/media_simplify_filenames.py | |
parent | ab651277d8f70c9930481b1b578a762c7eedd1b3 (diff) | |
download | Ishtar-45180c1f4f2e75b38b57d581f454b170ca68cfe1.tar.bz2 Ishtar-45180c1f4f2e75b38b57d581f454b170ca68cfe1.zip |
Tools to manage media files.
* clean unused
* find missing
* rename and simplify
Diffstat (limited to 'ishtar_common/management/commands/media_simplify_filenames.py')
-rw-r--r-- | ishtar_common/management/commands/media_simplify_filenames.py | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/ishtar_common/management/commands/media_simplify_filenames.py b/ishtar_common/management/commands/media_simplify_filenames.py new file mode 100644 index 000000000..2295431cd --- /dev/null +++ b/ishtar_common/management/commands/media_simplify_filenames.py @@ -0,0 +1,95 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# Copyright (C) 2019 Étienne Loks <etienne.loks_AT_peacefrogsDOTnet> + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. + +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +# See the file COPYING for details. + +import os.path +import sys + + +from django.conf import settings +from django.core.management.base import BaseCommand + +from ishtar_common.utils import find_all_symlink, \ + rename_and_simplify_media_name, get_used_media + + +class Command(BaseCommand): + args = '' + help = 'Simplify name of files in media' + + def add_arguments(self, parser): + parser.add_argument( + '--exclude', nargs='?', dest='exclude', default=None, + help="Field to exclude separated with \",\". Example: " + "ishtar_common.Import.imported_file," + "ishtar_common.Import.imported_images" + ) + parser.add_argument( + '--limit', nargs='?', dest='limit', default=None, + help="Field to limit to separated with \",\"." + ) + + def handle(self, *args, **options): + exclude = options['exclude'].split(',') if options['exclude'] else [] + limit = options['limit'].split(',') if options['limit'] else [] + verbose = options['verbosity'] + + current_links = dict(list(find_all_symlink(settings.MEDIA_ROOT))) + + new_names = {} + if verbose == 2: + sys.stdout.write("* list current media...\n") + sys.stdout.flush() + + media = get_used_media(exclude=exclude, limit=limit, + return_object_and_field=True) + ln = len(media) + for idx, result in enumerate(sorted(media, key=lambda x: x[2])): + if verbose == 2: + nb_point = 10 + sys.stdout.write("* processing {}/{} {}{}\r".format( + idx + 1, ln, (idx % nb_point) * ".", " " * nb_point) + ) + sys.stdout.flush() + obj, field_name, media = result + if not os.path.isfile(media): + # missing file... + continue + if media not in new_names: + new_name, modified = rename_and_simplify_media_name(media) + if not modified: + continue + new_names[media] = new_name + setattr(obj, field_name, new_names[media]) + obj._no_move = True + obj._cached_label_checked = True + obj.skip_history_when_saving = True + obj.save() + if verbose > 2: + sys.stdout.write("{} renamed {}\n".format( + media, new_names[media].split(os.sep)[-1])) + if media in current_links.keys(): + os.remove(current_links[media]) + os.symlink(new_names[media], current_links[media]) + if verbose > 2: + sys.stdout.write( + "symlink {} changed to point to {}\n".format( + current_links[media], new_names[media])) + current_links.pop(media) + if verbose == 2: + sys.stdout.write("\n") |