Use db for function instead of importing from sqlalchemy.
This commit is contained in:
parent
e348c8d21e
commit
a6da594aeb
@ -14,9 +14,6 @@
|
|||||||
You should have received a copy of the GNU Affero General Public License
|
You should have received a copy of the GNU Affero General Public License
|
||||||
along with Accountant. If not, see <http://www.gnu.org/licenses/>.
|
along with Accountant. If not, see <http://www.gnu.org/licenses/>.
|
||||||
"""
|
"""
|
||||||
from sqlalchemy import func, case, literal_column
|
|
||||||
from sqlalchemy.orm import validates
|
|
||||||
|
|
||||||
from accountant import db
|
from accountant import db
|
||||||
|
|
||||||
from .operations import Operation
|
from .operations import Operation
|
||||||
@ -35,17 +32,17 @@ class Account(db.Model):
|
|||||||
def query(cls, begin=None, end=None):
|
def query(cls, begin=None, end=None):
|
||||||
status_query = db.session.query(
|
status_query = db.session.query(
|
||||||
Operation.account_id,
|
Operation.account_id,
|
||||||
func.sum(Operation.value).label("future"),
|
db.func.sum(Operation.value).label("future"),
|
||||||
func.sum(
|
db.func.sum(
|
||||||
case(
|
db.case(
|
||||||
[(Operation.pointed,
|
[(Operation.pointed,
|
||||||
Operation.value)],
|
Operation.value)],
|
||||||
else_=0
|
else_=0
|
||||||
)
|
)
|
||||||
).label("pointed"),
|
).label("pointed"),
|
||||||
func.sum(
|
db.func.sum(
|
||||||
case(
|
db.case(
|
||||||
[(Operation.operation_date <= func.current_date(),
|
[(Operation.operation_date <= db.func.current_date(),
|
||||||
Operation.value)],
|
Operation.value)],
|
||||||
else_=0
|
else_=0
|
||||||
)
|
)
|
||||||
@ -57,19 +54,21 @@ class Account(db.Model):
|
|||||||
if begin and end:
|
if begin and end:
|
||||||
balance_query = db.session.query(
|
balance_query = db.session.query(
|
||||||
Operation.account_id,
|
Operation.account_id,
|
||||||
func.sum(
|
db.func.sum(
|
||||||
case(
|
db.case(
|
||||||
[(func.sign(Operation.value) == -1, Operation.value)],
|
[(db.func.sign(Operation.value) == -1,
|
||||||
|
Operation.value)],
|
||||||
else_=0
|
else_=0
|
||||||
)
|
)
|
||||||
).label("expenses"),
|
).label("expenses"),
|
||||||
func.sum(
|
db.func.sum(
|
||||||
case(
|
db.case(
|
||||||
[(func.sign(Operation.value) == 1, Operation.value)],
|
[(db.func.sign(Operation.value) == 1,
|
||||||
|
Operation.value)],
|
||||||
else_=0
|
else_=0
|
||||||
)
|
)
|
||||||
).label("revenues"),
|
).label("revenues"),
|
||||||
func.sum(Operation.value).label("balance")
|
db.func.sum(Operation.value).label("balance")
|
||||||
).filter(
|
).filter(
|
||||||
Operation.operation_date >= str(begin),
|
Operation.operation_date >= str(begin),
|
||||||
Operation.operation_date <= str(end)
|
Operation.operation_date <= str(end)
|
||||||
@ -81,12 +80,18 @@ class Account(db.Model):
|
|||||||
cls.id,
|
cls.id,
|
||||||
cls.name,
|
cls.name,
|
||||||
cls.authorized_overdraft,
|
cls.authorized_overdraft,
|
||||||
func.coalesce(status_query.c.current, 0).label('current'),
|
db.func.coalesce(
|
||||||
func.coalesce(status_query.c.pointed, 0).label('pointed'),
|
status_query.c.current, 0).label('current'),
|
||||||
func.coalesce(status_query.c.future, 0).label('future'),
|
db.func.coalesce(
|
||||||
func.coalesce(balance_query.c.expenses, 0).label('expenses'),
|
status_query.c.pointed, 0).label('pointed'),
|
||||||
func.coalesce(balance_query.c.revenues, 0).label('revenues'),
|
db.func.coalesce(
|
||||||
func.coalesce(balance_query.c.balance, 0).label('balance'),
|
status_query.c.future, 0).label('future'),
|
||||||
|
db.func.coalesce(
|
||||||
|
balance_query.c.expenses, 0).label('expenses'),
|
||||||
|
db.func.coalesce(
|
||||||
|
balance_query.c.revenues, 0).label('revenues'),
|
||||||
|
db.func.coalesce(
|
||||||
|
balance_query.c.balance, 0).label('balance'),
|
||||||
).outerjoin(
|
).outerjoin(
|
||||||
status_query, status_query.c.account_id == cls.id
|
status_query, status_query.c.account_id == cls.id
|
||||||
).outerjoin(
|
).outerjoin(
|
||||||
@ -97,19 +102,19 @@ class Account(db.Model):
|
|||||||
cls.id,
|
cls.id,
|
||||||
cls.name,
|
cls.name,
|
||||||
cls.authorized_overdraft,
|
cls.authorized_overdraft,
|
||||||
func.coalesce(status_query.c.current, 0).label('current'),
|
db.func.coalesce(status_query.c.current, 0).label('current'),
|
||||||
func.coalesce(status_query.c.pointed, 0).label('pointed'),
|
db.func.coalesce(status_query.c.pointed, 0).label('pointed'),
|
||||||
func.coalesce(status_query.c.future, 0).label('future'),
|
db.func.coalesce(status_query.c.future, 0).label('future'),
|
||||||
literal_column("0").label('expenses'),
|
db.literal_column("0").label('expenses'),
|
||||||
literal_column("0").label('revenues'),
|
db.literal_column("0").label('revenues'),
|
||||||
literal_column("0").label('balance'),
|
db.literal_column("0").label('balance'),
|
||||||
).outerjoin(
|
).outerjoin(
|
||||||
status_query, status_query.c.account_id == cls.id
|
status_query, status_query.c.account_id == cls.id
|
||||||
)
|
)
|
||||||
|
|
||||||
return query.order_by(cls.name)
|
return query.order_by(cls.name)
|
||||||
|
|
||||||
@validates('authorized_overdraft')
|
@db.validates('authorized_overdraft')
|
||||||
def validate_authorized_overdraft(self, key, authorized_overdraft):
|
def validate_authorized_overdraft(self, key, authorized_overdraft):
|
||||||
assert authorized_overdraft <= 0
|
assert authorized_overdraft <= 0
|
||||||
|
|
||||||
|
@ -18,9 +18,6 @@ from datetime import date
|
|||||||
|
|
||||||
import arrow
|
import arrow
|
||||||
|
|
||||||
from sqlalchemy import func, case, desc, true, false, text
|
|
||||||
from sqlalchemy.orm import column_property
|
|
||||||
|
|
||||||
from accountant import db
|
from accountant import db
|
||||||
|
|
||||||
|
|
||||||
@ -31,7 +28,7 @@ class Operation(db.Model):
|
|||||||
db.Date,
|
db.Date,
|
||||||
nullable=False,
|
nullable=False,
|
||||||
default=date.today,
|
default=date.today,
|
||||||
server_default=func.current_date(),
|
server_default=db.func.current_date(),
|
||||||
index=True
|
index=True
|
||||||
)
|
)
|
||||||
label = db.Column(db.String(500), nullable=False)
|
label = db.Column(db.String(500), nullable=False)
|
||||||
@ -59,32 +56,37 @@ class Operation(db.Model):
|
|||||||
db.Boolean,
|
db.Boolean,
|
||||||
nullable=False,
|
nullable=False,
|
||||||
default=False,
|
default=False,
|
||||||
server_default=false()
|
server_default=db.false()
|
||||||
)
|
)
|
||||||
|
|
||||||
confirmed = db.Column(
|
confirmed = db.Column(
|
||||||
db.Boolean,
|
db.Boolean,
|
||||||
nullable=False,
|
nullable=False,
|
||||||
default=True,
|
default=True,
|
||||||
server_default=true()
|
server_default=db.true()
|
||||||
)
|
)
|
||||||
|
|
||||||
canceled = db.Column(
|
canceled = db.Column(
|
||||||
db.Boolean,
|
db.Boolean,
|
||||||
nullable=False,
|
nullable=False,
|
||||||
default=False,
|
default=False,
|
||||||
server_default=false()
|
server_default=db.false()
|
||||||
)
|
)
|
||||||
|
|
||||||
sold = column_property(
|
sold = db.column_property(
|
||||||
func.sum(
|
db.func.sum(
|
||||||
case(
|
db.case(
|
||||||
whens={canceled: text("0")},
|
whens={canceled: db.text("0")},
|
||||||
else_=value
|
else_=value
|
||||||
)
|
)
|
||||||
).over(
|
).over(
|
||||||
partition_by=[account_id],
|
partition_by=[account_id],
|
||||||
order_by=["operation_date", desc("value"), desc("label"), id]
|
order_by=[
|
||||||
|
"operation_date",
|
||||||
|
db.desc("value"),
|
||||||
|
db.desc("label"),
|
||||||
|
id
|
||||||
|
]
|
||||||
).label("sold")
|
).label("sold")
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -125,7 +127,7 @@ class Operation(db.Model):
|
|||||||
).join(
|
).join(
|
||||||
base_query, base_query.c.id == cls.id
|
base_query, base_query.c.id == cls.id
|
||||||
).order_by(
|
).order_by(
|
||||||
desc(cls.operation_date),
|
db.desc(cls.operation_date),
|
||||||
cls.value,
|
cls.value,
|
||||||
cls.label,
|
cls.label,
|
||||||
)
|
)
|
||||||
@ -147,13 +149,13 @@ class Operation(db.Model):
|
|||||||
|
|
||||||
query = db.session.query(
|
query = db.session.query(
|
||||||
cls.category,
|
cls.category,
|
||||||
func.sum(
|
db.func.sum(
|
||||||
case([(func.sign(cls.value) == -1, cls.value)], else_=0)
|
db.case([(db.func.sign(cls.value) == -1, cls.value)], else_=0)
|
||||||
).label("expenses"),
|
).label("expenses"),
|
||||||
func.sum(
|
db.func.sum(
|
||||||
case([(func.sign(cls.value) == 1, cls.value)], else_=0)
|
db.case([(db.func.sign(cls.value) == 1, cls.value)], else_=0)
|
||||||
).label("revenues"),
|
).label("revenues"),
|
||||||
func.sum(cls.value).label("balance")
|
db.func.sum(cls.value).label("balance")
|
||||||
).filter(
|
).filter(
|
||||||
cls.account_id == account_id
|
cls.account_id == account_id
|
||||||
).filter(
|
).filter(
|
||||||
@ -176,8 +178,8 @@ class Operation(db.Model):
|
|||||||
|
|
||||||
end = end if end else arrow.now().ceil('month').date()
|
end = end if end else arrow.now().ceil('month').date()
|
||||||
|
|
||||||
sold = func.sum(cls.value).over(
|
sold = db.func.sum(cls.value).over(
|
||||||
order_by=[cls.operation_date, desc(cls.value), cls.label]
|
order_by=[cls.operation_date, db.desc(cls.value), cls.label]
|
||||||
)
|
)
|
||||||
|
|
||||||
previous = sold - cls.value
|
previous = sold - cls.value
|
||||||
@ -188,29 +190,29 @@ class Operation(db.Model):
|
|||||||
previous.label("previous")
|
previous.label("previous")
|
||||||
).filter(
|
).filter(
|
||||||
cls.account_id == account_id,
|
cls.account_id == account_id,
|
||||||
cls.canceled == false()
|
cls.canceled == db.false()
|
||||||
).subquery()
|
).subquery()
|
||||||
|
|
||||||
query = db.session.query(
|
query = db.session.query(
|
||||||
subquery.c.operation_date,
|
subquery.c.operation_date,
|
||||||
func.first_value(subquery.c.previous).over(
|
db.func.first_value(subquery.c.previous).over(
|
||||||
partition_by=subquery.c.operation_date
|
partition_by=subquery.c.operation_date
|
||||||
).label("open"),
|
).label("open"),
|
||||||
func.max(
|
db.func.max(
|
||||||
func.greatest(
|
db.func.greatest(
|
||||||
subquery.c.previous, subquery.c.sold
|
subquery.c.previous, subquery.c.sold
|
||||||
)
|
)
|
||||||
).over(
|
).over(
|
||||||
partition_by=subquery.c.operation_date
|
partition_by=subquery.c.operation_date
|
||||||
).label('high'),
|
).label('high'),
|
||||||
func.min(
|
db.func.min(
|
||||||
func.least(
|
db.func.least(
|
||||||
subquery.c.previous, subquery.c.sold
|
subquery.c.previous, subquery.c.sold
|
||||||
)
|
)
|
||||||
).over(
|
).over(
|
||||||
partition_by=subquery.c.operation_date
|
partition_by=subquery.c.operation_date
|
||||||
).label('low'),
|
).label('low'),
|
||||||
func.last_value(subquery.c.sold).over(
|
db.func.last_value(subquery.c.sold).over(
|
||||||
partition_by=subquery.c.operation_date
|
partition_by=subquery.c.operation_date
|
||||||
).label('close')
|
).label('close')
|
||||||
).distinct()
|
).distinct()
|
||||||
|
@ -16,8 +16,6 @@
|
|||||||
"""
|
"""
|
||||||
from calendar import monthrange
|
from calendar import monthrange
|
||||||
|
|
||||||
from sqlalchemy import desc, false
|
|
||||||
|
|
||||||
import arrow
|
import arrow
|
||||||
|
|
||||||
from accountant import db
|
from accountant import db
|
||||||
@ -66,7 +64,7 @@ class ScheduledOperation(db.Model):
|
|||||||
return db.session.query(
|
return db.session.query(
|
||||||
cls
|
cls
|
||||||
).order_by(
|
).order_by(
|
||||||
desc(cls.day),
|
db.desc(cls.day),
|
||||||
cls.value,
|
cls.value,
|
||||||
cls.label,
|
cls.label,
|
||||||
)
|
)
|
||||||
@ -77,7 +75,7 @@ class ScheduledOperation(db.Model):
|
|||||||
Operation
|
Operation
|
||||||
).filter(
|
).filter(
|
||||||
Operation.scheduled_operation_id == self.id,
|
Operation.scheduled_operation_id == self.id,
|
||||||
Operation.confirmed == false()
|
Operation.confirmed == db.false()
|
||||||
).delete()
|
).delete()
|
||||||
|
|
||||||
# 2) schedule remaining operations.
|
# 2) schedule remaining operations.
|
||||||
|
@ -23,8 +23,6 @@ from flask import current_app as app
|
|||||||
|
|
||||||
from flask.ext.login import UserMixin
|
from flask.ext.login import UserMixin
|
||||||
|
|
||||||
from sqlalchemy import true
|
|
||||||
|
|
||||||
from accountant import db
|
from accountant import db
|
||||||
|
|
||||||
|
|
||||||
@ -33,7 +31,7 @@ class User(UserMixin, db.Model):
|
|||||||
email = db.Column(db.String(200), nullable=False, unique=True, index=True)
|
email = db.Column(db.String(200), nullable=False, unique=True, index=True)
|
||||||
password = db.Column(db.String(100), nullable=True)
|
password = db.Column(db.String(100), nullable=True)
|
||||||
active = db.Column(db.Boolean, nullable=False, default=True,
|
active = db.Column(db.Boolean, nullable=False, default=True,
|
||||||
server_default=true())
|
server_default=db.true())
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_active(self):
|
def is_active(self):
|
||||||
|
Loading…
Reference in New Issue
Block a user