Skip to content

Commit 45abf51

Browse files
author
joao
committed
Script para migração de dados do tipo norma para materia
1 parent 435cd0d commit 45abf51

1 file changed

Lines changed: 155 additions & 0 deletions

File tree

scripts/migracao_norma_materia.py

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
import os
2+
3+
import sapl.utils
4+
5+
from sapl import settings
6+
from sapl.norma.models import NormaJuridica, TipoNormaJuridica
7+
from sapl.materia.models import MateriaLegislativa, TipoMateriaLegislativa, RegimeTramitacao
8+
from django.utils import timezone
9+
10+
from sapl.utils import texto_upload_path
11+
12+
13+
def migra_atas_normas(tipos_array):
14+
for t in tipos_array:
15+
print(t['old_descricao'])
16+
for x in NormaJuridica.objects.filter(tipo__descricao__icontains=t['old_descricao']):
17+
tipo_materia = get_or_create_tipo_materia(t['sigla'], t['descricao'])
18+
n_mat = create_materia_from_norma(x, tipo_materia)
19+
if n_mat is not None:
20+
if NormaJuridica.objects.get(pk=x.id).delete():
21+
print("Removida NormaJuridica:", x)
22+
else:
23+
print("Impossível copiar Norma para tipo Materia:", x)
24+
25+
for t in tipos_array:
26+
a = TipoNormaJuridica.objects.filter(descricao__icontains=t['old_descricao']).first()
27+
print(a, t['old_descricao'])
28+
if a is not None:
29+
print("Removido TipoNormaJuridica:", a)
30+
a.delete()
31+
else:
32+
print("Erro ao remover TipoNormaJuridica:", a)
33+
34+
35+
def get_or_create_tipo_materia(sigla, descricao):
36+
tipo_materia = TipoMateriaLegislativa.objects.filter(descricao=descricao, sigla=sigla).first()
37+
38+
if not tipo_materia:
39+
tipo_materia = TipoMateriaLegislativa(sigla=sigla, descricao=descricao)
40+
tipo_materia.save()
41+
42+
return tipo_materia
43+
44+
45+
def create_materia_from_norma(norma, tipo):
46+
if not norma or not tipo:
47+
print("Norma e/ou tipo invalidos:", norma, tipo)
48+
return False
49+
else:
50+
n_materia = MateriaLegislativa()
51+
n_materia.tipo = tipo
52+
if norma.numero:
53+
n_materia.numero = norma.numero
54+
if norma.ano:
55+
n_materia.ano = norma.ano
56+
if norma.data_publicacao:
57+
n_materia.data_publicacao = norma.data_publicacao
58+
if norma.ementa:
59+
n_materia.ementa = norma.ementa
60+
if norma.texto_integral:
61+
n_materia.texto_original = norma.texto_integral
62+
if norma.data_ultima_atualizacao:
63+
n_materia.data_ultima_atualizacao = norma.data_ultima_atualizacao
64+
if norma.user:
65+
n_materia.user = norma.user
66+
if norma.ip:
67+
n_materia.ip = norma.ip
68+
if norma.ultima_edicao:
69+
n_materia.ultima_edicao = norma.ultima_edicao
70+
if norma.data is None:
71+
n_materia.data_apresentacao = timezone.now()
72+
else:
73+
n_materia.data_apresentacao = norma.data
74+
n_materia.regime_tramitacao = RegimeTramitacao.objects.first()
75+
76+
n_materia.save()
77+
print("Criada nova MateriaLegislativa:", n_materia.__str__())
78+
update_file_location_to_new_model(n_materia)
79+
return True
80+
81+
82+
def update_file_location_to_new_model(new_model):
83+
try:
84+
initial_path = new_model.texto_original.path
85+
except ValueError:
86+
print("arquivo não existe", new_model.texto_original.name)
87+
return False
88+
89+
if not os.path.exists(initial_path):
90+
print("Arquivo nao existe:", initial_path)
91+
return False
92+
93+
# get new path using already defined function
94+
print(os.path.basename(new_model.texto_original.name))
95+
new_name = texto_upload_path(new_model, os.path.basename(new_model.texto_original.name), subpath=new_model.ano)
96+
new_path = os.path.join(settings.MEDIA_ROOT, new_name)
97+
print("New name:", new_name)
98+
print("Novo diretorio:", new_path)
99+
# check if the dir exists
100+
if not os.path.exists(os.path.dirname(new_path)):
101+
print("criando novo diretorio...:", os.path.dirname(new_path))
102+
os.makedirs(os.path.dirname(new_path))
103+
104+
if os.path.exists(os.path.dirname(new_path)):
105+
print("movendo arquivo de local:", new_path)
106+
os.rename(initial_path, new_path)
107+
# Update the file field
108+
new_model.texto_original.name = new_name
109+
new_model.save()
110+
print("success!", new_model.texto_original)
111+
return True
112+
else:
113+
print("Não foi possível criar diretorio:", os.path.dirname(new_path))
114+
115+
return False
116+
117+
118+
def main():
119+
tipos_movimentacao = [
120+
{
121+
'old_descricao': 'ATA COMISS',
122+
'descricao': 'Ata de Comissão',
123+
'sigla': 'ACM'
124+
},
125+
{
126+
'old_descricao': 'ATA CPI Nº 01',
127+
'descricao': 'Ata CPI',
128+
'sigla': 'ACPI'
129+
},
130+
{
131+
'old_descricao': 'Atas Audiências Públicas',
132+
'descricao': 'Ata de Audiência Pública',
133+
'sigla': 'AAP'
134+
},
135+
{
136+
'old_descricao': 'ATAS DA CPI Nº 2',
137+
'descricao': 'Ata de CPI',
138+
'sigla': 'ACPI'
139+
},
140+
{
141+
'old_descricao': 'Atas Reuniões Extraordinárias',
142+
'descricao': 'Ata de Reunião Extraordinária',
143+
'sigla': 'ARE'
144+
},
145+
{
146+
'old_descricao': 'Atas Reuniões Ordinárias',
147+
'descricao': 'Ata de Reunião Ordinária',
148+
'sigla': 'ARO'
149+
},
150+
]
151+
migra_atas_normas(tipos_movimentacao)
152+
153+
154+
if __name__ == '__main__':
155+
main()

0 commit comments

Comments
 (0)