From 2f62b1dd062f7b868c4968560093868fe11eda42 Mon Sep 17 00:00:00 2001 From: Ulysses Lara Date: Fri, 2 Aug 2019 10:44:03 -0300 Subject: [PATCH] Corrigindo problema de nomes iguais ao atualizar arquivo FIX #2891 (#2892) * Corrigindo problema de nomes iguais ao atualizar arquivo FIX #2891 * Subindo Migrations --- .../migrations/0011_auto_20190712_1053.py | 37 +++++++++++++ sapl/audiencia/models.py | 7 ++- .../migrations/0020_auto_20190712_1053.py | 37 +++++++++++++ sapl/comissoes/models.py | 7 ++- .../migrations/0052_auto_20190712_1053.py | 32 ++++++++++++ sapl/materia/models.py | 6 ++- .../migrations/0026_auto_20190712_1053.py | 27 ++++++++++ sapl/norma/models.py | 5 +- .../migrations/0022_auto_20190712_1053.py | 26 ++++++++++ sapl/protocoloadm/models.py | 5 +- .../migrations/0043_auto_20190712_1053.py | 52 +++++++++++++++++++ sapl/sessao/models.py | 8 ++- sapl/utils.py | 14 +++++ 13 files changed, 257 insertions(+), 6 deletions(-) create mode 100644 sapl/audiencia/migrations/0011_auto_20190712_1053.py create mode 100644 sapl/comissoes/migrations/0020_auto_20190712_1053.py create mode 100644 sapl/materia/migrations/0052_auto_20190712_1053.py create mode 100644 sapl/norma/migrations/0026_auto_20190712_1053.py create mode 100644 sapl/protocoloadm/migrations/0022_auto_20190712_1053.py create mode 100644 sapl/sessao/migrations/0043_auto_20190712_1053.py diff --git a/sapl/audiencia/migrations/0011_auto_20190712_1053.py b/sapl/audiencia/migrations/0011_auto_20190712_1053.py new file mode 100644 index 000000000..715c1d615 --- /dev/null +++ b/sapl/audiencia/migrations/0011_auto_20190712_1053.py @@ -0,0 +1,37 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.20 on 2019-07-12 13:53 +from __future__ import unicode_literals + +from django.db import migrations, models +import sapl.audiencia.models +import sapl.utils + + +class Migration(migrations.Migration): + + dependencies = [ + ('audiencia', '0010_auto_20190219_1511'), + ] + + operations = [ + migrations.AlterField( + model_name='anexoaudienciapublica', + name='arquivo', + field=models.FileField(storage=sapl.utils.OverwriteStorage(), upload_to=sapl.utils.texto_upload_path, verbose_name='Arquivo'), + ), + migrations.AlterField( + model_name='audienciapublica', + name='upload_anexo', + field=models.FileField(blank=True, null=True, storage=sapl.utils.OverwriteStorage(), upload_to=sapl.audiencia.models.anexo_upload_path, verbose_name='Anexo da Audiência Pública'), + ), + migrations.AlterField( + model_name='audienciapublica', + name='upload_ata', + field=models.FileField(blank=True, null=True, storage=sapl.utils.OverwriteStorage(), upload_to=sapl.audiencia.models.ata_upload_path, validators=[sapl.utils.restringe_tipos_de_arquivo_txt], verbose_name='Ata da Audiência Pública'), + ), + migrations.AlterField( + model_name='audienciapublica', + name='upload_pauta', + field=models.FileField(blank=True, null=True, storage=sapl.utils.OverwriteStorage(), upload_to=sapl.audiencia.models.pauta_upload_path, validators=[sapl.utils.restringe_tipos_de_arquivo_txt], verbose_name='Pauta da Audiência Pública'), + ), + ] diff --git a/sapl/audiencia/models.py b/sapl/audiencia/models.py index 76c36c6c0..7dc962e3a 100755 --- a/sapl/audiencia/models.py +++ b/sapl/audiencia/models.py @@ -7,7 +7,8 @@ from sapl.materia.models import MateriaLegislativa from sapl.parlamentares.models import (CargoMesa, Parlamentar) from sapl.utils import (YES_NO_CHOICES, SaplGenericRelation, - restringe_tipos_de_arquivo_txt, texto_upload_path) + restringe_tipos_de_arquivo_txt, texto_upload_path, + OverwriteStorage) def get_audiencia_media_path(instance, subpath, filename): @@ -89,6 +90,7 @@ class AudienciaPublica(models.Model): blank=True, null=True, upload_to=pauta_upload_path, + storage=OverwriteStorage(), verbose_name=_('Pauta da Audiência Pública'), validators=[restringe_tipos_de_arquivo_txt]) upload_ata = models.FileField( @@ -96,11 +98,13 @@ class AudienciaPublica(models.Model): null=True, upload_to=ata_upload_path, verbose_name=_('Ata da Audiência Pública'), + storage=OverwriteStorage(), validators=[restringe_tipos_de_arquivo_txt]) upload_anexo = models.FileField( blank=True, null=True, upload_to=anexo_upload_path, + storage=OverwriteStorage(), verbose_name=_('Anexo da Audiência Pública')) class Meta: @@ -156,6 +160,7 @@ class AnexoAudienciaPublica(models.Model): on_delete=models.PROTECT) arquivo = models.FileField( upload_to=texto_upload_path, + storage=OverwriteStorage(), verbose_name=_('Arquivo')) data = models.DateField( auto_now=timezone.now) diff --git a/sapl/comissoes/migrations/0020_auto_20190712_1053.py b/sapl/comissoes/migrations/0020_auto_20190712_1053.py new file mode 100644 index 000000000..2644cfe34 --- /dev/null +++ b/sapl/comissoes/migrations/0020_auto_20190712_1053.py @@ -0,0 +1,37 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.20 on 2019-07-12 13:53 +from __future__ import unicode_literals + +from django.db import migrations, models +import sapl.comissoes.models +import sapl.utils + + +class Migration(migrations.Migration): + + dependencies = [ + ('comissoes', '0019_auto_20181214_1023'), + ] + + operations = [ + migrations.AlterField( + model_name='documentoacessorio', + name='arquivo', + field=models.FileField(blank=True, null=True, storage=sapl.utils.OverwriteStorage(), upload_to=sapl.comissoes.models.anexo_upload_path, validators=[sapl.utils.restringe_tipos_de_arquivo_txt], verbose_name='Texto Integral'), + ), + migrations.AlterField( + model_name='reuniao', + name='upload_anexo', + field=models.FileField(blank=True, null=True, storage=sapl.utils.OverwriteStorage(), upload_to=sapl.comissoes.models.anexo_upload_path, verbose_name='Anexo da Reunião'), + ), + migrations.AlterField( + model_name='reuniao', + name='upload_ata', + field=models.FileField(blank=True, null=True, storage=sapl.utils.OverwriteStorage(), upload_to=sapl.comissoes.models.ata_upload_path, validators=[sapl.utils.restringe_tipos_de_arquivo_txt], verbose_name='Ata da Reunião'), + ), + migrations.AlterField( + model_name='reuniao', + name='upload_pauta', + field=models.FileField(blank=True, null=True, storage=sapl.utils.OverwriteStorage(), upload_to=sapl.comissoes.models.pauta_upload_path, validators=[sapl.utils.restringe_tipos_de_arquivo_txt], verbose_name='Pauta da Reunião'), + ), + ] diff --git a/sapl/comissoes/models.py b/sapl/comissoes/models.py index 2792c80d6..467716213 100644 --- a/sapl/comissoes/models.py +++ b/sapl/comissoes/models.py @@ -6,7 +6,8 @@ from model_utils import Choices from sapl.base.models import Autor from sapl.parlamentares.models import Parlamentar from sapl.utils import (YES_NO_CHOICES, SaplGenericRelation, - restringe_tipos_de_arquivo_txt, texto_upload_path) + restringe_tipos_de_arquivo_txt, texto_upload_path, + OverwriteStorage) @reversion.register() @@ -238,15 +239,18 @@ class Reuniao(models.Model): blank=True, null=True, upload_to=pauta_upload_path, verbose_name=_('Pauta da Reunião'), + storage=OverwriteStorage(), validators=[restringe_tipos_de_arquivo_txt]) upload_ata = models.FileField( blank=True, null=True, upload_to=ata_upload_path, verbose_name=_('Ata da Reunião'), + storage=OverwriteStorage(), validators=[restringe_tipos_de_arquivo_txt]) upload_anexo = models.FileField( blank=True, null=True, upload_to=anexo_upload_path, + storage=OverwriteStorage(), verbose_name=_('Anexo da Reunião')) class Meta: @@ -313,6 +317,7 @@ class DocumentoAcessorio(models.Model): null=True, upload_to=anexo_upload_path, verbose_name=_('Texto Integral'), + storage=OverwriteStorage(), validators=[restringe_tipos_de_arquivo_txt]) data_ultima_atualizacao = models.DateTimeField( diff --git a/sapl/materia/migrations/0052_auto_20190712_1053.py b/sapl/materia/migrations/0052_auto_20190712_1053.py new file mode 100644 index 000000000..1fac8d7ae --- /dev/null +++ b/sapl/materia/migrations/0052_auto_20190712_1053.py @@ -0,0 +1,32 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.20 on 2019-07-12 13:53 +from __future__ import unicode_literals + +from django.db import migrations, models +import sapl.materia.models +import sapl.utils + + +class Migration(migrations.Migration): + + dependencies = [ + ('materia', '0051_auto_20190703_1414'), + ] + + operations = [ + migrations.AlterField( + model_name='documentoacessorio', + name='arquivo', + field=models.FileField(blank=True, max_length=255, null=True, storage=sapl.utils.OverwriteStorage(), upload_to=sapl.materia.models.anexo_upload_path, validators=[sapl.utils.restringe_tipos_de_arquivo_txt], verbose_name='Texto Integral'), + ), + migrations.AlterField( + model_name='materialegislativa', + name='texto_original', + field=models.FileField(blank=True, null=True, storage=sapl.utils.OverwriteStorage(), upload_to=sapl.materia.models.materia_upload_path, validators=[sapl.utils.restringe_tipos_de_arquivo_txt], verbose_name='Texto Original'), + ), + migrations.AlterField( + model_name='proposicao', + name='texto_original', + field=models.FileField(blank=True, null=True, storage=sapl.utils.OverwriteStorage(), upload_to=sapl.materia.models.materia_upload_path, validators=[sapl.utils.restringe_tipos_de_arquivo_txt], verbose_name='Texto Original'), + ), + ] diff --git a/sapl/materia/models.py b/sapl/materia/models.py index 7a7f305ff..219b38971 100644 --- a/sapl/materia/models.py +++ b/sapl/materia/models.py @@ -18,7 +18,8 @@ from sapl.parlamentares.models import Parlamentar #from sapl.protocoloadm.models import Protocolo from sapl.utils import (RANGE_ANOS, YES_NO_CHOICES, SaplGenericForeignKey, SaplGenericRelation, restringe_tipos_de_arquivo_txt, - texto_upload_path, get_settings_auth_user_model) + texto_upload_path, get_settings_auth_user_model, + OverwriteStorage) EM_TRAMITACAO = [(1, 'Sim'), @@ -260,6 +261,7 @@ class MateriaLegislativa(models.Model): null=True, upload_to=materia_upload_path, verbose_name=_('Texto Original'), + storage=OverwriteStorage(), validators=[restringe_tipos_de_arquivo_txt]) texto_articulado = GenericRelation( @@ -538,6 +540,7 @@ class DocumentoAcessorio(models.Model): max_length=255, upload_to=anexo_upload_path, verbose_name=_('Texto Integral'), + storage=OverwriteStorage(), validators=[restringe_tipos_de_arquivo_txt]) proposicao = GenericRelation( @@ -801,6 +804,7 @@ class Proposicao(models.Model): blank=True, null=True, verbose_name=_('Texto Original'), + storage=OverwriteStorage(), validators=[restringe_tipos_de_arquivo_txt]) texto_articulado = GenericRelation( diff --git a/sapl/norma/migrations/0026_auto_20190712_1053.py b/sapl/norma/migrations/0026_auto_20190712_1053.py new file mode 100644 index 000000000..592e8a177 --- /dev/null +++ b/sapl/norma/migrations/0026_auto_20190712_1053.py @@ -0,0 +1,27 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.20 on 2019-07-12 13:53 +from __future__ import unicode_literals + +from django.db import migrations, models +import sapl.norma.models +import sapl.utils + + +class Migration(migrations.Migration): + + dependencies = [ + ('norma', '0025_auto_20190704_1403'), + ] + + operations = [ + migrations.AlterField( + model_name='anexonormajuridica', + name='anexo_arquivo', + field=models.FileField(blank=True, null=True, storage=sapl.utils.OverwriteStorage(), upload_to=sapl.norma.models.norma_upload_path, validators=[sapl.utils.restringe_tipos_de_arquivo_txt], verbose_name='Arquivo Anexo'), + ), + migrations.AlterField( + model_name='normajuridica', + name='texto_integral', + field=models.FileField(blank=True, null=True, storage=sapl.utils.OverwriteStorage(), upload_to=sapl.norma.models.norma_upload_path, validators=[sapl.utils.restringe_tipos_de_arquivo_txt], verbose_name='Texto Integral'), + ), + ] diff --git a/sapl/norma/models.py b/sapl/norma/models.py index 308ac48b0..7dc539ef6 100644 --- a/sapl/norma/models.py +++ b/sapl/norma/models.py @@ -12,7 +12,8 @@ from sapl.materia.models import MateriaLegislativa from sapl.utils import (RANGE_ANOS, YES_NO_CHOICES, restringe_tipos_de_arquivo_txt, texto_upload_path, - get_settings_auth_user_model) + get_settings_auth_user_model, + OverwriteStorage) @reversion.register() @@ -82,6 +83,7 @@ class NormaJuridica(models.Model): null=True, upload_to=norma_upload_path, verbose_name=_('Texto Integral'), + storage=OverwriteStorage(), validators=[restringe_tipos_de_arquivo_txt]) tipo = models.ForeignKey( TipoNormaJuridica, @@ -363,6 +365,7 @@ class AnexoNormaJuridica(models.Model): null=True, upload_to=norma_upload_path, verbose_name=_('Arquivo Anexo'), + storage=OverwriteStorage(), validators=[restringe_tipos_de_arquivo_txt]) ano = models.PositiveSmallIntegerField(verbose_name=_('Ano'), choices=RANGE_ANOS) diff --git a/sapl/protocoloadm/migrations/0022_auto_20190712_1053.py b/sapl/protocoloadm/migrations/0022_auto_20190712_1053.py new file mode 100644 index 000000000..9235dc75a --- /dev/null +++ b/sapl/protocoloadm/migrations/0022_auto_20190712_1053.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.20 on 2019-07-12 13:53 +from __future__ import unicode_literals + +from django.db import migrations, models +import sapl.utils + + +class Migration(migrations.Migration): + + dependencies = [ + ('protocoloadm', '0021_merge_20190429_1531'), + ] + + operations = [ + migrations.AlterField( + model_name='documentoacessorioadministrativo', + name='arquivo', + field=models.FileField(blank=True, null=True, storage=sapl.utils.OverwriteStorage(), upload_to=sapl.utils.texto_upload_path, verbose_name='Arquivo'), + ), + migrations.AlterField( + model_name='documentoadministrativo', + name='texto_integral', + field=models.FileField(blank=True, null=True, storage=sapl.utils.OverwriteStorage(), upload_to=sapl.utils.texto_upload_path, verbose_name='Texto Integral'), + ), + ] diff --git a/sapl/protocoloadm/models.py b/sapl/protocoloadm/models.py index f0e40f9c7..5f67054a4 100644 --- a/sapl/protocoloadm/models.py +++ b/sapl/protocoloadm/models.py @@ -7,7 +7,8 @@ import reversion from sapl.base.models import Autor from sapl.materia.models import TipoMateriaLegislativa, UnidadeTramitacao from sapl.utils import (RANGE_ANOS, YES_NO_CHOICES, texto_upload_path, - get_settings_auth_user_model) + get_settings_auth_user_model, + OverwriteStorage) @reversion.register() @@ -164,6 +165,7 @@ class DocumentoAdministrativo(models.Model): texto_integral = models.FileField( blank=True, null=True, + storage=OverwriteStorage(), upload_to=texto_upload_path, verbose_name=_('Texto Integral')) restrito = models.BooleanField(default=False, @@ -229,6 +231,7 @@ class DocumentoAcessorioAdministrativo(models.Model): blank=True, null=True, upload_to=texto_upload_path, + storage=OverwriteStorage(), verbose_name=_('Arquivo')) data = models.DateField(blank=True, null=True, verbose_name=_('Data')) autor = models.CharField( diff --git a/sapl/sessao/migrations/0043_auto_20190712_1053.py b/sapl/sessao/migrations/0043_auto_20190712_1053.py new file mode 100644 index 000000000..55fc526a5 --- /dev/null +++ b/sapl/sessao/migrations/0043_auto_20190712_1053.py @@ -0,0 +1,52 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.20 on 2019-07-12 13:53 +from __future__ import unicode_literals + +from django.db import migrations, models +import sapl.sessao.models +import sapl.utils + + +class Migration(migrations.Migration): + + dependencies = [ + ('sessao', '0042_merge_20190612_0925'), + ] + + operations = [ + migrations.AlterField( + model_name='justificativaausencia', + name='upload_anexo', + field=models.FileField(blank=True, null=True, storage=sapl.utils.OverwriteStorage(), upload_to=sapl.sessao.models.anexo_upload_path, verbose_name='Anexo de Justificativa'), + ), + migrations.AlterField( + model_name='orador', + name='upload_anexo', + field=models.FileField(blank=True, null=True, storage=sapl.utils.OverwriteStorage(), upload_to=sapl.sessao.models.anexo_upload_path, verbose_name='Anexo do Orador'), + ), + migrations.AlterField( + model_name='oradorexpediente', + name='upload_anexo', + field=models.FileField(blank=True, null=True, storage=sapl.utils.OverwriteStorage(), upload_to=sapl.sessao.models.anexo_upload_path, verbose_name='Anexo do Orador'), + ), + migrations.AlterField( + model_name='oradorordemdia', + name='upload_anexo', + field=models.FileField(blank=True, null=True, storage=sapl.utils.OverwriteStorage(), upload_to=sapl.sessao.models.anexo_upload_path, verbose_name='Anexo do Orador'), + ), + migrations.AlterField( + model_name='sessaoplenaria', + name='upload_anexo', + field=models.FileField(blank=True, null=True, storage=sapl.utils.OverwriteStorage(), upload_to=sapl.sessao.models.anexo_upload_path, verbose_name='Anexo da Sessão'), + ), + migrations.AlterField( + model_name='sessaoplenaria', + name='upload_ata', + field=models.FileField(blank=True, null=True, storage=sapl.utils.OverwriteStorage(), upload_to=sapl.sessao.models.ata_upload_path, validators=[sapl.utils.restringe_tipos_de_arquivo_txt], verbose_name='Ata da Sessão'), + ), + migrations.AlterField( + model_name='sessaoplenaria', + name='upload_pauta', + field=models.FileField(blank=True, null=True, storage=sapl.utils.OverwriteStorage(), upload_to=sapl.sessao.models.pauta_upload_path, validators=[sapl.utils.restringe_tipos_de_arquivo_txt], verbose_name='Pauta da Sessão'), + ), + ] diff --git a/sapl/sessao/models.py b/sapl/sessao/models.py index 04c08ec16..e6016895f 100644 --- a/sapl/sessao/models.py +++ b/sapl/sessao/models.py @@ -14,7 +14,8 @@ from sapl.parlamentares.models import (CargoMesa, Legislatura, Parlamentar, Partido, SessaoLegislativa) from sapl.utils import (YES_NO_CHOICES, SaplGenericRelation, get_settings_auth_user_model, - restringe_tipos_de_arquivo_txt, texto_upload_path) + restringe_tipos_de_arquivo_txt, texto_upload_path, + OverwriteStorage) @reversion.register() @@ -130,16 +131,19 @@ class SessaoPlenaria(models.Model): null=True, upload_to=pauta_upload_path, verbose_name=_('Pauta da Sessão'), + storage=OverwriteStorage(), validators=[restringe_tipos_de_arquivo_txt]) upload_ata = models.FileField( blank=True, null=True, upload_to=ata_upload_path, + storage=OverwriteStorage(), verbose_name=_('Ata da Sessão'), validators=[restringe_tipos_de_arquivo_txt]) upload_anexo = models.FileField( blank=True, null=True, + storage=OverwriteStorage(), upload_to=anexo_upload_path, verbose_name=_('Anexo da Sessão')) iniciada = models.NullBooleanField(blank=True, @@ -366,6 +370,7 @@ class AbstractOrador(models.Model): # Oradores upload_anexo = models.FileField( blank=True, null=True, + storage=OverwriteStorage(), upload_to=anexo_upload_path, verbose_name=_('Anexo do Orador')) @@ -708,6 +713,7 @@ class JustificativaAusencia(models.Model): upload_anexo = models.FileField( blank=True, null=True, + storage=OverwriteStorage(), upload_to=anexo_upload_path, verbose_name=_('Anexo de Justificativa')) diff --git a/sapl/utils.py b/sapl/utils.py index 8c10efc98..99a055937 100644 --- a/sapl/utils.py +++ b/sapl/utils.py @@ -23,6 +23,7 @@ from django.forms.widgets import SplitDateTimeWidget from django.utils import six, timezone from django.utils.safestring import mark_safe from django.utils.translation import ugettext_lazy as _ +from django.core.files.storage import FileSystemStorage import django_filters from easy_thumbnails import source_generators from floppyforms import ClearableFileInput @@ -984,3 +985,16 @@ def lista_anexados(principal, isMateriaLegislativa=True): if principal in anexados_total: anexados_total.remove(principal) return anexados_total + + +class OverwriteStorage(FileSystemStorage): + ''' + Solução derivada do gist: https://gist.github.com/fabiomontefuscolo/1584462 + + Muda o comportamento padrão do Django e o faz sobrescrever arquivos de + mesmo nome que foram carregados pelo usuário ao invés de renomeá-los. + ''' + def get_available_name(self, name, max_length=None): + if self.exists(name): + os.remove(os.path.join(settings.MEDIA_ROOT, name)) + return name \ No newline at end of file