Subversion Repositories SmartDukaan

Rev

Rev 22307 | Rev 22310 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

from elixir import *
from shop2020.clients.InventoryClient import InventoryClient
from shop2020.model.v1.catalog.impl import DataService
from shop2020.model.v1.catalog.impl.DataService import Item, Tag_Listing, \
    Tag_Ranking, PrivateDeals
from shop2020.thriftpy.model.v1.catalog.ttypes import status
from sqlalchemy.sql.expression import or_, desc
from sqlalchemy.sql.functions import now
import json
import optparse
import os
import pymongo
import pysolr
import shutil
import traceback
#import pymongo
#import pysolr


parser = optparse.OptionParser()
parser.add_option("-d", "--d", dest="dbHost",
                      default="127.0.0.1",
                      type="string", help="The HOST where the mysql server is running",
                      metavar="DBHOST")
parser.add_option("-s", "--s", dest="solrPath",
                      default="http://localhost:8984/solr/collection2",
                      type="string", help="Complete solr path",
                      metavar="SOLRHOST")
parser.add_option("-m", "--m", dest="mongoHost",
                      default="http://localhost:8984/solr/collection2",
                      type="string", help="Complete solr path",
                      metavar="HOST")

(options, args) = parser.parse_args()


DataService.initialize(db_hostname=options.hostname)
solr = pysolr.Solr(options.solrPath, timeout=10)

def get_mongo_connection(host='localhost', port=27017):
    global con
    if con is None:
        print "Establishing connection %s host and port %d" %(host,port)
        try:
            con = pymongo.MongoClient(host, port)
        except Exception, e:
            print e
            return None
    return con

class __SkuInfo:
    
    def __init__(self, id, ids, brand, model_name, category_id, subCategoryId,thumbnail, title, brand_synonyms, model_name_synonyms, source_product_names, \
                 category, subCategory):
        #Skubundle id
        self.id = id
        #_id of skus
        self.ids = ids
        self.brand = brand
        self.model_name = model_name
        self.category_id = category_id
        self.subCategoryId = subCategoryId
        self.thumbnail = thumbnail 
        self.title= title
        self.brand_synonyms = brand_synonyms
        self.model_name_synonyms = model_name_synonyms
        self.source_product_names = source_product_names
        self.category = category
        self.subCategory = subCategory
        
    def toJSON(self):
        return json.dumps(self, default=lambda o: o.__dict__, 
            sort_keys=True, indent=4)
        
class __Catalog:
    
    def __init__(self, id, rank, title, items):
        #Skubundle id
        self.id = id
        self.rank = rank
        self.title = title
        self._childDocuments_ = items
    def toJSON(self):
        return json.dumps(self, default=lambda o: o.__dict__)
        
class __Item:
    def __init__(self, id, color, tags):
        self.id = id
        self.color = color
        self._childDocuments_ = tags
        def toJSON(self):
            return json.dumps(self, default=lambda o: o.__dict__)
class __Tag:
    def __init__(self, id, sellingPrice, mop):
        self.id = id
        self.sellingPrice = sellingPrice
        self.mop = mop
        
        
#solr = pysolr.Solr(options.solrPath, timeout=10)


def todict(obj, classkey=None):
    if isinstance(obj, dict):
        data = {}
        for (k, v) in obj.items():
            data[k] = todict(v, classkey)
        return data
    elif hasattr(obj, "_ast"):
        return todict(obj._ast())
    elif hasattr(obj, "__iter__"):
        return [todict(v, classkey) for v in obj]
    elif hasattr(obj, "__dict__"):
        data = dict([(key, todict(value, classkey)) 
            for key, value in obj.__dict__.iteritems() 
            if not callable(value) and not key.startswith('_')])
        if classkey is not None and hasattr(obj, "__class__"):
            data[classkey] = obj.__class__.__name__
        return data
    else:
        return obj
    

def populateTagItems():
    
    
    tagRankingList = session.query(Tag_Ranking.catalogItemId).order_by(desc(Tag_Ranking.rankPoints)).all()
    
    
    
    catalogMap = {}
    #stmt = session.query(PrivateDeals).filter_by(isActive=1).filter(now().between(PrivateDeals.startDate, PrivateDeals.endDate)).subquery()
    #query = session.query(Item, privateDealAlias.dealPrice).outerjoin((privateDealAlias, Item.id==privateDealAlias.item_id)).filter(Item.status != status.PHASED_OUT)
    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)).filter(Tag_Listing.active==True)
    projection={'thumbnailImageUrl':1}
    for tag, item in tuples:
        if not catalogMap.has_key(item.catalog_item_id):
            catalogObj = {}
            catalogObj['title'] = " ".join(filter(None, [item.brand, item.model_name,  item.model_number]))
            catalogObj['identifier'] = item.catalog_item_id
            catalogObj['items'] = {}
            filterMap = {"_id":item.catalog_item_id}
            #Dont include it catalog not available
            try:
                catalogObj['imageUrl'] = get_mongo_connection(options.mongoHost).siteContent.find_one(filterMap, projection)['thumbnailImageUrl']
            except:
                continue
            try: 
                catalogObj['rank'] = tagRankingList.index(item.catalog_item_id)
            except:
                #A very big number
                catalogObj['rank'] = 500000

            catalogObj['categoryId'] = 3 if item.category in [10006, 10009] else 6   
            catalogMap[item.catalog_item_id] = catalogObj

        catalogObj = catalogMap.get(item.catalog_item_id)
        
        if not  catalogObj['items'].has_key(item.id):
            catalogObj['items'][item.id] = {'color': item.color, 'tagPricing':[]}
        
        itemMap = catalogObj['items'].get(item.id)
        itemMap['tagPricing'].append(tag)
        
    
    catalogObjs = []
    for catalogId, catalogMap in catalogMap.iteritems():
        itemsMap = catalogMap['items']
        itemObjs = []
        for itemId, itemMap in itemsMap.iteritems():
            tags = itemMap['tagPricing']
            for tag in tags:
                itemObj = {'id':('itemtag-%s-%s'%(itemId, tag.tag_id)), 'color_s':itemMap['color'],  'itemId_i': itemId, 'tagId_i':tag.tag_id, 
                           'mop_f': tag.mop, 'sellingPrice_f': tag.selling_price}
            itemObjs.append(itemObj)
        catalogObj = {'id':'catalog' + str(catalogId), 'rank_i':catalogMap['rank'], 'title_s': catalogMap['title'], '_childDocuments_':itemObjs, 'catalogId_i':catalogId}
        catalogObjs.append(catalogObj)
    
    print catalogObjs
    solr.delete(q='*:*')
    solr.add(catalogObjs)
        
        
        
            
        
    #items = Item.query.filter(Item.risky==True).filter(or_(Item.status==status.ACTIVE)).all()
#    global con
#    if con is None:
#        print "Establishing connection %s host and port %d" %(host,port)
#        try:
#            con = pymongo.MongoClient(host, port)
#        except Exception, e:
#            print e
#            return None
#    return con

def pushData():
    #rankPoints = populateRankPoints()
    populateTagItems()
    #orderedMap = orderIt
    #convertToSolrDoc() 
    
if __name__=='__main__':
    pushData()