create_pattern_examples.py 940 Bytes
# -*- coding: utf-8 -*-

from django.db import transaction
from django.core.management.base import BaseCommand

from dictionary.models import Pattern, PatternExample, Gender


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:
                    pattern.create_example(Gender.objects.get(id=gender_id))
    transaction.commit()
    transaction.leave_transaction_management()