Permalink
Browse files

Fix sum of boolean fields for postgresql

  • Loading branch information...
nijel committed Jul 19, 2013
1 parent afcf3a7 commit a8fd43c688debb41d9bf8487699ad85dd395fbc3
Showing with 58 additions and 4 deletions.
  1. +53 −0 trans/boolean_sum.py
  2. +5 −4 trans/models/translation.py
@@ -0,0 +1,53 @@
# -*- coding: utf-8 -*-
#
# Copyright © 2012 - 2013 Michal Čihař <michal@cihar.com>
#
# This file is part of Weblate <http://weblate.org/>
#
# 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 3 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/>.
#
'''
Wrapper for Sum to work with PostgreSQL database.
See also https://code.djangoproject.com/ticket/17564
'''
from django.conf import settings
from django.db.models.aggregates import Sum
from django.db.models.sql.aggregates import Sum as BaseSQLSum
class SQLSum(BaseSQLSum):
@property
def sql_template(self):
'''
Adds type casting to boolean values for PostgreSQL.
'''
if (settings.DATABASES['default']['ENGINE']
== 'django.db.backends.postgresql_psycopg2'):
return '%(function)s(%(field)s::int)'
return '%(function)s(%(field)s)'
class BooleanSum(Sum):
'''
Sum for boolean fields.
'''
def add_to_query(self, query, alias, col, source, is_summary):
'''
Generates query to use SQLSum class with type casting.
'''
aggregate = SQLSum(
col, source=source, is_summary=is_summary, **self.extra
)
query.aggregates[alias] = aggregate
@@ -42,6 +42,7 @@
from trans.models.project import Project
from trans.util import get_user_display, get_site_url, sleep_while_git_locked
from trans.mixins import URLMixin
from trans.boolean_sum import BooleanSum
class TranslationManager(models.Manager):
@@ -616,10 +617,10 @@ def update_stats(self):
# Grab stats
stats = self.unit_set.aggregate(
Sum('num_words'),
Sum('fuzzy'),
Sum('translated'),
Sum('has_failing_check'),
Sum('has_suggestion'),
BooleanSum('fuzzy'),
BooleanSum('translated'),
BooleanSum('has_failing_check'),
BooleanSum('has_suggestion'),
Count('id'),
)

0 comments on commit a8fd43c

Please sign in to comment.