From 956d6a5a3a80e5083ef3879ec10513c2c3a41079 Mon Sep 17 00:00:00 2001 From: LeandroRoberto Date: Wed, 28 Feb 2018 13:04:13 -0300 Subject: [PATCH] =?UTF-8?q?add=20limp=20de=20varia=C3=A7=C3=B5es=20de=20ar?= =?UTF-8?q?q=20em=20camp=20baseados=20em=20FileField?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../migrations/0021_clear_thumbnails_cache.py | 23 +++++++++++++++++++ sapl/utils.py | 18 +++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 sapl/parlamentares/migrations/0021_clear_thumbnails_cache.py 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')