<s> from django . db import transaction <EOL> from . import models , enums <EOL> @ transaction . atomic <EOL> def create_product ( code , name ) : <EOL> """<STR_LIT>""" <EOL> product_obj = models . Product ( <EOL> code = code , <EOL> name = name <EOL> ) <EOL> product_obj . save ( ) <EOL> return product_obj <EOL> @ transaction . atomic <EOL> def update_product ( id , ** kwargs ) : <EOL> product_obj = models . Product . objects . get ( id = id ) <EOL> for k , v in kwargs . items ( ) : <EOL> setattr ( product_obj , k , v ) <EOL> product_obj . save ( ) <EOL> def get_product ( id ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return models . Product . objects . get ( id = id ) <EOL> except models . Product . DoesNotExist : <EOL> return None <EOL> def get_product_transactions_by_ref ( reference ) : <EOL> """<STR_LIT>""" <EOL> return models . ProductTransaction . objects . filter ( reference = reference ) <EOL> @ transaction . atomic <EOL> def create_product_transaction ( product_id , trx_type , qty , reference = None ) : <EOL> """<STR_LIT>""" <EOL> product_obj = models . Product . objects . get ( id = product_id ) <EOL> trx_obj = product_obj . transactions . create ( <EOL> trx_type = trx_type , <EOL> qty = qty , <EOL> reference = reference <EOL> ) <EOL> return trx_obj <EOL> @ transaction . atomic <EOL> def cancel_product_transaction ( trx_id ) : <EOL> trx_obj = models . ProductTransaction . objects . get ( id = trx_id ) <EOL> trx_obj . trx_status = enums . TrxStatus . CANCELED <EOL> trx_obj . save ( ) <EOL> def list_products ( start = None , limit = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return models . Product . objects . filter ( ** kwargs ) [ start : limit ] <EOL> def list_categories ( ) : <EOL> return models . ProductCategory . objects . all ( ) </s>
