Subversion Repositories SmartDukaan

Rev

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

Rev 20089 Rev 20090
Line 5... Line 5...
5
xstr = lambda s: s or ""
5
xstr = lambda s: s or ""
6
database = Database()
6
database = Database()
7
ac = database.autocomplete()
7
ac = database.autocomplete()
8
 
8
 
9
class Suggestion():
9
class Suggestion():
10
    def __init__(self, category_id, subCategoryId, suggestion, word, category_name):
10
    def __init__(self, category_id, subCategoryId, suggestion, category_name):
11
        self.category_id = category_id
11
        self.category_id = category_id
12
        self.subCategoryId = subCategoryId
12
        self.subCategoryId = subCategoryId
13
        self.suggestion = suggestion
13
        self.suggestion = suggestion
14
        self.word = word
-
 
15
        self.category_name = category_name 
14
        self.category_name = category_name
16
 
15
 
17
 
16
 
18
def filterSuggestions(categorySuggestion):
17
def filterSuggestions(categorySuggestion):
19
    returnObj = []
18
    returnObj = []
20
    for cat in ('Mobiles','Tablets'):
19
    for cat in ('Mobiles','Tablets'):
21
        v = categorySuggestion.get(CATEGORY_MAP.get(cat))
20
        v = categorySuggestion.get(CATEGORY_MAP.get(cat))
22
        if v is not None:
21
        if v is not None:
23
            returnObj.append(v[0].__dict__)
22
            returnObj.append(v[0].__dict__)
24
            select_range = 2 if len(v[1:]) > 2 else len(v[1:]) 
23
            select_range = 5 if len(v[1:]) > 5 else len(v[1:]) 
25
            random_list = random.sample(set(v[1:]), select_range)
24
            random_list = random.sample(set(v[1:]), select_range)
26
            for random_suggestion in random_list:
25
            for random_suggestion in random_list:
27
                returnObj.append(random_suggestion.__dict__)
26
                returnObj.append(random_suggestion.__dict__)
28
            categorySuggestion.pop(CATEGORY_MAP.get(cat))
27
            categorySuggestion.pop(CATEGORY_MAP.get(cat))
29
    for v in categorySuggestion.itervalues():
28
    for v in categorySuggestion.itervalues():
30
        returnObj.append(v[0].__dict__)
29
        returnObj.append(v[0].__dict__)
31
        select_range = 2 if len(v[1:]) > 2 else len(v[1:]) 
30
        select_range = 3 if len(v[1:]) > 3 else len(v[1:]) 
32
        random_list = random.sample(set(v[1:]), select_range)
31
        random_list = random.sample(set(v[1:]), select_range)
33
        for random_suggestion in random_list:
32
        for random_suggestion in random_list:
34
            returnObj.append(random_suggestion.__dict__)
33
            returnObj.append(random_suggestion.__dict__)
35
    return returnObj
34
    return returnObj
36
 
35
 
Line 41... Line 40...
41
    if len(results) > 10:
40
    if len(results) > 10:
42
        """Lets group data"""
41
        """Lets group data"""
43
        for i in results:
42
        for i in results:
44
            if i.get('subCategoryId'):
43
            if i.get('subCategoryId'):
45
                if not categorySuggestion.has_key(i.get('subCategoryId')):
44
                if not categorySuggestion.has_key(i.get('subCategoryId')):
46
                    suggestion_obj_primary = Suggestion(i.get('category_id'),i.get('subCategoryId'),search_text, search_text, SUB_CATEGORY_MAP.get(i.get('subCategoryId')))
45
                    suggestion_obj_primary = Suggestion(i.get('category_id'),i.get('subCategoryId'),search_text,SUB_CATEGORY_MAP.get(i.get('subCategoryId')))
47
                    suggestion_obj = Suggestion(i.get('category_id'),i.get('subCategoryId'),i.get('title'), i.get('title'), None)
46
                    suggestion_obj = Suggestion(i.get('category_id'),i.get('subCategoryId'), i.get('title'),None)
48
                    categorySuggestion[i.get('subCategoryId')] = [suggestion_obj_primary,suggestion_obj]
47
                    categorySuggestion[i.get('subCategoryId')] = [suggestion_obj_primary,suggestion_obj]
49
                else:
48
                else:
50
                    suggestion_obj = Suggestion(i.get('category_id'),i.get('subCategoryId'),i.get('title'), i.get('title'), None)
49
                    suggestion_obj = Suggestion(i.get('category_id'),i.get('subCategoryId'),i.get('title'), None)
51
                    categorySuggestion.get(i.get('subCategoryId')).append(suggestion_obj)
50
                    categorySuggestion.get(i.get('subCategoryId')).append(suggestion_obj)
52
            else:
51
            else:
53
                if not categorySuggestion.has_key(i.get('category_id')):
52
                if not categorySuggestion.has_key(i.get('category_id')):
54
                    suggestion_obj_primary = Suggestion(i.get('category_id'),i.get('subCategoryId'),search_text, search_text, CATEGORY_MAP.get(i.get('category_id')), None)
53
                    suggestion_obj_primary = Suggestion(i.get('category_id'),i.get('subCategoryId'),search_text, CATEGORY_MAP.get(i.get('category_id')))
55
                    suggestion_obj = Suggestion(i.get('category_id'),i.get('subCategoryId'),i.get('title'), i.get('title'), None)
54
                    suggestion_obj = Suggestion(i.get('category_id'),i.get('subCategoryId'),i.get('title'), None)
56
                    categorySuggestion[i.get('category_id')] = [suggestion_obj_primary, suggestion_obj]
55
                    categorySuggestion[i.get('category_id')] = [suggestion_obj_primary, suggestion_obj]
57
                else:
56
                else:
58
                    suggestion_obj = Suggestion(i.get('category_id'),i.get('subCategoryId'),i.get('title'), i.get('title'))
57
                    suggestion_obj = Suggestion(i.get('category_id'),i.get('subCategoryId'),i.get('title'), None)
59
                    categorySuggestion.get(i.get('category_id')).append(suggestion_obj)
58
                    categorySuggestion.get(i.get('category_id')).append(suggestion_obj)
60
        
59
        
61
        returnObj = filterSuggestions(categorySuggestion)    
60
        returnObj = filterSuggestions(categorySuggestion)    
62
        
61
        
63
    else:
62
    else:
64
        for i in results:
63
        for i in results:
65
            returnObj.append({'suggestion':i.get('title'),'category_id':i.get('category_id'),'subCategoryId':i.get('subCategoryId'),'word':i.get('title'),'category_name':None})
64
            returnObj.append({'suggestion':i.get('title'),'category_id':i.get('category_id'),'subCategoryId':i.get('subCategoryId'),'word':i.get('title')})
66
    
65
    
67
    return returnObj
66
    return returnObj
68
 
67
 
69
 
68
 
70
def main():
69
def main():
-
 
70
    for i in getSuggestions("iphone"):
71
    pass
71
        print i
72
 
72
 
73
if __name__ == "__main__":
73
if __name__ == "__main__":
74
    main()
74
    main()
75
    
75