diff options
| author | Étienne Loks <etienne.loks@iggdrasil.net> | 2023-05-15 13:06:11 +0200 | 
|---|---|---|
| committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2023-05-15 13:08:15 +0200 | 
| commit | 0aed4a0824c32fc528517ae4573e5bb7f60029dc (patch) | |
| tree | 249e8e98f058c77884d27b298c1c6d96f0ef0771 /ishtar_common/management/commands | |
| parent | 788a699ec10c372d82219aac071622f779d8b63a (diff) | |
| download | Ishtar-0aed4a0824c32fc528517ae4573e5bb7f60029dc.tar.bz2 Ishtar-0aed4a0824c32fc528517ae4573e5bb7f60029dc.zip | |
✨ media_find_missing_files command: added set-alt-media-dir option to link missing media from an alternate directory and set-empty-image option to link to dummy images for missing images
Diffstat (limited to 'ishtar_common/management/commands')
| -rw-r--r-- | ishtar_common/management/commands/media_find_missing_files.py | 61 | 
1 files changed, 60 insertions, 1 deletions
| diff --git a/ishtar_common/management/commands/media_find_missing_files.py b/ishtar_common/management/commands/media_find_missing_files.py index 13159bfa7..f2cf8e7f6 100644 --- a/ishtar_common/management/commands/media_find_missing_files.py +++ b/ishtar_common/management/commands/media_find_missing_files.py @@ -53,6 +53,13 @@ class Command(BaseCommand):              '--hard', dest='hard', action='store_true',              default=False,              help='Search on the whole media dir.') +        parser.add_argument( +            '--set-empty-image', dest='set-empty-image', action='store_true', +            default=False, +            help='Set a link to empty-image for missing media.') +        parser.add_argument( +            '--set-alt-media-dir', dest='set-alt-media-dir', default=False, +            help='Try to set link from an alt directory for missing media.')      def handle(self, *args, **options):          exclude = options['exclude'].split(',') if options['exclude'] else [] @@ -60,6 +67,16 @@ class Command(BaseCommand):          try_fix = options['try-fix']          find_fix = options['find-fix']          hard = options['hard'] +        set_alt_media_dir = options["set-alt-media-dir"] +        set_empty_image = options["set-empty-image"] +        if set_alt_media_dir and not os.path.isdir(set_alt_media_dir): +            self.stdout.write(f'set-alt-media-dir: "{set_alt_media_dir}" is not a valid path.\n') +            return + +        if set_empty_image and set_alt_media_dir: +            self.stdout.write("set-empty-image and set-alt-media-dir options are not " +                              "compatible.\n") +            return          if try_fix and find_fix:              self.stdout.write("try-fix and find-fix options are not "                                "compatible.\n") @@ -95,8 +112,50 @@ class Command(BaseCommand):                      sys.stdout.write(                          "{} <- {}\n".format(item.encode('utf-8'),                                              source_file.encode('utf-8'))) +        if set_alt_media_dir: +            total = 0 +            for f in missing: +                f = os.path.abspath(f) +                if not f.startswith(settings.MEDIA_ROOT): +                    continue +                dest = os.path.join(set_alt_media_dir, f[len(settings.MEDIA_ROOT):]) +                if not os.path.isfile(dest): +                    continue +                directory = f.split(os.path.sep)[:-1] +                directory = os.sep + os.path.join(*directory) +                if not os.path.isdir(directory): +                    os.makedirs(directory) +                try: +                    os.symlink(dest, f) +                except FileExistsError: +                    continue +                total += 1 +            self.stdout.write(f"* {total} link to {set_alt_media_dir} created.\n") +            return -        if missing: +        if set_empty_image: +            empty_image = os.path.abspath( +                settings.LIB_BASE_PATH + "ishtar_common/static/media/images/empty-image" +            ) +            total = 0 +            for f in missing: +                f = os.path.abspath(f) +                if not f.startswith(settings.MEDIA_ROOT): +                    continue +                if f.lower().endswith(".jpg") or f.lower().endswith(".jpeg"): +                    cempty_image = empty_image + ".jpg" +                elif f.lower().endswith(".png"): +                    cempty_image = empty_image + ".png" +                else: +                    continue +                directory = f.split(os.path.sep)[:-1] +                directory = os.sep + os.path.join(*directory) +                if not os.path.isdir(directory): +                    os.makedirs(directory) +                os.symlink(cempty_image, f) +                total += 1 +            self.stdout.write(f"* {total} missing files set to empty image\n") +        elif missing:              if find_fix or try_fix:                  self.stdout.write("* missing file with no similar file "                                    "found:\n") | 
