Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
626 chandransh 1
#!/usr/bin/python
5440 phani.kuma 2
# coding: ascii
4090 chandransh 3
'''
4
This script is used to load item details in the catalog database.
5
It's now mostly used for Accessories since they come in huge numbers.
1080 chandransh 6
 
4090 chandransh 7
@author: Chandranshu
8
'''
5971 mandeep.dh 9
from elixir import *
9431 rajveer 10
from shop2020.thriftpy.model.v1.catalog.ttypes import Item
5971 mandeep.dh 11
import csv
12
import datetime
9431 rajveer 13
from datetime import timedelta
626 chandransh 14
import optparse
15
import xlrd
9431 rajveer 16
from shop2020.clients.CatalogClient import CatalogClient
17
from shop2020.clients.InventoryClient import InventoryClient
18
from shop2020.thriftpy.model.v1.inventory.ttypes import VendorItemPricing
19
from shop2020.utils.Utils import to_java_date
20
from shop2020.utils.EmailAttachmentSender import mail
15772 amit.gupta 21
import time
626 chandransh 22
 
23
if __name__ == '__main__' and __package__ is None:
24
    import sys
25
    import os
26
    sys.path.insert(0, os.getcwd())
27
 
28
 
9431 rajveer 29
def load_item_data(filename):
15729 amit.gupta 30
    message="Done successfully"
626 chandransh 31
    workbook = xlrd.open_workbook(filename)
32
    sheet = workbook.sheet_by_index(0)
33
    num_rows = sheet.nrows
9431 rajveer 34
    updatedOn = to_java_date(datetime.datetime.now())
35
    cclient = CatalogClient().get_client()
15773 amit.gupta 36
    prodClient = CatalogClient("catalog_service_server_host_prod").get_client()
37
    iclient = InventoryClient().get_client()
15778 amit.gupta 38
    rowsleft = [750,789,795,805,811,817,823,829,835,841,847,853,859,865,871,877,883,889,895,901,907,913,919,925,931,937,943,949,955,961,967,973,979,985,991,997,1003,1009,1015,1021,1027,1033,1039,1045,1051,1057,1063,1069,1075,1081,1087,1093,1099,1105,1111,1117,1123,1129,1135,1141,1147,1153,1159,1165,1171,1177,1183,1189,1195,1201,1207,1213,1219,1225,1231,1237,1243,1249,1255,1261,1267,1273,1279,1285,1291,1297,1303,1309,1315,1321,1327,1333,1339,1345,1351,1357,1363,1369,1375,1381,1387,1393,1399,1405,1411,1417,1423,1429,1435,1441,1447,1453,1459,1465,1471,1477,1483,1489,1495,1501,1507,1513,1519,1525,1531,1537,1543,1549,1555,1561,1567,1573,1579,1585,1591,1597,1603,1609,1615,1621,1627,1633,1639,1645,1651,1657,1663,1669,1675,1681,1687,1693,1699,1705,1711,1717,1723,1729,1735,1741,1747,1753,1759,1765,1771,1777,1783,1789,1795,1801,1807,1813,1819,1825,1831,1837,1843,1849,1855,1861,1867,1873,1879,1885,1891,1897,1903,1909,1915,1921,1927,1933,1939,1945,1951,1957,1963,1969,1975,1981,1987,1993,1999,2005,2011,2017,2023,2029,2035,2041,2047,2053,2059,2065,2071,2077,2083,2089,2095,2101,2107,2113,2119,2125,2131,2137,2143,2149,2155,2161,2167,2173,2179,2185,2191,2197,2203,2209,2215,2221,2227,2233,2239]
12650 amit.gupta 39
    tcategories = cclient.getAllCategories()
40
    parent_category_ids = [] 
41
    categoryMap = {}
15728 amit.gupta 42
    #0-Delhi 1-Maha 2-Blr 3-Ggn 4-Rajasthan 5-Telangana 6-Gujarat
43
 
12650 amit.gupta 44
    for tcategory in tcategories:
45
        if tcategory.parent_category_id not in parent_category_ids:
46
            parent_category_ids.append(tcategory.parent_category_id)
47
 
48
    for tcategory in tcategories:
49
        if tcategory.id not in parent_category_ids:
50
            categoryMap[tcategory.label] = tcategory.id
9431 rajveer 51
 
15769 amit.gupta 52
    message = ""
626 chandransh 53
    for rownum in range(1, num_rows):
15778 amit.gupta 54
        if rownum not in rowsleft:
55
            continue
15728 amit.gupta 56
        states = {}
9431 rajveer 57
        try:
15728 amit.gupta 58
            product_group, category_name, brand, model_number, model_name, color, mrp, sp, mop, dp, tp, nlc, start_date, preferred_vendor, risky, weight, item_type, states[0], states[3], states[1], states[2]  = sheet.row_values(rownum)[0:21]
9431 rajveer 59
            if isinstance(model_number, float):
60
                model_number = str(int(model_number))
5440 phani.kuma 61
 
9486 rajveer 62
 
9431 rajveer 63
            item = Item()
64
            item.productGroup = product_group
65
            item.brand = brand
66
            item.modelNumber = model_number
67
            item.modelName = model_name
68
            item.color = color
69
            item.mrp = round(mrp)
12650 amit.gupta 70
            #if item is risky
13044 amit.gupta 71
            if categoryMap.has_key(category_name.strip()):
12650 amit.gupta 72
                item.category = categoryMap[category_name.strip()]
73
                item.itemStatus = 8
13044 amit.gupta 74
            else:
75
                print "can't find {0} at row {1}".format(category_name, rownum)
13045 amit.gupta 76
                continue
9431 rajveer 77
            item.sellingPrice = round(sp)
78
            item.startDate = long(to_java_date(datetime.datetime(1899, 12, 30) + timedelta(days=int(start_date))))
13041 amit.gupta 79
            item.preferredVendor = preferred_vendor
12650 amit.gupta 80
            item.risky = int(risky)
9431 rajveer 81
            item.weight = weight
82
            item.updatedOn = updatedOn
83
            item.type = int(item_type)
15728 amit.gupta 84
            for key,val in states.iteritems():
85
                states[key] = val*100
15771 amit.gupta 86
 
9805 rajveer 87
            for c in (1,2,3,4,5):
15776 amit.gupta 88
                print "itemid", item.id
9805 rajveer 89
                try:
90
                    item_id = cclient.addItem(item)
13042 amit.gupta 91
                    print "for row {1} item added\t{0}".format(item_id, rownum)
12650 amit.gupta 92
                    item.id = item_id
15728 amit.gupta 93
                    if len(states)>0:
94
                        for c2 in (1,2,3,4,5):
95
                            try:
96
                                cclient.updateItemStateVat(item_id, states)
15775 amit.gupta 97
                                print "break 0"
15728 amit.gupta 98
                                break
99
                            except Exception as e:
100
                                cclient = CatalogClient().get_client()       
12650 amit.gupta 101
                    for c1 in (5,5,5,5,5):
102
                        try:
103
                            prodClient.addItem(item)
15728 amit.gupta 104
                            if len(states) > 0:
105
                                for c3 in (1,2,3,4,5):
106
                                    try:
107
                                        prodClient.updateItemStateVat(item_id, states)
15775 amit.gupta 108
                                        print "break 1"
15728 amit.gupta 109
                                        break
110
                                    except Exception as e:
111
                                        prodClient = CatalogClient("catalog_service_server_host_prod").get_client()
15775 amit.gupta 112
                            print "break 2"
12650 amit.gupta 113
                            break
114
                        except:
115
                            prodClient = CatalogClient("catalog_service_server_host_prod").get_client()
15775 amit.gupta 116
                    print "break 3"    
9805 rajveer 117
                    break
118
                except Exception as e:
119
                    cclient = CatalogClient().get_client()       
9431 rajveer 120
            vip = VendorItemPricing()
121
            vip.itemId = item_id
122
            vip.dealerPrice = round(dp)
123
            vip.mop = round(mop)
124
            vip.nlc = round(nlc)
125
            vip.transferPrice = round(tp)
126
            vip.vendorId = preferred_vendor
9805 rajveer 127
            for c in (1,2,3,4,5):
128
                try:
129
                    iclient.addVendorItemPricing(vip)
130
                    break
131
                except Exception as e:
132
                    iclient = InventoryClient().get_client()
133
 
9486 rajveer 134
        except Exception as e:
135
            message = message + "\t" + str(brand) + "\t" + str(model_name) + "\t" + str(model_number) + "\t" + str(e.message) + "\n"
12654 amit.gupta 136
    print message
11539 amit.gupta 137
    mail("build@shop2020.in", "cafe@nes", ["amit.gupta@shop2020.in", "chandan.kumar@shop2020.in"], "Problem while adding items", message, [], [], [])    
15773 amit.gupta 138
    print "Successfully updated the item list information."
626 chandransh 139
 
4725 phani.kuma 140
 
626 chandransh 141
def main():
142
    parser = optparse.OptionParser()
143
    parser.add_option("-f", "--file", dest="filename",
144
                   default="ItemList.xls", type="string",
145
                   help="Read the item list from FILE",
146
                   metavar="FILE")
147
    (options, args) = parser.parse_args()
148
    if len(args) != 0:
149
        parser.error("You've supplied extra arguments. Are you sure you want to run this program?")
150
    filename = options.filename
9431 rajveer 151
    load_item_data(filename)
626 chandransh 152
 
153
if __name__ == '__main__':
13041 amit.gupta 154
    main()