From: <al...@us...> - 2009-12-22 22:08:45
|
Revision: 13558 http://translate.svn.sourceforge.net/translate/?rev=13558&view=rev Author: alaaosh Date: 2009-12-22 22:08:37 +0000 (Tue, 22 Dec 2009) Log Message: ----------- function to sum columns. uses django's orm aggregate feature if available to let the rdbms does the summing Added Paths: ----------- src/trunk/Pootle/local_apps/pootle_misc/aggregate.py Added: src/trunk/Pootle/local_apps/pootle_misc/aggregate.py =================================================================== --- src/trunk/Pootle/local_apps/pootle_misc/aggregate.py (rev 0) +++ src/trunk/Pootle/local_apps/pootle_misc/aggregate.py 2009-12-22 22:08:37 UTC (rev 13558) @@ -0,0 +1,33 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +# Copyright 2008-2009 Zuza Software Foundation +# +# This file is part of Pootle. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, see <http://www.gnu.org/licenses/>. + +try: + from django.db.models import Sum + + def sum_column(queryset, column): + result = queryset.aggregate(result=Sum(column))['result'] + if result is None: + return 0 + else: + return result +except ImportError: + def sum_column(queryset, column): + return sum(queryset.values_list(column, flat=True)) + This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |