Subversion Repositories SmartDukaan

Rev

Rev 2246 | Rev 2277 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2133 chandransh 1
#!/usr/bin/python
2
 
3
import optparse
4
import sys
5
 
2185 rajveer 6
from thrift.Thrift import TException
2247 chandransh 7
import traceback
2185 rajveer 8
 
2133 chandransh 9
if __name__ == '__main__' and __package__ is None:
10
    import os
11
    sys.path.insert(0, os.getcwd())
12
 
13
from shop2020.model.v1.order.impl import DataAccessors, DataService
14
from shop2020.thriftpy.model.v1.order.ttypes import TransactionStatus
2186 rajveer 15
from shop2020.thriftpy.model.v1.user.ttypes import ShoppingCartException,\
16
    PromotionException
17
from shop2020.clients.UserClient import UserClient
18
from shop2020.clients.PromotionClient import PromotionClient
2133 chandransh 19
 
2185 rajveer 20
def reset_cart(transaction, userServiceClient):
21
    items = {}
22
    for order in transaction.orders:
23
        for line_item in order.lineitems:
24
            item_id = line_item.item_id
2240 chandransh 25
            if items.has_key(item_id):
26
                quantity = items[item_id] + line_item.quantity
27
            else:
2185 rajveer 28
                quantity = line_item.quantity
29
            items[item_id] = quantity
30
 
31
    print items
32
 
33
    try:
2240 chandransh 34
        userServiceClient.get_client().resetCart(transaction.shopping_cart_id, items);
2245 chandransh 35
    except ShoppingCartException, TException:
2185 rajveer 36
        print "Error while resetting the cart in the cart database"
2247 chandransh 37
        traceback.print_exc()
2185 rajveer 38
    except:
39
        print "Unexpected exception"
2247 chandransh 40
        traceback.print_exc()
2185 rajveer 41
 
42
def track_coupon_usage(transaction, userServiceClient):
43
    try:
2240 chandransh 44
        cart = userServiceClient.get_client().getCart(transaction.shopping_cart_id)
2241 chandransh 45
        coupon_code = cart.couponCode
2185 rajveer 46
 
47
        if coupon_code is not None and coupon_code != '':
48
            PromotionClient().get_client().trackCouponUsage(coupon_code, transaction.id, transaction.customer_id);
49
    except ShoppingCartException:
50
        print "Error occurred in reading CardId for transaction"
2247 chandransh 51
        traceback.print_exc()
2185 rajveer 52
    except PromotionException:
53
        print "Promotion Exception: "
2247 chandransh 54
        traceback.print_exc()
2185 rajveer 55
    except TException:
56
        print "Transport from Promotion Service failed:"
2247 chandransh 57
        traceback.print_exc()
2185 rajveer 58
    except Exception:
59
        print "Unexpected exception:"
2247 chandransh 60
        traceback.print_exc()
2185 rajveer 61
 
2133 chandransh 62
def main():
63
    parser = optparse.OptionParser()
64
    parser.add_option("-t", "--txn-id", dest="txn_id",
65
                   type="int",
66
                   help="mark the transaction TXN_ID and all its orders as successful",
67
                   metavar="TXN_ID")
68
    (options, args) = parser.parse_args()
69
    if len(args) != 0:
70
        parser.error("You've supplied extra arguments. Are you sure you want to run this program?")
71
    if options.txn_id == None:
72
        parser.error("No Transaction id supplied")
73
    DataService.initialize(echoOn=True)
2185 rajveer 74
    transaction = DataAccessors.get_transaction(options.txn_id)
2133 chandransh 75
    DataAccessors.change_transaction_status(options.txn_id, TransactionStatus.IN_PROCESS, "Payment received for the order")
2185 rajveer 76
    DataAccessors.enqueue_transaction_info_email(options.txn_id)
77
 
78
    user_service_client = UserClient()
79
    reset_cart(transaction, user_service_client)
80
    track_coupon_usage(transaction, user_service_client)
2133 chandransh 81
 
82
if __name__ == '__main__':
83
    main()