create_classifications.py 2.08 KB
#-*- coding:utf-8 -*-

from django.core.management.base import BaseCommand
from dictionary.models import Classification, ClassificationValue


class Command(BaseCommand):
    args = 'none'
    help = 'Creates default classifications'

    def handle(self, **options):
        create_classifications()

CLASSIFICATION_DATA = {
    u'pospolitość': (
        ('subst', 'skrs'),
        (
            (u'pospolita', None),
            (u'własna', (
                (u'osoba', (
                    (u'imię', None),
                    (u'nazwisko', (
                        (u'człon nazwiska', None),
                        (u'człon nazwiska (herb)', None),
                        (u'sufiks nazwiska', None),
                    )),
                    (u'patronimicum', None),
                    (u'przydomek', None),
                    (u'pseudonim', None),
                )),
                (u'geograficzna', None),
                (u'organizacja', None),
                (u'instytucja', None),
                (u'firma', None),
                (u'tytuł', None),
                (u'astr.', None),
                (u'święto', None),
                (u'język programowania', None),
                (u'marka', None),
                (u'środek lokomocji', None),
                (u'budowla', None),
                (u'członek rodu', None),
                (u'oprogramowanie', None),
                (u'krój pisma', None),
                (u'człon nazwy firmy', None),
            )),
        )
    ),
}


def add_values(cl, value_tree, parent=None):
    for label, subtree in value_tree:
        cl_value = ClassificationValue.objects.create(
            label=label, parent_node=parent, classification=cl)
        if subtree is not None:
            add_values(cl, subtree, cl_value)

def create_classifications():
    Classification.objects.all().delete()
    ClassificationValue.objects.all().delete()
    for name, data in CLASSIFICATION_DATA.iteritems():
        poses, value_tree = data
        cl = Classification.objects.create(name=name)
        cl.parts_of_speech = poses
        add_values(cl, value_tree)