diff options
author | Dan McGee <dan@archlinux.org> | 2011-06-14 19:10:16 -0500 |
---|---|---|
committer | Dan McGee <dan@archlinux.org> | 2011-06-14 19:10:16 -0500 |
commit | dc9dedaeb075630fa284036fb2f39de3c69004f9 (patch) | |
tree | 3f209e683dd4d7e548bc21e97af47527857fb414 /packages | |
parent | 037f67bb3c0b63239614ece9884b33c2d265b8ae (diff) |
Add JSON package details and files views
These are retrieved by adding 'json/' to the normal package details or
files view.
Signed-off-by: Dan McGee <dan@archlinux.org>
Diffstat (limited to 'packages')
-rw-r--r-- | packages/urls.py | 3 | ||||
-rw-r--r-- | packages/views.py | 54 |
2 files changed, 46 insertions, 11 deletions
diff --git a/packages/urls.py b/packages/urls.py index e0362fa2..3c2c87b0 100644 --- a/packages/urls.py +++ b/packages/urls.py @@ -2,8 +2,9 @@ from django.conf.urls.defaults import include, patterns package_patterns = patterns('packages.views', (r'^$', 'details'), + (r'^json/$', 'details_json'), (r'^files/$', 'files'), - (r'^maintainer/$', 'getmaintainer'), + (r'^files/json/$', 'files_json'), (r'^flag/$', 'flag'), (r'^flag/done/$', 'flag_confirmed', {}, 'package-flag-confirmed'), (r'^unflag/$', 'unflag'), diff --git a/packages/views.py b/packages/views.py index 23797f71..44a1db96 100644 --- a/packages/views.py +++ b/packages/views.py @@ -5,6 +5,7 @@ from django.contrib.auth.models import User from django.contrib.auth.decorators import permission_required from django.conf import settings from django.core.mail import send_mail +from django.core.serializers.json import DjangoJSONEncoder from django.db.models import Q from django.http import HttpResponse, Http404 from django.shortcuts import get_object_or_404, redirect @@ -24,9 +25,30 @@ from main.models import Package, PackageFile from main.models import Arch, Repo, Signoff from main.utils import make_choice from mirrors.models import MirrorUrl -from .models import PackageRelation +from .models import PackageRelation, PackageGroup from .utils import get_group_info, get_differences_info, get_wrong_permissions +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' ] + + 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) + data['groups'] = obj.groups.all() + return data + if isinstance(obj, PackageFile): + filename = obj.filename or '' + return obj.directory + filename + if isinstance(obj, (Repo, Arch, PackageGroup)): + return obj.name.lower() + return super(PackageJSONEncoder, self).default(obj) + def opensearch(request): if request.is_secure(): domain = "https://%s" % request.META['HTTP_HOST'] @@ -146,15 +168,6 @@ def group_details(request, arch, name): } return direct_to_template(request, 'packages/packages_list.html', context) -def getmaintainer(request, name, repo, arch): - "Returns the maintainers as plaintext." - - pkg = get_object_or_404(Package, - pkgname=name, repo__name__iexact=repo, arch__name=arch) - names = [m.username for m in pkg.maintainers] - - return HttpResponse(str('\n'.join(names)), mimetype='text/plain') - def coerce_limit_value(value): if not value: return None @@ -287,6 +300,27 @@ def files(request, name, repo, arch): return direct_to_template(request, template, {'pkg':pkg, 'files':fileslist}) +def details_json(request, name, repo, arch): + pkg = get_object_or_404(Package, + pkgname=name, repo__name__iexact=repo, arch__name=arch) + to_json = simplejson.dumps(pkg, ensure_ascii=False, + cls=PackageJSONEncoder) + return HttpResponse(to_json, mimetype='application/json') + +def files_json(request, name, repo, arch): + pkg = get_object_or_404(Package, + pkgname=name, repo__name__iexact=repo, arch__name=arch) + fileslist = PackageFile.objects.filter(pkg=pkg).order_by('directory', 'filename') + data = { + 'pkgname': pkg.pkgname, + 'repo': pkg.repo.name.lower(), + 'arch': pkg.arch.name.lower(), + 'files': fileslist, + } + to_json = simplejson.dumps(data, ensure_ascii=False, + cls=PackageJSONEncoder) + return HttpResponse(to_json, mimetype='application/json') + @permission_required('main.change_package') def unflag(request, name, repo, arch): pkg = get_object_or_404(Package, |