search_contact_index.py 1.73 KB
Newer Older
1 2 3 4
# -*- encoding: utf-8 -*-
from django.core.management.base import BaseCommand

from contact.search import ContactIndex
5
from search.models import SearchFormat
6 7 8 9 10 11 12 13 14 15 16 17 18 19
from search.search import SearchIndex


class Command(BaseCommand):

    help = "Query (search) contact index"

    def add_arguments(self, parser):
        parser.add_argument("criteria", nargs="+", type=str)

    def handle(self, *args, **options):
        self.stdout.write("{}".format(self.help))
        index = SearchIndex(ContactIndex())
        for criteria in options["criteria"]:
20 21 22
            result, total = index.search(
                criteria, data_format=SearchFormat.ATTR, page_size=100
            )
23
            self.stdout.write(
24 25 26 27 28 29 30
                "\nFound {} records searching for '{}'\n\n".format(
                    total, criteria
                )
            )
            self.stdout.write(
                "{:20.18} {:40.38} {:10.8} {:20.18} {}".format(
                    "name", "address", "postcode", "email", "score"
31 32 33 34 35 36 37
                )
            )
            self.stdout.write(
                (
                    "-------------------- "
                    "---------------------------------------- "
                    "---------- "
38 39
                    "-------------------- "
                    "-----------"
40 41 42 43
                )
            )
            for row in result:
                self.stdout.write(
44
                    "{:20.18} {:40.38} {:10.8} {:20.18} {}".format(
45 46 47 48
                        row.data.name,
                        row.data.address,
                        row.data.postcode,
                        row.data.email,
49
                        row.score,
50 51 52
                    )
                )
        self.stdout.write("{} - Complete".format(self.help))