Browse Source

Bring back all FKs from postgres DDL

pull/6/head
Marcio Mazza 10 years ago
parent
commit
efa80995b4
  1. 6
      comissoes/models.py
  2. 94
      legacy/scripts/adjust_fks.py
  3. 76
      materia/models.py
  4. 10
      norma/models.py
  5. 18
      parlamentares/models.py
  6. 35
      sessao/models.py

6
comissoes/models.py

@ -11,7 +11,7 @@ class CargoComissao(models.Model):
class Comissao(models.Model):
cod_comissao = models.AutoField(primary_key=True)
tip_comissao = models.IntegerField()
tip_comissao = models.ForeignKey(TipoComissao)
nom_comissao = models.CharField(max_length=60)
sgl_comissao = models.CharField(max_length=10)
dat_criacao = models.DateField()
@ -36,8 +36,8 @@ class Comissao(models.Model):
class ComposicaoComissao(models.Model):
cod_comp_comissao = models.AutoField(primary_key=True)
parlamentar = models.ForeignKey(Parlamentar)
cod_comissao = models.IntegerField()
cod_periodo_comp = models.IntegerField()
comissao = models.ForeignKey(Comissao)
periodo_comp = models.ForeignKey(PeriodoCompComissao)
cargo = models.ForeignKey(CargoComissao)
ind_titular = models.IntegerField()
dat_designacao = models.DateField()

94
legacy/scripts/adjust_fks.py

@ -0,0 +1,94 @@
from django.apps import apps
from django.db import models
from inspect import getsourcelines
# adjust FKs base on legacy postgres DDL
pglegapp = apps.get_app_config('pglegacy')
saplapps = {name: apps.get_app_config(name) for name in [
'parlamentares',
'comissoes',
'sessao',
'materia',
'norma',
'lexml',
'protocoloadm']}
modelname_to_app = {model.__name__: app
for appname, app in saplapps.iteritems()
for model in app.get_models()}
pgmodels = {model.__name__: model for model in pglegapp.get_models()}
def replace_fks(model, fk_models):
if model.__name__ not in pgmodels:
for line in getsourcelines(model)[0]:
yield line
return
pgfields = {f.name: f for f in pgmodels[model.__name__]._meta.fields}
for line in getsourcelines(model)[0]:
if line.startswith('class'):
yield line
elif ' = models.' in line:
fieldname = line.split()[0]
if fieldname not in pgfields:
if 'cod_' + fieldname in pgfields:
fieldname = 'cod_' + fieldname
else:
print '#### Field not in postgres models definition: %s : %s' % (model, fieldname)
yield line
continue
pgfield = pgfields[fieldname]
if isinstance(pgfield, models.ForeignKey):
# contribute to dependency list
fk_models.add(pgfield.related_model)
# remove cod_
if fieldname.startswith('cod_'):
fieldname = fieldname[4:]
else:
print '#### Field does not start with cod_: [%s] !!!' % fieldname
args = [pgfield.related_model.__name__]
for karg in ['blank=True', 'null=True']:
if karg in line:
args += [karg]
yield ' %s = models.ForeignKey(%s)\n' % (fieldname, ', '.join(args))
else:
yield line
else:
print '#### Unusual line: [%s] !!!' % line.rstrip('\n')
yield line
def preplace_fks(app):
fk_models = set()
lines = []
for model in app.get_models():
for line in replace_fks(model, fk_models):
lines.append(line)
lines += ['\n', '\n']
imports = []
for model in fk_models:
if model.__name__ not in modelname_to_app:
print '#### No app found for %s !!!!!!!' % model.__name__
continue
related_app = modelname_to_app[model.__name__]
if app != related_app:
imports.append('from %s.models import %s\n' % (
related_app.name, model.__name__))
imports = sorted(imports)
code = '''
from django.db import models
%s
''' % ''.join(imports + ['\n', '\n'] + lines)
code = code.strip()
print '######################################################\n\n'
print code

76
materia/models.py

@ -1,18 +1,21 @@
from django.db import models
from comissoes.models import Comissao
from norma.models import NormaJuridica
from parlamentares.models import Parlamentar
from parlamentares.models import Partido
class AcompMateria(models.Model):
cod_cadastro = models.AutoField(primary_key=True)
cod_materia = models.IntegerField()
materia = models.ForeignKey(MateriaLegislativa)
end_email = models.CharField(max_length=100)
txt_hash = models.CharField(max_length=8)
class Anexada(models.Model):
cod_materia_principal = models.IntegerField()
cod_materia_anexada = models.IntegerField()
materia_principal = models.ForeignKey(MateriaLegislativa)
materia_anexada = models.ForeignKey(MateriaLegislativa)
dat_anexacao = models.DateField()
dat_desanexacao = models.DateField(blank=True, null=True)
@ -25,31 +28,31 @@ class AssuntoMateria(models.Model):
class Autor(models.Model):
cod_autor = models.AutoField(primary_key=True)
cod_partido = models.IntegerField(blank=True, null=True)
cod_comissao = models.IntegerField(blank=True, null=True)
partido = models.ForeignKey(Partido, blank=True, null=True)
comissao = models.ForeignKey(Comissao, blank=True, null=True)
parlamentar = models.ForeignKey(Parlamentar, blank=True, null=True)
tip_autor = models.IntegerField()
tip_autor = models.ForeignKey(TipoAutor)
nom_autor = models.CharField(max_length=50, blank=True, null=True)
des_cargo = models.CharField(max_length=50, blank=True, null=True)
col_username = models.CharField(max_length=50, blank=True, null=True)
class Autoria(models.Model):
cod_autor = models.IntegerField()
cod_materia = models.IntegerField()
autor = models.ForeignKey(Autor)
materia = models.ForeignKey(MateriaLegislativa)
ind_primeiro_autor = models.IntegerField()
class DespachoInicial(models.Model):
cod_materia = models.IntegerField()
materia = models.ForeignKey(MateriaLegislativa)
num_ordem = models.IntegerField()
cod_comissao = models.IntegerField()
comissao = models.ForeignKey(Comissao)
class DocumentoAcessorio(models.Model):
cod_documento = models.AutoField(primary_key=True)
cod_materia = models.IntegerField()
tip_documento = models.IntegerField()
materia = models.ForeignKey(MateriaLegislativa)
tip_documento = models.ForeignKey(TipoDocumento)
nom_documento = models.CharField(max_length=30)
dat_documento = models.DateField(blank=True, null=True)
nom_autor_documento = models.CharField(max_length=50, blank=True, null=True)
@ -58,8 +61,8 @@ class DocumentoAcessorio(models.Model):
class LegislacaoCitada(models.Model):
cod_materia = models.IntegerField()
cod_norma = models.IntegerField()
materia = models.ForeignKey(MateriaLegislativa)
norma = models.ForeignKey(NormaJuridica)
des_disposicoes = models.CharField(max_length=15, blank=True, null=True)
des_parte = models.CharField(max_length=8, blank=True, null=True)
des_livro = models.CharField(max_length=7, blank=True, null=True)
@ -75,25 +78,25 @@ class LegislacaoCitada(models.Model):
class MateriaAssunto(models.Model):
cod_assunto = models.IntegerField()
cod_materia = models.IntegerField()
assunto = models.ForeignKey(AssuntoMateria)
materia = models.ForeignKey(MateriaLegislativa)
class MateriaLegislativa(models.Model):
cod_materia = models.AutoField(primary_key=True)
tip_id_basica = models.IntegerField()
tip_id_basica = models.ForeignKey(TipoMateriaLegislativa)
num_protocolo = models.IntegerField(blank=True, null=True)
num_ident_basica = models.IntegerField()
ano_ident_basica = models.SmallIntegerField()
dat_apresentacao = models.DateField(blank=True, null=True)
tip_apresentacao = models.CharField(max_length=1, blank=True, null=True)
cod_regime_tramitacao = models.IntegerField()
regime_tramitacao = models.ForeignKey(RegimeTramitacao)
dat_publicacao = models.DateField(blank=True, null=True)
tip_origem_externa = models.IntegerField(blank=True, null=True)
tip_origem_externa = models.ForeignKey(TipoMateriaLegislativa, blank=True, null=True)
num_origem_externa = models.CharField(max_length=5, blank=True, null=True)
ano_origem_externa = models.SmallIntegerField(blank=True, null=True)
dat_origem_externa = models.DateField(blank=True, null=True)
cod_local_origem_externa = models.IntegerField(blank=True, null=True)
local_origem_externa = models.ForeignKey(Origem, blank=True, null=True)
nom_apelido = models.CharField(max_length=50, blank=True, null=True)
num_dias_prazo = models.IntegerField(blank=True, null=True)
dat_fim_prazo = models.DateField(blank=True, null=True)
@ -109,9 +112,9 @@ class MateriaLegislativa(models.Model):
class Numeracao(models.Model):
cod_materia = models.IntegerField()
materia = models.ForeignKey(MateriaLegislativa)
num_ordem = models.IntegerField()
tip_materia = models.IntegerField()
tip_materia = models.ForeignKey(TipoMateriaLegislativa)
num_materia = models.CharField(max_length=5)
ano_materia = models.SmallIntegerField()
dat_materia = models.DateField(blank=True, null=True)
@ -133,8 +136,8 @@ class Origem(models.Model):
class Parecer(models.Model):
cod_relatoria = models.IntegerField()
cod_materia = models.IntegerField()
relatoria = models.ForeignKey(Relatoria)
materia = models.ForeignKey(MateriaLegislativa)
tip_conclusao = models.CharField(max_length=3, blank=True, null=True)
tip_apresentacao = models.CharField(max_length=1)
txt_parecer = models.TextField(blank=True, null=True)
@ -142,9 +145,9 @@ class Parecer(models.Model):
class Proposicao(models.Model):
cod_proposicao = models.AutoField(primary_key=True)
cod_materia = models.IntegerField(blank=True, null=True)
cod_autor = models.IntegerField()
tip_proposicao = models.IntegerField()
materia = models.ForeignKey(MateriaLegislativa, blank=True, null=True)
autor = models.ForeignKey(Autor)
tip_proposicao = models.ForeignKey(TipoProposicao)
dat_envio = models.DateTimeField()
dat_recebimento = models.DateTimeField(blank=True, null=True)
txt_descricao = models.CharField(max_length=100)
@ -161,10 +164,10 @@ class RegimeTramitacao(models.Model):
class Relatoria(models.Model):
cod_relatoria = models.AutoField(primary_key=True)
cod_materia = models.IntegerField()
materia = models.ForeignKey(MateriaLegislativa)
parlamentar = models.ForeignKey(Parlamentar)
tip_fim_relatoria = models.IntegerField(blank=True, null=True)
cod_comissao = models.IntegerField(blank=True, null=True)
tip_fim_relatoria = models.ForeignKey(TipoFimRelatoria, blank=True, null=True)
comissao = models.ForeignKey(Comissao, blank=True, null=True)
dat_desig_relator = models.DateField()
dat_destit_relator = models.DateField(blank=True, null=True)
@ -210,12 +213,12 @@ class TipoProposicao(models.Model):
class Tramitacao(models.Model):
cod_tramitacao = models.AutoField(primary_key=True)
cod_status = models.IntegerField(blank=True, null=True)
cod_materia = models.IntegerField()
status = models.ForeignKey(StatusTramitacao, blank=True, null=True)
materia = models.ForeignKey(MateriaLegislativa)
dat_tramitacao = models.DateField(blank=True, null=True)
cod_unid_tram_local = models.IntegerField(blank=True, null=True)
unid_tram_local = models.ForeignKey(UnidadeTramitacao, blank=True, null=True)
dat_encaminha = models.DateField(blank=True, null=True)
cod_unid_tram_dest = models.IntegerField(blank=True, null=True)
unid_tram_dest = models.ForeignKey(UnidadeTramitacao, blank=True, null=True)
ind_ult_tramitacao = models.IntegerField()
ind_urgencia = models.IntegerField()
sgl_turno = models.CharField(max_length=1, blank=True, null=True)
@ -225,6 +228,7 @@ class Tramitacao(models.Model):
class UnidadeTramitacao(models.Model):
cod_unid_tramitacao = models.AutoField(primary_key=True)
cod_comissao = models.IntegerField(blank=True, null=True)
cod_orgao = models.IntegerField(blank=True, null=True)
comissao = models.ForeignKey(Comissao, blank=True, null=True)
orgao = models.ForeignKey(Orgao, blank=True, null=True)
parlamentar = models.ForeignKey(Parlamentar, blank=True, null=True)

10
norma/models.py

@ -1,5 +1,7 @@
from django.db import models
from materia.models import MateriaLegislativa
class AssuntoNorma(models.Model):
cod_assunto = models.AutoField(primary_key=True)
@ -9,8 +11,8 @@ class AssuntoNorma(models.Model):
class NormaJuridica(models.Model):
cod_norma = models.AutoField(primary_key=True)
tip_norma = models.IntegerField()
cod_materia = models.IntegerField(blank=True, null=True)
tip_norma = models.ForeignKey(TipoNormaJuridica)
materia = models.ForeignKey(MateriaLegislativa, blank=True, null=True)
num_norma = models.IntegerField()
ano_norma = models.SmallIntegerField()
tip_esfera_federacao = models.CharField(max_length=1)
@ -37,7 +39,7 @@ class TipoNormaJuridica(models.Model):
class VinculoNormaJuridica(models.Model):
cod_vinculo = models.AutoField(primary_key=True)
cod_norma_referente = models.IntegerField()
cod_norma_referida = models.IntegerField()
norma_referente = models.ForeignKey(NormaJuridica)
norma_referida = models.ForeignKey(NormaJuridica)
tip_vinculo = models.CharField(max_length=1, blank=True, null=True)
ind_excluido = models.CharField(max_length=1)

18
parlamentares/models.py

@ -25,8 +25,8 @@ class Coligacao(models.Model):
class ComposicaoColigacao(models.Model):
cod_partido = models.IntegerField()
cod_coligacao = models.IntegerField()
partido = models.ForeignKey(Partido)
coligacao = models.ForeignKey(Coligacao)
class Localidade(models.Model):
@ -44,8 +44,8 @@ class NivelInstrucao(models.Model):
class Parlamentar(models.Model):
###
cod_nivel_instrucao = models.IntegerField(blank=True, null=True)
tip_situacao_militar = models.IntegerField(blank=True, null=True)
nivel_instrucao = models.ForeignKey(NivelInstrucao, blank=True, null=True)
tip_situacao_militar = models.ForeignKey(TipoSituacaoMilitar, blank=True, null=True)
nom_completo = models.CharField(max_length=50)
nom_parlamentar = models.CharField(max_length=50, blank=True, null=True)
sex_parlamentar = models.CharField(max_length=1)
@ -58,7 +58,7 @@ class Parlamentar(models.Model):
num_tel_parlamentar = models.CharField(max_length=50, blank=True, null=True)
num_fax_parlamentar = models.CharField(max_length=50, blank=True, null=True)
end_residencial = models.CharField(max_length=100, blank=True, null=True)
cod_localidade_resid = models.IntegerField(blank=True, null=True)
localidade_resid = models.ForeignKey(Localidade, blank=True, null=True)
num_cep_resid = models.CharField(max_length=9, blank=True, null=True)
num_tel_resid = models.CharField(max_length=50, blank=True, null=True)
num_fax_resid = models.CharField(max_length=50, blank=True, null=True)
@ -73,7 +73,7 @@ class Parlamentar(models.Model):
class Dependente(models.Model):
cod_dependente = models.AutoField(primary_key=True)
tip_dependente = models.IntegerField()
tip_dependente = models.ForeignKey(TipoDependente)
parlamentar = models.ForeignKey(Parlamentar)
nom_dependente = models.CharField(max_length=50)
sex_dependente = models.CharField(max_length=1)
@ -86,16 +86,16 @@ class Dependente(models.Model):
class Filiacao(models.Model):
dat_filiacao = models.DateField()
parlamentar = models.ForeignKey(Parlamentar)
cod_partido = models.IntegerField()
partido = models.ForeignKey(Partido)
dat_desfiliacao = models.DateField(blank=True, null=True)
class Mandato(models.Model):
cod_mandato = models.AutoField(primary_key=True)
parlamentar = models.ForeignKey(Parlamentar)
tip_afastamento = models.IntegerField(blank=True, null=True)
tip_afastamento = models.ForeignKey(TipoAfastamento, blank=True, null=True)
legislatura = models.ForeignKey(Legislatura)
cod_coligacao = models.IntegerField(blank=True, null=True)
coligacao = models.ForeignKey(Coligacao, blank=True, null=True)
tip_causa_fim_mandato = models.IntegerField(blank=True, null=True)
dat_fim_mandato = models.DateField(blank=True, null=True)
num_votos_recebidos = models.IntegerField(blank=True, null=True)

35
sessao/models.py

@ -1,6 +1,9 @@
from django.db import models
from parlamentares.models import CargoMesa, Legislatura, Parlamentar
from materia.models import MateriaLegislativa
from parlamentares.models import CargoMesa
from parlamentares.models import Parlamentar
from parlamentares.models import SessaoLegislativa
class ExpedienteMateria(models.Model):
@ -15,21 +18,21 @@ class ExpedienteMateria(models.Model):
class ExpedienteSessaoPlenaria(models.Model):
cod_sessao_plen = models.IntegerField()
cod_expediente = models.IntegerField()
sessao_plen = models.ForeignKey(SessaoPlenaria)
expediente = models.ForeignKey(TipoExpediente)
txt_expediente = models.TextField(blank=True, null=True)
class MesaSessaoPlenaria(models.Model):
cargo = models.ForeignKey(CargoMesa)
cod_sessao_leg = models.IntegerField()
sessao_leg = models.ForeignKey(SessaoLegislativa)
parlamentar = models.ForeignKey(Parlamentar)
cod_sessao_plen = models.IntegerField()
sessao_plen = models.ForeignKey(SessaoPlenaria)
ind_excluido = models.IntegerField(blank=True, null=True)
class Oradores(models.Model):
cod_sessao_plen = models.IntegerField()
sessao_plen = models.ForeignKey(SessaoPlenaria)
parlamentar = models.ForeignKey(Parlamentar)
num_ordem = models.IntegerField()
url_discurso = models.CharField(max_length=150, blank=True, null=True)
@ -44,8 +47,8 @@ class OradoresExpediente(models.Model):
class OrdemDia(models.Model):
cod_ordem = models.AutoField(primary_key=True)
cod_sessao_plen = models.IntegerField()
cod_materia = models.IntegerField()
sessao_plen = models.ForeignKey(SessaoPlenaria)
materia = models.ForeignKey(MateriaLegislativa)
dat_ordem = models.DateField()
txt_observacao = models.TextField(blank=True, null=True)
num_ordem = models.IntegerField()
@ -62,9 +65,9 @@ class OrdemDiaPresenca(models.Model):
class RegistroVotacao(models.Model):
cod_votacao = models.AutoField(primary_key=True)
tip_resultado_votacao = models.IntegerField()
cod_materia = models.IntegerField()
cod_ordem = models.IntegerField()
tip_resultado_votacao = models.ForeignKey(TipoResultadoVotacao)
materia = models.ForeignKey(MateriaLegislativa)
ordem = models.ForeignKey(OrdemDia)
num_votos_sim = models.IntegerField()
num_votos_nao = models.IntegerField()
num_abstencao = models.IntegerField()
@ -72,16 +75,16 @@ class RegistroVotacao(models.Model):
class RegistroVotacaoParlamentar(models.Model):
cod_votacao = models.IntegerField()
votacao = models.ForeignKey(RegistroVotacao)
parlamentar = models.ForeignKey(Parlamentar)
vot_parlamentar = models.CharField(max_length=10)
class SessaoPlenaria(models.Model):
cod_sessao_plen = models.AutoField(primary_key=True)
cod_andamento_sessao = models.IntegerField(blank=True, null=True)
tip_sessao = models.IntegerField()
cod_sessao_leg = models.IntegerField()
andamento_sessao = models.ForeignKey(AndamentoSessao, blank=True, null=True)
tip_sessao = models.ForeignKey(TipoSessaoPlenaria)
sessao_leg = models.ForeignKey(SessaoLegislativa)
legislatura = models.ForeignKey(Legislatura)
tip_expediente = models.CharField(max_length=10)
dat_inicio_sessao = models.DateField()
@ -96,7 +99,7 @@ class SessaoPlenaria(models.Model):
class SessaoPlenariaPresenca(models.Model):
cod_presenca_sessao = models.AutoField(primary_key=True)
cod_sessao_plen = models.IntegerField()
sessao_plen = models.ForeignKey(SessaoPlenaria)
parlamentar = models.ForeignKey(Parlamentar)
dat_sessao = models.DateField(blank=True, null=True)

Loading…
Cancel
Save