From 379707e7b6688848e4efe09ebc73c70bff5f265f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ses=C3=B3stris=20Vieira?= Date: Thu, 21 Sep 2023 11:26:35 -0300 Subject: [PATCH] Corrige o path de upload dos banners dos eventos --- .../migrations/0045_alter_evento_banner.py | 56 +++++++++++++++++++ sigi/apps/eventos/models.py | 6 +- 2 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 sigi/apps/eventos/migrations/0045_alter_evento_banner.py diff --git a/sigi/apps/eventos/migrations/0045_alter_evento_banner.py b/sigi/apps/eventos/migrations/0045_alter_evento_banner.py new file mode 100644 index 0000000..8179533 --- /dev/null +++ b/sigi/apps/eventos/migrations/0045_alter_evento_banner.py @@ -0,0 +1,56 @@ +# Generated by Django 4.2.4 on 2023-09-21 14:00 +from pathlib import Path +from django.db import migrations, models +from django.conf import settings + +upload_to = settings.MEDIA_ROOT / "apps/eventos/evento/banner/" + + +def forwards(apps, schema_editor): + Evento = apps.get_model("eventos", "Evento") + for e in Evento.objects.exclude(banner="").exclude(banner=None): + old_file = Path(e.banner.path) + if old_file.exists(): + new_file = upload_to / e.banner.name + old_file.rename(new_file) + e.banner = str(new_file.resolve()) + else: + e.banner = "" + e.save() + + +def backwards(apps, schema_editor): + Evento = apps.get_model("eventos", "Evento") + for e in Evento.objects.exclude(banner="").exclude(banner=None): + new_file = Path(e.banner.path) + if new_file.exists(): + old_file = settings.MEDIA_ROOT / e.banner.name + new_file.rename(old_file) + e.banner = str(old_file.resolve()) + else: + e.banner = "" + e.save() + + +class Migration(migrations.Migration): + dependencies = [ + ( + "eventos", + "0044_evento_chave_inscricao_evento_contato_inscricao_and_more", + ), + ] + + operations = [ + migrations.AlterField( + model_name="evento", + name="banner", + field=models.ImageField( + blank=True, + default="", + upload_to="apps/eventos/evento/banner/", + verbose_name="banner do evento", + ), + preserve_default=False, + ), + migrations.RunPython(forwards, backwards), + ] diff --git a/sigi/apps/eventos/models.py b/sigi/apps/eventos/models.py index 84d8a9f..bc155e7 100644 --- a/sigi/apps/eventos/models.py +++ b/sigi/apps/eventos/models.py @@ -458,7 +458,11 @@ class Evento(models.Model): blank=True, help_text=_("telefone da pessoa de contato na casa anfitriĆ£"), ) - banner = models.ImageField(_("banner do evento"), blank=True, null=True) + banner = models.ImageField( + _("banner do evento"), + blank=True, + upload_to="apps/eventos/evento/banner/", + ) data_cancelamento = models.DateField( _("Data de cancelamento"), blank=True, null=True )