From 0efba8934240822937a4a77f875413e42ba12e8d Mon Sep 17 00:00:00 2001 From: Slava Bacherikov Date: Sat, 5 May 2012 17:41:09 +0300 Subject: [PATCH 1/3] Change in PackageMixin --- euscanwww/djeuscan/managers.py | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/euscanwww/djeuscan/managers.py b/euscanwww/djeuscan/managers.py index 91d5f44..cfdc2b0 100644 --- a/euscanwww/djeuscan/managers.py +++ b/euscanwww/djeuscan/managers.py @@ -5,21 +5,20 @@ djeuscan.managers from django.db import models 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 + class PackageMixin(object): - - def n_packaged(self): - res = self.aggregate(models.Sum('n_packaged'))['n_packaged__sum'] - 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) - + + n_packaged = gen_n_function('n_packaged') + n_overlay = gen_n_function('n_overlay') + n_versions = gen_n_function('n_versions') + def n_upstream(self): return self.n_versions() - self.n_packaged() - self.n_overlay() From b4162313f24231f2f0b9383155eab9ff9d05a147 Mon Sep 17 00:00:00 2001 From: Slava Bacherikov Date: Sat, 5 May 2012 18:28:41 +0300 Subject: [PATCH 2/3] 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( From 3a56aa4391b785ab7b73c71f500957cbe69b0430 Mon Sep 17 00:00:00 2001 From: Slava Bacherikov Date: Sat, 5 May 2012 18:50:13 +0300 Subject: [PATCH 3/3] Rewrote some for_ methods in PackageMixin class --- euscanwww/djeuscan/managers.py | 51 +++++++++++++--------------------- 1 file changed, 19 insertions(+), 32 deletions(-) diff --git a/euscanwww/djeuscan/managers.py b/euscanwww/djeuscan/managers.py index bdde4f5..2307ca6 100644 --- a/euscanwww/djeuscan/managers.py +++ b/euscanwww/djeuscan/managers.py @@ -13,6 +13,22 @@ def gen_n_function(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 } @@ -80,38 +96,9 @@ class PackageMixin(object): ) return packages.filter(version__overlay=overlay).distinct() - def for_maintainer(self, maintainer, last_versions=False): - """ - Returns packages that belong to the given maintainer - """ - 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 + for_maintainer = gen_for_function('maintainers') + for_herd = gen_for_function('herds') + for_category = gen_for_function('category') class PackageQuerySet(models.query.QuerySet, PackageMixin):