Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
13828 kshitij.so 1
from elixir import * 
2
from shop2020.model.v1.catalog.impl import DataService
3
from shop2020.model.v1.catalog.impl.DataService import PrivateDeals, Item
4
from sqlalchemy.sql.functions import now
5
from datetime import datetime, timedelta
6
import pymongo
7
from dtr.utils.utils import to_java_date
13843 kshitij.so 8
import optparse
13828 kshitij.so 9
 
10
dealsMap = {}
11
con = None
12
dealsCatalogIds = []
13
itemCatalogMap = {}
14
timestamp = datetime.now()
15
print to_java_date(timestamp)
16
 
13843 kshitij.so 17
parser = optparse.OptionParser()
18
parser.add_option("-H", "--host", dest="hostname",
19
                      default="localhost",
20
                      type="string", help="The HOST where the DB server is running",
21
                      metavar="HOST")
13828 kshitij.so 22
 
13843 kshitij.so 23
DataService.initialize(db_hostname=options.hostname)
24
 
13828 kshitij.so 25
def get_mongo_connection(host='localhost', port=27017):
26
    global con
27
    if con is None:
28
        print "Establishing connection %s host and port %d" %(host,port)
29
        try:
30
            con = pymongo.MongoClient(host, port)
31
        except Exception, e:
32
            print e
33
            return None
34
    return con
35
 
36
def getPrivateDeals():
37
    global dealsMap
38
    dealsMap = dict()
39
    all_active_items_query =  session.query(PrivateDeals).filter(PrivateDeals.isActive==True).filter(now().between(PrivateDeals.startDate, PrivateDeals.endDate + timedelta(days=0)))
40
    all_active_private_deals = all_active_items_query.all()
41
    if all_active_private_deals is not None or all_active_private_deals!=[]:
42
        for active_private_deal in all_active_private_deals:
43
            item = Item.get_by(id = active_private_deal.item_id)
44
            if item.sellingPrice >  active_private_deal.dealPrice and item.status==3:
45
                dealsMap[active_private_deal.item_id] = active_private_deal
46
 
47
def getItemsToUpdate():
48
    global dealsCatalogIds
49
    global itemCatalogMap
50
    saholicCatalogIds = list(get_mongo_connection().Catalog.MasterData.find({'rank':{"$gt":0},'source_id':4}))
51
    for d in saholicCatalogIds:
52
        dealsCatalogIds.append(long(d['identifier']))
53
    items = Item.query.filter(Item.catalog_item_id.in_(dealsCatalogIds)).all()
54
    for item in items:
55
        temp = []
56
        if not itemCatalogMap.has_key(item.catalog_item_id):
57
            temp.append(item)
58
            print "****",type(item.catalog_item_id)
59
            itemCatalogMap[item.catalog_item_id] = temp
60
        else:
61
            val = itemCatalogMap.get(item.catalog_item_id)
62
            for l in val:
63
                temp.append(l)
64
            temp.append(item)
65
            itemCatalogMap[item.catalog_item_id] = temp
66
 
67
    for saholicCatalogId in saholicCatalogIds:
68
        d_items = itemCatalogMap.get(long(saholicCatalogId['identifier']))
69
        available_price = None
70
        for d_item in d_items: 
71
            if d_item.status == 3:
72
                in_stock =1
73
            else:
74
                in_stock = 0
75
                continue
76
            if dealsMap.get(d_item.id) is not None:
77
                available_price = dealsMap.get(d_item.id).dealPrice
78
            if (available_price !=None):
79
                break
80
        if (available_price is None):
81
            in_stock = 0
82
            for d_item in d_items:
83
                if d_item.status == 3:
84
                    available_price = d_item.sellingPrice
85
                    in_stock =1
86
                    break
87
        print long(saholicCatalogId['identifier'])
88
        print in_stock
89
        print available_price
90
        print dealsMap.get(d_item.id)
91
        print "++++++++++++++++++++++++++"
92
        if available_price > 0 or available_price is None:
93
            get_mongo_connection().Catalog.MasterData.update({'identifier':saholicCatalogId['identifier']}, {'$set' : {'available_price':available_price,'updatedOn':to_java_date(timestamp),'in_stock':in_stock}}, multi=True)
94
        else:
95
            get_mongo_connection().Catalog.MasterData.update({'identifier':saholicCatalogId['identifier']}, {'$set' : {'updatedOn':to_java_date(timestamp),'in_stock':in_stock}}, multi=True)
96
 
97
def main():
98
    getPrivateDeals()
99
    getItemsToUpdate()
100
 
101
if __name__=='__main__':
102
    main()