diff options
author | Dan McGee <dan@archlinux.org> | 2013-05-11 01:22:19 +0200 |
---|---|---|
committer | Dan McGee <dan@archlinux.org> | 2014-02-02 22:19:40 +0100 |
commit | 35a44f7ca75a9cc25e5502df9b9a6844f3eb49fe (patch) | |
tree | 7ea60366dea99df8234094c364c8ad05ac2d1123 | |
parent | 7c70083ed5b7cf0fbd1f66551c088a3c963b258c (diff) | |
download | archweb-35a44f7ca75a9cc25e5502df9b9a6844f3eb49fe.tar.gz archweb-35a44f7ca75a9cc25e5502df9b9a6844f3eb49fe.tar.xz |
Releng release JSON view
FS#35049.
Signed-off-by: Dan McGee <dan@archlinux.org>
-rw-r--r-- | releng/urls.py | 2 | ||||
-rw-r--r-- | releng/views.py | 45 |
2 files changed, 47 insertions, 0 deletions
diff --git a/releng/urls.py b/releng/urls.py index 76c3634..ca76eb2 100644 --- a/releng/urls.py +++ b/releng/urls.py @@ -14,6 +14,8 @@ feedback_patterns = patterns('releng.views', releases_patterns = patterns('releng.views', (r'^$', ReleaseListView.as_view(), {}, 'releng-release-list'), + (r'^json/$', + 'releases_json', {}, 'releng-release-list-json'), (r'^(?P<version>[-.\w]+)/$', ReleaseDetailView.as_view(), {}, 'releng-release-detail'), (r'^(?P<version>[-.\w]+)/torrent/$', diff --git a/releng/views.py b/releng/views.py index b1c76a4..ef81a65 100644 --- a/releng/views.py +++ b/releng/views.py @@ -1,7 +1,10 @@ from base64 import b64decode +import json from django import forms from django.conf import settings +from django.core.serializers.json import DjangoJSONEncoder +from django.core.urlresolvers import reverse from django.db.models import Count, Max from django.http import Http404, HttpResponse from django.shortcuts import get_object_or_404, redirect, render @@ -238,4 +241,46 @@ def release_torrent(request, version): response['Content-Disposition'] = 'attachment; filename=%s' % filename return response + +class ReleaseJSONEncoder(DjangoJSONEncoder): + release_attributes = ('release_date', 'version', 'kernel_version', + 'created', 'md5_sum', 'sha1_sum') + + def default(self, obj): + if hasattr(obj, '__iter__'): + # mainly for queryset serialization + return list(obj) + if isinstance(obj, Release): + data = {attr: getattr(obj, attr) or None + for attr in self.release_attributes} + data['available'] = obj.available + data['iso_url'] = '/' + obj.iso_url() + data['magnet_uri'] = obj.magnet_uri() + data['torrent_url'] = reverse('releng-release-torrent', args=[obj.version]) + data['info'] = obj.info_html() + torrent_data = obj.torrent() + if torrent_data: + torrent_data.pop('url_list', None) + data['torrent'] = torrent_data + return data + return super(ReleaseJSONEncoder, self).default(obj) + + +def releases_json(request): + releases = Release.objects.all() + try: + latest_version = Release.objects.filter(available=True).values_list( + 'version', flat=True).latest() + except Release.DoesNotExist: + latest_version = None + + data = { + 'version': 1, + 'releases': releases, + 'latest_version': latest_version, + } + to_json = json.dumps(data, ensure_ascii=False, cls=ReleaseJSONEncoder) + response = HttpResponse(to_json, content_type='application/json') + return response + # vim: set ts=4 sw=4 et: |