summaryrefslogtreecommitdiff
path: root/packages/views/search.py
diff options
context:
space:
mode:
authorDan McGee <dan@archlinux.org>2012-04-07 14:52:39 -0500
committerDan McGee <dan@archlinux.org>2012-04-07 14:58:28 -0500
commit5d9f87c02bd8ad73fdb27600e0afe71284e3082f (patch)
tree0d24baee0fa28d6bc2bdde2c8b40ce365a941d3e /packages/views/search.py
parent9684e7c095585059033ac29339b058931a6547e3 (diff)
Add JSON search viewrelease_2012-04-07
This still might change and should not be viewed as a public API yet. This has been a longstanding request in FS#13026. Signed-off-by: Dan McGee <dan@archlinux.org>
Diffstat (limited to 'packages/views/search.py')
-rw-r--r--packages/views/search.py26
1 files changed, 26 insertions, 0 deletions
diff --git a/packages/views/search.py b/packages/views/search.py
index a23c6b4a..a09de0a7 100644
--- a/packages/views/search.py
+++ b/packages/views/search.py
@@ -4,11 +4,14 @@ from django import forms
from django.contrib.admin.widgets import AdminDateWidget
from django.contrib.auth.models import User
from django.db.models import Q
+from django.http import HttpResponse
from django.views.generic import list_detail
+from django.utils import simplejson
from main.models import Package, Arch, Repo
from main.utils import make_choice
from ..models import PackageRelation
+from ..utils import PackageJSONEncoder
def coerce_limit_value(value):
@@ -157,4 +160,27 @@ def search(request, page=None):
template_object_name="package",
extra_context=page_dict)
+
+def search_json(request):
+ limit = 250
+
+ container = {
+ 'version': 2,
+ 'limit': limit,
+ 'valid': False,
+ 'results': [],
+ }
+
+ if request.GET:
+ form = PackageSearchForm(data=request.GET)
+ if form.is_valid():
+ packages = Package.objects.normal()
+ packages = parse_form(form, packages)[:limit]
+ container['results'] = packages
+ container['valid'] = True
+
+ to_json = simplejson.dumps(container, ensure_ascii=False,
+ cls=PackageJSONEncoder)
+ return HttpResponse(to_json, mimetype='application/json')
+
# vim: set ts=4 sw=4 et: