зеркало из https://github.com/mozilla/FlightDeck.git
Merge pull request #31 from rfreebern/bug-665955-rss_feeds
Bug 665955 rss feeds
This commit is contained in:
Коммит
d891be7aa5
|
@ -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,
|
||||
limit=20):
|
||||
limit=20, score_on=None):
|
||||
|
||||
get_packages = lambda x: Package.objects.manual_order(
|
||||
[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'
|
||||
# nested awesomenezz!
|
||||
query = dict(text=dict(_all=searchq)) if searchq else dict(match_all={})
|
||||
|
||||
fq = dict(
|
||||
filtered=dict(
|
||||
query=query,
|
||||
|
@ -45,6 +46,8 @@ def query(searchq, type_=None, user=None, filter_by_user=False, page=1,
|
|||
if filters:
|
||||
q = q.filter(**filters)
|
||||
|
||||
if score_on:
|
||||
q.score(script='_score * doc[\'%s\'].value' % score_on)
|
||||
|
||||
try:
|
||||
page = int(page)
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
{% block title %}{% if q %}{{q}} - {% endif %}Search - {% endblock %}
|
||||
|
||||
{% 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" />
|
||||
<script src="/media/jetpack/js/FlightDeck.Browser.js"></script>
|
||||
{% endblock %}
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
{% block title %}{% if q %}{{q}} - {% endif %}Search - {% endblock %}
|
||||
|
||||
{% 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" />
|
||||
<script src="/media/jetpack/js/FlightDeck.Browser.js"></script>
|
||||
{% endblock %}
|
||||
|
|
|
@ -1,11 +1,9 @@
|
|||
import commonware
|
||||
|
||||
from django.conf import settings
|
||||
from django.contrib.auth.models import User
|
||||
|
||||
from nose.tools import eq_
|
||||
from pyes import StringQuery, FieldQuery, FieldParameter
|
||||
from elasticutils import S
|
||||
from elasticutils.tests import ESTestCase
|
||||
|
||||
from jetpack.models import Package
|
||||
|
@ -24,8 +22,8 @@ def create_library(name):
|
|||
|
||||
def create_package(name, type, **kwargs):
|
||||
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):
|
||||
|
@ -119,7 +117,6 @@ class QueryTest(ESTestCase):
|
|||
|
||||
fixtures = ('mozilla_user', 'users', 'core_sdk')
|
||||
|
||||
|
||||
def test_initial_packages_excluded(self):
|
||||
bar = create_addon('super bar')
|
||||
create_addon('super baz')
|
||||
|
@ -146,6 +143,23 @@ class QueryTest(ESTestCase):
|
|||
data = query('foo')
|
||||
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):
|
||||
"""
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
from django.conf.urls.defaults import url, patterns
|
||||
from search.feeds import PackageFeed
|
||||
|
||||
urlpatterns = patterns('search.views',
|
||||
url(r'^$', 'search_home', name='search'),
|
||||
|
@ -8,7 +9,10 @@ urlpatterns = patterns('search.views',
|
|||
url(r'^(?P<type_>addon|library)/$', '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/(?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'),
|
||||
)
|
||||
|
|
Загрузка…
Ссылка в новой задаче