Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
20081 kshitij.so 1
from walrus import *
2
from dtr.utils.utils import get_mongo_connection, REVERSE_SOURCE_MAP, CATEGORY_MAP, SUB_CATEGORY_MAP
3
import random
4
 
5
xstr = lambda s: s or ""
6
database = Database()
7
ac = database.autocomplete()
8
 
9
class Suggestion():
10
    def __init__(self, category_id, subCategoryId, suggestion, word):
11
        self.category_id = category_id
12
        self.subCategoryId = subCategoryId
13
        self.suggestion = suggestion
14
        self.word = word
15
 
16
def addData():
17
    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})
18
    catalogMap = {}
19
    for item in items:
20
        if not catalogMap.has_key(item['skuBundleId']):
21
            title = xstr(item['brand'])+" "+xstr(item['model_name'])
22
            catalogMap[item['skuBundleId']] = {'brand':item['brand'],'title':title,'category':item['category_id'],'subCategory':item['subCategoryId']}
23
    for skuBundleId, details in catalogMap.iteritems():
24
        if not ac.exists(obj_id=skuBundleId):
25
            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')
26
 
27
 
28
def autoComplete():
29
    print "++++++++++++++"
30
    c = 0
31
    for i in ac.search("iPhone Plus"):
32
        c = c+1
33
        print i
34
    print c
35
 
36
def filterSuggestions(categorySuggestion):
37
    returnObj = []
38
    for cat in ('Mobiles','Tablets'):
39
        v = categorySuggestion.get(CATEGORY_MAP.get(cat))
40
        if v is not None:
41
            returnObj.append(v[0].__dict__)
42
            select_range = 2 if len(v[1:]) > 2 else len(v[1:]) 
43
            random_list = random.sample(set(v[1:]), select_range)
44
            for random_suggestion in random_list:
45
                returnObj.append(random_suggestion.__dict__)
46
            categorySuggestion.pop(CATEGORY_MAP.get(cat))
47
    for v in categorySuggestion.itervalues():
48
        returnObj.append(v[0].__dict__)
49
        select_range = 2 if len(v[1:]) > 2 else len(v[1:]) 
50
        random_list = random.sample(set(v[1:]), select_range)
51
        for random_suggestion in random_list:
52
            returnObj.append(random_suggestion.__dict__)
53
    return returnObj
54
 
55
def getSuggestions(search_text):
56
    categorySuggestion = {}
57
    results = list(ac.search(search_text))
58
    if len(results) > 10:
59
        """Lets group data"""
60
        for i in results:
61
            if i.get('subCategoryId'):
62
                if not categorySuggestion.has_key(i.get('subCategoryId')):
63
                    suggestion_obj_primary = Suggestion(i.get('category_id'),i.get('subCategoryId'),search_text+" in "+SUB_CATEGORY_MAP.get(i.get('subCategoryId')), search_text)
64
                    suggestion_obj = Suggestion(i.get('category_id'),i.get('subCategoryId'),i.get('title'), i.get('title'))
65
                    categorySuggestion[i.get('subCategoryId')] = [suggestion_obj_primary,suggestion_obj]
66
                else:
67
                    suggestion_obj = Suggestion(i.get('category_id'),i.get('subCategoryId'),i.get('title'), i.get('title'))
68
                    categorySuggestion.get(i.get('subCategoryId')).append(suggestion_obj)
69
            else:
70
                if not categorySuggestion.has_key(i.get('category_id')):
71
                    suggestion_obj_primary = Suggestion(i.get('category_id'),i.get('subCategoryId'),search_text+" in "+CATEGORY_MAP.get(i.get('category_id')), search_text)
72
                    suggestion_obj = Suggestion(i.get('category_id'),i.get('subCategoryId'),i.get('title'), i.get('title'))
73
                    categorySuggestion[i.get('category_id')] = [suggestion_obj_primary, suggestion_obj]
74
                else:
75
                    suggestion_obj = Suggestion(i.get('category_id'),i.get('subCategoryId'),i.get('title'), i.get('title'))
76
                    categorySuggestion.get(i.get('category_id')).append(suggestion_obj)
77
 
78
        returnObj = filterSuggestions(categorySuggestion)    
79
 
80
    else:
81
        for i in results:
82
            returnObj.append({'suggestion':i.get('title'),'category_id':i.get('category_id'),'subCategoryId':i.get('subCategoryId')})
83
 
84
    return returnObj
85
 
86
 
87
if __name__ == "__main__":
88
    ac.flush()
89
    addData()
90
    for i in getSuggestions("apple"):
91
        print i
92