export_templates.py 1.14 KB
# -*- coding: utf-8 -*-
from django.core.management.base import BaseCommand
from common.util import uniprint, json_encode
from dictionary.management.commands.export_template import export_template
from dictionary.models import TableTemplate


class Command(BaseCommand):
    help = "Exports all templates to JSON."

    def handle(self, *args, **options):
        uniprint(json_encode(export_templates()))


def export_templates():
    data = []
    for tt in TableTemplate.objects.all():
        data_row = export_template(tt)
        data_row['parts_of_speech'] = list(
            tt.parts_of_speech.values_list('symbol', flat=True))
        data_row['pattern_types'] = list(
            tt.pattern_types.values_list('symbol', 'lexical_class_id'))
        data_row['attributes'] = list(
            tt.attributes.values_list('name', flat=True))
        data_row['attribute_values'] = list(
            tt.attribute_values.values_list('value', 'attribute__name'))
        data_row['cell_attributes'] = list(
            tt.cell_attributes.values_list('name', flat=True))
        data_row['takes_gender'] = tt.takes_gender
        data.append(data_row)
    return data