euscanwww: cleanups, drop stupid API

Signed-off-by: Corentin Chary <corentincj@iksaif.net>
This commit is contained in:
Corentin Chary 2011-11-15 17:08:29 +01:00
parent 66e20ebc26
commit 5b2476c9a0
4 changed files with 14 additions and 92 deletions

View File

@ -1,45 +0,0 @@
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,13 +3,11 @@ 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+.-]*)/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'),
@ -17,7 +15,6 @@ categories_patterns = patterns('euscan.views',
herds_patterns = patterns('euscan.views',
(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'),
@ -25,13 +22,13 @@ herds_patterns = patterns('euscan.views',
maintainers_patterns = patterns('euscan.views',
(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'),
)
urlpatterns = patterns('euscan.views',
# Global stuff
(r'^$', 'index'),
url(r'^feed/$', GlobalFeed(), name='global_feed'),
(r'^about/$', 'about'),
@ -39,6 +36,8 @@ urlpatterns = patterns('euscan.views',
(r'^statistics/charts/(?P<chart>[\w\-]+).png$', 'chart'),
(r'^world/$', 'world'),
(r'^world/scan/$', 'world_scan'),
# Real data
(r'^categories/', include(categories_patterns)),
(r'^herds/', include(herds_patterns)),
(r'^maintainers/', include(maintainers_patterns)),

View File

@ -5,7 +5,6 @@ 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
@ -36,21 +35,13 @@ def categories(request):
return { 'categories' : categories }
def category_data(request, category):
@render_to('euscan/category.html')
def category(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'
@ -60,19 +51,12 @@ def herds(request):
n_versions=Sum('n_versions'))
return { 'herds' : herds }
def herd_data(request, herd):
@render_to('euscan/herd.html')
def herd(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)
@ -83,21 +67,14 @@ def maintainers(request):
return { 'maintainers' : maintainers }
def maintainer_data(request, maintainer_id):
@render_to('euscan/maintainer.html')
def maintainer(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/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):
@render_to('euscan/package.html')
def package(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)
@ -108,14 +85,6 @@ def package_data(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()

View File

@ -104,8 +104,8 @@ MIDDLEWARE_CLASSES = (
'django.middleware.cache.UpdateCacheMiddleware',
'django.middleware.cache.FetchFromCacheMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
)
@ -124,12 +124,11 @@ INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
#'django.contrib.sites',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.admin',
'south',
'euscan',
# Uncomment the next line to enable the admin:
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
)