Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove haystack from code base #4039

Merged
merged 6 commits into from
May 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions readthedocs/api/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
from readthedocs.core.utils import trigger_build
from readthedocs.projects.models import ImportedFile, Project

from .utils import PostAuthentication, SearchMixin
from .utils import PostAuthentication

log = logging.getLogger(__name__)


class ProjectResource(ModelResource, SearchMixin):
class ProjectResource(ModelResource):

"""API resource for Project model."""

Expand Down Expand Up @@ -139,7 +139,7 @@ def prepend_urls(self):
]


class FileResource(ModelResource, SearchMixin):
class FileResource(ModelResource):

"""API resource for ImportedFile model."""

Expand All @@ -152,17 +152,12 @@ class Meta(object):
include_absolute_url = True
authentication = PostAuthentication()
authorization = DjangoAuthorization()
search_facets = ['project']

def prepend_urls(self):
return [
url(
r'^(?P<resource_name>%s)/schema/$' % self._meta.resource_name,
self.wrap_view('get_schema'), name='api_get_schema'),
url(
r'^(?P<resource_name>%s)/search%s$' %
(self._meta.resource_name, trailing_slash()),
self.wrap_view('get_search'), name='api_get_search'),
url(
r'^(?P<resource_name>%s)/anchor%s$' %
(self._meta.resource_name, trailing_slash()),
Expand Down
138 changes: 1 addition & 137 deletions readthedocs/api/utils.py
Original file line number Diff line number Diff line change
@@ -1,154 +1,18 @@
"""Utility classes for api module"""
from __future__ import absolute_import
from builtins import object
import logging

from django.core.paginator import Paginator, InvalidPage
from django.http import Http404
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext

from haystack.utils import Highlighter
from tastypie.authentication import BasicAuthentication
from tastypie.authorization import Authorization
from tastypie.resources import ModelResource
from tastypie.exceptions import NotFound, ImmediateHttpResponse
from tastypie import http
from tastypie.utils.mime import build_content_type
from tastypie.exceptions import NotFound

from readthedocs.core.forms import FacetedSearchForm

log = logging.getLogger(__name__)


class SearchMixin(object):

"""
Adds a search api to any ModelResource provided the model is indexed.

The search can be configured using the Meta class in each ModelResource.
The search is limited to the model defined by the meta queryset. If the
search is invalid, a 400 Bad Request will be raised.

e.g.

class Meta:
# Return facet counts for each facetname
search_facets = ['facetname1', 'facetname1']

# Number of results returned per page
search_page_size = 20

# Highlight search terms in the text
search_highlight = True
"""

def get_search(self, request, **kwargs):
self.method_check(request, allowed=['get'])
self.is_authenticated(request)
self.throttle_check(request)
object_list = self._search(
request, self._meta.queryset.model,
facets=getattr(self._meta, 'search_facets', []),
page_size=getattr(self._meta, 'search_page_size', 20),
highlight=getattr(self._meta, 'search_highlight', True))
self.log_throttled_access(request)
return self.create_response(request, object_list)

def _url_template(self, query, selected_facets):
"""
Construct a url template to assist with navigating the resources.

This looks a bit nasty but urllib.urlencode resulted in even
nastier output...
"""
query_params = []
for facet in selected_facets:
query_params.append(('selected_facets', facet))
query_params += [('q', query), ('format', 'json'), ('page', '{0}')]
query_string = '&'.join('='.join(p) for p in query_params)
url_template = reverse('api_get_search', kwargs={
'resource_name': self._meta.resource_name,
'api_name': 'v1'
})
return url_template + '?' + query_string

def _search(self, request, model, facets=None, page_size=20,
highlight=True):
# pylint: disable=too-many-locals
"""
Return a paginated list of objects for a request.

`model`
Limit the search to a particular model
`facets`
A list of facets to include with the results
"""
form = FacetedSearchForm(request.GET, facets=facets or [],
models=(model,), load_all=True)
if not form.is_valid():
return self.error_response({'errors': form.errors}, request)
results = form.search()

paginator = Paginator(results, page_size)
try:
page = paginator.page(int(request.GET.get('page', 1)))
except InvalidPage:
raise Http404(ugettext("Sorry, no results on that page."))

objects = []
query = request.GET.get('q', '')
highlighter = Highlighter(query)
for result in page.object_list:
if not result:
continue
text = result.text
if highlight:
text = highlighter.highlight(text)
bundle = self.build_bundle(obj=result.object, request=request)
bundle = self.full_dehydrate(bundle)
bundle.data['text'] = text
objects.append(bundle)

url_template = self._url_template(query,
form['selected_facets'].value())
page_data = {
'number': page.number,
'per_page': paginator.per_page,
'num_pages': paginator.num_pages,
'page_range': paginator.page_range,
'object_count': paginator.count,
'url_template': url_template,
}
if page.has_next():
page_data['url_next'] = url_template.format(
page.next_page_number())
if page.has_previous():
page_data['url_prev'] = url_template.format(
page.previous_page_number())

object_list = {
'page': page_data,
'objects': objects,
}
if facets:
object_list.update({'facets': results.facet_counts()})
return object_list

# XXX: This method is available in the latest tastypie, remove
# once available in production.
def error_response(self, errors, request):
if request:
desired_format = self.determine_format(request)
else:
desired_format = self._meta.default_format
serialized = self.serialize(request, errors, desired_format)
response = http.HttpBadRequest(
content=serialized,
content_type=build_content_type(desired_format))
raise ImmediateHttpResponse(response=response)


class PostAuthentication(BasicAuthentication):

"""Require HTTP Basic authentication for any method other than GET."""
Expand Down
45 changes: 0 additions & 45 deletions readthedocs/core/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
from django.contrib.auth.models import User
from django.forms.fields import CharField
from django.utils.translation import ugettext_lazy as _
from haystack.forms import SearchForm
from haystack.query import SearchQuerySet

from .models import UserProfile

Expand Down Expand Up @@ -88,46 +86,3 @@ def valid_value(self, value):
if ':' not in value:
return False
return True


class FacetedSearchForm(SearchForm):

"""
Supports fetching faceted results with a corresponding query.

`facets`
A list of facet names for which to get facet counts
`models`
Limit the search to one or more models
"""

selected_facets = FacetField(required=False)

def __init__(self, *args, **kwargs):
facets = kwargs.pop('facets', [])
models = kwargs.pop('models', [])
super(FacetedSearchForm, self).__init__(*args, **kwargs)

for facet in facets:
self.searchqueryset = self.searchqueryset.facet(facet)
if models:
self.searchqueryset = self.searchqueryset.models(*models)

def clean_selected_facets(self):
facets = self.cleaned_data['selected_facets']
cleaned_facets = []
clean = SearchQuerySet().query.clean
for facet in facets:
field, value = facet.split(':', 1)
if not value: # Ignore empty values
continue
value = clean(value)
cleaned_facets.append(u'%s:"%s"' % (field, value))
return cleaned_facets

def search(self):
sqs = super(FacetedSearchForm, self).search()
for facet in self.cleaned_data['selected_facets']:
sqs = sqs.narrow(facet)
self.searchqueryset = sqs
return sqs
117 changes: 0 additions & 117 deletions readthedocs/projects/search_indexes.py

This file was deleted.

10 changes: 0 additions & 10 deletions readthedocs/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,6 @@ def INSTALLED_APPS(self): # noqa
'annoying',
'django_extensions',
'messages_extends',

# daniellindsleyrocksdahouse
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weird comment here 😁

'haystack',
'tastypie',

# our apps
Expand Down Expand Up @@ -313,13 +310,6 @@ def USE_PROMOS(self): # noqa
GROK_API_HOST = 'https://api.grokthedocs.com'
SERVE_DOCS = ['public']

# Haystack
HAYSTACK_CONNECTIONS = {
'default': {
'ENGINE': 'haystack.backends.simple_backend.SimpleEngine',
},
}

# Elasticsearch settings.
ES_HOSTS = ['127.0.0.1:9200']
ES_DEFAULT_NUM_REPLICAS = 0
Expand Down
Loading