diff --git a/sapl/sessao/migrations/0054_popula_expedientepautasessao.py b/sapl/sessao/migrations/0054_popula_expedientepautasessao.py new file mode 100644 index 000000000..3adfde3dc --- /dev/null +++ b/sapl/sessao/migrations/0054_popula_expedientepautasessao.py @@ -0,0 +1,53 @@ +from __future__ import unicode_literals + +from django.db import migrations + + +class Migration(migrations.Migration): + dependencies = [ + ('sessao', '0053_auto_20200609_1501'), + ] + + operations = [ + migrations.RunSQL( + """ + create or replace view sessao_expedientepauta as + with autoria_materia as ( + select ma.materia_id, array_agg(ba.nome) as autores + from materia_autoria ma + join base_autor ba on (ma.autor_id = ba.id) + group by ma.materia_id + order by ma.materia_id, autores + ), + tramitacao_numeracao_materia as ( + select distinct on (m.id) + m.id as materia_id, + mt.id as tramitacao_id, + mn.id as numeracao_id + from materia_materialegislativa as m + left join materia_tramitacao mt on (m.id = mt.materia_id) + left join materia_numeracao mn on (m.id = mn.materia_id) + order by materia_id, mt.data_tramitacao DESC, mt.id DESC, + mn.id DESC + ), + sessao_expedientepauta as ( + select ex.id as id, + ex.id as expediente_id, + ex.sessao_plenaria_id as sessao_plenaria_id, + tnm.materia_id as materia_id, + tnm.tramitacao_id as tramitacao_id, + tnm.numeracao_id as numeracao_id, + am.autores as autores + from sessao_expedientemateria ex + join tramitacao_numeracao_materia tnm on ( + ex.materia_id = tnm.materia_id + ) + left join autoria_materia am on ( + ex.materia_id = am.materia_id + ) + ) + select * + from sessao_expedientepauta + """ + ) + ] diff --git a/sapl/sessao/migrations/0055_expedientepautasessao.py b/sapl/sessao/migrations/0055_expedientepautasessao.py new file mode 100644 index 000000000..67362bf90 --- /dev/null +++ b/sapl/sessao/migrations/0055_expedientepautasessao.py @@ -0,0 +1,24 @@ +# Generated by Django 2.2.13 on 2020-09-24 23:29 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('sessao', '0054_popula_expedientepautasessao'), + ] + + operations = [ + migrations.CreateModel( + name='ExpedientePautaSessao', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('autores', models.CharField(blank=True, max_length=545)), + ], + options={ + 'db_table': 'sessao_expedientepauta', + 'managed': False, + }, + ), + ] diff --git a/sapl/sessao/models.py b/sapl/sessao/models.py index 8585be2ad..2f791db6a 100644 --- a/sapl/sessao/models.py +++ b/sapl/sessao/models.py @@ -9,7 +9,7 @@ from model_utils import Choices import reversion from sapl.base.models import Autor -from sapl.materia.models import MateriaLegislativa +from sapl.materia.models import MateriaLegislativa, Tramitacao, Numeracao from sapl.parlamentares.models import (CargoMesa, Legislatura, Parlamentar, Partido, SessaoLegislativa) from sapl.utils import (YES_NO_CHOICES, SaplGenericRelation, @@ -944,4 +944,32 @@ class RegistroLeitura(models.Model): raise ValidationError( 'RegistroLeitura deve ter exatamente um dos campos ' 'ordem ou expediente preenchido. Ambos estão preenchidos: ' - '{}, {}'. format(self.ordem, self.expediente)) \ No newline at end of file + '{}, {}'. format(self.ordem, self.expediente)) + + +class ExpedientePautaSessao(models.Model): + expediente = models.ForeignKey( + ExpedienteMateria, on_delete=models.DO_NOTHING + ) + sessao_plenaria = models.ForeignKey( + SessaoPlenaria, on_delete=models.DO_NOTHING + ) + materia = models.ForeignKey( + MateriaLegislativa, on_delete=models.DO_NOTHING + ) + tramitacao = models.ForeignKey( + Tramitacao, blank=True, null=True, on_delete=models.DO_NOTHING + ) + numeracao = models.ForeignKey( + Numeracao, blank=True, null=True, on_delete=models.DO_NOTHING + ) + autores = models.CharField( + max_length=545, blank=True + ) + + class Meta: + managed = False + db_table = "sessao_expedientepauta" + + def __str__(self): + return '{}'.format(self.expediente)