summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--releng/urls.py2
-rw-r--r--releng/views.py45
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: