Subversion Repositories SmartDukaan

Rev

Rev 15773 | Rev 15775 | 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)
94
                                break
95
                            except Exception as e:
96
                                cclient = CatalogClient().get_client()       
12650 amit.gupta 97
                    for c1 in (5,5,5,5,5):
98
                        try:
99
                            prodClient.addItem(item)
15728 amit.gupta 100
                            if len(states) > 0:
101
                                for c3 in (1,2,3,4,5):
102
                                    try:
103
                                        prodClient.updateItemStateVat(item_id, states)
104
                                        break
105
                                    except Exception as e:
106
                                        prodClient = CatalogClient("catalog_service_server_host_prod").get_client()
12650 amit.gupta 107
                            break
108
                        except:
109
                            prodClient = CatalogClient("catalog_service_server_host_prod").get_client()
9805 rajveer 110
                    break
111
                except Exception as e:
112
                    cclient = CatalogClient().get_client()       
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, [], [], [])    
15773 amit.gupta 131
    print "Successfully updated the item list information."
626 chandransh 132
 
4725 phani.kuma 133
 
626 chandransh 134
def main():
135
    parser = optparse.OptionParser()
136
    parser.add_option("-f", "--file", dest="filename",
137
                   default="ItemList.xls", type="string",
138
                   help="Read the item list from FILE",
139
                   metavar="FILE")
140
    (options, args) = parser.parse_args()
141
    if len(args) != 0:
142
        parser.error("You've supplied extra arguments. Are you sure you want to run this program?")
143
    filename = options.filename
9431 rajveer 144
    load_item_data(filename)
626 chandransh 145
 
146
if __name__ == '__main__':
13041 amit.gupta 147
    main()