apis now use manager

This commit is contained in:
volpino 2012-05-05 10:22:59 +02:00
parent 6464187342
commit 7ad08128cd
3 changed files with 57 additions and 76 deletions

View File

@ -1,46 +1,19 @@
from piston.handler import AnonymousBaseHandler from piston.handler import AnonymousBaseHandler
from piston.utils import rc from piston.utils import rc
from django.db.models import Sum, Max
from django.core.exceptions import ObjectDoesNotExist from django.core.exceptions import ObjectDoesNotExist
from django.forms.models import model_to_dict from django.forms.models import model_to_dict
from djeuscan.models import Version, Package, Herd, Maintainer, EuscanResult, \ from djeuscan.models import Version, Package, Herd, Maintainer, EuscanResult, \
VersionLog VersionLog
from djeuscan.helpers import catch_and_return
from djeuscan.helpers import xint
# replace default XMLEmitter with ours # replace default XMLEmitter with ours
from piston.emitters import Emitter from piston.emitters import Emitter
from emitters import EuscanXMLEmitter from .emitters import EuscanXMLEmitter
Emitter.register('xml', EuscanXMLEmitter, 'text/xml; charset=utf-8') Emitter.register('xml', EuscanXMLEmitter, 'text/xml; charset=utf-8')
def renameFields(vqs, fields):
ret = []
for n in vqs:
for tr in fields:
if tr[0] in n:
n[tr[1]] = n[tr[0]]
del n[tr[0]]
ret.append(n)
return ret
class catch_and_return(object):
def __init__(self, err, response):
self.err = err
self.response = response
def __call__(self, fn):
def wrapper(*args, **kwargs):
try:
return fn(*args, **kwargs)
except self.err:
return self.response
return wrapper
# /api/1.0/ # /api/1.0/
class RootHandler(AnonymousBaseHandler): class RootHandler(AnonymousBaseHandler):
allowed_methods = ('GET',) allowed_methods = ('GET',)
@ -54,19 +27,15 @@ class StatisticsHandler(AnonymousBaseHandler):
allowed_methods = ('GET',) allowed_methods = ('GET',)
def read(self, request): def read(self, request):
_aggr = Package.objects.aggregate
data = {} data = {}
data['n_packaged'] = xint(_aggr(Sum('n_packaged'))['n_packaged__sum']) data['n_packaged'] = Package.objects.n_packaged()
data['n_overlay'] = xint(_aggr(Sum('n_overlay'))['n_overlay__sum']) data['n_overlay'] = Package.objects.n_overlay()
data['n_versions'] = xint(_aggr(Sum('n_versions'))['n_versions__sum']) data['n_versions'] = Package.objects.n_versions()
data['n_upstream'] = data['n_versions'] - data['n_packaged'] - \ data['n_upstream'] = Package.objects.n_upstream()
data['n_overlay']
data['n_packages'] = Package.objects.count() data['n_packages'] = Package.objects.count()
data['n_herds'] = Herd.objects.count() data['n_herds'] = Herd.objects.count()
data['n_maintainers'] = Maintainer.objects.count() data['n_maintainers'] = Maintainer.objects.count()
data['last_scan'] = EuscanResult.objects.get( data['last_scan'] = EuscanResult.objects.latest().datetime
id=EuscanResult.objects.aggregate(Max('id'))['id__max']
).datetime
return data return data
@ -76,22 +45,8 @@ class MaintainersHandler(AnonymousBaseHandler):
allowed_methods = ('GET',) allowed_methods = ('GET',)
def read(self, request): def read(self, request):
maintainers = Package.objects.filter(maintainers__isnull=False) maintainers = Package.objects.maintainers(rename=True)
maintainers = maintainers.values(
'maintainers__id',
'maintainers__name',
'maintainers__email'
)
maintainers = maintainers.annotate(n_packaged=Sum('n_packaged'),
n_overlay=Sum('n_overlay'),
n_versions=Sum('n_versions'))
maintainers = renameFields(
maintainers,
[('maintainers__id', 'id'),
('maintainers__name', 'name'),
('maintainers__email', 'email')]
)
return {'maintainers': maintainers} return {'maintainers': maintainers}
@ -102,14 +57,8 @@ class HerdsHandler(AnonymousBaseHandler):
def read(self, request): def read(self, request):
# FIXME: optimize the query, it uses 'LEFT OUTER JOIN' # FIXME: optimize the query, it uses 'LEFT OUTER JOIN'
# instead of 'INNER JOIN' # instead of 'INNER JOIN'
herds = Package.objects.filter(herds__isnull=False) herds = Package.objects.herds(rename=True)
herds = herds.values('herds__herd').annotate(
n_packaged=Sum('n_packaged'),
n_overlay=Sum('n_overlay'),
n_versions=Sum('n_versions')
)
herds = renameFields(herds, [('herds__herd', 'herd')])
return {'herds': herds} return {'herds': herds}
@ -118,10 +67,7 @@ class CategoriesHandler(AnonymousBaseHandler):
allowed_methods = ('GET',) allowed_methods = ('GET',)
def read(self, request): def read(self, request):
categories = Package.objects.values('category') categories = Package.objects.categories()
categories = categories.annotate(n_packaged=Sum('n_packaged'),
n_overlay=Sum('n_overlay'),
n_versions=Sum('n_versions'))
return {'categories': categories} return {'categories': categories}
@ -142,22 +88,17 @@ class PackagesHandler(AnonymousBaseHandler):
data = {} data = {}
if 'category' in kwargs: if 'category' in kwargs:
packages = Package.objects.filter(category=kwargs['category']) packages = Package.objects.for_category(kwargs['category'])
data = {'category': kwargs['category']} data = {'category': kwargs['category']}
elif 'herd' in kwargs: elif 'herd' in kwargs:
herd = Herd.objects.get(herd=kwargs['herd']) herd = Herd.objects.get(herd=kwargs['herd'])
packages = Package.objects.filter(herds__id=herd.id) packages = Package.objects.for_herd(herd)
data = {'herd': herd} data = {'herd': herd}
elif 'maintainer_id' in kwargs: elif 'maintainer_id' in kwargs:
maintainer = Maintainer.objects.get(id=kwargs['maintainer_id']) maintainer = Maintainer.objects.get(id=kwargs['maintainer_id'])
packages = Package.objects.filter(maintainers__id=maintainer.id) packages = Package.objects.for_maintainer(maintainer)
data = {'maintainer': maintainer} data = {'maintainer': maintainer}
packages = packages.select_related(
'last_version_gentoo',
'last_version_overlay',
'last_version_upstream'
)
data['packages'] = packages data['packages'] = packages
if not data: if not data:
@ -173,11 +114,13 @@ class PackageHandler(AnonymousBaseHandler):
@catch_and_return(ObjectDoesNotExist, rc.NOT_FOUND) @catch_and_return(ObjectDoesNotExist, rc.NOT_FOUND)
def read(self, request, category, package): def read(self, request, category, package):
package = Package.objects.get(category=category, name=package) package = Package.objects.get(category=category, name=package)
package.homepages = package.homepage.split(' ')
versions = Version.objects.filter(package=package) versions = Version.objects.filter(package=package)
log = EuscanResult.objects.filter(package=package).\ log = EuscanResult.objects.filter(package=package).\
order_by('-datetime')[:1] order_by('-datetime')[:1]
log = log[0] if log else None log = log[0] if log else None
vlog = VersionLog.objects.filter(package=package).order_by('-id') vlog = VersionLog.objects.filter(package=package).order_by('-id')
herds = [] herds = []

View File

@ -41,3 +41,28 @@ def packages_from_names(data):
else: else:
packages.extend(Package.objects.filter(name=pkg)) packages.extend(Package.objects.filter(name=pkg))
return packages return packages
def rename_fields(vqs, fields):
ret = []
for n in vqs:
for tr in fields:
if tr[0] in n:
n[tr[1]] = n[tr[0]]
del n[tr[0]]
ret.append(n)
return ret
class catch_and_return(object):
def __init__(self, err, response):
self.err = err
self.response = response
def __call__(self, fn):
def wrapper(*args, **kwargs):
try:
return fn(*args, **kwargs)
except self.err:
return self.response
return wrapper

View File

@ -3,7 +3,7 @@ djeuscan.managers
""" """
from django.db import models from django.db import models
from djeuscan.helpers import xint from djeuscan.helpers import xint, rename_fields
class PackageMixin(object): class PackageMixin(object):
@ -33,7 +33,7 @@ class PackageMixin(object):
n_versions=models.Sum('n_versions') n_versions=models.Sum('n_versions')
) )
def herds(self): def herds(self, rename=False):
""" """
Returns all the available herds Returns all the available herds
""" """
@ -45,9 +45,13 @@ class PackageMixin(object):
n_overlay=models.Sum('n_overlay'), n_overlay=models.Sum('n_overlay'),
n_versions=models.Sum('n_versions') n_versions=models.Sum('n_versions')
) )
if rename:
res = rename_fields(res, [('herds__herd', 'herd')])
return res return res
def maintainers(self): def maintainers(self, rename=False):
""" """
Returns all the available maintainers Returns all the available maintainers
""" """
@ -59,6 +63,15 @@ class PackageMixin(object):
n_overlay=models.Sum('n_overlay'), n_overlay=models.Sum('n_overlay'),
n_versions=models.Sum('n_versions') n_versions=models.Sum('n_versions')
) )
if rename:
res = rename_fields(
res,
[('maintainers__id', 'id'),
('maintainers__name', 'name'),
('maintainers__email', 'email')]
)
return res return res
def overlays(self): def overlays(self):