2013-02-08 13:07:27 +01:00
"""
This file is part of Accountant .
Accountant is free software : you can redistribute it and / or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
Foobar 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 Affero General Public License for more details .
You should have received a copy of the GNU Affero General Public License
along with Accountant . If not , see < http : / / www . gnu . org / licenses / > .
"""
2013-12-03 22:22:25 +01:00
from . . import api
2013-12-03 23:01:17 +01:00
from . . model import db , session_scope
2013-12-03 22:22:25 +01:00
from . . model . entries import Entry
from . . model . operations import Operation
from . . model . scheduled_operations import ScheduledOperation
from flask import json , request
2013-01-24 00:01:42 +01:00
from sqlalchemy import func , desc
from sqlalchemy . ext . hybrid import hybrid_property , hybrid_method
2013-07-28 23:25:51 +02:00
from sqlalchemy . orm import sessionmaker , column_property , aliased
from sqlalchemy . sql import func , select , case
2013-01-24 00:01:42 +01:00
2013-12-03 22:22:25 +01:00
@api.route ( " /entries/<account_id>/<year>/<month> " )
2013-01-24 00:01:42 +01:00
def get_entries ( account_id , year , month ) :
"""
Return entries for an account , year , and month .
"""
2013-12-03 23:01:17 +01:00
with session_scope ( ) as session :
base_query = session . query (
Operation ,
case ( whens = { Operation . canceled : None } , else_ = func . sum ( Operation . value ) . over ( partition_by = " canceled " , order_by = " operation_date, value desc, label desc " ) ) . label ( " sold " )
) . filter ( Operation . account_id == account_id ) . order_by (
desc ( Operation . operation_date ) ,
Operation . value ,
Operation . label ,
) . subquery ( )
query = session . query ( base_query ) . select_from ( base_query ) . filter ( func . date_trunc ( ' month ' , base_query . c . operation_date ) == " %s - %s -01 " % ( year , month ) )
return json . dumps ( [ {
" id " : i . id ,
" pointed " : i . pointed ,
" operation_date " : i . operation_date . strftime ( " % Y- % m- %d " ) ,
" label " : i . label ,
" value " : str ( i . value ) ,
" category " : i . category ,
" sold " : str ( i . sold ) if not i . canceled else None ,
" account_id " : i . account_id ,
" canceled " : i . canceled ,
" scheduled_operation_id " : i . scheduled_operation_id
} for i in query . all ( ) ] )
2013-01-24 00:01:42 +01:00
2013-12-03 22:22:25 +01:00
@api.route ( " /entries " , methods = [ " PUT " ] )
2013-01-24 00:01:42 +01:00
def add_entry ( ) :
2013-12-03 23:01:17 +01:00
with session_scope ( ) as session :
2013-01-24 15:27:08 +01:00
entry = Entry (
operation_date = request . json [ ' operation_date ' ] ,
2013-01-26 01:15:07 +01:00
pointed = request . json [ ' pointed ' ] ,
2013-01-24 15:27:08 +01:00
label = request . json [ ' label ' ] ,
value = request . json [ ' value ' ] ,
category = request . json [ ' category ' ] ,
2013-07-28 23:25:51 +02:00
account_id = request . json [ ' account_id ' ] ,
scheduled_operation_id = request . json [ ' scheduled_operation_id ' ]
2013-01-24 15:27:08 +01:00
)
2013-12-03 22:22:25 +01:00
2013-01-24 15:27:08 +01:00
session . add ( entry )
2013-12-03 22:22:25 +01:00
2013-01-24 15:27:08 +01:00
return json . dumps ( " Entry added. " )
2013-01-24 00:01:42 +01:00
2013-12-03 22:22:25 +01:00
@api.route ( " /entries/<entry_id> " , methods = [ " PUT " ] )
2013-01-24 00:01:42 +01:00
def update_entry ( entry_id ) :
2013-12-03 23:01:17 +01:00
with session_scope ( ) as session :
2013-01-24 15:27:08 +01:00
entry = session . query ( Entry ) . filter ( Entry . id == entry_id ) . first ( )
2013-12-03 22:22:25 +01:00
2013-01-24 15:27:08 +01:00
entry . id = entry_id
entry . operation_date = request . json [ ' operation_date ' ]
2013-01-26 01:15:07 +01:00
entry . pointed = request . json [ ' pointed ' ]
2013-01-24 15:27:08 +01:00
entry . label = request . json [ ' label ' ]
entry . value = request . json [ ' value ' ]
entry . category = request . json [ ' category ' ]
entry . account_id = request . json [ ' account_id ' ]
2013-07-28 23:25:51 +02:00
entry . scheduled_operation_id = request . json [ ' scheduled_operation_id ' ]
2013-12-03 22:22:25 +01:00
2013-01-24 15:27:08 +01:00
session . merge ( entry )
2013-12-03 22:22:25 +01:00
2013-01-24 15:27:08 +01:00
return json . dumps ( " Entry # %s updated. " % entry_id )
2013-01-24 00:01:42 +01:00
2013-12-03 22:22:25 +01:00
@api.route ( " /entries/<entry_id> " , methods = [ " DELETE " ] )
2013-01-24 00:01:42 +01:00
def delete_entry ( entry_id ) :
2013-12-03 23:01:17 +01:00
with session_scope ( ) as session :
2013-01-24 15:27:08 +01:00
entry = session . query ( Entry ) . filter ( Entry . id == entry_id ) . first ( )
2013-12-03 22:22:25 +01:00
2013-01-24 15:27:08 +01:00
session . delete ( entry )
2013-12-03 22:22:25 +01:00
2013-01-24 15:27:08 +01:00
return json . dumps ( " Entry # %s deleted. " % entry_id )
2013-01-24 00:01:42 +01:00