Change in PackageMixin, create ANNOTATE_DICT

This commit is contained in:
Slava Bacherikov 2012-05-05 18:28:41 +03:00
parent 0efba89342
commit b4162313f2

View File

@ -13,11 +13,14 @@ def gen_n_function(field_name):
return n_method return n_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):
n_packaged = gen_n_function('n_packaged') for name in N_LIST:
n_overlay = gen_n_function('n_overlay') locals()[name] = gen_n_function(name)
n_versions = gen_n_function('n_versions')
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()
@ -26,11 +29,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):
""" """
@ -39,11 +38,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')])
@ -57,11 +52,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(