| 3232 |
varun.gupt |
1 |
'''
|
|
|
2 |
Created on 31-Aug-2011
|
|
|
3 |
|
|
|
4 |
@author: Varun Gupta
|
|
|
5 |
'''
|
|
|
6 |
import tornado.httpserver
|
|
|
7 |
import tornado.ioloop
|
|
|
8 |
import tornado.web
|
|
|
9 |
import json, os, ConfigParser, sys
|
|
|
10 |
from PyLucene.Retriever import Retriever
|
| 3313 |
varun.gupt |
11 |
from Utils import getItemsWithTopScore, isPriceSame, getProductClusters, getFilteredClustersWithTopScores, \
|
| 3440 |
varun.gupt |
12 |
getDisplayInfo, getSynonyms
|
| 3232 |
varun.gupt |
13 |
|
| 3235 |
rajveer |
14 |
cmd_folder = os.path.dirname(os.path.abspath(os.environ["HOME"] + "/code/trunk/PyProj/src/shop2020/"))
|
| 3232 |
varun.gupt |
15 |
if cmd_folder not in sys.path:
|
|
|
16 |
sys.path.insert(0, cmd_folder)
|
|
|
17 |
|
|
|
18 |
from shop2020.clients.CatalogClient import CatalogClient
|
| 3350 |
varun.gupt |
19 |
from shop2020.thriftpy.model.v1.catalog.ttypes import status
|
| 3440 |
varun.gupt |
20 |
|
| 3232 |
varun.gupt |
21 |
class BaseHandler(tornado.web.RequestHandler):
|
|
|
22 |
def get_current_user(self):
|
|
|
23 |
return self.get_secure_cookie('userauth')
|
|
|
24 |
|
|
|
25 |
class LoginHandler(BaseHandler):
|
|
|
26 |
def get(self):
|
|
|
27 |
self.loader = tornado.template.Loader('HTMLTemplates')
|
|
|
28 |
self.write(self.loader.load('LoginForm.html').generate())
|
|
|
29 |
|
|
|
30 |
def post(self):
|
|
|
31 |
config = ConfigParser.SafeConfigParser()
|
|
|
32 |
config.read('app.cfg')
|
|
|
33 |
|
|
|
34 |
username = self.get_argument('username')
|
|
|
35 |
password = self.get_argument('password')
|
|
|
36 |
|
|
|
37 |
if username == config.get('auth', 'username') and password == config.get('auth', 'password'):
|
|
|
38 |
print 'Password Matched'
|
|
|
39 |
self.set_secure_cookie("userauth", username + '_' + password)
|
|
|
40 |
self.redirect('/')
|
|
|
41 |
else:
|
|
|
42 |
self.redirect('/login')
|
|
|
43 |
|
| 3440 |
varun.gupt |
44 |
class FeedbackHandler(BaseHandler):
|
|
|
45 |
|
|
|
46 |
def save(self, entity, source, feedback_type, selected_item = None):
|
|
|
47 |
self.feedback_file = '/tmp/feedback.json'
|
|
|
48 |
file_to_read = open(self.feedback_file, 'r')
|
|
|
49 |
|
|
|
50 |
feedbacks_json = file_to_read.read()
|
|
|
51 |
file_to_read.close()
|
|
|
52 |
|
|
|
53 |
feedbacks = json.loads(feedbacks_json) if len(feedbacks_json) > 1 else {}
|
|
|
54 |
|
|
|
55 |
if entity not in feedbacks: feedbacks[entity] = {}
|
|
|
56 |
|
|
|
57 |
feedbacks[entity][source] = {'type': feedback_type}
|
|
|
58 |
|
|
|
59 |
if selected_item is not None: feedbacks[entity][source]['selected_item'] = selected_item
|
|
|
60 |
|
|
|
61 |
file_to_write = open(self.feedback_file, 'w')
|
|
|
62 |
file_to_write.write(json.dumps(feedbacks))
|
|
|
63 |
file_to_write.close()
|
|
|
64 |
|
|
|
65 |
def post(self):
|
|
|
66 |
feedback_type = self.get_argument('type')
|
|
|
67 |
entity_id = self.get_argument('entityId')
|
|
|
68 |
price_data_source = self.get_argument('source')
|
|
|
69 |
|
|
|
70 |
print feedback_type, entity_id, price_data_source
|
|
|
71 |
|
|
|
72 |
if feedback_type == 'select':
|
|
|
73 |
selected_item = self.get_argument('selected')
|
|
|
74 |
print selected_item
|
|
|
75 |
self.save(entity_id, price_data_source, feedback_type, selected_item)
|
|
|
76 |
else:
|
|
|
77 |
self.save(entity_id, price_data_source, feedback_type)
|
|
|
78 |
|
|
|
79 |
def get(self):
|
|
|
80 |
print 'GET: Feedback data'
|
|
|
81 |
self.feedback_file = '/tmp/feedback.json'
|
|
|
82 |
file_to_read = open(self.feedback_file, 'r')
|
|
|
83 |
|
|
|
84 |
feedbacks_json = file_to_read.read()
|
|
|
85 |
file_to_read.close()
|
|
|
86 |
|
|
|
87 |
self.write(feedbacks_json)
|
|
|
88 |
|
| 3232 |
varun.gupt |
89 |
class MainHandler(BaseHandler):
|
|
|
90 |
|
|
|
91 |
@tornado.web.authenticated
|
|
|
92 |
def get(self):
|
|
|
93 |
self.loader = tornado.template.Loader('HTMLTemplates')
|
|
|
94 |
catalog_client = CatalogClient().get_client()
|
| 3350 |
varun.gupt |
95 |
items = catalog_client.getAllItemsByStatus(status.ACTIVE)
|
|
|
96 |
items.extend(catalog_client.getAllItemsByStatus(status.PAUSED))
|
|
|
97 |
items.extend(catalog_client.getAllItemsByStatus(status.PAUSED_BY_RISK))
|
| 3440 |
varun.gupt |
98 |
synonyms = getSynonyms()
|
|
|
99 |
print synonyms
|
| 3232 |
varun.gupt |
100 |
retriever = Retriever()
|
|
|
101 |
products = {}
|
|
|
102 |
|
|
|
103 |
for item in items:
|
|
|
104 |
if item.category in (10002, 10003, 10004, 10005, 10010): products[item.catalogItemId] = item
|
|
|
105 |
|
|
|
106 |
comparative_prices = []
|
| 3313 |
varun.gupt |
107 |
|
| 3232 |
varun.gupt |
108 |
for item in sorted(products.itervalues(), key = lambda item: item.brand):
|
|
|
109 |
try:
|
|
|
110 |
model_name = item.modelName.strip() if len(item.modelName.strip()) > 0 else None
|
|
|
111 |
model_number = item.modelNumber.strip() if len(item.modelNumber.strip()) > 0 else None
|
|
|
112 |
|
| 3440 |
varun.gupt |
113 |
synonyms_for_this_model = synonyms[item.catalogItemId] if item.catalogItemId in synonyms else None
|
| 3232 |
varun.gupt |
114 |
|
| 3440 |
varun.gupt |
115 |
search_results = retriever.retrieve(model_number = model_number, model_name = model_name, brand = item.brand, synonyms = synonyms_for_this_model)
|
|
|
116 |
|
| 3313 |
varun.gupt |
117 |
clusters = getProductClusters(search_results)
|
|
|
118 |
filtered_clusters = getFilteredClustersWithTopScores(clusters)
|
| 3232 |
varun.gupt |
119 |
|
| 3313 |
varun.gupt |
120 |
display_info = getDisplayInfo(filtered_clusters)
|
|
|
121 |
|
| 3232 |
varun.gupt |
122 |
product_name = "%s " % item.brand
|
|
|
123 |
product_name += "%s " % model_name if model_name is not None else ''
|
|
|
124 |
product_name += model_number if model_number is not None else ''
|
|
|
125 |
|
| 3440 |
varun.gupt |
126 |
display_info['entity_id'] = item.catalogItemId
|
| 3313 |
varun.gupt |
127 |
display_info['product_name'] = product_name
|
|
|
128 |
display_info['saholic'] = {'price': item.sellingPrice}
|
|
|
129 |
comparative_prices.append(display_info)
|
| 3232 |
varun.gupt |
130 |
except Exception as e:
|
| 3313 |
varun.gupt |
131 |
print e
|
| 3232 |
varun.gupt |
132 |
|
|
|
133 |
self.write(self.loader.load('PriceChart.html').generate(data = comparative_prices))
|
|
|
134 |
|
|
|
135 |
|
|
|
136 |
settings = {
|
|
|
137 |
'static_path': os.path.join(os.path.dirname(__file__), 'static'),
|
|
|
138 |
'login_url': '/login',
|
|
|
139 |
'cookie_secret' :"61oETzKXQAGaYdkL5gEmGeJJFuYh7EQnp2XdTP1o/Vo="
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
application = tornado.web.Application([
|
|
|
143 |
(r"/", MainHandler),
|
|
|
144 |
(r"/login", LoginHandler),
|
| 3440 |
varun.gupt |
145 |
(r"/feedback", FeedbackHandler),
|
| 3232 |
varun.gupt |
146 |
(r"/(jquery-1.6.2.min\.js)", tornado.web.StaticFileHandler, dict(path=settings['static_path']))
|
|
|
147 |
], **settings)
|
|
|
148 |
|
|
|
149 |
if __name__ == '__main__':
|
|
|
150 |
http_server = tornado.httpserver.HTTPServer(application)
|
|
|
151 |
http_server.listen(8889)
|
|
|
152 |
tornado.ioloop.IOLoop.instance().start()
|