Subversion Repositories SmartDukaan

Rev

Blame | Last modification | View Log | RSS feed

from walrus import *
from dtr.utils.utils import get_mongo_connection, REVERSE_SOURCE_MAP, CATEGORY_MAP, SUB_CATEGORY_MAP
import random

xstr = lambda s: s or ""
database = Database()
ac = database.autocomplete()

class Suggestion():
    def __init__(self, category_id, subCategoryId, suggestion, word):
        self.category_id = category_id
        self.subCategoryId = subCategoryId
        self.suggestion = suggestion
        self.word = word

def addData():
    items = get_mongo_connection().Catalog.MasterData.find({'source_id' : { "$in": REVERSE_SOURCE_MAP.keys() } },{'skuBundleId':1,'brand':1,'model_name':1,'brand':1,'category_id':1,'subCategoryId':1})
    catalogMap = {}
    for item in items:
        if not catalogMap.has_key(item['skuBundleId']):
            title = xstr(item['brand'])+" "+xstr(item['model_name'])
            catalogMap[item['skuBundleId']] = {'brand':item['brand'],'title':title,'category':item['category_id'],'subCategory':item['subCategoryId']}
    for skuBundleId, details in catalogMap.iteritems():
        if not ac.exists(obj_id=skuBundleId):
            print ac.store(obj_id=skuBundleId,title=details.get('title'),data={'skuBundleId':skuBundleId,'title':details.get('title'),'category_id':details.get('category'),'subCategoryId':details.get('subCategory'),'brand':details.get('brand')},obj_type='entry')


def autoComplete():
    print "++++++++++++++"
    c = 0
    for i in ac.search("iPhone Plus"):
        c = c+1
        print i
    print c

def filterSuggestions(categorySuggestion):
    returnObj = []
    for cat in ('Mobiles','Tablets'):
        v = categorySuggestion.get(CATEGORY_MAP.get(cat))
        if v is not None:
            returnObj.append(v[0].__dict__)
            select_range = 2 if len(v[1:]) > 2 else len(v[1:]) 
            random_list = random.sample(set(v[1:]), select_range)
            for random_suggestion in random_list:
                returnObj.append(random_suggestion.__dict__)
            categorySuggestion.pop(CATEGORY_MAP.get(cat))
    for v in categorySuggestion.itervalues():
        returnObj.append(v[0].__dict__)
        select_range = 2 if len(v[1:]) > 2 else len(v[1:]) 
        random_list = random.sample(set(v[1:]), select_range)
        for random_suggestion in random_list:
            returnObj.append(random_suggestion.__dict__)
    return returnObj

def getSuggestions(search_text):
    categorySuggestion = {}
    results = list(ac.search(search_text))
    if len(results) > 10:
        """Lets group data"""
        for i in results:
            if i.get('subCategoryId'):
                if not categorySuggestion.has_key(i.get('subCategoryId')):
                    suggestion_obj_primary = Suggestion(i.get('category_id'),i.get('subCategoryId'),search_text+" in "+SUB_CATEGORY_MAP.get(i.get('subCategoryId')), search_text)
                    suggestion_obj = Suggestion(i.get('category_id'),i.get('subCategoryId'),i.get('title'), i.get('title'))
                    categorySuggestion[i.get('subCategoryId')] = [suggestion_obj_primary,suggestion_obj]
                else:
                    suggestion_obj = Suggestion(i.get('category_id'),i.get('subCategoryId'),i.get('title'), i.get('title'))
                    categorySuggestion.get(i.get('subCategoryId')).append(suggestion_obj)
            else:
                if not categorySuggestion.has_key(i.get('category_id')):
                    suggestion_obj_primary = Suggestion(i.get('category_id'),i.get('subCategoryId'),search_text+" in "+CATEGORY_MAP.get(i.get('category_id')), search_text)
                    suggestion_obj = Suggestion(i.get('category_id'),i.get('subCategoryId'),i.get('title'), i.get('title'))
                    categorySuggestion[i.get('category_id')] = [suggestion_obj_primary, suggestion_obj]
                else:
                    suggestion_obj = Suggestion(i.get('category_id'),i.get('subCategoryId'),i.get('title'), i.get('title'))
                    categorySuggestion.get(i.get('category_id')).append(suggestion_obj)
        
        returnObj = filterSuggestions(categorySuggestion)    
        
    else:
        for i in results:
            returnObj.append({'suggestion':i.get('title'),'category_id':i.get('category_id'),'subCategoryId':i.get('subCategoryId')})
    
    return returnObj
        

if __name__ == "__main__":
    ac.flush()
    addData()
    for i in getSuggestions("apple"):
        print i