Subversion Repositories SmartDukaan

Rev

Rev 15774 | Rev 15776 | 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()
38
 
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):
15728 amit.gupta 54
        states = {}
9431 rajveer 55
        try:
15728 amit.gupta 56
            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 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
15771 amit.gupta 84
 
9805 rajveer 85
            for c in (1,2,3,4,5):
86
                try:
87
                    item_id = cclient.addItem(item)
13042 amit.gupta 88
                    print "for row {1} item added\t{0}".format(item_id, rownum)
12650 amit.gupta 89
                    item.id = item_id
15728 amit.gupta 90
                    if len(states)>0:
91
                        for c2 in (1,2,3,4,5):
92
                            try:
93
                                cclient.updateItemStateVat(item_id, states)
15775 amit.gupta 94
                                print "break 0"
15728 amit.gupta 95
                                break
96
                            except Exception as e:
97
                                cclient = CatalogClient().get_client()       
12650 amit.gupta 98
                    for c1 in (5,5,5,5,5):
99
                        try:
100
                            prodClient.addItem(item)
15728 amit.gupta 101
                            if len(states) > 0:
102
                                for c3 in (1,2,3,4,5):
103
                                    try:
104
                                        prodClient.updateItemStateVat(item_id, states)
15775 amit.gupta 105
                                        print "break 1"
15728 amit.gupta 106
                                        break
107
                                    except Exception as e:
108
                                        prodClient = CatalogClient("catalog_service_server_host_prod").get_client()
15775 amit.gupta 109
                            print "break 2"
12650 amit.gupta 110
                            break
111
                        except:
112
                            prodClient = CatalogClient("catalog_service_server_host_prod").get_client()
15775 amit.gupta 113
                    print "break 3"    
9805 rajveer 114
                    break
115
                except Exception as e:
116
                    cclient = CatalogClient().get_client()       
9431 rajveer 117
            vip = VendorItemPricing()
118
            vip.itemId = item_id
119
            vip.dealerPrice = round(dp)
120
            vip.mop = round(mop)
121
            vip.nlc = round(nlc)
122
            vip.transferPrice = round(tp)
123
            vip.vendorId = preferred_vendor
9805 rajveer 124
            for c in (1,2,3,4,5):
125
                try:
126
                    iclient.addVendorItemPricing(vip)
127
                    break
128
                except Exception as e:
129
                    iclient = InventoryClient().get_client()
130
 
9486 rajveer 131
        except Exception as e:
132
            message = message + "\t" + str(brand) + "\t" + str(model_name) + "\t" + str(model_number) + "\t" + str(e.message) + "\n"
12654 amit.gupta 133
    print message
11539 amit.gupta 134
    mail("build@shop2020.in", "cafe@nes", ["amit.gupta@shop2020.in", "chandan.kumar@shop2020.in"], "Problem while adding items", message, [], [], [])    
15773 amit.gupta 135
    print "Successfully updated the item list information."
626 chandransh 136
 
4725 phani.kuma 137
 
626 chandransh 138
def main():
139
    parser = optparse.OptionParser()
140
    parser.add_option("-f", "--file", dest="filename",
141
                   default="ItemList.xls", type="string",
142
                   help="Read the item list from FILE",
143
                   metavar="FILE")
144
    (options, args) = parser.parse_args()
145
    if len(args) != 0:
146
        parser.error("You've supplied extra arguments. Are you sure you want to run this program?")
147
    filename = options.filename
9431 rajveer 148
    load_item_data(filename)
626 chandransh 149
 
150
if __name__ == '__main__':
13041 amit.gupta 151
    main()