prepare_lexeme_lists.py 1.12 KB
# -*- coding: utf-8 -*-
from django.contrib.auth.models import AnonymousUser
from django.core.management.base import BaseCommand
from django.db.transaction import atomic

from common.util import json_encode
from dictionary.ajax_lexeme_slickgrid import LexemeQuery
from dictionary.models import visible_vocabularies, LexemeList


class Command(BaseCommand):
    help = "Prepare lexeme lists for the reader view."

    filter = {"group_op": "AND", "rules": []}
    vocabularies = sorted(
        visible_vocabularies(AnonymousUser()).values_list('id', flat=True))

    @atomic
    def handle(self, *args, **options):
        for a_fronte in (True, False):
            LexemeList.objects.get_or_create(
                filter=json_encode(self.filter), a_fronte=a_fronte,
                vocabularies=json_encode(self.vocabularies))
            sort_rules = ['a_fronte' if a_fronte else 'a_tergo']
            query = LexemeQuery({
                'filter': self.filter,
                'sort_rules': sort_rules,
                'visible_vocabs': self.vocabularies,
                'reader': True
            })
            query.reload_lexeme_list()