diff options
Diffstat (limited to 'packages')
-rw-r--r-- | packages/urls.py | 1 | ||||
-rw-r--r-- | packages/utils.py | 37 | ||||
-rw-r--r-- | packages/views/__init__.py | 54 | ||||
-rw-r--r-- | packages/views/search.py | 28 |
4 files changed, 72 insertions, 48 deletions
diff --git a/packages/urls.py b/packages/urls.py index 9a063f43..1fd54a7e 100644 --- a/packages/urls.py +++ b/packages/urls.py @@ -22,6 +22,7 @@ urlpatterns = patterns('packages.views', (r'^update/$', 'update'), (r'^$', 'search', {}, 'packages-search'), + (r'^search/json/$', 'search_json'), (r'^(?P<page>\d+)/$', 'search'), (r'^differences/$', 'arch_differences', {}, 'packages-differences'), diff --git a/packages/utils.py b/packages/utils.py index ab3c074f..55a2901a 100644 --- a/packages/utils.py +++ b/packages/utils.py @@ -2,13 +2,15 @@ from collections import defaultdict from itertools import chain from operator import itemgetter +from django.core.serializers.json import DjangoJSONEncoder from django.db import connection from django.db.models import Count, Max, F from django.contrib.auth.models import User -from main.models import Package, Arch, Repo +from main.models import Package, PackageDepend, PackageFile, Arch, Repo from main.utils import cache_function, groupby_preserve_order, PackageStandin from .models import (PackageGroup, PackageRelation, + License, Conflict, Provision, Replacement, SignoffSpecification, Signoff, DEFAULT_SIGNOFF_SPEC) @cache_function(127) @@ -426,4 +428,37 @@ def get_signoff_groups(repos=None, user=None): return signoff_groups + +class PackageJSONEncoder(DjangoJSONEncoder): + pkg_attributes = [ 'pkgname', 'pkgbase', 'repo', 'arch', 'pkgver', + 'pkgrel', 'epoch', 'pkgdesc', 'url', 'filename', 'compressed_size', + 'installed_size', 'build_date', 'last_update', 'flag_date', + 'maintainers', 'packager' ] + pkg_list_attributes = [ 'groups', 'licenses', 'conflicts', + 'provides', 'replaces', 'depends' ] + + def default(self, obj): + if hasattr(obj, '__iter__'): + # mainly for queryset serialization + return list(obj) + if isinstance(obj, Package): + data = dict((attr, getattr(obj, attr)) + for attr in self.pkg_attributes) + for attr in self.pkg_list_attributes: + data[attr] = getattr(obj, attr).all() + return data + if isinstance(obj, PackageFile): + filename = obj.filename or '' + return obj.directory + filename + if isinstance(obj, (Repo, Arch)): + return obj.name.lower() + if isinstance(obj, (PackageGroup, License)): + return obj.name + if isinstance(obj, (Conflict, Provision, Replacement, PackageDepend)): + return unicode(obj) + elif isinstance(obj, User): + return obj.username + return super(PackageJSONEncoder, self).default(obj) + + # vim: set ts=4 sw=4 et: diff --git a/packages/views/__init__.py b/packages/views/__init__.py index aa2721af..fc132105 100644 --- a/packages/views/__init__.py +++ b/packages/views/__init__.py @@ -4,7 +4,6 @@ from urllib import urlencode from django.contrib import messages from django.contrib.auth.decorators import permission_required from django.contrib.auth.models import User -from django.core.serializers.json import DjangoJSONEncoder from django.http import HttpResponse, Http404 from django.shortcuts import get_object_or_404, redirect from django.utils import simplejson @@ -12,50 +11,19 @@ from django.views.decorators.http import require_POST from django.views.decorators.vary import vary_on_headers from django.views.generic.simple import direct_to_template -from main.models import Package, PackageFile, PackageDepend, Arch, Repo +from main.models import Package, PackageFile, Arch, Repo from mirrors.models import MirrorUrl -from ..models import (PackageRelation, PackageGroup, License, - Conflict, Provision, Replacement) +from mirrors.utils import get_mirror_url_for_download +from ..models import PackageRelation from ..utils import (get_group_info, get_differences_info, - multilib_differences, get_wrong_permissions) + multilib_differences, get_wrong_permissions, PackageJSONEncoder) # make other views available from this same package from .flag import flaghelp, flag, flag_confirmed, unflag, unflag_all -from .search import search +from .search import search, search_json from .signoff import signoffs, signoff_package, signoff_options, signoffs_json -class PackageJSONEncoder(DjangoJSONEncoder): - pkg_attributes = [ 'pkgname', 'pkgbase', 'repo', 'arch', 'pkgver', - 'pkgrel', 'epoch', 'pkgdesc', 'url', 'filename', 'compressed_size', - 'installed_size', 'build_date', 'last_update', 'flag_date', - 'maintainers', 'packager' ] - pkg_list_attributes = [ 'groups', 'licenses', 'conflicts', - 'provides', 'replaces', 'depends' ] - - def default(self, obj): - if hasattr(obj, '__iter__'): - # mainly for queryset serialization - return list(obj) - if isinstance(obj, Package): - data = dict((attr, getattr(obj, attr)) - for attr in self.pkg_attributes) - for attr in self.pkg_list_attributes: - data[attr] = getattr(obj, attr).all() - return data - if isinstance(obj, PackageFile): - filename = obj.filename or '' - return obj.directory + filename - if isinstance(obj, (Repo, Arch)): - return obj.name.lower() - if isinstance(obj, (PackageGroup, License)): - return obj.name - if isinstance(obj, (Conflict, Provision, Replacement, PackageDepend)): - return unicode(obj) - elif isinstance(obj, User): - return obj.username - return super(PackageJSONEncoder, self).default(obj) - def opensearch(request): if request.is_secure(): domain = "https://%s" % request.META['HTTP_HOST'] @@ -225,21 +193,15 @@ def files_json(request, name, repo, arch): def download(request, name, repo, arch): pkg = get_object_or_404(Package, pkgname=name, repo__name__iexact=repo, arch__name=arch) - mirror_urls = MirrorUrl.objects.filter( - mirror__public=True, mirror__active=True, - protocol__protocol__iexact='HTTP') - # look first for an 'Any' URL, then fall back to any HTTP URL - filtered_urls = mirror_urls.filter(mirror__country='Any')[:1] - if not filtered_urls: - filtered_urls = mirror_urls[:1] - if not filtered_urls: + url = get_mirror_url_for_download() + if not url: raise Http404 arch = pkg.arch.name if pkg.arch.agnostic: # grab the first non-any arch to fake the download path arch = Arch.objects.exclude(agnostic=True)[0].name values = { - 'host': filtered_urls[0].url, + 'host': url.url, 'arch': arch, 'repo': pkg.repo.name.lower(), 'file': pkg.filename, diff --git a/packages/views/search.py b/packages/views/search.py index 1431893d..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): @@ -101,7 +104,7 @@ def parse_form(form, packages): if form.cleaned_data['name']: name = form.cleaned_data['name'] - packages = packages.filter(pkgname__icontains=name) + packages = packages.filter(pkgname=name) if form.cleaned_data['desc']: desc = form.cleaned_data['desc'] @@ -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: |