create_pattern_examples.py 1.08 KB
# -*- coding: utf-8 -*-

from django.db import transaction
from django.core.management.base import BaseCommand
from dictionary.models import Pattern, PatternExample


class Command(BaseCommand):
    args = 'none'
    help = 'Creates forms for filtering'

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


def create_examples():
    transaction.enter_transaction_management()
    for pattern in Pattern.objects.all():
        gender_ids = pattern.lexemeinflectionpattern_set.values_list(
            'gender', flat=True).distinct()
        for gender_id in gender_ids:
            if gender_id is not None:
                existing_example = PatternExample.objects.filter(
                    pattern=pattern, gender_id=gender_id)
                if not existing_example:
                    lexeme = pattern.lexemeinflectionpattern_set.filter(
                        gender_id=gender_id)[0].lexeme
                    PatternExample.objects.create(
                        lexeme=lexeme, pattern=pattern, gender_id=gender_id)
    transaction.commit()
    transaction.leave_transaction_management()