summaryrefslogtreecommitdiff
path: root/releng/views.py
diff options
context:
space:
mode:
authorDan McGee <dan@archlinux.org>2013-05-10 18:22:19 -0500
committerDan McGee <dan@archlinux.org>2014-02-02 15:19:40 -0600
commit35a44f7ca75a9cc25e5502df9b9a6844f3eb49fe (patch)
tree7ea60366dea99df8234094c364c8ad05ac2d1123 /releng/views.py
parent7c70083ed5b7cf0fbd1f66551c088a3c963b258c (diff)
Releng release JSON viewrelease_2014-02-02
FS#35049. Signed-off-by: Dan McGee <dan@archlinux.org>
Diffstat (limited to 'releng/views.py')
-rw-r--r--releng/views.py45
1 files changed, 45 insertions, 0 deletions
diff --git a/releng/views.py b/releng/views.py
index b1c76a4a..ef81a65c 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: