Subversion Repositories SmartDukaan

Rev

Rev 15769 | Rev 15771 | 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
626 chandransh 21
 
22
if __name__ == '__main__' and __package__ is None:
23
    import sys
24
    import os
25
    sys.path.insert(0, os.getcwd())
26
 
27
 
9431 rajveer 28
def load_item_data(filename):
15729 amit.gupta 29
    message="Done successfully"
626 chandransh 30
    workbook = xlrd.open_workbook(filename)
31
    sheet = workbook.sheet_by_index(0)
32
    num_rows = sheet.nrows
9431 rajveer 33
    updatedOn = to_java_date(datetime.datetime.now())
34
    cclient = CatalogClient().get_client()
12650 amit.gupta 35
    tcategories = cclient.getAllCategories()
36
    parent_category_ids = [] 
37
    categoryMap = {}
15728 amit.gupta 38
    #0-Delhi 1-Maha 2-Blr 3-Ggn 4-Rajasthan 5-Telangana 6-Gujarat
39
 
12650 amit.gupta 40
    for tcategory in tcategories:
41
        if tcategory.parent_category_id not in parent_category_ids:
42
            parent_category_ids.append(tcategory.parent_category_id)
43
 
44
    for tcategory in tcategories:
45
        if tcategory.id not in parent_category_ids:
46
            categoryMap[tcategory.label] = tcategory.id
47
    prodClient = CatalogClient("catalog_service_server_host_prod").get_client()
9431 rajveer 48
    iclient = InventoryClient().get_client()
49
 
15769 amit.gupta 50
    message = ""
626 chandransh 51
    for rownum in range(1, num_rows):
15769 amit.gupta 52
        print "Row Num", rownum
15728 amit.gupta 53
        states = {}
9431 rajveer 54
        try:
15728 amit.gupta 55
            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]
15770 amit.gupta 56
            print "Last States", states[2]
9431 rajveer 57
            if isinstance(model_number, float):
58
                model_number = str(int(model_number))
5440 phani.kuma 59
 
9486 rajveer 60
 
9431 rajveer 61
            item = Item()
62
            item.productGroup = product_group
63
            item.brand = brand
64
            item.modelNumber = model_number
65
            item.modelName = model_name
66
            item.color = color
67
            item.mrp = round(mrp)
12650 amit.gupta 68
            #if item is risky
13044 amit.gupta 69
            if categoryMap.has_key(category_name.strip()):
12650 amit.gupta 70
                item.category = categoryMap[category_name.strip()]
71
                item.itemStatus = 8
13044 amit.gupta 72
            else:
73
                print "can't find {0} at row {1}".format(category_name, rownum)
13045 amit.gupta 74
                continue
9431 rajveer 75
            item.sellingPrice = round(sp)
76
            item.startDate = long(to_java_date(datetime.datetime(1899, 12, 30) + timedelta(days=int(start_date))))
13041 amit.gupta 77
            item.preferredVendor = preferred_vendor
12650 amit.gupta 78
            item.risky = int(risky)
9431 rajveer 79
            item.weight = weight
80
            item.updatedOn = updatedOn
81
            item.type = int(item_type)
15728 amit.gupta 82
            for key,val in states.iteritems():
83
                states[key] = val*100
9805 rajveer 84
            for c in (1,2,3,4,5):
85
                try:
86
                    item_id = cclient.addItem(item)
13042 amit.gupta 87
                    print "for row {1} item added\t{0}".format(item_id, rownum)
12650 amit.gupta 88
                    item.id = item_id
15728 amit.gupta 89
                    if len(states)>0:
90
                        for c2 in (1,2,3,4,5):
91
                            try:
92
                                cclient.updateItemStateVat(item_id, states)
93
                                break
94
                            except Exception as e:
95
                                cclient = CatalogClient().get_client()       
12650 amit.gupta 96
                    for c1 in (5,5,5,5,5):
97
                        try:
98
                            prodClient.addItem(item)
15728 amit.gupta 99
                            if len(states) > 0:
100
                                for c3 in (1,2,3,4,5):
101
                                    try:
102
                                        prodClient.updateItemStateVat(item_id, states)
103
                                        break
104
                                    except Exception as e:
105
                                        prodClient = CatalogClient("catalog_service_server_host_prod").get_client()
12650 amit.gupta 106
                            break
107
                        except:
108
                            prodClient = CatalogClient("catalog_service_server_host_prod").get_client()
9805 rajveer 109
                    break
110
                except Exception as e:
111
                    cclient = CatalogClient().get_client()       
1080 chandransh 112
 
9431 rajveer 113
            vip = VendorItemPricing()
114
            vip.itemId = item_id
115
            vip.dealerPrice = round(dp)
116
            vip.mop = round(mop)
117
            vip.nlc = round(nlc)
118
            vip.transferPrice = round(tp)
119
            vip.vendorId = preferred_vendor
9805 rajveer 120
            for c in (1,2,3,4,5):
121
                try:
122
                    iclient.addVendorItemPricing(vip)
123
                    break
124
                except Exception as e:
125
                    iclient = InventoryClient().get_client()
126
 
9486 rajveer 127
        except Exception as e:
128
            message = message + "\t" + str(brand) + "\t" + str(model_name) + "\t" + str(model_number) + "\t" + str(e.message) + "\n"
12654 amit.gupta 129
    print message
11539 amit.gupta 130
    mail("build@shop2020.in", "cafe@nes", ["amit.gupta@shop2020.in", "chandan.kumar@shop2020.in"], "Problem while adding items", message, [], [], [])    
626 chandransh 131
 
4725 phani.kuma 132
 
626 chandransh 133
def main():
134
    parser = optparse.OptionParser()
135
    parser.add_option("-f", "--file", dest="filename",
136
                   default="ItemList.xls", type="string",
137
                   help="Read the item list from FILE",
138
                   metavar="FILE")
139
    (options, args) = parser.parse_args()
140
    if len(args) != 0:
141
        parser.error("You've supplied extra arguments. Are you sure you want to run this program?")
142
    filename = options.filename
9431 rajveer 143
    load_item_data(filename)
626 chandransh 144
 
145
if __name__ == '__main__':
13041 amit.gupta 146
    main()