Tomas Gavenciak
9 years ago
1 changed files with 81 additions and 0 deletions
@ -0,0 +1,81 @@ |
|||
# -*- coding: utf-8 -*- |
|||
|
|||
import logging as log |
|||
from .models import Resitel |
|||
import reversion |
|||
|
|||
def merge_props(r1, r2, prop, pretend=True, smaller=False, equal=True): |
|||
"""Merge r2.`prop` into r1.`prop`. |
|||
|
|||
If r1.`prop` unset, use r1.`prop`=r2.`prop`. |
|||
If both set and equal=True, warn if not equal. |
|||
If both set and smaller=True, use the smaller one. |
|||
With pretend=True does not modify r1. |
|||
""" |
|||
a1 = r1.__getattribute__(prop) |
|||
a2 = r2.__getattribute__(prop) |
|||
if not a1: |
|||
if not pretend: |
|||
r1.__setattr__(prop, a2) |
|||
elif a2: |
|||
if equal and a1 != a2: |
|||
log.warn(u"merge: Ruzna %s: %s VS %s", prop, a1, a2) |
|||
if smaller: |
|||
if not pretend: |
|||
r1.__setattr__(prop, min(a1, a2)) |
|||
|
|||
|
|||
def merge_Resitel(rbase, rmerge, pretend=True): |
|||
"""Zahrne data a vztahy Resitele rmerge do Resitele rbase, pak smaze rmerge. |
|||
|
|||
Selze pro uzivatele s user!=NULL. S pretend=True nezmeni databazi. |
|||
""" |
|||
# Ma relace: skola |
|||
# Je v relaci: user, reseni, soustredeni_ucastnici, vysledky_base(VIEW) |
|||
|
|||
log.info(u"merge: %s <- %s", unicode(rbase), unicode(rmerge)) |
|||
|
|||
assert not rbase.user |
|||
assert not rmerge.user |
|||
assert rbase != rmerge |
|||
|
|||
if (rbase.jmeno != rmerge.jmeno) or (rbase.prijmeni != rmerge.prijmeni): |
|||
log.error(u"merge: Ruzna jmena: %s VS %s", rbase, rmerge) |
|||
if rbase.rok_maturity != rmerge.rok_maturity: |
|||
log.error(u"merge: Ruzne roky maturity: %s VS %s", rbase.rok_maturity, rmerge.rok_maturity) |
|||
|
|||
with reversion.create_revision(): |
|||
reversion.set_comment('Merge duplicitnich Resitelu: %r <- %r' % (rbase.pk, rmerge.pk)) |
|||
|
|||
merge_props(rbase, rmerge, 'skola', pretend=pretend) |
|||
merge_props(rbase, rmerge, 'datum_narozeni', pretend=pretend) |
|||
merge_props(rbase, rmerge, 'datum_prihlaseni', pretend=pretend) |
|||
merge_props(rbase, rmerge, 'datum_souhlasu_zasilani', pretend=pretend, smaller=True, equal=False) |
|||
merge_props(rbase, rmerge, 'datum_souhlasu_udaje', pretend=pretend, smaller=True, equal=False) |
|||
merge_props(rbase, rmerge, 'email', pretend=pretend) |
|||
if rmerge.import_mamoper_id and not pretend: |
|||
rbase.import_mamoper_id += ' ' + rmerge.import_mamoper_id |
|||
if rmerge.poznamka and not pretend: |
|||
rbase.poznamka += ' ' + rmerge.poznamka |
|||
merge_props(rbase, rmerge, 'mesto', pretend=pretend) |
|||
merge_props(rbase, rmerge, 'pohlavi_muz', pretend=pretend) |
|||
merge_props(rbase, rmerge, 'psc', pretend=pretend) |
|||
merge_props(rbase, rmerge, 'stat', pretend=pretend) |
|||
merge_props(rbase, rmerge, 'telefon', pretend=pretend) |
|||
merge_props(rbase, rmerge, 'ulice', pretend=pretend) |
|||
merge_props(rbase, rmerge, 'zasilat', pretend=pretend) |
|||
|
|||
for res in rmerge.reseni.all(): |
|||
if not pretend: |
|||
res.resitel = rbase |
|||
res.save() |
|||
|
|||
for uc in rmerge.soustredeni_ucastnici_set.all(): |
|||
if not pretend: |
|||
uc.resitel = rbase |
|||
uc.save() |
|||
|
|||
if not pretend: |
|||
rmerge.delete() |
|||
|
|||
|
Loading…
Reference in new issue