2012-05-01 16:56:09 +02:00
|
|
|
"""
|
|
|
|
djeuscan.managers
|
|
|
|
"""
|
|
|
|
|
|
|
|
from django.db import models
|
2012-05-05 10:54:04 +02:00
|
|
|
from djeuscan.helpers import xint, rename_fields, select_related_last_versions
|
2012-05-01 16:56:09 +02:00
|
|
|
|
2012-05-05 16:55:59 +02:00
|
|
|
|
|
|
|
def _gen_n_function(field_name):
|
2012-05-05 16:41:09 +02:00
|
|
|
def n_method(self):
|
|
|
|
res = self.aggregate(models.Sum(field_name))[field_name + '__sum']
|
2012-05-01 16:56:09 +02:00
|
|
|
return xint(res)
|
2012-05-05 16:41:09 +02:00
|
|
|
n_method.func_name = field_name
|
|
|
|
return n_method
|
2012-05-01 16:56:09 +02:00
|
|
|
|
|
|
|
|
2012-05-05 16:55:59 +02:00
|
|
|
def _gen_for_function(field):
|
2012-05-05 17:50:13 +02:00
|
|
|
def for_method(self, val, last_versions=False):
|
|
|
|
"""
|
|
|
|
Returns packages that belong to the given parametrs
|
|
|
|
"""
|
2012-05-05 16:55:59 +02:00
|
|
|
res = self.filter(**{field: val})
|
2012-05-05 17:50:13 +02:00
|
|
|
|
|
|
|
if last_versions:
|
|
|
|
select_related_last_versions(res)
|
|
|
|
|
|
|
|
return res
|
2012-05-05 16:55:59 +02:00
|
|
|
|
2012-05-05 17:50:13 +02:00
|
|
|
for_method.func_name = 'for_' + field
|
|
|
|
return for_method
|
|
|
|
|
|
|
|
|
2012-05-05 16:59:18 +02:00
|
|
|
ANNOTATE_DICT = {name: models.Sum(name)
|
|
|
|
for name in ['n_packaged', 'n_overlay', 'n_versions']}
|
2012-05-05 17:28:41 +02:00
|
|
|
|
|
|
|
|
2012-05-05 16:41:09 +02:00
|
|
|
class PackageMixin(object):
|
2012-05-05 16:55:59 +02:00
|
|
|
|
|
|
|
for_maintainer = _gen_for_function('maintainers')
|
|
|
|
for_herd = _gen_for_function('herds')
|
|
|
|
for_category = _gen_for_function('category')
|
|
|
|
|
2012-05-05 16:59:18 +02:00
|
|
|
n_packaged = _gen_n_function("n_packaged")
|
|
|
|
n_overlay = _gen_n_function("n_overlay")
|
|
|
|
n_versions = _gen_n_function("n_versions")
|
2012-05-05 16:55:59 +02:00
|
|
|
|
2012-05-01 16:56:09 +02:00
|
|
|
def n_upstream(self):
|
|
|
|
return self.n_versions() - self.n_packaged() - self.n_overlay()
|
|
|
|
|
|
|
|
def categories(self):
|
2012-05-01 17:08:49 +02:00
|
|
|
"""
|
|
|
|
Returns all the available categories
|
|
|
|
"""
|
2012-05-05 17:28:41 +02:00
|
|
|
return self.values('category').annotate(**ANNOTATE_DICT)
|
2012-05-01 16:56:09 +02:00
|
|
|
|
2012-05-05 10:22:59 +02:00
|
|
|
def herds(self, rename=False):
|
2012-05-01 17:08:49 +02:00
|
|
|
"""
|
|
|
|
Returns all the available herds
|
|
|
|
"""
|
2012-05-01 16:56:09 +02:00
|
|
|
# FIXME: optimize the query, it uses 'LEFT OUTER JOIN' instead of
|
|
|
|
# 'INNER JOIN'
|
|
|
|
res = self.filter(herds__isnull=False)
|
2012-05-05 17:28:41 +02:00
|
|
|
res = res.values('herds__herd').annotate(**ANNOTATE_DICT)
|
2012-05-05 10:22:59 +02:00
|
|
|
|
|
|
|
if rename:
|
|
|
|
res = rename_fields(res, [('herds__herd', 'herd')])
|
|
|
|
|
2012-05-01 16:56:09 +02:00
|
|
|
return res
|
|
|
|
|
2012-05-05 10:22:59 +02:00
|
|
|
def maintainers(self, rename=False):
|
2012-05-01 17:08:49 +02:00
|
|
|
"""
|
|
|
|
Returns all the available maintainers
|
|
|
|
"""
|
2012-05-01 16:56:09 +02:00
|
|
|
res = self.filter(maintainers__isnull=False).values(
|
|
|
|
'maintainers__id', 'maintainers__name', 'maintainers__email'
|
|
|
|
)
|
2012-05-05 17:28:41 +02:00
|
|
|
res = res.annotate(**ANNOTATE_DICT)
|
2012-05-05 10:22:59 +02:00
|
|
|
|
|
|
|
if rename:
|
|
|
|
res = rename_fields(
|
|
|
|
res,
|
|
|
|
[('maintainers__id', 'id'),
|
|
|
|
('maintainers__name', 'name'),
|
|
|
|
('maintainers__email', 'email')]
|
|
|
|
)
|
|
|
|
|
2012-05-01 16:56:09 +02:00
|
|
|
return res
|
|
|
|
|
|
|
|
def overlays(self):
|
2012-05-01 17:08:49 +02:00
|
|
|
"""
|
|
|
|
Returns the all available overlays
|
|
|
|
"""
|
2012-05-01 16:56:09 +02:00
|
|
|
res = self.values('version__overlay').exclude(version__overlay='')
|
|
|
|
return res.distinct()
|
2012-05-01 17:08:49 +02:00
|
|
|
|
|
|
|
def for_overlay(self, overlay):
|
|
|
|
"""
|
|
|
|
Returns packages that belong to the given overlay
|
|
|
|
"""
|
|
|
|
packages = self.values(
|
|
|
|
'id', 'name', 'category', 'n_versions', 'n_packaged', 'n_overlay'
|
|
|
|
)
|
|
|
|
return packages.filter(version__overlay=overlay).distinct()
|
2012-05-01 21:15:55 +02:00
|
|
|
|
2012-05-05 10:03:02 +02:00
|
|
|
|
|
|
|
class PackageQuerySet(models.query.QuerySet, PackageMixin):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class PackageManager(models.Manager, PackageMixin):
|
|
|
|
def get_query_set(self):
|
|
|
|
return PackageQuerySet(self.model, using=self._db)
|
2012-05-05 11:48:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
class VersionLogMixin(object):
|
|
|
|
def for_package(self, package, order=False):
|
2012-05-07 19:09:08 +02:00
|
|
|
res = self.filter(package=package)
|
2012-05-05 11:48:12 +02:00
|
|
|
if order:
|
|
|
|
res = res.order_by('-id')
|
|
|
|
return res
|
|
|
|
|
|
|
|
def for_maintainer(self, maintainer, order=False):
|
|
|
|
res = self.filter(package__maintainers__id=maintainer.id)
|
|
|
|
if order:
|
|
|
|
res = res.order_by('-id')
|
|
|
|
return res
|
|
|
|
|
|
|
|
def for_category(self, category, order=False):
|
|
|
|
res = self.filter(package__category=category)
|
|
|
|
if order:
|
|
|
|
res = res.order_by('-id')
|
|
|
|
return res
|
|
|
|
|
2012-05-12 16:07:49 +02:00
|
|
|
def for_herd(self, herd, order=False):
|
|
|
|
res = self.filter(package__herds__id=herd.id)
|
|
|
|
if order:
|
|
|
|
res = res.order_by('-id')
|
|
|
|
return res
|
|
|
|
|
2012-05-05 11:48:12 +02:00
|
|
|
|
|
|
|
class VersionLogQuerySet(models.query.QuerySet, VersionLogMixin):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class VersionLogManager(models.Manager, VersionLogMixin):
|
|
|
|
def get_query_set(self):
|
|
|
|
return VersionLogQuerySet(self.model, using=self._db)
|