2020-10-25 23:03:02 +01:00
|
|
|
from django.core.management import BaseCommand
|
|
|
|
|
from whoosh.writing import AsyncWriter
|
|
|
|
|
|
|
|
|
|
import documents.index as index
|
|
|
|
|
from documents.mixins import Renderable
|
|
|
|
|
from documents.models import Document
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Command(Renderable, BaseCommand):
|
|
|
|
|
|
2020-10-27 17:08:18 +01:00
|
|
|
help = "Manages the document index."
|
2020-10-25 23:03:02 +01:00
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
self.verbosity = 0
|
|
|
|
|
BaseCommand.__init__(self, *args, **kwargs)
|
|
|
|
|
|
2020-10-27 17:08:18 +01:00
|
|
|
def add_arguments(self, parser):
|
|
|
|
|
parser.add_argument("command", choices=['reindex', 'optimize'])
|
|
|
|
|
|
2020-10-25 23:03:02 +01:00
|
|
|
def handle(self, *args, **options):
|
|
|
|
|
|
|
|
|
|
self.verbosity = options["verbosity"]
|
|
|
|
|
|
2020-10-27 17:08:18 +01:00
|
|
|
if options['command'] == 'reindex':
|
|
|
|
|
documents = Document.objects.all()
|
|
|
|
|
|
|
|
|
|
ix = index.open_index(recreate=True)
|
2020-10-25 23:03:02 +01:00
|
|
|
|
2020-10-27 17:08:18 +01:00
|
|
|
with AsyncWriter(ix) as writer:
|
|
|
|
|
for document in documents:
|
|
|
|
|
index.update_document(writer, document)
|
2020-10-25 23:03:02 +01:00
|
|
|
|
2020-10-27 17:08:18 +01:00
|
|
|
elif options['command'] == 'optimize':
|
|
|
|
|
index.open_index().optimize()
|