Merge pull request #31 from rfreebern/bug-665955-rss_feeds

Bug 665955 rss feeds
This commit is contained in:
Sean McArthur 2011-07-19 09:30:13 -07:00
Родитель e1279f3cab 89025012dd
Коммит d891be7aa5
6 изменённых файлов: 104 добавлений и 12 удалений

69
apps/search/feeds.py Normal file
Просмотреть файл

@ -0,0 +1,69 @@
from django.contrib.syndication.views import Feed
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext as _
from search.helpers import query
class PackageFeed(Feed):
search_type = False
search_query = False
def description(self):
description = 'New '
if (self.search_type):
description += '%(type)s '
description += 'packages in the Add-on Builder'
if (self.search_query):
description += ' matching search "%(query)s"'
return _(description) % {'type': self.search_type,
'query': self.search_query}
def get_object(self, request, type_):
self.search_type = type_
self.search_query = request.GET.get('q', '')
if type_ == 'combined':
self.search_type = None
return query(self.search_query, self.search_type, user=request.user,
filter_by_user=False, page=1, limit=20,
score_on='created_at')
def title(self):
title = 'Add-on Builder: New '
if self.search_type:
title += '%(type)s '
title += 'packages'
if (self.search_query):
title += ' matching search "%(query)s"'
return _(title) % {'type': self.search_type,
'query': self.search_query}
def link(self):
if self.search_type in ['addon', 'library']:
url = reverse('search_by_type', args=[self.search_type])
else:
url = reverse('search.combined')
if self.search_query:
url += '?q=%s' % self.search_query
return url
def items(self, data):
return data['pager'].object_list
def item_title(self, item):
return item.full_name
def item_description(self, item):
return item.description
def item_link(self, item):
return item.get_absolute_url()
def item_author_name(self, item):
return item.author.get_full_name()
def item_author_link(self, item):
return item.get_author_profile_url()
def item_pubdate(self, item):
return item.created_at

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

@ -5,7 +5,7 @@ from jetpack.models import Package
def query(searchq, type_=None, user=None, filter_by_user=False, page=1, def query(searchq, type_=None, user=None, filter_by_user=False, page=1,
limit=20): limit=20, score_on=None):
get_packages = lambda x: Package.objects.manual_order( get_packages = lambda x: Package.objects.manual_order(
[z['_id'] for z in x['hits']['hits']]).active() [z['_id'] for z in x['hits']['hits']]).active()
@ -14,6 +14,7 @@ def query(searchq, type_=None, user=None, filter_by_user=False, page=1,
# to deal with version_text='initial' or 'copy' # to deal with version_text='initial' or 'copy'
# nested awesomenezz! # nested awesomenezz!
query = dict(text=dict(_all=searchq)) if searchq else dict(match_all={}) query = dict(text=dict(_all=searchq)) if searchq else dict(match_all={})
fq = dict( fq = dict(
filtered=dict( filtered=dict(
query=query, query=query,
@ -45,6 +46,8 @@ def query(searchq, type_=None, user=None, filter_by_user=False, page=1,
if filters: if filters:
q = q.filter(**filters) q = q.filter(**filters)
if score_on:
q.score(script='_score * doc[\'%s\'].value' % score_on)
try: try:
page = int(page) page = int(page)
@ -59,7 +62,7 @@ def query(searchq, type_=None, user=None, filter_by_user=False, page=1,
) )
if user and user.is_authenticated(): if user and user.is_authenticated():
facet = q.get_facet('author') facet = q.get_facet('author')
data['my_total'] = facet.values()[0] if facet else 0 data['my_total'] = facet.values()[0] if facet else 0
return data return data

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

@ -3,6 +3,7 @@
{% block title %}{% if q %}{{q}} - {% endif %}Search - {% endblock %} {% block title %}{% if q %}{{q}} - {% endif %}Search - {% endblock %}
{% block head %} {% block head %}
<link rel="alternate" type="application/rss+xml" href="rss/{% if q %}?q={{q}}{% endif %}" />
<link rel="stylesheet" type="text/css" href="/media/search/css/Search.css" /> <link rel="stylesheet" type="text/css" href="/media/search/css/Search.css" />
<script src="/media/jetpack/js/FlightDeck.Browser.js"></script> <script src="/media/jetpack/js/FlightDeck.Browser.js"></script>
{% endblock %} {% endblock %}

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

@ -3,6 +3,7 @@
{% block title %}{% if q %}{{q}} - {% endif %}Search - {% endblock %} {% block title %}{% if q %}{{q}} - {% endif %}Search - {% endblock %}
{% block head %} {% block head %}
<link rel="alternate" type="application/rss+xml" href="rss/{% if q %}?q={{q}}{% endif %}" />
<link rel="stylesheet" type="text/css" href="/media/search/css/Search.css" /> <link rel="stylesheet" type="text/css" href="/media/search/css/Search.css" />
<script src="/media/jetpack/js/FlightDeck.Browser.js"></script> <script src="/media/jetpack/js/FlightDeck.Browser.js"></script>
{% endblock %} {% endblock %}

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

@ -1,11 +1,9 @@
import commonware import commonware
from django.conf import settings
from django.contrib.auth.models import User from django.contrib.auth.models import User
from nose.tools import eq_ from nose.tools import eq_
from pyes import StringQuery, FieldQuery, FieldParameter from pyes import StringQuery, FieldQuery, FieldParameter
from elasticutils import S
from elasticutils.tests import ESTestCase from elasticutils.tests import ESTestCase
from jetpack.models import Package from jetpack.models import Package
@ -24,8 +22,8 @@ def create_library(name):
def create_package(name, type, **kwargs): def create_package(name, type, **kwargs):
u = User.objects.get(username='john') u = User.objects.get(username='john')
return Package.objects.create(full_name=name, author=u, type=type, **kwargs) return Package.objects.create(full_name=name, author=u, type=type,
**kwargs)
class TestSearch(ESTestCase): class TestSearch(ESTestCase):
@ -119,7 +117,6 @@ class QueryTest(ESTestCase):
fixtures = ('mozilla_user', 'users', 'core_sdk') fixtures = ('mozilla_user', 'users', 'core_sdk')
def test_initial_packages_excluded(self): def test_initial_packages_excluded(self):
bar = create_addon('super bar') bar = create_addon('super bar')
create_addon('super baz') create_addon('super baz')
@ -146,6 +143,23 @@ class QueryTest(ESTestCase):
data = query('foo') data = query('foo')
eq_(1, data['total']) eq_(1, data['total'])
def test_custom_scoring(self):
baz = create_addon('score baz')
baz.latest.set_version('1.0')
quux = create_addon('score quux')
quux.latest.set_version('1.0')
quux.latest.set_version('1.1')
self.es.refresh()
data = query('score', score_on='version')
"""
Since quux has more versions than baz, it will have a higher score
and should be the first result.
"""
eq_([p.name for p in data['pager'].object_list], [quux.name, baz.name])
class AggregateQueryTest(ESTestCase): class AggregateQueryTest(ESTestCase):
""" """

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

@ -1,14 +1,18 @@
from django.conf.urls.defaults import url, patterns from django.conf.urls.defaults import url, patterns
from search.feeds import PackageFeed
urlpatterns = patterns('search.views', urlpatterns = patterns('search.views',
url(r'^$', 'search_home', name='search'), url(r'^$', 'search_home', name='search'),
url(r'^combined/$', 'combined', name='search.combined'), url(r'^combined/$', 'combined', name='search.combined'),
url(r'^(?P<type_>addon|library)/$', 'search_by_type', url(r'^(?P<type_>addon|library)/$', 'search_by_type',
name='search_by_type'), name='search_by_type'),
url(r'^(?P<type_>addon|library|combined)/rss/$', PackageFeed()),
url(r'^me/$', 'me', name='search.me'), url(r'^me/$', 'me', name='search.me'),
url(r'^me/(?P<type_>addon|library)/$', 'me_by_type', name='search.me.by_type'), url(r'^me/(?P<type_>addon|library)/$', 'me_by_type',
name='search.me.by_type'),
) )