Subversion Repositories SmartDukaan

Rev

Rev 31560 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 31560 Rev 31595
Line 20... Line 20...
20
# import pysolr
20
# import pysolr
21
 
21
 
22
CATEGORY_MASTER = [10006, 10010, 14202, 14203]
22
CATEGORY_MASTER = [10006, 10010, 14202, 14203]
23
parser = optparse.OptionParser()
23
parser = optparse.OptionParser()
24
parser.add_option("-d", "--d", dest="dbHost",
24
parser.add_option("-d", "--d", dest="dbHost",
25
                      default="127.0.0.1",
25
                  default="127.0.0.1",
26
                      type="string", help="The HOST where the mysql server is running",
26
                  type="string", help="The HOST where the mysql server is running",
27
                      metavar="DBHOST")
27
                  metavar="DBHOST")
28
parser.add_option("-s", "--s", dest="solrPath",
28
parser.add_option("-s", "--s", dest="solrPath",
29
                      default="http://localhost:8984/solr/demo",
29
                  default="http://localhost:8984/solr/demo",
30
                      type="string", help="Complete solr path",
30
                  type="string", help="Complete solr path",
31
                      metavar="SOLRHOST")
31
                  metavar="SOLRHOST")
32
parser.add_option("-m", "--m", dest="mongoHost",
32
parser.add_option("-m", "--m", dest="mongoHost",
33
                      default="localhost",
33
                  default="localhost",
34
                      type="string", help="Complete solr path",
34
                  type="string", help="Complete solr path",
35
                      metavar="HOST")
35
                  metavar="HOST")
36
 
36
 
37
(options, args) = parser.parse_args()
37
(options, args) = parser.parse_args()
38
 
38
 
39
con = None
39
con = None
40
 
40
 
Line 53... Line 53...
53
            return None
53
            return None
54
    return con
54
    return con
55
 
55
 
56
 
56
 
57
class __SkuInfo:
57
class __SkuInfo:
58
    
58
 
59
    def __init__(self, id, ids, brand, model_name, category_id, subCategoryId, thumbnail, title, brand_synonyms, model_name_synonyms, source_product_names, \
59
    def __init__(self, id, ids, brand, model_name, category_id, subCategoryId, thumbnail, title, brand_synonyms, model_name_synonyms, source_product_names, \
60
                 category, subCategory):
60
                 category, subCategory):
61
        # Skubundle id
61
        # Skubundle id
62
        self.id = id
62
        self.id = id
63
        # _id of skus
63
        # _id of skus
64
        self.ids = ids
64
        self.ids = ids
65
        self.brand = brand
65
        self.brand = brand
66
        self.model_name = model_name
66
        self.model_name = model_name
67
        self.category_id = category_id
67
        self.category_id = category_id
68
        self.subCategoryId = subCategoryId
68
        self.subCategoryId = subCategoryId
69
        self.thumbnail = thumbnail 
69
        self.thumbnail = thumbnail
70
        self.title = title
70
        self.title = title
71
        self.brand_synonyms = brand_synonyms
71
        self.brand_synonyms = brand_synonyms
72
        self.model_name_synonyms = model_name_synonyms
72
        self.model_name_synonyms = model_name_synonyms
73
        self.source_product_names = source_product_names
73
        self.source_product_names = source_product_names
74
        self.category = category
74
        self.category = category
75
        self.subCategory = subCategory
75
        self.subCategory = subCategory
76
        
76
 
77
    def toJSON(self):
77
    def toJSON(self):
78
        return json.dumps(self, default=lambda o: o.__dict__,
78
        return json.dumps(self, default=lambda o: o.__dict__,
79
            sort_keys=True, indent=4)
79
                          sort_keys=True, indent=4)
-
 
80
 
80
 
81
 
81
        
-
 
82
class __Catalog:
82
class __Catalog:
83
    
83
 
84
    def __init__(self, id, rank, title, items):
84
    def __init__(self, id, rank, title, items):
85
        # Skubundle id
85
        # Skubundle id
86
        self.id = id
86
        self.id = id
87
        self.rank = rank
87
        self.rank = rank
88
        self.title = title
88
        self.title = title
89
        self._childDocuments_ = items
89
        self._childDocuments_ = items
90
 
90
 
91
    def toJSON(self):
91
    def toJSON(self):
92
        return json.dumps(self, default=lambda o: o.__dict__)
92
        return json.dumps(self, default=lambda o: o.__dict__)
93
 
93
 
94
        
94
 
95
class __Item:
95
class __Item:
96
 
96
 
97
    def __init__(self, id, color, tags):
97
    def __init__(self, id, color, tags):
98
        self.id = id
98
        self.id = id
99
        self.color = color
99
        self.color = color
Line 107... Line 107...
107
 
107
 
108
    def __init__(self, id, sellingPrice, mop):
108
    def __init__(self, id, sellingPrice, mop):
109
        self.id = id
109
        self.id = id
110
        self.sellingPrice = sellingPrice
110
        self.sellingPrice = sellingPrice
111
        self.mop = mop
111
        self.mop = mop
112
        
112
 
113
# solr = pysolr.Solr(options.solrPath, timeout=10)
113
# solr = pysolr.Solr(options.solrPath, timeout=10)
114
 
114
 
115
 
115
 
116
def todict(obj, classkey=None):
116
def todict(obj, classkey=None):
117
    if isinstance(obj, dict):
117
    if isinstance(obj, dict):
Line 122... Line 122...
122
    elif hasattr(obj, "_ast"):
122
    elif hasattr(obj, "_ast"):
123
        return todict(obj._ast())
123
        return todict(obj._ast())
124
    elif hasattr(obj, "__iter__"):
124
    elif hasattr(obj, "__iter__"):
125
        return [todict(v, classkey) for v in obj]
125
        return [todict(v, classkey) for v in obj]
126
    elif hasattr(obj, "__dict__"):
126
    elif hasattr(obj, "__dict__"):
127
        data = dict([(key, todict(value, classkey)) 
127
        data = dict([(key, todict(value, classkey))
128
            for key, value in obj.__dict__.iteritems() 
128
                     for key, value in obj.__dict__.iteritems()
129
            if not callable(value) and not key.startswith('_')])
129
                     if not callable(value) and not key.startswith('_')])
130
        if classkey is not None and hasattr(obj, "__class__"):
130
        if classkey is not None and hasattr(obj, "__class__"):
131
            data[classkey] = obj.__class__.__name__
131
            data[classkey] = obj.__class__.__name__
132
        return data
132
        return data
133
    else:
133
    else:
134
        return obj
134
        return obj
135
 
135
 
136
    
136
 
137
def ungzipResponse(r):
137
def ungzipResponse(r):
138
    headers = r.info()
138
    headers = r.info()
139
    if headers.get('Content-Encoding') == 'gzip':
139
    if headers.get('Content-Encoding') == 'gzip':
140
        url_f = StringIO.StringIO(r.read())
140
        url_f = StringIO.StringIO(r.read())
141
        gz = gzip.GzipFile(fileobj=url_f)
141
        gz = gzip.GzipFile(fileobj=url_f)
Line 171... Line 171...
171
    for rawAvailability in rawAvailabilityList:
171
    for rawAvailability in rawAvailabilityList:
172
        if not availabilityItemMap.has_key(rawAvailability['Itemid']):
172
        if not availabilityItemMap.has_key(rawAvailability['Itemid']):
173
            availabilityItemMap[rawAvailability['Itemid']] = {}
173
            availabilityItemMap[rawAvailability['Itemid']] = {}
174
        if not availabilityItemMap[rawAvailability['Itemid']].has_key(rawAvailability['Warehouseid']):
174
        if not availabilityItemMap[rawAvailability['Itemid']].has_key(rawAvailability['Warehouseid']):
175
            availabilityItemMap[rawAvailability['Itemid']][rawAvailability['Warehouseid']] = {"netAvailability":0, "netPo":0}
175
            availabilityItemMap[rawAvailability['Itemid']][rawAvailability['Warehouseid']] = {"netAvailability":0, "netPo":0}
176
        
176
 
177
        availabilityItemMap[rawAvailability['Itemid']][rawAvailability['Warehouseid']]['netAvailability'] += int(rawAvailability['Netavailability'])
177
        availabilityItemMap[rawAvailability['Itemid']][rawAvailability['Warehouseid']]['netAvailability'] += int(rawAvailability['Netavailability'])
178
    
178
 
179
    for rawPendingPo in rawPendingPoList:
179
    for rawPendingPo in rawPendingPoList:
180
        if not availabilityItemMap.has_key(rawPendingPo['Itemid']):
180
        if not availabilityItemMap.has_key(rawPendingPo['Itemid']):
181
            availabilityItemMap[rawPendingPo['Itemid']] = {}
181
            availabilityItemMap[rawPendingPo['Itemid']] = {}
182
        if not availabilityItemMap[rawPendingPo['Itemid']].has_key(rawPendingPo['Warehouseid']):
182
        if not availabilityItemMap[rawPendingPo['Itemid']].has_key(rawPendingPo['Warehouseid']):
183
            availabilityItemMap[rawPendingPo['Itemid']][rawPendingPo['Warehouseid']] = {"netAvailability":0, "netPo":0}
183
            availabilityItemMap[rawPendingPo['Itemid']][rawPendingPo['Warehouseid']] = {"netAvailability":0, "netPo":0}
184
        availabilityItemMap[rawPendingPo['Itemid']][rawPendingPo['Warehouseid']]['netPo'] += int(rawPendingPo['Unfulfilled'])
184
        availabilityItemMap[rawPendingPo['Itemid']][rawPendingPo['Warehouseid']]['netPo'] += int(rawPendingPo['Unfulfilled'])
185
        
185
 
186
    return availabilityItemMap
186
    return availabilityItemMap
187
 
187
 
188
 
188
 
189
def populateTagItems():
189
def populateTagItems():
190
    availabilityItemMap = getItemMap()
190
    availabilityItemMap = getItemMap()
Line 197... Line 197...
197
    catalogMap = {}
197
    catalogMap = {}
198
    # stmt = session.query(PrivateDeals).filter_by(isActive=1).filter(now().between(PrivateDeals.startDate, PrivateDeals.endDate)).subquery()
198
    # stmt = session.query(PrivateDeals).filter_by(isActive=1).filter(now().between(PrivateDeals.startDate, PrivateDeals.endDate)).subquery()
199
    # query = session.query(Item, privateDealAlias.dealPrice).outerjoin((privateDealAlias, Item.id==privateDealAlias.item_id)).filter(Item.status != status.PHASED_OUT)
199
    # query = session.query(Item, privateDealAlias.dealPrice).outerjoin((privateDealAlias, Item.id==privateDealAlias.item_id)).filter(Item.status != status.PHASED_OUT)
200
    tuples = session.query(Tag_Listing, Item).join((Item, Item.id == Tag_Listing.item_id)).filter(or_(Item.status == status.ACTIVE, Item.status == status.PAUSED_BY_RISK, Item.status == status.PARTIALLY_ACTIVE))
200
    tuples = session.query(Tag_Listing, Item).join((Item, Item.id == Tag_Listing.item_id)).filter(or_(Item.status == status.ACTIVE, Item.status == status.PAUSED_BY_RISK, Item.status == status.PARTIALLY_ACTIVE))
201
    projection = {'defaultImageUrl':1}
201
    projection = {'defaultImageUrl':1}
202
    excludeBrands = ["Live Demo", "FOC", "Dummy", "FOC HANDSET"]
202
    excludeBrands = ["Live Demo", "Dummy", "FOC HANDSET"]
203
    for tag, item in tuples:
203
    for tag, item in tuples:
204
        if item.brand in excludeBrands:
204
        if item.brand in excludeBrands:
205
            continue
205
            continue
206
        if not catalogMap.has_key(item.catalog_item_id):
206
        if not catalogMap.has_key(item.catalog_item_id):
207
            catalogObj = {}
207
            catalogObj = {}
Line 222... Line 222...
222
            except:
222
            except:
223
                try:
223
                try:
224
                    catalogObj['imageUrl'] = 'https://images.smartdukaan.com/uploads/campaigns/' + str(item.catalog_item_id) + '.jpg'
224
                    catalogObj['imageUrl'] = 'https://images.smartdukaan.com/uploads/campaigns/' + str(item.catalog_item_id) + '.jpg'
225
                except:
225
                except:
226
                    traceback.print_exc()
226
                    traceback.print_exc()
227
            try: 
227
            try:
228
                catalogObj['rank'] = rankingList.index(item.catalog_item_id)
228
                catalogObj['rank'] = rankingList.index(item.catalog_item_id)
229
            except:
229
            except:
230
                # A very big number
230
                # A very big number
231
                traceback.print_exc()
231
                traceback.print_exc()
232
                catalogObj['rank'] = 50000000
232
                catalogObj['rank'] = 50000000
233
            if featureMap.has_key(item.catalog_item_id) and featureMap.get(item.catalog_item_id) is not None:
233
            if featureMap.has_key(item.catalog_item_id) and featureMap.get(item.catalog_item_id) is not None:
234
                    catalogObj['feature'] = featureMap.get(item.catalog_item_id)
234
                catalogObj['feature'] = featureMap.get(item.catalog_item_id)
235
            # Mobile and tablets are showcased in same category
235
            # Mobile and tablets are showcased in same category
236
            catalogObj['categoryId'] = item.category if item.category in CATEGORY_MASTER else 6
236
            catalogObj['categoryId'] = item.category if item.category in CATEGORY_MASTER else 6
237
            # Temporay change need to be removed  
237
            # Temporay change need to be removed  
238
            if item.category == 10006:
238
            if item.category == 10006:
239
                catalogObj['categoryId'] = 3
239
                catalogObj['categoryId'] = 3
Line 246... Line 246...
246
        if availabilityItemMap.has_key(str(item.id)):
246
        if availabilityItemMap.has_key(str(item.id)):
247
            for warehouseId, avMap in availabilityItemMap[str(item.id)].iteritems():
247
            for warehouseId, avMap in availabilityItemMap[str(item.id)].iteritems():
248
                print warehouseId, avMap, item.color, item.brand, item.model_name, item.model_number, item.catalog_item_id
248
                print warehouseId, avMap, item.color, item.brand, item.model_name, item.model_number, item.catalog_item_id
249
                if not catalogObj['avColor'].has_key(int(warehouseId)):
249
                if not catalogObj['avColor'].has_key(int(warehouseId)):
250
                    catalogObj['avColor'][int(warehouseId)] = 0
250
                    catalogObj['avColor'][int(warehouseId)] = 0
251
                
-
 
-
 
251
 
252
                color = catalogObj['avColor'][int(warehouseId)]
252
                color = catalogObj['avColor'][int(warehouseId)]
253
                if avMap['netAvailability'] > 0:
253
                if avMap['netAvailability'] > 0:
254
                    color = 2
254
                    color = 2
255
                elif avMap['netAvailability'] + avMap['netPo']:
255
                elif avMap['netAvailability'] + avMap['netPo']:
256
                    color = max(color, 1)
256
                    color = max(color, 1)
Line 260... Line 260...
260
 
260
 
261
        if not catalogObj['items'].has_key(item.id):
261
        if not catalogObj['items'].has_key(item.id):
262
            catalogObj['items'][item.id] = {'color': item.color.replace("f_", ""), 'tagPricing':[]}
262
            catalogObj['items'][item.id] = {'color': item.color.replace("f_", ""), 'tagPricing':[]}
263
            itemMap = catalogObj['items'].get(item.id)
263
            itemMap = catalogObj['items'].get(item.id)
264
            itemMap['tagPricing'].append(tag)
264
            itemMap['tagPricing'].append(tag)
265
    
265
 
266
    catalogObjs = []
266
    catalogObjs = []
267
    for catalogId, catalogMap in catalogMap.iteritems():
267
    for catalogId, catalogMap in catalogMap.iteritems():
268
        print "catalogMap", catalogMap
268
        print "catalogMap", catalogMap
269
        active = False
269
        active = False
270
        itemsMap = catalogMap['items']
270
        itemsMap = catalogMap['items']
Line 294... Line 294...
294
                      'w8468_i':0,
294
                      'w8468_i':0,
295
                      'w8889_i':0,
295
                      'w8889_i':0,
296
                      'w8947_i':0,
296
                      'w8947_i':0,
297
                      'w9213_i':0,
297
                      'w9213_i':0,
298
                      'w9203_i':0,
298
                      'w9203_i':0,
-
 
299
                      'show_default_b': "FOC" in catalogMap['brand'],
299
                      'mop_f': mop
300
                      'mop_f': mop
300
                      }
301
                      }
301
        for whId, color in catalogMap['avColor'].iteritems():
302
        for whId, color in catalogMap['avColor'].iteritems():
302
            print catalogId, whId, color, catalogMap['title']
303
            print catalogId, whId, color, catalogMap['title']
303
            catalogObj['w%d_i'%(whId)] = color
304
            catalogObj['w%d_i'%(whId)] = color
Line 311... Line 312...
311
 
312
 
312
 
313
 
313
def pushData():
314
def pushData():
314
    populateTagItems()
315
    populateTagItems()
315
 
316
 
316
    
317
 
317
if __name__ == '__main__':
318
if __name__ == '__main__':
318
    pushData()
319
    pushData()