|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Generated by Django 1.11.15 on 2019-05-16 20:59
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
from django.db import migrations
|
|
|
|
|
|
|
|
from datetime import date
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
def spoj_k_organizatorum_osoby(apps, scema_editor):
|
|
|
|
Organizator = apps.get_model('seminar', 'Organizator')
|
|
|
|
Resitel = apps.get_model('seminar', 'Resitel')
|
|
|
|
Osoba = apps.get_model('seminar', 'Osoba')
|
|
|
|
for org in Organizator.objects.all():
|
|
|
|
|
|
|
|
# Spárování organizátora s osobou
|
|
|
|
# Myšlenka: Když najdeme řešitele pro daného uživatele, tak se vezme Osoba příslušná uživateli,
|
|
|
|
# Pokud nenajdeme uživatele, tak ještě zkusíme dohledat Osobu podle e-mailu
|
|
|
|
user = org.user
|
|
|
|
if user is None:
|
|
|
|
logger.error(f'Org {org} nemá uživatele!')
|
|
|
|
# Je to podezřelé, ale prostě vyrobíme novou osobu.
|
|
|
|
osoba = Osoba(user=user)
|
|
|
|
# Téhle osobě nejdou nastavit detaily, protože žádné nemáme.
|
|
|
|
# Datum registrace budiž minimální
|
|
|
|
osoba.datum_registrace = date.min
|
|
|
|
else:
|
|
|
|
logger.info(f'Org {org.user.email}(ID: {org.id}) má uživatele {user}')
|
|
|
|
# 💢💢💢 Python nemá goto, ale prý má výjimky… 💢💢💢
|
|
|
|
class EndException(Exception): pass
|
|
|
|
try:
|
|
|
|
# Hledáme podle uživatele
|
|
|
|
resitele = Resitel.objects.filter(user=user)
|
|
|
|
if resitele.count() != 0 and user is not None:
|
|
|
|
osoba = resitele.first().osoba
|
|
|
|
logger.info(f'Našel jsem řešitele {resitele.first().email} podle uživatele, používám jeho Osobu')
|
|
|
|
raise EndException
|
|
|
|
|
|
|
|
# Hledáme podle e-mailu
|
|
|
|
osoby = Osoba.objects.filter(email__iexact=user.email)
|
|
|
|
if osoby.count() != 0 and user.email != '':
|
|
|
|
osoba = osoby.first()
|
|
|
|
if osoba.user is None:
|
|
|
|
osoba.user = user
|
|
|
|
logger.info(f'Našel jsem Osobu {osoby.first().email} podle e-mailu')
|
|
|
|
raise EndException
|
|
|
|
|
|
|
|
# Fallback
|
|
|
|
logger.warning(f'Org neměl řešitele, zakládám novou Osobu.')
|
|
|
|
osoba = Osoba(user=user)
|
|
|
|
|
|
|
|
# Přesun informací z usera do osoby
|
|
|
|
# pro osoby z řešitelů (jediné dosud existující osoby) již v minule migraci
|
|
|
|
osoba.jmeno = user.first_name
|
|
|
|
osoba.prijmeni = user.last_name
|
|
|
|
osoba.email = user.email
|
|
|
|
user.jmeno += " (Uživatel!)"
|
|
|
|
user.prijmeni += " (Uživatel!)"
|
|
|
|
osoba.datum_registrace = min(osoba.datum_registrace, user.date_joined, key=lambda d: d if d is not None else date.max)
|
|
|
|
user.save()
|
|
|
|
|
|
|
|
except EndException: pass
|
|
|
|
|
|
|
|
# Přesun informací z organizátora do jeho osoby
|
|
|
|
osoba.prezdivka = org.prezdivka if org.prezdivka is not None else ''
|
|
|
|
osoba.foto = org.foto
|
|
|
|
|
|
|
|
# Všechno uložit
|
|
|
|
osoba.save()
|
|
|
|
org.osoba = osoba
|
|
|
|
org.save()
|
|
|
|
|
|
|
|
def fix_problem(apps, schema_editor):
|
|
|
|
Problem = apps.get_model('seminar', 'Problem')
|
|
|
|
Organizator = apps.get_model('seminar', 'Organizator')
|
|
|
|
for pr in Problem.objects.all():
|
|
|
|
if pr.autor_old is not None:
|
|
|
|
pr.autor = Organizator.objects.filter(osoba__user=pr.autor_old).first()
|
|
|
|
else:
|
|
|
|
pr.autor = None
|
|
|
|
if pr.opravovatel is not None:
|
|
|
|
if Organizator.objects.filter(osoba__user=pr.opravovatel).first() is not None:
|
|
|
|
pr.opravovatele.add(Organizator.objects.filter(osoba__user=pr.opravovatel).first())
|
|
|
|
else:
|
|
|
|
logger.error(f'WTF, nespárovaný opravovatel {pr.opravovatel} problému {pr}')
|
|
|
|
pr.save()
|
|
|
|
|
|
|
|
def fix_pohadka(apps, schema_editor):
|
|
|
|
Pohadka = apps.get_model('seminar', 'Pohadka')
|
|
|
|
Organizator = apps.get_model('seminar', 'Organizator')
|
|
|
|
for poh in Pohadka.objects.all():
|
|
|
|
if poh.autor_old is not None:
|
|
|
|
poh.autor = Organizator.objects.filter(osoba__user=poh.autor_old).first()
|
|
|
|
else:
|
|
|
|
poh.autor = None
|
|
|
|
poh.save()
|
|
|
|
|
|
|
|
def fix_novinka(apps, schema_editor):
|
|
|
|
Novinky = apps.get_model('seminar', 'Novinky')
|
|
|
|
Organizator = apps.get_model('seminar', 'Organizator')
|
|
|
|
for nov in Novinky.objects.all():
|
|
|
|
nov.autor = Organizator.objects.filter(osoba__user=nov.autor_old).first()
|
|
|
|
nov.save()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Migration(migrations.Migration):
|
|
|
|
|
|
|
|
dependencies = [
|
|
|
|
('seminar', '0051_resitel_to_osoba'),
|
|
|
|
]
|
|
|
|
|
|
|
|
operations = [
|
|
|
|
migrations.RunPython(spoj_k_organizatorum_osoby, migrations.RunPython.noop),
|
|
|
|
migrations.RunPython(fix_problem, migrations.RunPython.noop),
|
|
|
|
migrations.RunPython(fix_pohadka, migrations.RunPython.noop),
|
|
|
|
migrations.RunPython(fix_novinka, migrations.RunPython.noop),
|
|
|
|
]
|