diff --git a/accountant/api/models/accounts.py b/accountant/api/models/accounts.py index a0e1b4b..897e949 100644 --- a/accountant/api/models/accounts.py +++ b/accountant/api/models/accounts.py @@ -14,9 +14,6 @@ You should have received a copy of the GNU Affero General Public License along with Accountant. If not, see . """ -from sqlalchemy import func, case, literal_column -from sqlalchemy.orm import validates - from accountant import db from .operations import Operation @@ -35,17 +32,17 @@ class Account(db.Model): def query(cls, begin=None, end=None): status_query = db.session.query( Operation.account_id, - func.sum(Operation.value).label("future"), - func.sum( - case( + db.func.sum(Operation.value).label("future"), + db.func.sum( + db.case( [(Operation.pointed, Operation.value)], else_=0 ) ).label("pointed"), - func.sum( - case( - [(Operation.operation_date <= func.current_date(), + db.func.sum( + db.case( + [(Operation.operation_date <= db.func.current_date(), Operation.value)], else_=0 ) @@ -57,19 +54,21 @@ class Account(db.Model): if begin and end: balance_query = db.session.query( Operation.account_id, - func.sum( - case( - [(func.sign(Operation.value) == -1, Operation.value)], + db.func.sum( + db.case( + [(db.func.sign(Operation.value) == -1, + Operation.value)], else_=0 ) ).label("expenses"), - func.sum( - case( - [(func.sign(Operation.value) == 1, Operation.value)], + db.func.sum( + db.case( + [(db.func.sign(Operation.value) == 1, + Operation.value)], else_=0 ) ).label("revenues"), - func.sum(Operation.value).label("balance") + db.func.sum(Operation.value).label("balance") ).filter( Operation.operation_date >= str(begin), Operation.operation_date <= str(end) @@ -81,12 +80,18 @@ class Account(db.Model): cls.id, cls.name, cls.authorized_overdraft, - func.coalesce(status_query.c.current, 0).label('current'), - func.coalesce(status_query.c.pointed, 0).label('pointed'), - func.coalesce(status_query.c.future, 0).label('future'), - func.coalesce(balance_query.c.expenses, 0).label('expenses'), - func.coalesce(balance_query.c.revenues, 0).label('revenues'), - func.coalesce(balance_query.c.balance, 0).label('balance'), + db.func.coalesce( + status_query.c.current, 0).label('current'), + db.func.coalesce( + status_query.c.pointed, 0).label('pointed'), + db.func.coalesce( + 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( status_query, status_query.c.account_id == cls.id ).outerjoin( @@ -97,19 +102,19 @@ class Account(db.Model): cls.id, cls.name, cls.authorized_overdraft, - func.coalesce(status_query.c.current, 0).label('current'), - func.coalesce(status_query.c.pointed, 0).label('pointed'), - func.coalesce(status_query.c.future, 0).label('future'), - literal_column("0").label('expenses'), - literal_column("0").label('revenues'), - literal_column("0").label('balance'), + db.func.coalesce(status_query.c.current, 0).label('current'), + db.func.coalesce(status_query.c.pointed, 0).label('pointed'), + db.func.coalesce(status_query.c.future, 0).label('future'), + db.literal_column("0").label('expenses'), + db.literal_column("0").label('revenues'), + db.literal_column("0").label('balance'), ).outerjoin( status_query, status_query.c.account_id == cls.id ) return query.order_by(cls.name) - @validates('authorized_overdraft') + @db.validates('authorized_overdraft') def validate_authorized_overdraft(self, key, authorized_overdraft): assert authorized_overdraft <= 0 diff --git a/accountant/api/models/operations.py b/accountant/api/models/operations.py index 26b1312..b6ae971 100644 --- a/accountant/api/models/operations.py +++ b/accountant/api/models/operations.py @@ -18,9 +18,6 @@ from datetime import date import arrow -from sqlalchemy import func, case, desc, true, false, text -from sqlalchemy.orm import column_property - from accountant import db @@ -31,7 +28,7 @@ class Operation(db.Model): db.Date, nullable=False, default=date.today, - server_default=func.current_date(), + server_default=db.func.current_date(), index=True ) label = db.Column(db.String(500), nullable=False) @@ -59,32 +56,37 @@ class Operation(db.Model): db.Boolean, nullable=False, default=False, - server_default=false() + server_default=db.false() ) confirmed = db.Column( db.Boolean, nullable=False, default=True, - server_default=true() + server_default=db.true() ) canceled = db.Column( db.Boolean, nullable=False, default=False, - server_default=false() + server_default=db.false() ) - sold = column_property( - func.sum( - case( - whens={canceled: text("0")}, + sold = db.column_property( + db.func.sum( + db.case( + whens={canceled: db.text("0")}, else_=value ) ).over( 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") ) @@ -125,7 +127,7 @@ class Operation(db.Model): ).join( base_query, base_query.c.id == cls.id ).order_by( - desc(cls.operation_date), + db.desc(cls.operation_date), cls.value, cls.label, ) @@ -147,13 +149,13 @@ class Operation(db.Model): query = db.session.query( cls.category, - func.sum( - case([(func.sign(cls.value) == -1, cls.value)], else_=0) + db.func.sum( + db.case([(db.func.sign(cls.value) == -1, cls.value)], else_=0) ).label("expenses"), - func.sum( - case([(func.sign(cls.value) == 1, cls.value)], else_=0) + db.func.sum( + db.case([(db.func.sign(cls.value) == 1, cls.value)], else_=0) ).label("revenues"), - func.sum(cls.value).label("balance") + db.func.sum(cls.value).label("balance") ).filter( cls.account_id == account_id ).filter( @@ -176,8 +178,8 @@ class Operation(db.Model): end = end if end else arrow.now().ceil('month').date() - sold = func.sum(cls.value).over( - order_by=[cls.operation_date, desc(cls.value), cls.label] + sold = db.func.sum(cls.value).over( + order_by=[cls.operation_date, db.desc(cls.value), cls.label] ) previous = sold - cls.value @@ -188,29 +190,29 @@ class Operation(db.Model): previous.label("previous") ).filter( cls.account_id == account_id, - cls.canceled == false() + cls.canceled == db.false() ).subquery() query = db.session.query( 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 ).label("open"), - func.max( - func.greatest( + db.func.max( + db.func.greatest( subquery.c.previous, subquery.c.sold ) ).over( partition_by=subquery.c.operation_date ).label('high'), - func.min( - func.least( + db.func.min( + db.func.least( subquery.c.previous, subquery.c.sold ) ).over( partition_by=subquery.c.operation_date ).label('low'), - func.last_value(subquery.c.sold).over( + db.func.last_value(subquery.c.sold).over( partition_by=subquery.c.operation_date ).label('close') ).distinct() diff --git a/accountant/api/models/scheduled_operations.py b/accountant/api/models/scheduled_operations.py index d81a0dc..eef1c1c 100644 --- a/accountant/api/models/scheduled_operations.py +++ b/accountant/api/models/scheduled_operations.py @@ -16,8 +16,6 @@ """ from calendar import monthrange -from sqlalchemy import desc, false - import arrow from accountant import db @@ -66,7 +64,7 @@ class ScheduledOperation(db.Model): return db.session.query( cls ).order_by( - desc(cls.day), + db.desc(cls.day), cls.value, cls.label, ) @@ -77,7 +75,7 @@ class ScheduledOperation(db.Model): Operation ).filter( Operation.scheduled_operation_id == self.id, - Operation.confirmed == false() + Operation.confirmed == db.false() ).delete() # 2) schedule remaining operations. diff --git a/accountant/api/models/users.py b/accountant/api/models/users.py index 5812163..80ac15d 100644 --- a/accountant/api/models/users.py +++ b/accountant/api/models/users.py @@ -23,8 +23,6 @@ from flask import current_app as app from flask.ext.login import UserMixin -from sqlalchemy import true - 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) password = db.Column(db.String(100), nullable=True) active = db.Column(db.Boolean, nullable=False, default=True, - server_default=true()) + server_default=db.true()) @property def is_active(self):