Added in uninstall command.

This commit is contained in:
Dave Hall 2011-09-08 16:53:07 +01:00
parent 9c9a4f5c6f
commit 266e8291f8
6 changed files with 76 additions and 7 deletions

View file

@ -15,15 +15,17 @@ class WatsonSearchChangeList(ChangeList):
def __init__(self, request, model, list_display, list_display_links, list_filter, date_hierarchy, search_fields, list_select_related, list_per_page, list_editable, model_admin):
"""Initializes the search engine."""
# Clear the search fields.
search_fields = ()
# Initialize the change list.
super(WatsonSearchChangeList, self).__init__(request, model, list_display, list_display_links, list_filter, date_hierarchy, search_fields, list_select_related, list_per_page, list_editable, model_admin)
def get_query_set(self):
"""Creates the query set."""
# Do the basic searching.
qs = super(WatsonSearchChangeList, self).get_query_set()
search_fields = self.search_fields
self.search_fields = ()
try:
qs = super(WatsonSearchChangeList, self).get_query_set()
finally:
self.search_fields = search_fields
# Do the full text searching.
if self.query.strip():
qs = self.model_admin.search_engine.filter(qs, self.query, ranking=False)

View file

@ -22,8 +22,12 @@ class SearchBackend(object):
"""Base class for all search backends."""
def is_installed(self):
"""Checks whether django-watson is installed."""
return True
def do_install(self):
"""Generates the SQL needed to install django-watson."""
"""Executes the SQL needed to install django-watson."""
pass
supports_ranking = False
@ -73,8 +77,17 @@ class PostgresSearchBackend(SearchBackend):
"""A search backend that uses native PostgreSQL full text indices."""
def is_installed(self):
"""Checks whether django-watson is installed."""
cursor = connection.cursor()
cursor.execute("""
SELECT attname FROM pg_attribute
WHERE attrelid = (SELECT oid FROM pg_class WHERE relname = 'watson_searchentry') AND attname = 'search_tsv';
""")
return bool(cursor.fetchall())
def do_install(self):
"""Generates the PostgreSQL specific SQL code to install django-watson."""
"""Executes the PostgreSQL specific SQL code to install django-watson."""
connection.cursor().execute("""
-- Ensure that plpgsql is installed.
CREATE OR REPLACE FUNCTION make_plpgsql() RETURNS VOID LANGUAGE SQL AS
@ -109,6 +122,16 @@ class PostgresSearchBackend(SearchBackend):
CREATE TRIGGER watson_searchentry_trigger BEFORE INSERT OR UPDATE
ON watson_searchentry FOR EACH ROW EXECUTE PROCEDURE watson_searchentry_trigger_handler();
""")
def do_uninstall(self):
"""Executes the PostgreSQL specific SQL code to uninstall django-watson."""
connection.cursor().execute("""
ALTER TABLE watson_searchentry DROP COLUMN search_tsv;
DROP TRIGGER watson_searchentry_trigger ON watson_searchentry;
DROP FUNCTION watson_searchentry_trigger_handler();
""")
supports_ranking = True

View file

@ -15,4 +15,10 @@ class Command(NoArgsCommand):
"""Runs the management command."""
verbosity = int(options.get("verbosity", 1))
backend = get_backend()
install_sql = backend.do_install()
if backend.is_installed():
if verbosity >= 2:
self.stdout.write("django-watson is already installed.\n")
else:
backend.do_install()
if verbosity >= 2:
self.stdout.write("django-watson has been successfully installed.\n")

View file

@ -0,0 +1,24 @@
"""Destroys the database indices needed by django-watson."""
from django.core.management.base import NoArgsCommand
from django.db import transaction
from watson.registration import get_backend
class Command(NoArgsCommand):
help = "Destroys the database indices needed by django-watson."
@transaction.commit_on_success
def handle_noargs(self, **options):
"""Runs the management command."""
verbosity = int(options.get("verbosity", 1))
backend = get_backend()
if backend.is_installed():
backend.do_uninstall()
if verbosity >= 2:
self.stdout.write("django-watson has been successfully uninstalled.\n")
else:
if verbosity >= 2:
self.stdout.write("django-watson is not installed.\n")

View file

@ -16,6 +16,8 @@ class Migration(DataMigration):
def backwards(self, orm):
"Write your backwards methods here."
call_command("uninstallwatson", verbosity=0)
models = {

View file

@ -15,6 +15,7 @@ from django.conf.urls.defaults import *
from django.conf import settings
from django.contrib import admin
from django.contrib.auth.models import User
from django.http import HttpResponseNotFound, HttpResponseServerError
from django import template
from django.utils import simplejson as json
@ -99,6 +100,9 @@ class SearchTestBase(TestCase):
@watson.update_index()
def setUp(self):
# If migrations are off, then this is needed to get the indices installed. It has to
# be called in the setUp() method, but multiple invocations should be safe.
call_command("installwatson", verbosity=0)
# Remove all the current registered models.
self.registered_models = watson.get_registered_models()
for model in self.registered_models:
@ -479,6 +483,14 @@ urlpatterns = patterns("",
)
def handler404(request):
return HttpResponseNotFound("Not found")
def handler500(request):
return HttpResponseServerError("Server error")
class AdminIntegrationTest(SearchTestBase):
urls = "watson.tests"