Browse Source

Add a special migration for comissoes app

pull/6/head
Marcio Mazza 10 years ago
parent
commit
68a4f917da
  1. 30
      comissoes/models.py
  2. 13
      legacy/scripts/field_renames.py
  3. 68
      legacy/scripts/migration.py

30
comissoes/models.py

@ -53,7 +53,7 @@ class Comissao(models.Model):
return self.nome
class PeriodoCompComissao(models.Model):
class Periodo(models.Model): # PeriodoCompComissao
data_inicio = models.DateField(verbose_name=_(u'Data Início')) # dat_inicio_periodo
data_fim = models.DateField(blank=True, null=True, verbose_name=_(u'Data Fim')) # dat_fim_periodo
@ -77,21 +77,29 @@ class CargoComissao(models.Model):
return self.nome
class ComposicaoComissao(models.Model):
parlamentar = models.ForeignKey(Parlamentar) # cod_parlamentar
comissao = models.ForeignKey(Comissao) # cod_comissao
periodo_comp = models.ForeignKey(PeriodoCompComissao) # cod_periodo_comp
cargo = models.ForeignKey(CargoComissao) # cod_cargo
titular = models.BooleanField(verbose_name=_(u'Titular')) # ind_titular
data_designacao = models.DateField(verbose_name=_(u'Data Designação')) # dat_designacao
data_desligamento = models.DateField(blank=True, null=True, verbose_name=_(u'Data Desligamento')) # dat_desligamento
motivo_desligamento = models.CharField(max_length=150, blank=True, null=True, verbose_name=_(u'Motivo Desligamento')) # des_motivo_desligamento
observacao = models.CharField(max_length=150, blank=True, null=True, verbose_name=_(u'Observação')) # obs_composicao
class Composicao(models.Model): # IGNORE
comissao = models.ForeignKey(Comissao, verbose_name=_(u'Comissão')) # cod_comissao
periodo = models.ForeignKey(Periodo, verbose_name=_(u'Período')) # cod_periodo_comp
class Meta:
verbose_name = _(u'Composição de Comissão')
verbose_name_plural = _(u'Composições de Comissão')
class Participacao(models.Model): # ComposicaoComissao
composicao = models.ForeignKey(Composicao) # cod_comissao
parlamentar = models.ForeignKey(Parlamentar) # cod_parlamentar
cargo = models.ForeignKey(CargoComissao) # cod_cargo
titular = models.BooleanField(verbose_name=_(u'Titular')) # ind_titular
data_designacao = models.DateField(verbose_name=_(u'Data Designação')) # dat_designacao
data_desligamento = models.DateField(blank=True, null=True, verbose_name=_(u'Data Desligamento')) # dat_desligamento
motivo_desligamento = models.CharField(max_length=150, blank=True, null=True, verbose_name=_(u'Motivo Desligamento')) # des_motivo_desligamento
observacao = models.CharField(max_length=150, blank=True, null=True, verbose_name=_(u'Observação')) # obs_composicao
class Meta:
verbose_name = _(u'Participação em Comissão')
verbose_name_plural = _(u'Participações em Comissão')
def __unicode__(self):
return 'TODO...'

13
legacy/scripts/field_renames.py

@ -1,7 +1,7 @@
from collections import defaultdict
import re
from collections import defaultdict, OrderedDict
from difflib import SequenceMatcher
from inspect import getsourcelines
from collections import OrderedDict
from migration_base import appconfs, legacy_app
@ -34,10 +34,19 @@ def print_commented_source(model):
field_renames = OrderedDict()
model_renames = {}
for app in appconfs:
for model in app.models.values():
new_to_old = OrderedDict()
lines = getsourcelines(model)[0]
class_line = lines[0].strip()
match = re.match('class (.+)\(models\.Model\): *\# *(.*)', class_line)
if match:
model_name, rename = match.groups()
assert model_name == model.__name__
model_renames[model] = rename
for line in lines:
if is_field_line(line):
new = get_field(line)

68
legacy/scripts/migration.py

@ -4,9 +4,11 @@ from django.db import connections, models
from django.db.models.base import ModelBase
from model_mommy import mommy
from field_renames import field_renames
from field_renames import field_renames, model_renames
from migration_base import legacy_app, appconfs
from parlamentares.models import Parlamentar
from comissoes.models import Composicao, Participacao
def info(msg):
@ -27,7 +29,7 @@ def special(model, fieldname):
@special(Parlamentar, 'unid_deliberativa')
def none_to_false(value):
def none_to_false(obj, value):
# Field is defined as not null in legacy db, but data includes null values
# => transform None to False
if value is None:
@ -35,6 +37,24 @@ def none_to_false(value):
return bool(value)
@special(Participacao, 'composicao')
def get_participacao_composicao(obj, value):
# value parameter is ignored
new = Composicao()
for new_field, value in [('comissao', obj.cod_comissao),
('periodo', obj.cod_periodo_comp)]:
model_field = Composicao._meta.get_field(new_field)
value = get_related_if_foreignkey(model_field, '???', value)
setattr(new, new_field, value)
previous = Composicao.objects.filter(comissao=new.comissao, periodo=new.periodo)
if previous:
assert len(previous) == 1
return previous[0]
else:
new.save()
return new
def migrate(obj=appconfs, count_limit=None):
# warning: model/app migration order is of utmost importance
@ -96,12 +116,17 @@ def make_stub(model, id):
def migrate_model(model, to_delete, count_limit=None):
legacy_model_name = model_renames.get(model, model.__name__)
if legacy_model_name.upper() == 'IGNORE':
print 'Model ignored: %s' % model.__name__
return
print 'Migrating %s...' % model.__name__
# clear all model entries
model.objects.all().delete()
legacy_model = legacy_app.get_model(model.__name__)
legacy_model = legacy_app.get_model(legacy_model_name)
old_pk_name = legacy_model._meta.pk.name
# setup migration strategy for tables with or without a pk
@ -131,30 +156,35 @@ def migrate_model(model, to_delete, count_limit=None):
model_field = model._meta.get_field(new_field)
transform = special_transforms.get(model_field)
if transform:
value = transform(value)
value = transform(old, value)
else:
# check for a relation
if isinstance(model_field, models.ForeignKey) and value is not None:
try:
value = model_field.related_model.objects.get(id=value)
except ObjectDoesNotExist:
msg = 'FK [%s (%s) : %s] not found for value %s' % (
model.__name__, old_pk, model_field.name, value)
if value == 0:
# we interpret FK == 0 as actually FK == NONE
value = None
warn(msg + ' => NONE for zero value')
else:
value = make_stub(model_field.related_model, value)
warn(msg + ' => STUB CREATED')
else:
assert value
value = get_related_if_foreignkey(model_field, old_pk, value)
setattr(new, new_field, value)
save(new, old_pk)
if getattr(old, 'ind_excluido', False):
to_delete.append(new)
def get_related_if_foreignkey(model_field, old_pk, value):
if isinstance(model_field, models.ForeignKey) and value is not None:
try:
value = model_field.related_model.objects.get(id=value)
except ObjectDoesNotExist:
msg = 'FK [%s (%s) : %s] not found for value %s' % (
model_field.model.__name__, old_pk, model_field.name, value)
if value == 0:
# we interpret FK == 0 as actually FK == NONE
value = None
warn(msg + ' => NONE for zero value')
else:
value = make_stub(model_field.related_model, value)
warn(msg + ' => STUB CREATED')
else:
assert value
return value
def get_ind_excluido(obj):
legacy_model = legacy_app.get_model(type(obj).__name__)
return getattr(legacy_model.objects.get(**{legacy_model._meta.pk.name: obj.id}), 'ind_excluido', False)

Loading…
Cancel
Save