From 66c294c920359cab25b6bfc8f964821da8bb4709 Mon Sep 17 00:00:00 2001 From: "Bc. Petr Pecha" Date: Thu, 25 Aug 2016 09:04:39 +0200 Subject: [PATCH] korektury | data migrace autor -> autor_org MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Migrace autora z CharField na odkaz na Organizator Datova migrace: hledani objektu Organizatora, ktery patri ke korekture nebo diskuznimu prispevku. Pokud neni organizator nalezeny tak je SMAZÁN. --- ...revod_autora_z_charField_na_Organizator.py | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 korektury/migrations/0011_prevod_autora_z_charField_na_Organizator.py diff --git a/korektury/migrations/0011_prevod_autora_z_charField_na_Organizator.py b/korektury/migrations/0011_prevod_autora_z_charField_na_Organizator.py new file mode 100644 index 00000000..dc57088d --- /dev/null +++ b/korektury/migrations/0011_prevod_autora_z_charField_na_Organizator.py @@ -0,0 +1,58 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models + +def transform_autor(apps, schema_editor): + print + Organizator = apps.get_model('seminar', 'Organizator') + + # preorgovani oprav + Oprava = apps.get_model('korektury', 'Oprava') + for oprava in Oprava.objects.all(): + jmeno = oprava.autor.split() + if len(jmeno) == 2: + try: + org = Organizator.objects.get(user__first_name=jmeno[0], + user__last_name=jmeno[1]) + oprava.autor_org = org + oprava.save() + print "{} uložen".format(oprava.autor) + except: + print "Org nenalezen -- mažu korekturu" +# oprava.delete() + else: + print "Org nenalezen -- mažu korekturu" + oprava.delete() + + # preorgovani komentaru + Komentar = apps.get_model('korektury', 'Komentar') + for komentar in Komentar.objects.all(): + jmeno = komentar.autor.split() + if len(jmeno) == 2: + try: + org = Organizator.objects.get(user__first_name=jmeno[0], + user__last_name=jmeno[1]) + komentar.autor_org = org + komentar.save() + print "{} uložen".format(oprava.autor) + except: + print "Org nenalezen -- mažu korekturu" +# oprava.delete() + else: + print "Org nenalezen -- mažu korekturu" + komentar.delete() + +def back(apps, schema_editor): + pass + + +class Migration(migrations.Migration): + + dependencies = [ + ('korektury', '0010_Pridani_odkazu_na_organizatora'), + ] + + operations = [ + migrations.RunPython(transform_autor, back), + ]