euscan-ng/euscanwww/djeuscan/api/handlers.py

168 lines
5.2 KiB
Python
Raw Normal View History

2012-04-28 18:16:05 +02:00
from piston.handler import AnonymousBaseHandler
from piston.utils import rc
from django.core.exceptions import ObjectDoesNotExist
from django.forms.models import model_to_dict
2012-04-28 18:16:05 +02:00
from djeuscan.models import Version, Package, Herd, Maintainer, EuscanResult, \
VersionLog
2012-05-05 10:22:59 +02:00
from djeuscan.helpers import catch_and_return
# replace default XMLEmitter with ours
from piston.emitters import Emitter
2012-05-05 10:22:59 +02:00
from .emitters import EuscanXMLEmitter
Emitter.register('xml', EuscanXMLEmitter, 'text/xml; charset=utf-8')
2012-04-28 18:16:05 +02:00
# /api/1.0/
class RootHandler(AnonymousBaseHandler):
allowed_methods = ('GET',)
def read(self, request):
return {'api-version': '1.0'}
2012-04-28 18:16:05 +02:00
# /api/1.0/statistics
class StatisticsHandler(AnonymousBaseHandler):
allowed_methods = ('GET',)
def read(self, request):
data = {}
2012-05-05 10:22:59 +02:00
data['n_packaged'] = Package.objects.n_packaged()
data['n_overlay'] = Package.objects.n_overlay()
data['n_versions'] = Package.objects.n_versions()
data['n_upstream'] = Package.objects.n_upstream()
data['n_packages'] = Package.objects.count()
data['n_herds'] = Herd.objects.count()
data['n_maintainers'] = Maintainer.objects.count()
2012-05-05 10:22:59 +02:00
data['last_scan'] = EuscanResult.objects.latest().datetime
return data
2012-04-28 18:16:05 +02:00
# /api/1.0/maintainers
class MaintainersHandler(AnonymousBaseHandler):
allowed_methods = ('GET',)
def read(self, request):
2012-05-05 10:22:59 +02:00
maintainers = Package.objects.maintainers(rename=True)
2012-04-28 18:16:05 +02:00
return {'maintainers': maintainers}
# /api/1.0/herds
class HerdsHandler(AnonymousBaseHandler):
allowed_methods = ('GET',)
def read(self, request):
2012-04-28 18:16:05 +02:00
# FIXME: optimize the query, it uses 'LEFT OUTER JOIN'
# instead of 'INNER JOIN'
2012-05-05 10:22:59 +02:00
herds = Package.objects.herds(rename=True)
2012-04-28 18:16:05 +02:00
return {'herds': herds}
# /api/1.0/categories
class CategoriesHandler(AnonymousBaseHandler):
allowed_methods = ('GET',)
def read(self, request):
2012-05-05 10:22:59 +02:00
categories = Package.objects.categories()
2012-04-28 18:16:05 +02:00
return {'categories': categories}
# /api/1.0/packages/by-maintainer/
# /api/1.0/packages/by-category/
# /api/1.0/packages/by-herd/
class PackagesHandler(AnonymousBaseHandler):
allowed_methods = ('GET',)
fields = ('category', 'name', 'n_packaged', 'n_overlay', 'n_versions',
('last_version_gentoo', ('version',)),
('last_version_overlay', ('version',)),
('last_version_upstream', ('version',)))
model = Package
@catch_and_return(ObjectDoesNotExist, rc.NOT_FOUND)
def read(self, request, **kwargs):
data = {}
if 'category' in kwargs:
2012-05-05 10:22:59 +02:00
packages = Package.objects.for_category(kwargs['category'])
2012-04-28 18:16:05 +02:00
data = {'category': kwargs['category']}
elif 'herd' in kwargs:
herd = Herd.objects.get(herd=kwargs['herd'])
2012-05-05 10:22:59 +02:00
packages = Package.objects.for_herd(herd)
2012-04-28 18:16:05 +02:00
data = {'herd': herd}
elif 'maintainer_id' in kwargs:
maintainer = Maintainer.objects.get(id=kwargs['maintainer_id'])
2012-05-05 10:22:59 +02:00
packages = Package.objects.for_maintainer(maintainer)
2012-04-28 18:16:05 +02:00
data = {'maintainer': maintainer}
data['packages'] = packages
if not data:
return rc.NOT_FOUND
return data
2012-04-28 18:16:05 +02:00
# /api/1.0/package/
class PackageHandler(AnonymousBaseHandler):
allowed_methods = ('GET',)
@catch_and_return(ObjectDoesNotExist, rc.NOT_FOUND)
def read(self, request, category, package):
package = Package.objects.get(category=category, name=package)
2012-05-05 10:22:59 +02:00
versions = Version.objects.filter(package=package)
2012-05-05 10:22:59 +02:00
2012-04-28 18:16:05 +02:00
log = EuscanResult.objects.filter(package=package).\
order_by('-datetime')[:1]
log = log[0] if log else None
2012-05-05 10:22:59 +02:00
vlog = VersionLog.objects.filter(package=package).order_by('-id')
herds = []
for herd in package.herds.all():
herds.append(model_to_dict(herd, ['herd']))
maintainers = []
for maintainer in package.maintainers.all():
maintainers.append(model_to_dict(maintainer, ['name', 'email']))
version_log = []
for v in vlog:
2012-04-28 18:16:05 +02:00
v = model_to_dict(v, ['version', 'revision', 'slot', 'overlay',
'datetime', 'action'])
if v['action'] == VersionLog.VERSION_ADDED:
v['action'] = 'added'
if v['action'] == VersionLog.VERSION_REMOVED:
v['action'] = 'removed'
version_log.append(v)
upstream = []
packaged = []
for version in versions:
unpackaged = not version.packaged
2012-04-28 18:16:05 +02:00
version = model_to_dict(
version,
['version', 'revision', 'slot', 'overlay', 'urls']
)
if unpackaged:
upstream.append(version)
else:
packaged.append(version)
package = model_to_dict(package, ['category', 'name', 'description',
'homepage'])
package['herds'] = herds
package['maintainers'] = maintainers
package['packaged'] = packaged
package['upstream'] = upstream
package['vlog'] = version_log
if log:
package['log'] = model_to_dict(log, ['result', 'datetime'])
return package