Subversion Repositories SmartDukaan

Rev

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