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

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


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))


    def handle(self, *args, **options):
        transaction.enter_transaction_management()
        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(self.vocabularies, self.filter, sort_rules)
            query.reload_lexeme_list()
        transaction.commit()
        transaction.leave_transaction_management()