diff --git a/sapl/parlamentares/migrations/0021_clear_thumbnails_cache.py b/sapl/parlamentares/migrations/0021_clear_thumbnails_cache.py new file mode 100644 index 000000000..b3581816f --- /dev/null +++ b/sapl/parlamentares/migrations/0021_clear_thumbnails_cache.py @@ -0,0 +1,23 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + + +from django.db import migrations +from sapl.utils import clear_thumbnails_cache + + +def clear_thumbnails_cache_migrate(apps, schema_editor): + Parlamentar = apps.get_model("parlamentares", "Parlamentar") + parlamentares = Parlamentar.objects.all() + clear_thumbnails_cache(parlamentares, 'fotografia') + + +class Migration(migrations.Migration): + + dependencies = [ + ('parlamentares', '0020_fix_inicio_mandato'), + ] + + operations = [ + migrations.RunPython(clear_thumbnails_cache_migrate), + ] diff --git a/sapl/utils.py b/sapl/utils.py index 3a3cb3fad..488b2d8d6 100644 --- a/sapl/utils.py +++ b/sapl/utils.py @@ -22,6 +22,7 @@ from django_filters.filterset import STRICTNESS from easy_thumbnails import source_generators from floppyforms import ClearableFileInput from reversion.admin import VersionAdmin +from unipath.path import Path import django_filters import magic @@ -36,6 +37,23 @@ def pil_image(source, exif_orientation=False, **options): return source_generators.pil_image(source, exif_orientation, **options) +def clear_thumbnails_cache(queryset, field): + + for r in queryset: + assert hasattr(r, field), _( + 'Objeto da listagem não possui o campo informado') + + if not getattr(r, field): + continue + + path = Path(getattr(r, field).path) + cache_files = path.parent.walk() + + for cf in cache_files: + if cf != path: + cf.remove() + + def normalize(txt): return unicodedata_normalize( 'NFKD', txt).encode('ASCII', 'ignore').decode('ASCII')