commit
37a623a97a
@ -5,20 +5,38 @@ djeuscan.managers
|
|||||||
from django.db import models
|
from django.db import models
|
||||||
from djeuscan.helpers import xint, rename_fields, select_related_last_versions
|
from djeuscan.helpers import xint, rename_fields, select_related_last_versions
|
||||||
|
|
||||||
|
def gen_n_function(field_name):
|
||||||
|
def n_method(self):
|
||||||
|
res = self.aggregate(models.Sum(field_name))[field_name + '__sum']
|
||||||
|
return xint(res)
|
||||||
|
n_method.func_name = field_name
|
||||||
|
return n_method
|
||||||
|
|
||||||
|
|
||||||
|
def gen_for_function(field):
|
||||||
|
def for_method(self, val, last_versions=False):
|
||||||
|
"""
|
||||||
|
Returns packages that belong to the given parametrs
|
||||||
|
"""
|
||||||
|
res = self.filter(**{field : val})
|
||||||
|
|
||||||
|
if last_versions:
|
||||||
|
select_related_last_versions(res)
|
||||||
|
|
||||||
|
return res
|
||||||
|
|
||||||
|
for_method.func_name = 'for_' + field
|
||||||
|
return for_method
|
||||||
|
|
||||||
|
|
||||||
|
N_LIST = ['n_packaged','n_overlay','n_versions']
|
||||||
|
|
||||||
|
ANNOTATE_DICT = { name: models.Sum(name) for name in N_LIST }
|
||||||
|
|
||||||
class PackageMixin(object):
|
class PackageMixin(object):
|
||||||
|
|
||||||
def n_packaged(self):
|
for name in N_LIST:
|
||||||
res = self.aggregate(models.Sum('n_packaged'))['n_packaged__sum']
|
locals()[name] = gen_n_function(name)
|
||||||
return xint(res)
|
|
||||||
|
|
||||||
def n_overlay(self):
|
|
||||||
res = self.aggregate(models.Sum('n_overlay'))['n_overlay__sum']
|
|
||||||
return xint(res)
|
|
||||||
|
|
||||||
def n_versions(self):
|
|
||||||
res = self.aggregate(models.Sum('n_versions'))['n_versions__sum']
|
|
||||||
return xint(res)
|
|
||||||
|
|
||||||
def n_upstream(self):
|
def n_upstream(self):
|
||||||
return self.n_versions() - self.n_packaged() - self.n_overlay()
|
return self.n_versions() - self.n_packaged() - self.n_overlay()
|
||||||
@ -27,11 +45,7 @@ class PackageMixin(object):
|
|||||||
"""
|
"""
|
||||||
Returns all the available categories
|
Returns all the available categories
|
||||||
"""
|
"""
|
||||||
return self.values('category').annotate(
|
return self.values('category').annotate(**ANNOTATE_DICT)
|
||||||
n_packaged=models.Sum('n_packaged'),
|
|
||||||
n_overlay=models.Sum('n_overlay'),
|
|
||||||
n_versions=models.Sum('n_versions')
|
|
||||||
)
|
|
||||||
|
|
||||||
def herds(self, rename=False):
|
def herds(self, rename=False):
|
||||||
"""
|
"""
|
||||||
@ -40,11 +54,7 @@ class PackageMixin(object):
|
|||||||
# FIXME: optimize the query, it uses 'LEFT OUTER JOIN' instead of
|
# FIXME: optimize the query, it uses 'LEFT OUTER JOIN' instead of
|
||||||
# 'INNER JOIN'
|
# 'INNER JOIN'
|
||||||
res = self.filter(herds__isnull=False)
|
res = self.filter(herds__isnull=False)
|
||||||
res = res.values('herds__herd').annotate(
|
res = res.values('herds__herd').annotate(**ANNOTATE_DICT)
|
||||||
n_packaged=models.Sum('n_packaged'),
|
|
||||||
n_overlay=models.Sum('n_overlay'),
|
|
||||||
n_versions=models.Sum('n_versions')
|
|
||||||
)
|
|
||||||
|
|
||||||
if rename:
|
if rename:
|
||||||
res = rename_fields(res, [('herds__herd', 'herd')])
|
res = rename_fields(res, [('herds__herd', 'herd')])
|
||||||
@ -58,11 +68,7 @@ class PackageMixin(object):
|
|||||||
res = self.filter(maintainers__isnull=False).values(
|
res = self.filter(maintainers__isnull=False).values(
|
||||||
'maintainers__id', 'maintainers__name', 'maintainers__email'
|
'maintainers__id', 'maintainers__name', 'maintainers__email'
|
||||||
)
|
)
|
||||||
res = res.annotate(
|
res = res.annotate(**ANNOTATE_DICT)
|
||||||
n_packaged=models.Sum('n_packaged'),
|
|
||||||
n_overlay=models.Sum('n_overlay'),
|
|
||||||
n_versions=models.Sum('n_versions')
|
|
||||||
)
|
|
||||||
|
|
||||||
if rename:
|
if rename:
|
||||||
res = rename_fields(
|
res = rename_fields(
|
||||||
@ -90,38 +96,9 @@ class PackageMixin(object):
|
|||||||
)
|
)
|
||||||
return packages.filter(version__overlay=overlay).distinct()
|
return packages.filter(version__overlay=overlay).distinct()
|
||||||
|
|
||||||
def for_maintainer(self, maintainer, last_versions=False):
|
for_maintainer = gen_for_function('maintainers')
|
||||||
"""
|
for_herd = gen_for_function('herds')
|
||||||
Returns packages that belong to the given maintainer
|
for_category = gen_for_function('category')
|
||||||
"""
|
|
||||||
res = self.filter(maintainers__id=maintainer.id)
|
|
||||||
|
|
||||||
if last_versions:
|
|
||||||
select_related_last_versions(res)
|
|
||||||
|
|
||||||
return res
|
|
||||||
|
|
||||||
def for_herd(self, herd, last_versions=False):
|
|
||||||
"""
|
|
||||||
Returns packages that belong to the given herd
|
|
||||||
"""
|
|
||||||
res = self.filter(herds__id=herd.id)
|
|
||||||
|
|
||||||
if last_versions:
|
|
||||||
select_related_last_versions(res)
|
|
||||||
|
|
||||||
return res
|
|
||||||
|
|
||||||
def for_category(self, category, last_versions=False):
|
|
||||||
"""
|
|
||||||
Returns packages that belong to the given category
|
|
||||||
"""
|
|
||||||
res = self.filter(category=category)
|
|
||||||
|
|
||||||
if last_versions:
|
|
||||||
select_related_last_versions(res)
|
|
||||||
|
|
||||||
return res
|
|
||||||
|
|
||||||
|
|
||||||
class PackageQuerySet(models.query.QuerySet, PackageMixin):
|
class PackageQuerySet(models.query.QuerySet, PackageMixin):
|
||||||
|
Loading…
Reference in New Issue
Block a user