From b4162313f24231f2f0b9383155eab9ff9d05a147 Mon Sep 17 00:00:00 2001 From: Slava Bacherikov Date: Sat, 5 May 2012 18:28:41 +0300 Subject: [PATCH] Change in PackageMixin, create ANNOTATE_DICT --- euscanwww/djeuscan/managers.py | 31 +++++++++++-------------------- 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/euscanwww/djeuscan/managers.py b/euscanwww/djeuscan/managers.py index cfdc2b0..bdde4f5 100644 --- a/euscanwww/djeuscan/managers.py +++ b/euscanwww/djeuscan/managers.py @@ -13,12 +13,15 @@ def gen_n_function(field_name): 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): - - n_packaged = gen_n_function('n_packaged') - n_overlay = gen_n_function('n_overlay') - n_versions = gen_n_function('n_versions') - + + for name in N_LIST: + locals()[name] = gen_n_function(name) + def n_upstream(self): return self.n_versions() - self.n_packaged() - self.n_overlay() @@ -26,11 +29,7 @@ class PackageMixin(object): """ Returns all the available categories """ - return self.values('category').annotate( - n_packaged=models.Sum('n_packaged'), - n_overlay=models.Sum('n_overlay'), - n_versions=models.Sum('n_versions') - ) + return self.values('category').annotate(**ANNOTATE_DICT) def herds(self, rename=False): """ @@ -39,11 +38,7 @@ class PackageMixin(object): # FIXME: optimize the query, it uses 'LEFT OUTER JOIN' instead of # 'INNER JOIN' res = self.filter(herds__isnull=False) - res = res.values('herds__herd').annotate( - n_packaged=models.Sum('n_packaged'), - n_overlay=models.Sum('n_overlay'), - n_versions=models.Sum('n_versions') - ) + res = res.values('herds__herd').annotate(**ANNOTATE_DICT) if rename: res = rename_fields(res, [('herds__herd', 'herd')]) @@ -57,11 +52,7 @@ class PackageMixin(object): res = self.filter(maintainers__isnull=False).values( 'maintainers__id', 'maintainers__name', 'maintainers__email' ) - res = res.annotate( - n_packaged=models.Sum('n_packaged'), - n_overlay=models.Sum('n_overlay'), - n_versions=models.Sum('n_versions') - ) + res = res.annotate(**ANNOTATE_DICT) if rename: res = rename_fields(