euscanwww: add some json

This is not a real API, I will break it !

Signed-off-by: Corentin Chary <corentincj@iksaif.net>
This commit is contained in:
Corentin Chary 2011-09-21 10:12:01 +02:00
parent 14971584af
commit 1bd42f303d
3 changed files with 91 additions and 11 deletions

View File

@ -0,0 +1,45 @@
from django.db.models.query import QuerySet
from django.db.models import Model
from django.http import HttpResponse
from django.utils import simplejson
from django.core import serializers
try:
from functools import wraps
except ImportError:
def wraps(wrapped, assigned=('__module__', '__name__', '__doc__'),
updated=('__dict__',)):
def inner(wrapper):
for attr in assigned:
setattr(wrapper, attr, getattr(wrapped, attr))
for attr in updated:
getattr(wrapper, attr).update(getattr(wrapped, attr, {}))
return wrapper
return inner
class DjangoJSONEncoder(simplejson.JSONEncoder):
def default(self, obj):
if isinstance(obj, QuerySet):
# `default` must return a python serializable
# structure, the easiest way is to load the JSON
# string produced by `serialize` and return it
return simplejson.loads(serializers.serialize('json', obj))
if isinstance(obj, Model):
# Must be iterable to be serilized
obj = [obj]
return simplejson.loads(serializers.serialize('json', obj))
return simplejson.JSONEncoder.default(self, obj)
def render_to_json(function):
@wraps(function)
def wrapper(request, *args, **kwargs):
output = function(request, *args, **kwargs)
if not isinstance(output, dict):
return output
output = simplejson.dumps(output, cls=DjangoJSONEncoder)
return HttpResponse(mimetype='application/json', content=output)
return wrapper

View File

@ -3,25 +3,29 @@ from feeds import *
package_patterns = patterns('euscan.views', package_patterns = patterns('euscan.views',
url(r'^(?P<category>[\w+][\w+.-]*)/(?P<package>[\w+][\w+.-]*)/feed/$', PackageFeed(), name='package_feed'), url(r'^(?P<category>[\w+][\w+.-]*)/(?P<package>[\w+][\w+.-]*)/feed/$', PackageFeed(), name='package_feed'),
(r'^(?P<category>[\w+][\w+.-]*)/(?P<package>[\w+][\w+.-]*)/json/$', 'package_json'),
(r'^(?P<category>[\w+][\w+.-]*)/(?P<package>[\w+][\w+.-]*)/$', 'package'), (r'^(?P<category>[\w+][\w+.-]*)/(?P<package>[\w+][\w+.-]*)/$', 'package'),
) )
categories_patterns = patterns('euscan.views', categories_patterns = patterns('euscan.views',
(r'^(?P<category>[\w+][\w+.-]*)/view/$', 'category'), (r'^(?P<category>[\w+][\w+.-]*)/(view/)?$', 'category'),
(r'^(?P<category>[\w+][\w+.-]*)/json/$', 'category_json'),
url(r'^(?P<category>[\w+][\w+.-]*)/feed/$', CategoryFeed(), name='category_feed'), url(r'^(?P<category>[\w+][\w+.-]*)/feed/$', CategoryFeed(), name='category_feed'),
(r'^(?P<category>[\w+][\w+.-]*)/charts/(?P<chart>[\w\-]+).png$', 'chart_category'), (r'^(?P<category>[\w+][\w+.-]*)/charts/(?P<chart>[\w\-]+).png$', 'chart_category'),
(r'^$', 'categories'), (r'^$', 'categories'),
) )
herds_patterns = patterns('euscan.views', herds_patterns = patterns('euscan.views',
(r'^(?P<herd>[\@\{\}\w+.-]*)/view/$', 'herd'), (r'^(?P<herd>[\@\{\}\w+.-]*)/(view/)?$', 'herd'),
(r'^(?P<herd>[\@\{\}\w+.-]*)/json/$', 'herd_json'),
url(r'^(?P<herd>[\@\{\}\w+.-]*)/feed/$', HerdFeed(), name='herd_feed'), url(r'^(?P<herd>[\@\{\}\w+.-]*)/feed/$', HerdFeed(), name='herd_feed'),
(r'^(?P<herd>[\@\{\}\w+.-]*)/charts/(?P<chart>[\w\-]+).png$', 'chart_herd'), (r'^(?P<herd>[\@\{\}\w+.-]*)/charts/(?P<chart>[\w\-]+).png$', 'chart_herd'),
(r'^$', 'herds'), (r'^$', 'herds'),
) )
maintainers_patterns = patterns('euscan.views', maintainers_patterns = patterns('euscan.views',
(r'^(?P<maintainer_id>\d+)/view/$', 'maintainer'), (r'^(?P<maintainer_id>\d+)/(view/)?$', 'maintainer'),
(r'^(?P<maintainer_id>\d+)/json/$', 'maintainer_json'),
url(r'^(?P<maintainer_id>\d+)/feed/$', MaintainerFeed(), name='maintainer_feed'), url(r'^(?P<maintainer_id>\d+)/feed/$', MaintainerFeed(), name='maintainer_feed'),
(r'^(?P<maintainer_id>\d+)/charts/(?P<chart>[\w\-]+).png$', 'chart_maintainer'), (r'^(?P<maintainer_id>\d+)/charts/(?P<chart>[\w\-]+).png$', 'chart_maintainer'),
(r'^$', 'maintainers'), (r'^$', 'maintainers'),

View File

@ -5,6 +5,7 @@ from django.db.models import Sum, Max
from euscan.models import Version, Package, Herd, Maintainer, EuscanResult, VersionLog from euscan.models import Version, Package, Herd, Maintainer, EuscanResult, VersionLog
from euscan.forms import WorldForm, PackagesForm from euscan.forms import WorldForm, PackagesForm
from euscan.decorators import render_to_json
import charts import charts
@ -35,13 +36,21 @@ def categories(request):
return { 'categories' : categories } return { 'categories' : categories }
@render_to('euscan/category.html')
def category(request, category): def category_data(request, category):
packages = Package.objects.filter(category=category) packages = Package.objects.filter(category=category)
if not packages: if not packages:
raise Http404 raise Http404
return { 'category' : category, 'packages' : packages } return { 'category' : category, 'packages' : packages }
@render_to('euscan/category.html')
def category(request, category):
return category_data(request, category)
@render_to_json
def category_json(request, category):
return category_data(request, category)
@render_to('euscan/herds.html') @render_to('euscan/herds.html')
def herds(request): def herds(request):
# FIXME: optimize the query, it uses 'LEFT OUTER JOIN' instead of 'INNER JOIN' # FIXME: optimize the query, it uses 'LEFT OUTER JOIN' instead of 'INNER JOIN'
@ -51,12 +60,19 @@ def herds(request):
n_versions=Sum('n_versions')) n_versions=Sum('n_versions'))
return { 'herds' : herds } return { 'herds' : herds }
@render_to('euscan/herd.html') def herd_data(request, herd):
def herd(request, herd):
herd = get_object_or_404(Herd, herd=herd) herd = get_object_or_404(Herd, herd=herd)
packages = Package.objects.filter(herds__id=herd.id) packages = Package.objects.filter(herds__id=herd.id)
return { 'herd' : herd, 'packages' : packages } return { 'herd' : herd, 'packages' : packages }
@render_to('euscan/herd.html')
def herd(request, herd):
return herd_data(request, herd)
@render_to_json
def herd_json(request, herd):
return herd_data(request, herd)
@render_to('euscan/maintainers.html') @render_to('euscan/maintainers.html')
def maintainers(request): def maintainers(request):
maintainers = Package.objects.filter(maintainers__isnull=False) maintainers = Package.objects.filter(maintainers__isnull=False)
@ -67,14 +83,21 @@ def maintainers(request):
return { 'maintainers' : maintainers } return { 'maintainers' : maintainers }
@render_to('euscan/maintainer.html')
def maintainer(request, maintainer_id): def maintainer_data(request, maintainer_id):
maintainer = get_object_or_404(Maintainer, id=maintainer_id) maintainer = get_object_or_404(Maintainer, id=maintainer_id)
packages = Package.objects.filter(maintainers__id=maintainer.id) packages = Package.objects.filter(maintainers__id=maintainer.id)
return { 'maintainer' : maintainer, 'packages' : packages } return { 'maintainer' : maintainer, 'packages' : packages }
@render_to('euscan/package.html') @render_to('euscan/maintainer.html')
def package(request, category, package): def maintainer(request, maintainer_id):
return maintainer_data(request, maintainer_id)
@render_to_json
def maintainer_json(request, maintainer_id):
return maintainer_data(request, maintainer_id)
def package_data(request, category, package):
package = get_object_or_404(Package, category=category, name=package) package = get_object_or_404(Package, category=category, name=package)
package.homepages = package.homepage.split(' ') package.homepages = package.homepage.split(' ')
packaged = Version.objects.filter(package=package, packaged=True) packaged = Version.objects.filter(package=package, packaged=True)
@ -85,6 +108,14 @@ def package(request, category, package):
return { 'package' : package, 'packaged' : packaged, return { 'package' : package, 'packaged' : packaged,
'upstream' : upstream, 'log' : log, 'vlog' : vlog } 'upstream' : upstream, 'log' : log, 'vlog' : vlog }
@render_to('euscan/package.html')
def package(request, category, package):
return package_data(request, category, package)
@render_to_json
def package_json(request, category, package):
return package_data(request, category, package)
@render_to('euscan/world.html') @render_to('euscan/world.html')
def world(request): def world(request):
world_form = WorldForm() world_form = WorldForm()