Revert "use ES to serve personas search results (bug 726847)"

This reverts commit 9d0dcd6408.
This commit is contained in:
Chris Van 2012-02-29 16:04:58 -08:00
Родитель 52f332265a
Коммит 8d6ab9be28
3 изменённых файлов: 35 добавлений и 220 удалений

Просмотреть файл

@ -321,7 +321,7 @@ def addon_factory(version_kw={}, file_kw={}, **kw):
for key, value in kw.items():
setattr(a, key, value)
if type_ == amo.ADDON_PERSONA:
a.type = type_
a.update(type=type_)
Persona.objects.create(addon_id=a.id, persona_id=a.id)
a.save()
return a

Просмотреть файл

@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
import json
import sys
import urlparse
from django.conf import settings
@ -19,7 +20,6 @@ from addons.models import Addon, AddonCategory, Category, Persona
from search import views
from search.tests import SphinxTestCase
from search.utils import floor_version
from search.views import DEFAULT_NUM_PERSONAS
from tags.models import Tag
from versions.compare import num as vnum, version_int as vint, MAXVERSION
from versions.models import ApplicationsVersions
@ -163,32 +163,17 @@ class TestSearchboxTarget(amo.tests.ESTestCase):
reverse('apps.search'))
class SearchBase(amo.tests.ESTestCase):
def check_sort_links(self, key, title=None, sort_by=None, reverse=True):
r = self.client.get(self.url, dict(sort=key))
eq_(r.status_code, 200)
doc = pq(r.content)
if title:
if hasattr(self, 'MOBILE'):
menu = doc('#sort-menu')
eq_(menu.find('span').text(), title)
eq_(menu.find('.selected').text(), title)
else:
eq_(doc('#sorter .selected').text(), title)
if sort_by:
a = r.context['pager'].object_list
eq_(list(a), sorted(a, key=lambda x: getattr(x, sort_by),
reverse=reverse))
class TestESSearch(SearchBase):
class TestESSearch(amo.tests.ESTestCase):
fixtures = ['base/apps', 'base/category', 'tags/tags']
@classmethod
def setUpClass(cls):
super(TestESSearch, cls).setUpClass()
cls.setUpIndex()
def setUp(self):
self.url = reverse('search.search')
self.search_views = ('search.search', 'apps.search')
self.setUpIndex()
self.addons = Addon.objects.filter(status=amo.STATUS_PUBLIC,
disabled_by_user=False)
for addon in self.addons:
@ -227,6 +212,21 @@ class TestESSearch(SearchBase):
assert 'sort=users' not in sorter.text(), (
'Sort by "Most Users" should not appear for search tools.')
def check_sort_links(self, key, title, sort_by=None, reverse=True):
r = self.client.get(self.url, dict(sort=key))
eq_(r.status_code, 200)
doc = pq(r.content)
if hasattr(self, 'MOBILE'):
menu = doc('#sort-menu')
eq_(menu.find('span').text(), title)
eq_(menu.find('.selected').text(), title)
else:
eq_(doc('#sorter .selected').text(), title)
if sort_by:
a = r.context['pager'].object_list
eq_(list(a), sorted(a, key=lambda x: getattr(x, sort_by),
reverse=reverse))
def test_results_sort_default(self):
self.check_sort_links(None, 'Relevance', 'weekly_downloads')
@ -613,164 +613,6 @@ class TestESSearch(SearchBase):
eq_(self.get_results(r), themes)
class TestPersonasSearch(amo.tests.ESTestCase):
fixtures = ['base/apps', 'base/category', 'tags/tags']
#fixtures = ['base/apps']
def check_sort_links(self, key, title=None, sort_by=None, reverse=True):
r = self.client.get(self.url, dict(sort=key))
eq_(r.status_code, 200)
doc = pq(r.content)
if title:
if hasattr(self, 'MOBILE'):
menu = doc('#sort-menu')
eq_(menu.find('span').text(), title)
eq_(menu.find('.selected').text(), title)
else:
eq_(doc('#sorter .selected').text(), title)
if sort_by:
a = r.context['pager'].object_list
eq_(list(a), sorted(a, key=lambda x: getattr(x, sort_by),
reverse=reverse))
def setUp(self):
waffle.models.Switch.objects.create(name='replace-sphinx', active=True)
self.url = urlparams(reverse('search.search'), atype=amo.ADDON_PERSONA)
# Index the extensions created in `setUpClass`.
self.setUpIndex()
# Add some public personas.
self.personas = []
for status in amo.REVIEWED_STATUSES:
self.personas.append(
amo.tests.addon_factory(type=amo.ADDON_PERSONA, status=status))
# Add some unreviewed personas.
for status in set(amo.STATUS_CHOICES) - set(amo.REVIEWED_STATUSES):
amo.tests.addon_factory(type=amo.ADDON_PERSONA, status=status)
self.refresh()
def test_sort_order_default(self):
self.check_sort_links(None, sort_by='weekly_downloads')
def test_sort_order_unknown(self):
self.check_sort_links('xxx')
def test_sort_order_users(self):
self.check_sort_links('users', sort_by='average_daily_users')
def test_sort_order_rating(self):
self.check_sort_links('rating', sort_by='bayesian_rating')
def test_sort_order_newest(self):
self.check_sort_links('created', sort_by='created')
def get_results(self, r):
"""Return pks of add-ons shown on search results page."""
return sorted(a.pk for a in r.context['pager'].object_list)
def test_heading(self):
r = self.client.get(self.url)
eq_(r.status_code, 200)
eq_(pq(r.content)('.results-count strong').text(), None)
r = self.client.get(self.url + '&q=ballin')
eq_(r.status_code, 200)
eq_(pq(r.content)('.results-count strong').text(), 'ballin')
def test_results_blank_query(self):
# Not to be confused with PersonaID.
personas_ids = sorted(p.id for p in self.personas)
extensions_ids = sorted(Addon.objects
.filter(type=amo.ADDON_EXTENSION,
status__in=amo.REVIEWED_STATUSES,
disabled_by_user=False)
.values_list('id', flat=True))
# Personas should show only personas.
r = self.client.get(self.url, follow=True)
eq_(r.status_code, 200)
eq_(self.get_results(r), personas_ids)
eq_(pq(r.content)('.personas-grid li').length, len(personas_ids))
# Extensions should show only extensions.
r = self.client.get(reverse('search.search'),
dict(atype=amo.ADDON_EXTENSION))
eq_(r.status_code, 200)
eq_(self.get_results(r), extensions_ids)
def check_name_results(self, params, expected):
r = self.client.get(urlparams(self.url, **params), follow=True)
eq_(r.status_code, 200)
got = self.get_results(r)
eq_(got, expected,
'Got %s. Expected these for %s: %s' % (got, params, expected))
def test_results_name_query(self):
p1 = self.personas[0]
p1.name = 'Harry Potter'
p1.save()
p2 = self.personas[1]
p2.name = 'The Life Aquatic with SeaVan'
p2.save()
self.refresh()
# Empty search term should return everything.
self.check_name_results({'q': ''}, sorted(p.id for p in self.personas))
# Garbage search terms should return nothing.
for term in ('xxx', 'garbage', '£'):
self.check_name_results({'q': term}, [])
# Try to match 'Harry Potter'.
for term in ('harry', 'potter', 'har', 'pot', 'harry pooper'):
self.check_name_results({'q': term}, [p1.pk])
# Try to match 'The Life Aquatic with SeaVan'.
for term in ('life', 'aquatic', 'seavan', 'sea van'):
self.check_name_results({'q': term}, [p2.pk])
def test_results_applications(self):
expected = sorted(p.id for p in self.personas)
# Persona results should not filter on `appver` nor `platform`.
permutations = [
{},
{'appver': amo.FIREFOX.id},
{'appver': amo.THUNDERBIRD.id},
{'platform': amo.PLATFORM_MAC.id},
{'appver': amo.SEAMONKEY.id, 'platform': amo.PLATFORM_WIN.id},
]
for p in permutations:
self.check_name_results(dict(q='', **p), expected)
# Now ensure we get the same results for Firefox as for Thunderbird.
self.url = self.url.replace('firefox', 'thunderbird')
self.check_name_results({}, expected)
def test_pagination(self):
# Generate some (22) personas to get us to two pages.
left_to_add = DEFAULT_NUM_PERSONAS - len(self.personas) + 1
for x in xrange(left_to_add):
self.personas.append(
amo.tests.addon_factory(type=amo.ADDON_PERSONA))
self.refresh()
# Page one should show 21 personas.
r = self.client.get(self.url, follow=True)
eq_(r.status_code, 200)
eq_(pq(r.content)('.personas-grid li').length, DEFAULT_NUM_PERSONAS)
# Page two should show 1 persona.
r = self.client.get(self.url + '&page=2', follow=True)
eq_(r.status_code, 200)
eq_(pq(r.content)('.personas-grid li').length, 1)
def test_search_redirects():
changes = (
('q=yeah&sort=newest', 'q=yeah&sort=updated'),

Просмотреть файл

@ -8,9 +8,8 @@ from django.utils import translation
import commonware.log
import jingo
from mobility.decorators import mobile_template
from tower import ugettext as _
import waffle
from mobility.decorators import mobile_template
import amo
import bandwagon.views
@ -26,8 +25,7 @@ from . import forms
from .client import SearchError, CollectionsClient, PersonasClient
from .forms import SecondarySearchForm, ESSearchForm
DEFAULT_NUM_COLLECTIONS = 20
DEFAULT_NUM_PERSONAS = 21 # Results appear in a grid of 3 personas x 7 rows.
DEFAULT_NUM_RESULTS = 20
log = commonware.log.getLogger('z.search')
@ -204,47 +202,22 @@ def _get_sorts(request, sort):
def _personas(request):
"""Handle the request for persona searches."""
if waffle.switch_is_active('replace-sphinx'):
initial = dict(request.GET.items())
# Ignore these filters since return the same results for Firefox
# as for Thunderbird, etc.
initial.update(appver=None, platform=None)
form = SecondarySearchForm(request.GET or {})
form.is_valid()
form = ESSearchForm(initial, type=amo.ADDON_PERSONA)
form.is_valid()
query = form.cleaned_data.get('q', '')
qs = Addon.search().filter(status__in=amo.REVIEWED_STATUSES,
is_disabled=False)
filters = ['sort', 'platform']
mapping = {'users': '-average_daily_users',
'rating': '-bayesian_rating',
'created': '-created',
'name': 'name_sort',
'updated': '-last_updated',
'hotness': '-hotness'}
results = _filter_search(request, qs, form.cleaned_data, filters,
mapping, [amo.ADDON_PERSONA])
else:
# Set the default number of personas per page.
initial = {'pp': DEFAULT_NUM_PERSONAS}
initial.update(request.GET)
form = SecondarySearchForm(initial)
form.is_valid()
query = form.cleaned_data.get('q', '')
query = form.data.get('q', '')
search_opts = {}
search_opts['limit'] = form.cleaned_data.get('pp', DEFAULT_NUM_PERSONAS)
search_opts['limit'] = form.cleaned_data.get('pp', DEFAULT_NUM_RESULTS)
page = form.cleaned_data.get('page') or 1
search_opts['offset'] = (page - 1) * search_opts['limit']
if not waffle.switch_is_active('replace-sphinx'):
try:
results = PersonasClient().query(query, **search_opts)
except SearchError:
return jingo.render(request, 'search/down.html', {}, status=503)
try:
results = PersonasClient().query(query, **search_opts)
except SearchError:
return jingo.render(request, 'search/down.html', {}, status=503)
pager = amo.utils.paginate(request, results, per_page=search_opts['limit'])
pager = amo.utils.paginate(request, results, search_opts['limit'])
categories, filter, _, _ = browse.views.personas_listing(request)
c = dict(pager=pager, form=form, categories=categories, query=query,
filter=filter, search_placeholder='personas')
@ -259,7 +232,7 @@ def _collections(request):
query = form.cleaned_data.get('q', '')
search_opts = {}
search_opts['limit'] = form.cleaned_data.get('pp', DEFAULT_NUM_COLLECTIONS)
search_opts['limit'] = form.cleaned_data.get('pp', DEFAULT_NUM_RESULTS)
page = form.cleaned_data.get('page') or 1
search_opts['offset'] = (page - 1) * search_opts['limit']
search_opts['sort'] = form.cleaned_data.get('sortby')