File: clear_index.py

package info (click to toggle)
django-haystack 3.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,504 kB
  • sloc: python: 23,475; xml: 1,708; sh: 74; makefile: 71
file content (67 lines) | stat: -rw-r--r-- 2,265 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from django.core.management.base import BaseCommand

from haystack import connections


class Command(BaseCommand):
    help = "Clears out the search index completely."  # noqa A003

    def add_arguments(self, parser):
        parser.add_argument(
            "--noinput",
            action="store_false",
            dest="interactive",
            default=True,
            help="If provided, no prompts will be issued to the user and the data will be wiped out.",
        )
        parser.add_argument(
            "-u",
            "--using",
            action="append",
            default=[],
            help="Update only the named backend (can be used multiple times). "
            "By default all backends will be updated.",
        )
        parser.add_argument(
            "--nocommit",
            action="store_false",
            dest="commit",
            default=True,
            help="Will pass commit=False to the backend.",
        )

    def handle(self, **options):
        """Clears out the search index completely."""
        self.verbosity = int(options.get("verbosity", 1))
        self.commit = options.get("commit", True)

        using = options.get("using")
        if not using:
            using = connections.connections_info.keys()

        if options.get("interactive", True):
            self.stdout.write(
                "WARNING: This will irreparably remove EVERYTHING from your search index in connection '%s'."
                % "', '".join(using)
            )
            self.stdout.write(
                "Your choices after this are to restore from backups or rebuild via the `rebuild_index` command."
            )

            yes_or_no = input("Are you sure you wish to continue? [y/N] ")

            if not yes_or_no.lower().startswith("y"):
                self.stdout.write("No action taken.")
                return

        if self.verbosity >= 1:
            self.stdout.write(
                "Removing all documents from your index because you said so."
            )

        for backend_name in using:
            backend = connections[backend_name].get_backend()
            backend.clear(commit=self.commit)

        if self.verbosity >= 1:
            self.stdout.write("All documents removed.")