mamweb/seminar/tools.py

83 lines
2.7 KiB
Python
Raw Normal View History

2015-10-25 12:23:38 +01:00
# -*- coding: utf-8 -*-
import logging as log
from .models import Resitel
from reversion import revisions as reversion
2015-10-25 12:23:38 +01:00
def merge_props(r1, r2, prop, pretend=True, smaller=False, equal=True):
2019-05-11 01:15:05 +02:00
"""Merge r2.`prop` into r1.`prop`.
2015-10-25 12:23:38 +01:00
2019-05-11 01:15:05 +02:00
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:
2019-06-20 22:58:59 +02:00
log.warn("merge: Ruzna %s: %s VS %s", prop, a1, a2)
2019-05-11 01:15:05 +02:00
if smaller:
if not pretend:
r1.__setattr__(prop, min(a1, a2))
2015-10-25 12:23:38 +01:00
def merge_Resitel(rbase, rmerge, pretend=True):
2019-05-11 01:15:05 +02:00
"""Zahrne data a vztahy Resitele rmerge do Resitele rbase, pak smaze rmerge.
2015-10-25 12:23:38 +01:00
2019-05-11 01:15:05 +02:00
Selze pro uzivatele s user!=NULL. S pretend=True nezmeni databazi.
"""
# Ma relace: skola
# Je v relaci: user, reseni, soustredeni_ucastnici, vysledky_base(VIEW)
2015-10-25 12:23:38 +01:00
2019-06-20 22:58:59 +02:00
log.info("merge: %s <- %s", unicode(rbase), unicode(rmerge))
2015-10-25 12:23:38 +01:00
2019-05-11 01:15:05 +02:00
assert not rbase.user
assert not rmerge.user
assert rbase != rmerge
2015-10-25 12:23:38 +01:00
2019-05-11 01:15:05 +02:00
if (rbase.jmeno != rmerge.jmeno) or (rbase.prijmeni != rmerge.prijmeni):
2019-06-20 22:58:59 +02:00
log.error("merge: Ruzna jmena: %s VS %s", rbase, rmerge)
2019-05-11 01:15:05 +02:00
if rbase.rok_maturity != rmerge.rok_maturity:
2019-06-20 22:58:59 +02:00
log.error("merge: Ruzne roky maturity: %s VS %s", rbase.rok_maturity, rmerge.rok_maturity)
2015-10-25 12:23:38 +01:00
2019-05-11 01:15:05 +02:00
with reversion.create_revision():
reversion.set_comment('Merge duplicitnich Resitelu: %r <- %r' % (rbase.pk, rmerge.pk))
2015-10-25 12:23:38 +01:00
2019-05-11 01:15:05 +02:00
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)
2015-10-25 12:23:38 +01:00
2019-05-11 01:15:05 +02:00
for res in rmerge.reseni.all():
if not pretend:
res.resitel = rbase
res.save()
2015-10-25 12:23:38 +01:00
2019-05-11 01:15:05 +02:00
for uc in rmerge.soustredeni_ucastnici_set.all():
if not pretend:
uc.resitel = rbase
uc.save()
2015-10-25 12:23:38 +01:00
2019-05-11 01:15:05 +02:00
if not pretend:
rmerge.delete()
rbase.save()
2015-10-25 12:23:38 +01:00