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

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

from dictionary.models import PatternExample, Gender
from patterns.models import Pattern


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

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


@atomic
def create_examples():
    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))