Commit 9a087355 authored by Michal Čihař's avatar Michal Čihař

Replace BooleanSum with Django native stuff

We now no longer need to guess database backend.

Fixes #946
Signed-off-by: default avatarMichal Čihař <michal@cihar.com>
parent 61ad48fa
......@@ -23,56 +23,16 @@ 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
from django.db.models import Sum, When, Case, IntegerField
def get_template():
'''
Adds type casting to boolean values for PostgreSQL.
'''
engine = settings.DATABASES['default']['ENGINE']
if 'psycopg2' in engine or 'postgres' in engine:
return '%(function)s(%(field)s::int)'
return '%(function)s(%(field)s)'
class SQLSum(BaseSQLSum):
@property
def sql_template(self):
"""
Returns template for the SQL
"""
return get_template()
class BooleanSum(Sum):
'''
Sum for boolean fields.
'''
# pylint: disable=W0223
def add_to_query(self, query, alias, col, source, is_summary):
'''
Generates query to use SQLSum class with type casting.
Used for Django 1.7
'''
aggregate = SQLSum(
col, source=source, is_summary=is_summary, **self.extra
def do_boolean_sum(field):
"""Wrapper to generate SUM on boolean values"""
cond = {field: True}
return Sum(
Case(
When(then=1, **cond),
default=0,
output_field=IntegerField()
)
query.aggregates[alias] = aggregate
def _patch_aggregate(self, query):
"""
Wrapper to disable compatibility layer in Django 1.8
"""
return
@property
def template(self):
"""
Returns template for the SQL
Used for Django 1.8 and newer
"""
return get_template()
)
......@@ -4,7 +4,7 @@ from __future__ import unicode_literals
from django.db import migrations
from django.db.models.aggregates import Sum
from weblate.trans.boolean_sum import BooleanSum
from weblate.trans.boolean_sum import do_boolean_sum
def fill_in_have_comment(apps, schema_editor):
......@@ -13,7 +13,7 @@ def fill_in_have_comment(apps, schema_editor):
for translation in Translation.objects.all():
stats = translation.unit_set.aggregate(
Sum('num_words'),
BooleanSum('has_comment'),
has_comment__sum=do_boolean_sum('has_comment'),
)
if stats['num_words__sum'] is not None:
translation.have_comment = int(stats['has_comment__sum'])
......
......@@ -44,7 +44,7 @@ from weblate.trans.site import get_site_url
from weblate.trans.util import translation_percent, split_plural
from weblate.accounts.avatar import get_user_display
from weblate.trans.mixins import URLMixin, PercentMixin, LoggerMixin
from weblate.trans.boolean_sum import BooleanSum
from weblate.trans.boolean_sum import do_boolean_sum
from weblate.accounts.models import notify_new_string, get_author_name
from weblate.trans.models.changes import Change
from weblate.trans.checklists import TranslationChecklist
......@@ -608,12 +608,12 @@ class Translation(models.Model, URLMixin, PercentMixin, LoggerMixin):
# Grab stats
stats = self.unit_set.aggregate(
Sum('num_words'),
BooleanSum('fuzzy'),
BooleanSum('translated'),
BooleanSum('has_failing_check'),
BooleanSum('has_suggestion'),
BooleanSum('has_comment'),
Count('id'),
fuzzy__sum=do_boolean_sum('fuzzy'),
translated__sum=do_boolean_sum('translated'),
has_failing_check__sum=do_boolean_sum('has_failing_check'),
has_suggestion__sum=do_boolean_sum('has_suggestion'),
has_comment__sum=do_boolean_sum('has_comment'),
)
# Check if we have any units
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment