Made raw queries portable to postgresql engine as well

This commit is contained in:
Tom Price
2015-03-03 02:44:16 +00:00
parent 2456092da5
commit 31d246fb27
2 changed files with 22 additions and 9 deletions

View File

@@ -1,4 +1,5 @@
from django.core.urlresolvers import reverse_lazy from django.core.urlresolvers import reverse_lazy
from django.db import connection
from django.http import Http404, HttpResponseRedirect from django.http import Http404, HttpResponseRedirect
from django.views import generic from django.views import generic
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
@@ -14,12 +15,16 @@ class InvoiceIndex(generic.ListView):
def get_queryset(self): def get_queryset(self):
# Manual query is the only way I have found to do this efficiently. Not ideal but needs must # Manual query is the only way I have found to do this efficiently. Not ideal but needs must
query = self.model.objects.raw( if connection.vendor == 'postgresql':
"SELECT *, " sql = """SELECT * FROM (SELECT *,
"(SELECT SUM(ei.cost * ei.quantity) FROM RIGS_eventitem AS ei WHERE ei.event_id = i.event_id) AS cost, " (SELECT SUM (ei.cost * ei.quantity) FROM "RIGS_eventitem" AS ei where ei.event_id = i.event_id) AS cost,
"(SELECT SUM(p.amount) FROM RIGS_payment AS p WHERE p.invoice_id = i.id) AS payments FROM RIGS_invoice as i " (SELECT SUM(p.amount) FROM "RIGS_payment" AS p WHERE p.invoice_id = i.id) AS payments
"HAVING (cost - payments) > 0;" FROM "RIGS_invoice" as i) AS sub
) WHERE (cost - payments) > 0;"""
else:
sql = "SELECT *, (SELECT SUM(ei.cost * ei.quantity) FROM RIGS_eventitem AS ei WHERE ei.event_id = i.event_id) AS cost, (SELECT SUM(p.amount) FROM RIGS_payment AS p WHERE p.invoice_id = i.id) AS payments FROM RIGS_invoice as i HAVING (cost - payments) > 0;"
query = self.model.objects.raw(sql)
items = [] items = []

View File

@@ -1,7 +1,7 @@
import hashlib import hashlib
import datetime import datetime
from django.db import models from django.db import models, connection
from django.contrib.auth.models import AbstractUser from django.contrib.auth.models import AbstractUser
from django.conf import settings from django.conf import settings
from django.utils.functional import cached_property from django.utils.functional import cached_property
@@ -248,7 +248,11 @@ class Event(models.Model, RevisionMixin):
@property @property
def sum_total(self): def sum_total(self):
# Manual querying is required for efficiency whilst maintaining floating point arithmetic # Manual querying is required for efficiency whilst maintaining floating point arithmetic
total = self.items.raw("SELECT id, SUM(quantity * cost) AS sum_total FROM RIGS_eventitem WHERE event_id=%i" % self.id)[0] if connection.vendor == 'postgresql':
sql = "SELECT SUM(quantity * cost) AS sum_total FROM \"RIGS_eventitem\" WHERE event_id=%i" % self.id
else:
sql = "SELECT id, SUM(quantity * cost) AS sum_total FROM RIGS_eventitem WHERE event_id=%i" % self.id
total = self.items.raw(sql)[0]
if total.sum_total: if total.sum_total:
return total.sum_total return total.sum_total
return 0.0 return 0.0
@@ -331,7 +335,11 @@ class Invoice(models.Model):
@property @property
def payment_total(self): def payment_total(self):
# Manual querying is required for efficiency whilst maintaining floating point arithmetic # Manual querying is required for efficiency whilst maintaining floating point arithmetic
total = self.payment_set.raw("SELECT id, SUM(amount) AS total FROM RIGS_payment WHERE invoice_id=%i" % self.id)[0] if connection.vendor == 'postgresql':
sql = "SELECT SUM(amount) AS total FROM \"RIGS_payment\" WHERE invoice_id=%i" % self.id
else:
sql = "SELECT id, SUM(amount) AS total FROM RIGS_payment WHERE invoice_id=%i" % self.id
total = self.payment_set.raw(sql)[0]
if total.total: if total.total:
return total.total return total.total
return 0.0 return 0.0