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)
|
||||
return c
|
||||
|
||||
def save(self):
|
||||
super().save()
|
||||
# *Node.save() aktualizuje název *Nodu.
|
||||
self.rocniknode.save()
|
||||
|
||||
def cislo_pdf_filename(self, filename):
|
||||
rocnik = str(self.rocnik.rocnik)
|
||||
|
@ -477,6 +481,11 @@ class Cislo(SeminarModelBase):
|
|||
return None
|
||||
return c
|
||||
|
||||
def save(self):
|
||||
super().save()
|
||||
# *Node.save() aktualizuje název *Nodu.
|
||||
self.cislonode.save()
|
||||
|
||||
@reversion.register(ignore_duplicates=True)
|
||||
class Organizator(SeminarModelBase):
|
||||
# zmena dedicnosti z models.Model na SeminarModelBase, potencialni vznik bugu
|
||||
|
@ -707,6 +716,12 @@ class Tema(Problem):
|
|||
return "t{}".format(self.kod)
|
||||
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 Meta:
|
||||
db_table = 'seminar_clanky'
|
||||
|
@ -727,6 +742,11 @@ class Clanek(Problem):
|
|||
return "c{}".format(self.kod)
|
||||
return '<Není zadaný>'
|
||||
|
||||
def save(self):
|
||||
super().save()
|
||||
# *Node.save() aktualizuje název *Nodu.
|
||||
self.claneknode.save()
|
||||
|
||||
class Text(SeminarModelBase):
|
||||
class Meta:
|
||||
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)
|
||||
|
||||
def save(self):
|
||||
super().save()
|
||||
# *Node.save() aktualizuje název *Nodu.
|
||||
for tn in self.textnode_set:
|
||||
tn.save()
|
||||
|
||||
|
||||
|
||||
class Uloha(Problem):
|
||||
|
@ -778,6 +804,12 @@ class Uloha(Problem):
|
|||
return name
|
||||
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)
|
||||
class Reseni(SeminarModelBase):
|
||||
|
@ -959,6 +991,10 @@ class Pohadka(SeminarModelBase):
|
|||
uryvek = self.text if len(self.text) < 50 else self.text[:(50-3)]+"..."
|
||||
return uryvek
|
||||
|
||||
def save(self):
|
||||
super().save()
|
||||
# *Node.save() aktualizuje název *Nodu.
|
||||
self.pohadkanode.save()
|
||||
|
||||
@reversion.register(ignore_duplicates=True)
|
||||
class Soustredeni_Ucastnici(SeminarModelBase):
|
||||
|
@ -1069,6 +1105,11 @@ class Konfera(models.Model):
|
|||
def __str__(self):
|
||||
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.
|
||||
@reversion.register(ignore_duplicates=True)
|
||||
|
@ -1147,6 +1188,7 @@ class TreeNode(PolymorphicModel):
|
|||
verbose_name = "TreeNode"
|
||||
verbose_name_plural = "TreeNody"
|
||||
|
||||
# TODO: Nechceme radši jako root vyžadovat přímo RocnikNode?
|
||||
root = models.ForeignKey('TreeNode',
|
||||
related_name="potomci_set",
|
||||
null = True,
|
||||
|
@ -1176,7 +1218,15 @@ class TreeNode(PolymorphicModel):
|
|||
self.succ.print_tree(indent=indent)
|
||||
|
||||
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 Meta:
|
||||
|
@ -1186,8 +1236,9 @@ class RocnikNode(TreeNode):
|
|||
rocnik = models.OneToOneField(Rocnik,
|
||||
on_delete = models.PROTECT, # Pokud chci mazat ročník, musím si Node pořešit ručně
|
||||
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 Meta:
|
||||
|
@ -1197,16 +1248,31 @@ class CisloNode(TreeNode):
|
|||
cislo = models.OneToOneField(Cislo,
|
||||
on_delete = models.PROTECT, # Pokud chci mazat číslo, musím si Node pořešit ručně
|
||||
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 Meta:
|
||||
db_table = 'seminar_nodes_mezicislo'
|
||||
verbose_name = 'Mezičíslo (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):
|
||||
""" Obsahuje příspěvky k tématu v daném čísle """
|
||||
|
@ -1217,8 +1283,9 @@ class TemaVCisleNode(TreeNode):
|
|||
tema = models.ForeignKey(Tema,
|
||||
on_delete=models.PROTECT, # Pokud chci mazat téma, musím si Node pořešit ručně
|
||||
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 Meta:
|
||||
|
@ -1230,8 +1297,9 @@ class KonferaNode(TreeNode):
|
|||
verbose_name = "konfera",
|
||||
null=True,
|
||||
blank=False)
|
||||
def __str__(self):
|
||||
return 'KonferaNode: '+str(self.konfera)
|
||||
|
||||
def aktualizuj_nazev(self):
|
||||
self.nazev = "KonferaNode: "+str(self.konfera)
|
||||
|
||||
class ClanekNode(TreeNode):
|
||||
class Meta:
|
||||
|
@ -1243,8 +1311,9 @@ class ClanekNode(TreeNode):
|
|||
verbose_name = "článek",
|
||||
null=True,
|
||||
blank=False)
|
||||
def __str__(self):
|
||||
return 'ClanekNode: '+str(self.clanek)
|
||||
|
||||
def aktualizuj_nazev(self):
|
||||
self.nazev = "ClanekNode: "+str(self.clanek)
|
||||
|
||||
class UlohaZadaniNode(TreeNode):
|
||||
class Meta:
|
||||
|
@ -1256,8 +1325,9 @@ class UlohaZadaniNode(TreeNode):
|
|||
verbose_name = "úloha",
|
||||
null=True,
|
||||
blank=False)
|
||||
def __str__(self):
|
||||
return 'UlohaZadaniNode: '+str(self.uloha)
|
||||
|
||||
def aktualizuj_nazev(self):
|
||||
self.nazev = "UlohaZadaniNode: "+str(self.uloha)
|
||||
|
||||
class PohadkaNode(TreeNode):
|
||||
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ě
|
||||
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 Meta:
|
||||
|
@ -1281,8 +1352,9 @@ class UlohaVzorakNode(TreeNode):
|
|||
verbose_name = "úloha",
|
||||
null=True,
|
||||
blank=False)
|
||||
def __str__(self):
|
||||
return 'UlohaVzorakNode: '+str(self.uloha)
|
||||
|
||||
def aktualizuj_nazev(self):
|
||||
self.nazev = "UlohaVzorakNode: "+str(self.uloha)
|
||||
|
||||
class TextNode(TreeNode):
|
||||
class Meta:
|
||||
|
@ -1292,8 +1364,9 @@ class TextNode(TreeNode):
|
|||
text = models.ForeignKey(Text,
|
||||
on_delete=models.PROTECT,
|
||||
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.
|
||||
#class VysledkyBase(SeminarModelBase):
|
||||
|
|
Loading…
Reference in a new issue