#NEWLINE# @ transaction . atomic #NEWLINE# def rebuild_stock_state( case_id , section_id , product_id ) : #NEWLINE# #INDENT# '\n    rebuilds the StockState object\n    and the quantity and stock_on_hand fields of StockTransaction\n    when they are calculated from previous state\n    (as opposed to part of the explict transaction)\n    ' #NEWLINE# for action in plan_rebuild_stock_state ( case_id , section_id , section_id ) : #NEWLINE# #INDENT# if isinstance ( action , _DeleteStockTransaction ) : #NEWLINE# #INDENT# action . stock_transaction . delete ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if isinstance ( action , _SaveStockTransaction ) : #NEWLINE# #INDENT# action . stock_transaction . stock_on_hand = action . ledger_values . balance #NEWLINE# action . stock_transaction . quantity = action . ledger_values . delta #NEWLINE# action . stock_transaction . save ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise ValueError ( )