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:
parent
14971584af
commit
1bd42f303d
45
euscanwww/euscan/decorators.py
Normal file
45
euscanwww/euscan/decorators.py
Normal 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
|
||||
|
||||
|
@ -3,25 +3,29 @@ from feeds import *
|
||||
|
||||
package_patterns = patterns('euscan.views',
|
||||
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'),
|
||||
)
|
||||
|
||||
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'),
|
||||
(r'^(?P<category>[\w+][\w+.-]*)/charts/(?P<chart>[\w\-]+).png$', 'chart_category'),
|
||||
(r'^$', 'categories'),
|
||||
)
|
||||
|
||||
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'),
|
||||
(r'^(?P<herd>[\@\{\}\w+.-]*)/charts/(?P<chart>[\w\-]+).png$', 'chart_herd'),
|
||||
(r'^$', 'herds'),
|
||||
)
|
||||
|
||||
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'),
|
||||
(r'^(?P<maintainer_id>\d+)/charts/(?P<chart>[\w\-]+).png$', 'chart_maintainer'),
|
||||
(r'^$', 'maintainers'),
|
||||
|
@ -5,6 +5,7 @@ from django.db.models import Sum, Max
|
||||
|
||||
from euscan.models import Version, Package, Herd, Maintainer, EuscanResult, VersionLog
|
||||
from euscan.forms import WorldForm, PackagesForm
|
||||
from euscan.decorators import render_to_json
|
||||
|
||||
import charts
|
||||
|
||||
@ -35,13 +36,21 @@ def categories(request):
|
||||
|
||||
return { 'categories' : categories }
|
||||
|
||||
@render_to('euscan/category.html')
|
||||
def category(request, category):
|
||||
|
||||
def category_data(request, category):
|
||||
packages = Package.objects.filter(category=category)
|
||||
if not packages:
|
||||
raise Http404
|
||||
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')
|
||||
def herds(request):
|
||||
# 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'))
|
||||
return { 'herds' : herds }
|
||||
|
||||
@render_to('euscan/herd.html')
|
||||
def herd(request, herd):
|
||||
def herd_data(request, herd):
|
||||
herd = get_object_or_404(Herd, herd=herd)
|
||||
packages = Package.objects.filter(herds__id=herd.id)
|
||||
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')
|
||||
def maintainers(request):
|
||||
maintainers = Package.objects.filter(maintainers__isnull=False)
|
||||
@ -67,14 +83,21 @@ def maintainers(request):
|
||||
|
||||
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)
|
||||
packages = Package.objects.filter(maintainers__id=maintainer.id)
|
||||
return { 'maintainer' : maintainer, 'packages' : packages }
|
||||
|
||||
@render_to('euscan/package.html')
|
||||
def package(request, category, package):
|
||||
@render_to('euscan/maintainer.html')
|
||||
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.homepages = package.homepage.split(' ')
|
||||
packaged = Version.objects.filter(package=package, packaged=True)
|
||||
@ -85,6 +108,14 @@ def package(request, category, package):
|
||||
return { 'package' : package, 'packaged' : packaged,
|
||||
'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')
|
||||
def world(request):
|
||||
world_form = WorldForm()
|
||||
|
Loading…
Reference in New Issue
Block a user