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', 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'),
@ -17,7 +15,6 @@ categories_patterns = patterns('euscan.views',
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'),
@ -25,13 +22,13 @@ herds_patterns = patterns('euscan.views',
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'),
) )
urlpatterns = patterns('euscan.views', urlpatterns = patterns('euscan.views',
# Global stuff
(r'^$', 'index'), (r'^$', 'index'),
url(r'^feed/$', GlobalFeed(), name='global_feed'), url(r'^feed/$', GlobalFeed(), name='global_feed'),
(r'^about/$', 'about'), (r'^about/$', 'about'),
@ -39,6 +36,8 @@ urlpatterns = patterns('euscan.views',
(r'^statistics/charts/(?P<chart>[\w\-]+).png$', 'chart'), (r'^statistics/charts/(?P<chart>[\w\-]+).png$', 'chart'),
(r'^world/$', 'world'), (r'^world/$', 'world'),
(r'^world/scan/$', 'world_scan'), (r'^world/scan/$', 'world_scan'),
# Real data
(r'^categories/', include(categories_patterns)), (r'^categories/', include(categories_patterns)),
(r'^herds/', include(herds_patterns)), (r'^herds/', include(herds_patterns)),
(r'^maintainers/', include(maintainers_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.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
@ -36,21 +35,13 @@ def categories(request):
return { 'categories' : categories } return { 'categories' : categories }
@render_to('euscan/category.html')
def category_data(request, category): def category(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'
@ -60,19 +51,12 @@ def herds(request):
n_versions=Sum('n_versions')) n_versions=Sum('n_versions'))
return { 'herds' : herds } 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) 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)
@ -83,21 +67,14 @@ def maintainers(request):
return { 'maintainers' : maintainers } return { 'maintainers' : maintainers }
@render_to('euscan/maintainer.html')
def maintainer_data(request, maintainer_id): def maintainer(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/maintainer.html') @render_to('euscan/package.html')
def maintainer(request, maintainer_id): def package(request, category, package):
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)
@ -108,14 +85,6 @@ def package_data(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()

View File

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