Merge branch 'destroy_polymorphism' into data_migrations
This commit is contained in:
commit
b7a20231c7
2 changed files with 201 additions and 21 deletions
107
seminar/migrations/0068_treenode_nazev.py
Normal file
107
seminar/migrations/0068_treenode_nazev.py
Normal file
|
@ -0,0 +1,107 @@
|
||||||
|
# Generated by Django 2.2.5 on 2019-09-26 19:35
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
# Migrace nejspíš neumí volat metody modelů:
|
||||||
|
# https://stackoverflow.com/questions/28777338/django-migrations-runpython-not-able-to-call-model-methods#37685925
|
||||||
|
|
||||||
|
def fix_RocnikNode_names(apps,schema_editor):
|
||||||
|
Objects = apps.get_model('seminar', 'RocnikNode')
|
||||||
|
for obj in Objects.objects.all():
|
||||||
|
obj.nazev = str(obj.rocnik)+" (RocnikNode)"
|
||||||
|
obj.save()
|
||||||
|
|
||||||
|
def fix_CisloNode_names(apps,schema_editor):
|
||||||
|
Objects = apps.get_model('seminar', 'CisloNode')
|
||||||
|
for obj in Objects.objects.all():
|
||||||
|
obj.nazev = str(obj.cislo)+" (CisloNode)"
|
||||||
|
obj.save()
|
||||||
|
|
||||||
|
def fix_MezicisloNode_names(apps,schema_editor):
|
||||||
|
Objects = apps.get_model('seminar', 'MezicisloNode')
|
||||||
|
for obj in Objects.objects.all():
|
||||||
|
if obj.prev:
|
||||||
|
if (obj.prev.get_real_instance_class() != CisloNode and
|
||||||
|
obj.prev.get_real_instance_class() != MezicisloNode):
|
||||||
|
raise ValueError("Předchůdce není číslo!")
|
||||||
|
posledni = obj.prev.cislo
|
||||||
|
obj.nazev = "Mezičíslo po čísle"+str(posledni)+" (MezicisloNode)"
|
||||||
|
elif obj.root:
|
||||||
|
if obj.root.get_real_instance_class() != RocnikNode:
|
||||||
|
raise ValueError("Kořen stromu není ročník!")
|
||||||
|
rocnik = obj.root.rocnik
|
||||||
|
obj.nazev = "První mezičíslo ročníku "+" (MezicisloNode)"
|
||||||
|
else:
|
||||||
|
print("!!!!! Nějaké neidentifikované mezičíslo !!!!!")
|
||||||
|
obj.nazev = "Neidentifikovatelné mezičíslo! (MezicisloNode)"
|
||||||
|
obj.save()
|
||||||
|
|
||||||
|
def fix_TemaVCisleNode_names(apps,schema_editor):
|
||||||
|
Objects = apps.get_model('seminar', 'TemaVCisleNode')
|
||||||
|
for obj in Objects.objects.all():
|
||||||
|
obj.nazev = str(obj.tema)+" (TemaVCisleNode)"
|
||||||
|
obj.save()
|
||||||
|
|
||||||
|
def fix_KonferaNode_names(apps,schema_editor):
|
||||||
|
Objects = apps.get_model('seminar', 'KonferaNode')
|
||||||
|
for obj in Objects.objects.all():
|
||||||
|
obj.nazev = str(obj.konfera)+" (KonferaNode)"
|
||||||
|
obj.save()
|
||||||
|
|
||||||
|
def fix_ClanekNode_names(apps,schema_editor):
|
||||||
|
Objects = apps.get_model('seminar', 'ClanekNode')
|
||||||
|
for obj in Objects.objects.all():
|
||||||
|
obj.nazev = str(obj.clanek)+" (ClanekNode)"
|
||||||
|
obj.save()
|
||||||
|
|
||||||
|
def fix_UlohaZadaniNode_names(apps,schema_editor):
|
||||||
|
Objects = apps.get_model('seminar', 'UlohaZadaniNode')
|
||||||
|
for obj in Objects.objects.all():
|
||||||
|
obj.nazev = str(obj.uloha)+" (UlohaZadaniNode)"
|
||||||
|
obj.save()
|
||||||
|
|
||||||
|
def fix_PohadkaNode_names(apps,schema_editor):
|
||||||
|
Objects = apps.get_model('seminar', 'PohadkaNode')
|
||||||
|
for obj in Objects.objects.all():
|
||||||
|
obj.nazev = str(obj.pohadka)+" (PohadkaNode)"
|
||||||
|
obj.save()
|
||||||
|
|
||||||
|
def fix_UlohaVzorakNode_names(apps,schema_editor):
|
||||||
|
Objects = apps.get_model('seminar', 'UlohaVzorakNode')
|
||||||
|
for obj in Objects.objects.all():
|
||||||
|
obj.nazev = str(obj.uloha)+" (UlohaVzorakNode)"
|
||||||
|
obj.save()
|
||||||
|
|
||||||
|
def fix_TextNode_names(apps,schema_editor):
|
||||||
|
Objects = apps.get_model('seminar', 'TextNode')
|
||||||
|
for obj in Objects.objects.all():
|
||||||
|
obj.nazev = str(obj.text)+" (TextNode)"
|
||||||
|
obj.save()
|
||||||
|
|
||||||
|
def fix_all_names(apps,schema_editor):
|
||||||
|
fix_RocnikNode_names(apps,schema_editor)
|
||||||
|
fix_CisloNode_names(apps,schema_editor)
|
||||||
|
fix_MezicisloNode_names(apps,schema_editor)
|
||||||
|
fix_TemaVCisleNode_names(apps,schema_editor)
|
||||||
|
fix_KonferaNode_names(apps,schema_editor)
|
||||||
|
fix_ClanekNode_names(apps,schema_editor)
|
||||||
|
fix_UlohaZadaniNode_names(apps,schema_editor)
|
||||||
|
fix_PohadkaNode_names(apps,schema_editor)
|
||||||
|
fix_UlohaVzorakNode_names(apps,schema_editor)
|
||||||
|
fix_TextNode_names(apps,schema_editor)
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('seminar', '0067_auto_20190814_0805'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='treenode',
|
||||||
|
name='nazev',
|
||||||
|
field=models.TextField(help_text='Tento název se zobrazuje v nabídkách pro výběr vhodného TreeNode', null=True, verbose_name='název tohoto node'),
|
||||||
|
),
|
||||||
|
migrations.RunPython(fix_all_names),
|
||||||
|
]
|
|
@ -384,6 +384,10 @@ class Rocnik(SeminarModelBase):
|
||||||
cache.set(name, c, 300)
|
cache.set(name, c, 300)
|
||||||
return c
|
return c
|
||||||
|
|
||||||
|
def save(self):
|
||||||
|
super().save()
|
||||||
|
# *Node.save() aktualizuje název *Nodu.
|
||||||
|
self.rocniknode.save()
|
||||||
|
|
||||||
def cislo_pdf_filename(self, filename):
|
def cislo_pdf_filename(self, filename):
|
||||||
rocnik = str(self.rocnik.rocnik)
|
rocnik = str(self.rocnik.rocnik)
|
||||||
|
@ -477,6 +481,11 @@ class Cislo(SeminarModelBase):
|
||||||
return None
|
return None
|
||||||
return c
|
return c
|
||||||
|
|
||||||
|
def save(self):
|
||||||
|
super().save()
|
||||||
|
# *Node.save() aktualizuje název *Nodu.
|
||||||
|
self.cislonode.save()
|
||||||
|
|
||||||
@reversion.register(ignore_duplicates=True)
|
@reversion.register(ignore_duplicates=True)
|
||||||
class Organizator(SeminarModelBase):
|
class Organizator(SeminarModelBase):
|
||||||
# zmena dedicnosti z models.Model na SeminarModelBase, potencialni vznik bugu
|
# zmena dedicnosti z models.Model na SeminarModelBase, potencialni vznik bugu
|
||||||
|
@ -707,6 +716,12 @@ class Tema(Problem):
|
||||||
return "t{}".format(self.kod)
|
return "t{}".format(self.kod)
|
||||||
return '<Není zadaný>'
|
return '<Není zadaný>'
|
||||||
|
|
||||||
|
def save(self):
|
||||||
|
super().save()
|
||||||
|
# *Node.save() aktualizuje název *Nodu.
|
||||||
|
for tvcn in self_temavcislenode_set:
|
||||||
|
tvcn.save()
|
||||||
|
|
||||||
class Clanek(Problem):
|
class Clanek(Problem):
|
||||||
class Meta:
|
class Meta:
|
||||||
db_table = 'seminar_clanky'
|
db_table = 'seminar_clanky'
|
||||||
|
@ -727,6 +742,11 @@ class Clanek(Problem):
|
||||||
return "c{}".format(self.kod)
|
return "c{}".format(self.kod)
|
||||||
return '<Není zadaný>'
|
return '<Není zadaný>'
|
||||||
|
|
||||||
|
def save(self):
|
||||||
|
super().save()
|
||||||
|
# *Node.save() aktualizuje název *Nodu.
|
||||||
|
self.claneknode.save()
|
||||||
|
|
||||||
class Text(SeminarModelBase):
|
class Text(SeminarModelBase):
|
||||||
class Meta:
|
class Meta:
|
||||||
db_table = 'seminar_texty'
|
db_table = 'seminar_texty'
|
||||||
|
@ -744,6 +764,12 @@ class Text(SeminarModelBase):
|
||||||
|
|
||||||
# obrázky mají návaznost opačným směrem (vazba z druhé strany)
|
# obrázky mají návaznost opačným směrem (vazba z druhé strany)
|
||||||
|
|
||||||
|
def save(self):
|
||||||
|
super().save()
|
||||||
|
# *Node.save() aktualizuje název *Nodu.
|
||||||
|
for tn in self.textnode_set:
|
||||||
|
tn.save()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Uloha(Problem):
|
class Uloha(Problem):
|
||||||
|
@ -778,6 +804,12 @@ class Uloha(Problem):
|
||||||
return name
|
return name
|
||||||
return '<Není zadaný>'
|
return '<Není zadaný>'
|
||||||
|
|
||||||
|
def save(self):
|
||||||
|
super().save()
|
||||||
|
# *Node.save() aktualizuje název *Nodu.
|
||||||
|
self.ulohazadaninode.save()
|
||||||
|
self.ulohavzoraknode.save()
|
||||||
|
|
||||||
|
|
||||||
@reversion.register(ignore_duplicates=True)
|
@reversion.register(ignore_duplicates=True)
|
||||||
class Reseni(SeminarModelBase):
|
class Reseni(SeminarModelBase):
|
||||||
|
@ -959,6 +991,10 @@ class Pohadka(SeminarModelBase):
|
||||||
uryvek = self.text if len(self.text) < 50 else self.text[:(50-3)]+"..."
|
uryvek = self.text if len(self.text) < 50 else self.text[:(50-3)]+"..."
|
||||||
return uryvek
|
return uryvek
|
||||||
|
|
||||||
|
def save(self):
|
||||||
|
super().save()
|
||||||
|
# *Node.save() aktualizuje název *Nodu.
|
||||||
|
self.pohadkanode.save()
|
||||||
|
|
||||||
@reversion.register(ignore_duplicates=True)
|
@reversion.register(ignore_duplicates=True)
|
||||||
class Soustredeni_Ucastnici(SeminarModelBase):
|
class Soustredeni_Ucastnici(SeminarModelBase):
|
||||||
|
@ -1069,6 +1105,11 @@ class Konfera(models.Model):
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "{}: ({})".format(self.nazev, self.soustredeni)
|
return "{}: ({})".format(self.nazev, self.soustredeni)
|
||||||
|
|
||||||
|
def save(self):
|
||||||
|
super().save()
|
||||||
|
# *Node.save() aktualizuje název *Nodu.
|
||||||
|
self.konferanode.save()
|
||||||
|
|
||||||
|
|
||||||
# Vazebna tabulka. Mozna se generuje automaticky.
|
# Vazebna tabulka. Mozna se generuje automaticky.
|
||||||
@reversion.register(ignore_duplicates=True)
|
@reversion.register(ignore_duplicates=True)
|
||||||
|
@ -1147,6 +1188,7 @@ class TreeNode(PolymorphicModel):
|
||||||
verbose_name = "TreeNode"
|
verbose_name = "TreeNode"
|
||||||
verbose_name_plural = "TreeNody"
|
verbose_name_plural = "TreeNody"
|
||||||
|
|
||||||
|
# TODO: Nechceme radši jako root vyžadovat přímo RocnikNode?
|
||||||
root = models.ForeignKey('TreeNode',
|
root = models.ForeignKey('TreeNode',
|
||||||
related_name="potomci_set",
|
related_name="potomci_set",
|
||||||
null = True,
|
null = True,
|
||||||
|
@ -1176,7 +1218,15 @@ class TreeNode(PolymorphicModel):
|
||||||
self.succ.print_tree(indent=indent)
|
self.succ.print_tree(indent=indent)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.nazev
|
if self.nazev:
|
||||||
|
return self.nazev
|
||||||
|
else:
|
||||||
|
#TODO: logování
|
||||||
|
return "Nepojmenovaný Treenode"
|
||||||
|
|
||||||
|
def save(self):
|
||||||
|
self.aktualizuj_nazev()
|
||||||
|
super().save()
|
||||||
|
|
||||||
class RocnikNode(TreeNode):
|
class RocnikNode(TreeNode):
|
||||||
class Meta:
|
class Meta:
|
||||||
|
@ -1186,8 +1236,9 @@ class RocnikNode(TreeNode):
|
||||||
rocnik = models.OneToOneField(Rocnik,
|
rocnik = models.OneToOneField(Rocnik,
|
||||||
on_delete = models.PROTECT, # Pokud chci mazat ročník, musím si Node pořešit ručně
|
on_delete = models.PROTECT, # Pokud chci mazat ročník, musím si Node pořešit ručně
|
||||||
verbose_name = "ročník")
|
verbose_name = "ročník")
|
||||||
def __str__(self):
|
|
||||||
return 'RocnikNode: '+str(self.rocnik)
|
def aktualizuj_nazev(self):
|
||||||
|
self.nazev = "RocnikNode: "+str(self.rocnik)
|
||||||
|
|
||||||
class CisloNode(TreeNode):
|
class CisloNode(TreeNode):
|
||||||
class Meta:
|
class Meta:
|
||||||
|
@ -1197,16 +1248,31 @@ class CisloNode(TreeNode):
|
||||||
cislo = models.OneToOneField(Cislo,
|
cislo = models.OneToOneField(Cislo,
|
||||||
on_delete = models.PROTECT, # Pokud chci mazat číslo, musím si Node pořešit ručně
|
on_delete = models.PROTECT, # Pokud chci mazat číslo, musím si Node pořešit ručně
|
||||||
verbose_name = "číslo")
|
verbose_name = "číslo")
|
||||||
def __str__(self):
|
|
||||||
return 'CisloNode: '+str(self.poradi)
|
def aktualizuj_nazev(self):
|
||||||
|
self.nazev = "CisloNode: "+str(self.cislo)
|
||||||
|
|
||||||
class MezicisloNode(TreeNode):
|
class MezicisloNode(TreeNode):
|
||||||
class Meta:
|
class Meta:
|
||||||
db_table = 'seminar_nodes_mezicislo'
|
db_table = 'seminar_nodes_mezicislo'
|
||||||
verbose_name = 'Mezičíslo (Node)'
|
verbose_name = 'Mezičíslo (Node)'
|
||||||
verbose_name_plural = 'Mezičísla (Node)'
|
verbose_name_plural = 'Mezičísla (Node)'
|
||||||
def __str__(self):
|
|
||||||
return 'MezicisloNode'
|
def aktualizuj_nazev(self):
|
||||||
|
if self.prev:
|
||||||
|
if (self.prev.get_real_instance_class() != CisloNode and
|
||||||
|
self.prev.get_real_instance_class() != MezicisloNode):
|
||||||
|
raise ValueError("Předchůdce není číslo!")
|
||||||
|
posledni = self.prev.cislo
|
||||||
|
self.nazev = "MezicisloNode: Mezičíslo po čísle"+str(posledni)
|
||||||
|
elif self.root:
|
||||||
|
if self.root.get_real_instance_class() != RocnikNode:
|
||||||
|
raise ValueError("Kořen stromu není ročník!")
|
||||||
|
rocnik = self.root.rocnik
|
||||||
|
self.nazev = "MezicisloNode: První mezičíslo ročníku "+str(rocnik)
|
||||||
|
else:
|
||||||
|
print("!!!!! Nějaké neidentifikované mezičíslo !!!!!")
|
||||||
|
self.nazev = "MezicisloNode: Neidentifikovatelné mezičíslo!"
|
||||||
|
|
||||||
class TemaVCisleNode(TreeNode):
|
class TemaVCisleNode(TreeNode):
|
||||||
""" Obsahuje příspěvky k tématu v daném čísle """
|
""" Obsahuje příspěvky k tématu v daném čísle """
|
||||||
|
@ -1217,8 +1283,9 @@ class TemaVCisleNode(TreeNode):
|
||||||
tema = models.ForeignKey(Tema,
|
tema = models.ForeignKey(Tema,
|
||||||
on_delete=models.PROTECT, # Pokud chci mazat téma, musím si Node pořešit ručně
|
on_delete=models.PROTECT, # Pokud chci mazat téma, musím si Node pořešit ručně
|
||||||
verbose_name = "téma v čísle")
|
verbose_name = "téma v čísle")
|
||||||
def __str__(self):
|
|
||||||
return 'TemaVCisleNode: tema: '+str(self.tema)
|
def aktualizuj_nazev(self):
|
||||||
|
self.nazev = "TemaVCisleNode: "+str(self.tema)
|
||||||
|
|
||||||
class KonferaNode(TreeNode):
|
class KonferaNode(TreeNode):
|
||||||
class Meta:
|
class Meta:
|
||||||
|
@ -1230,8 +1297,9 @@ class KonferaNode(TreeNode):
|
||||||
verbose_name = "konfera",
|
verbose_name = "konfera",
|
||||||
null=True,
|
null=True,
|
||||||
blank=False)
|
blank=False)
|
||||||
def __str__(self):
|
|
||||||
return 'KonferaNode: '+str(self.konfera)
|
def aktualizuj_nazev(self):
|
||||||
|
self.nazev = "KonferaNode: "+str(self.konfera)
|
||||||
|
|
||||||
class ClanekNode(TreeNode):
|
class ClanekNode(TreeNode):
|
||||||
class Meta:
|
class Meta:
|
||||||
|
@ -1243,8 +1311,9 @@ class ClanekNode(TreeNode):
|
||||||
verbose_name = "článek",
|
verbose_name = "článek",
|
||||||
null=True,
|
null=True,
|
||||||
blank=False)
|
blank=False)
|
||||||
def __str__(self):
|
|
||||||
return 'ClanekNode: '+str(self.clanek)
|
def aktualizuj_nazev(self):
|
||||||
|
self.nazev = "ClanekNode: "+str(self.clanek)
|
||||||
|
|
||||||
class UlohaZadaniNode(TreeNode):
|
class UlohaZadaniNode(TreeNode):
|
||||||
class Meta:
|
class Meta:
|
||||||
|
@ -1256,8 +1325,9 @@ class UlohaZadaniNode(TreeNode):
|
||||||
verbose_name = "úloha",
|
verbose_name = "úloha",
|
||||||
null=True,
|
null=True,
|
||||||
blank=False)
|
blank=False)
|
||||||
def __str__(self):
|
|
||||||
return 'UlohaZadaniNode: '+str(self.uloha)
|
def aktualizuj_nazev(self):
|
||||||
|
self.nazev = "UlohaZadaniNode: "+str(self.uloha)
|
||||||
|
|
||||||
class PohadkaNode(TreeNode):
|
class PohadkaNode(TreeNode):
|
||||||
class Meta:
|
class Meta:
|
||||||
|
@ -1268,8 +1338,9 @@ class PohadkaNode(TreeNode):
|
||||||
on_delete=models.PROTECT, # Pokud chci mazat pohádku, musím si Node pořešit ručně
|
on_delete=models.PROTECT, # Pokud chci mazat pohádku, musím si Node pořešit ručně
|
||||||
verbose_name = "pohádka",
|
verbose_name = "pohádka",
|
||||||
)
|
)
|
||||||
def __str__(self):
|
|
||||||
return 'PohadkaNode: '+str(self.pohadka)
|
def aktualizuj_nazev(self):
|
||||||
|
self.nazev = "PohadkaNode: "+str(self.pohadka)
|
||||||
|
|
||||||
class UlohaVzorakNode(TreeNode):
|
class UlohaVzorakNode(TreeNode):
|
||||||
class Meta:
|
class Meta:
|
||||||
|
@ -1281,8 +1352,9 @@ class UlohaVzorakNode(TreeNode):
|
||||||
verbose_name = "úloha",
|
verbose_name = "úloha",
|
||||||
null=True,
|
null=True,
|
||||||
blank=False)
|
blank=False)
|
||||||
def __str__(self):
|
|
||||||
return 'UlohaVzorakNode: '+str(self.uloha)
|
def aktualizuj_nazev(self):
|
||||||
|
self.nazev = "UlohaVzorakNode: "+str(self.uloha)
|
||||||
|
|
||||||
class TextNode(TreeNode):
|
class TextNode(TreeNode):
|
||||||
class Meta:
|
class Meta:
|
||||||
|
@ -1292,8 +1364,9 @@ class TextNode(TreeNode):
|
||||||
text = models.ForeignKey(Text,
|
text = models.ForeignKey(Text,
|
||||||
on_delete=models.PROTECT,
|
on_delete=models.PROTECT,
|
||||||
verbose_name = 'text')
|
verbose_name = 'text')
|
||||||
def __str__(self):
|
|
||||||
return 'TextNode: '+str(self.text)
|
def aktualizuj_nazev(self):
|
||||||
|
self.nazev = "TextNode: "+str(self.text)
|
||||||
|
|
||||||
## FIXME: Logiku přesunout do views.
|
## FIXME: Logiku přesunout do views.
|
||||||
#class VysledkyBase(SeminarModelBase):
|
#class VysledkyBase(SeminarModelBase):
|
||||||
|
|
Loading…
Reference in a new issue