| 557 |
chandransh |
1 |
'''
|
|
|
2 |
Created on 20-Jul-2010
|
|
|
3 |
|
|
|
4 |
@author: ashish
|
|
|
5 |
'''
|
|
|
6 |
from shop2020.thriftpy.model.v1.user.ttypes import WidgetException as WException
|
|
|
7 |
from shop2020.thriftpy.model.v1.user.ttypes import WidgetType as WType
|
|
|
8 |
from shop2020.thriftpy.model.v1.user.ttypes import RatingType as RType
|
|
|
9 |
from shop2020.model.v1.user.impl.Dataservice import Widget, Item, WidgetItem,\
|
|
|
10 |
Ratings, UserRatings
|
|
|
11 |
from elixir import *
|
|
|
12 |
import datetime
|
|
|
13 |
|
|
|
14 |
def get_widget_by_id(id):
|
|
|
15 |
|
|
|
16 |
return Widget.get_by(id=id)
|
|
|
17 |
|
|
|
18 |
def get_item_by_id(id):
|
|
|
19 |
return Item.get_by(id=id)
|
|
|
20 |
|
|
|
21 |
def get_item_by_item_id(item_id):
|
|
|
22 |
return Item.get_by(item_id=item_id)
|
|
|
23 |
|
|
|
24 |
def get_widget_item(item, widget):
|
|
|
25 |
try:
|
|
|
26 |
query = WidgetItem.query.filter_by(item=item)
|
|
|
27 |
query = query.filter_by(widget=widget)
|
|
|
28 |
return query.one()
|
|
|
29 |
except:
|
|
|
30 |
return None
|
|
|
31 |
|
|
|
32 |
def add_widget(widget):
|
|
|
33 |
if not widget:
|
|
|
34 |
raise WException(101, "Cannot add null widget")
|
|
|
35 |
|
|
|
36 |
if widget.id:
|
|
|
37 |
raise WException(101, "id is already assigned")
|
|
|
38 |
|
|
|
39 |
w = Widget()
|
|
|
40 |
if widget.customer_id:
|
|
|
41 |
w.customer_id = widget.customer_id
|
|
|
42 |
w.type = widget.type
|
|
|
43 |
w.name = widget.name
|
|
|
44 |
w.enabled = 1
|
|
|
45 |
|
|
|
46 |
session.commit()
|
|
|
47 |
|
|
|
48 |
for item in widget.items:
|
|
|
49 |
w_item = get_item_by_item_id(item.item_id)
|
|
|
50 |
if not w_item:
|
|
|
51 |
#item is not present in system, we need to add the item
|
|
|
52 |
w_item = Item()
|
|
|
53 |
w_item.item_id = item.item_id
|
|
|
54 |
w_item.enabled = 1
|
|
|
55 |
w_item.snippet = item.snippet
|
|
|
56 |
session.commit()
|
|
|
57 |
#create a widgetitem mapping here now
|
|
|
58 |
widget_item = get_widget_item(w_item, w)
|
|
|
59 |
if not widget_item:
|
|
|
60 |
widget_item = WidgetItem()
|
|
|
61 |
widget_item.widget = w
|
|
|
62 |
widget_item.item = w_item
|
|
|
63 |
|
|
|
64 |
widget_item.added_on = datetime.datetime.now()
|
|
|
65 |
session.commit()
|
|
|
66 |
|
|
|
67 |
def add_items_to_widget(widget_id, items):
|
|
|
68 |
widget = get_widget_by_id(widget_id)
|
|
|
69 |
if not widget:
|
|
|
70 |
raise WException(101, "Widget not found")
|
|
|
71 |
for item in items:
|
|
|
72 |
i = get_item_by_item_id(item)
|
|
|
73 |
if i:
|
|
|
74 |
#check if widgetitem is already present
|
|
|
75 |
witem = get_widget_item(i, widget)
|
|
|
76 |
if not witem:
|
|
|
77 |
witem = WidgetItem()
|
|
|
78 |
witem.widget = widget
|
|
|
79 |
witem.item = i
|
|
|
80 |
witem.added_on = datetime.datetime.now()
|
|
|
81 |
session.commit()
|
|
|
82 |
|
|
|
83 |
def get_my_research_widget(user_id):
|
|
|
84 |
query = Widget.query.filter_by(customer_id=user_id)
|
|
|
85 |
query = query.filter_by(type=WType.MY_RESEARCH)
|
|
|
86 |
query = query.filter_by(enabled=1)
|
|
|
87 |
try:
|
|
|
88 |
widget = query.one()
|
|
|
89 |
except:
|
|
|
90 |
widget = None
|
|
|
91 |
|
|
|
92 |
if not widget:
|
|
|
93 |
#research widget not present
|
|
|
94 |
widget = Widget()
|
|
|
95 |
widget.type = WType.MY_RESEARCH
|
|
|
96 |
widget.enabled = 1
|
|
|
97 |
widget.customer_id = user_id
|
|
|
98 |
widget.name = "My Research"
|
|
|
99 |
session.commit()
|
|
|
100 |
return widget
|
|
|
101 |
|
|
|
102 |
def update_my_research(user_id, item_id):
|
|
|
103 |
isNew = False
|
|
|
104 |
#get my research widget
|
|
|
105 |
research_widget = get_my_research_widget(user_id)
|
|
|
106 |
#search for item id
|
|
|
107 |
item = get_item_by_item_id(item_id)
|
|
|
108 |
if not item:
|
|
|
109 |
#Ideally it should throw an exception, but we are allowing additions here for now.
|
|
|
110 |
item = Item()
|
|
|
111 |
item.item_id = item_id
|
|
|
112 |
item.snippet = "Lorem Ipsum"
|
|
|
113 |
item.enabled = 1
|
|
|
114 |
session.commit()
|
|
|
115 |
witem = get_widget_item(item, research_widget)
|
|
|
116 |
if not witem:
|
|
|
117 |
witem = WidgetItem()
|
|
|
118 |
witem.widget = research_widget
|
|
|
119 |
witem.item = item
|
|
|
120 |
isNew = True
|
|
|
121 |
witem.added_on = datetime.datetime.now()
|
|
|
122 |
session.commit()
|
|
|
123 |
return isNew
|
|
|
124 |
|
|
|
125 |
def delete_item_from_widget(widget_id, item_id):
|
|
|
126 |
widget = get_widget_by_id(widget_id)
|
|
|
127 |
item = get_item_by_item_id(item_id)
|
|
|
128 |
if not item:
|
|
|
129 |
#ideally it should throw an exception, but we are returning simply
|
|
|
130 |
return
|
|
|
131 |
query = WidgetItem.query.filter_by(item=item)
|
|
|
132 |
query = query.filter_by(widget=widget)
|
|
|
133 |
witem = query.one()
|
|
|
134 |
witem.delete()
|
|
|
135 |
session.commit()
|
|
|
136 |
return
|
|
|
137 |
|
|
|
138 |
def delete_item_from_my_research(user_id, item_id):
|
|
|
139 |
#get my research widget
|
|
|
140 |
research_widget = get_my_research_widget(user_id)
|
|
|
141 |
#search for item id
|
|
|
142 |
delete_item_from_widget(research_widget.id, item_id )
|
|
|
143 |
|
|
|
144 |
def update_widget(widget_id, enabled):
|
|
|
145 |
widget = get_widget_by_id(widget_id)
|
|
|
146 |
if not widget:
|
|
|
147 |
raise WException(101, "widget not present")
|
|
|
148 |
if enabled:
|
|
|
149 |
widget.enabled = 1
|
|
|
150 |
else:
|
|
|
151 |
widget.enabled = 0
|
|
|
152 |
session.commit()
|
|
|
153 |
|
|
|
154 |
def update_widget_item(widget_item, enabled):
|
|
|
155 |
|
|
|
156 |
pass
|
|
|
157 |
|
|
|
158 |
def get_widget_by_user_id(type, user_id, is_session_id, enabled):
|
|
|
159 |
try:
|
|
|
160 |
query = Widget.query.filter_by(type = type)
|
|
|
161 |
if is_session_id == True:
|
|
|
162 |
query = query.query.filter_by(session_id=user_id)
|
|
|
163 |
else:
|
|
|
164 |
query = query.filter_by(customer_id=user_id)
|
|
|
165 |
|
|
|
166 |
if enabled:
|
|
|
167 |
query = query.filter_by(enabled=1)
|
|
|
168 |
|
|
|
169 |
return query.one()
|
|
|
170 |
except Exception as inst:
|
|
|
171 |
print type(inst)
|
|
|
172 |
print inst.args
|
|
|
173 |
print inst
|
|
|
174 |
raise WException(101, "no widget found")
|
|
|
175 |
|
|
|
176 |
|
|
|
177 |
def get_widget_by_type(type, user_id, enabled):
|
|
|
178 |
try:
|
|
|
179 |
query = Widget.query.filter_by(type = type)
|
|
|
180 |
if enabled:
|
|
|
181 |
query = query.filter_by(enabled=1)
|
|
|
182 |
|
|
|
183 |
if user_id:
|
|
|
184 |
query = query.filter_by(customer_id=user_id)
|
|
|
185 |
return query.one()
|
|
|
186 |
except:
|
|
|
187 |
raise WException(101, "no widget found")
|
|
|
188 |
"""
|
|
|
189 |
except Exception as inst:
|
|
|
190 |
print type(inst)
|
|
|
191 |
print inst.args
|
|
|
192 |
print inst
|
|
|
193 |
raise WException(101, "no widget found")
|
|
|
194 |
"""
|
|
|
195 |
|
|
|
196 |
|
|
|
197 |
def get_my_research(user_id):
|
|
|
198 |
return get_widget_by_type(WType.MY_RESEARCH, user_id)
|
|
|
199 |
|
|
|
200 |
def get_browse_history(user_id, is_session_id):
|
|
|
201 |
if is_session_id == True:
|
|
|
202 |
return get_widget_by_user_id(WType.BROWSE_HISTORY, user_id, is_session_id)
|
|
|
203 |
else:
|
|
|
204 |
return get_widget_by_user_id(WType.BROWSE_HISTORY, user_id, is_session_id)
|
|
|
205 |
|
|
|
206 |
def get_ratings(user_id, item_id):
|
|
|
207 |
query = Ratings.query.filter_by(item_id = item_id)
|
|
|
208 |
type_ratings = query.all()
|
|
|
209 |
if user_id:
|
|
|
210 |
query = UserRatings.query.filter_by(item_id = item_id)
|
|
|
211 |
query = query.filter_by(user_id = user_id)
|
|
|
212 |
try:
|
|
|
213 |
user_rating = query.one()
|
|
|
214 |
except:
|
|
|
215 |
return type_ratings, None
|
|
|
216 |
return type_ratings, user_rating
|
|
|
217 |
return type_ratings, None
|
|
|
218 |
|
|
|
219 |
def update_ratings(user_id, item_id, rating, type):
|
|
|
220 |
if user_id:
|
|
|
221 |
if type == RType.USER_CURRENT:
|
|
|
222 |
query = UserRatings.query.filter_by(item_id = item_id)
|
|
|
223 |
query = query.filter_by(user_id = user_id)
|
|
|
224 |
try:
|
|
|
225 |
user_rating = query.one()
|
|
|
226 |
except:
|
|
|
227 |
user_rating = UserRatings()
|
|
|
228 |
user_rating.user_id = user_id
|
|
|
229 |
user_rating.item_id = item_id
|
|
|
230 |
user_rating.rating = rating
|
|
|
231 |
session.commit()
|
|
|
232 |
|
|
|
233 |
#update cumulative ratings
|
|
|
234 |
query = UserRatings.query.filter_by(item_id = item_id)
|
|
|
235 |
ratings = query.all()
|
|
|
236 |
user_count = len(ratings)
|
|
|
237 |
|
|
|
238 |
query = Ratings.query.filter_by(item_id = item_id)
|
|
|
239 |
query = Ratings.query.filter_by(type = RType.USER_ALL)
|
|
|
240 |
try:
|
|
|
241 |
current_rating = query.one()
|
|
|
242 |
new_rating = (current_rating.rating*(user_count -1) + rating)/user_count
|
|
|
243 |
current_rating.rating = new_rating
|
|
|
244 |
except:
|
|
|
245 |
current_rating = Ratings()
|
|
|
246 |
current_rating.type= RType.USER_ALL
|
|
|
247 |
current_rating.item_id = item_id
|
|
|
248 |
current_rating.rating = rating
|
|
|
249 |
|
|
|
250 |
session.commit()
|
|
|
251 |
else:
|
|
|
252 |
query = Ratings.query.filter_by(item_id = item_id)
|
|
|
253 |
query = Ratings.query.filter_by(type = type)
|
|
|
254 |
try:
|
|
|
255 |
current_rating = query.one()
|
|
|
256 |
current_rating.rating = rating
|
|
|
257 |
except:
|
|
|
258 |
current_rating = Ratings()
|
|
|
259 |
current_rating.type= type
|
|
|
260 |
current_rating.item_id = item_id
|
|
|
261 |
current_rating.rating = rating
|
|
|
262 |
|
|
|
263 |
session.commit()
|
|
|
264 |
|
|
|
265 |
|
|
|
266 |
def update_browse_history(user_id, item_id, is_session_id):
|
|
|
267 |
if is_session_id == True:
|
|
|
268 |
query = Widget.query.filter_by(session_id = user_id)
|
|
|
269 |
query = query.filter_by(type = WType.BROWSE_HISTORY)
|
|
|
270 |
try:
|
|
|
271 |
current_widget = query.one()
|
|
|
272 |
except:
|
|
|
273 |
current_widget = Widget()
|
|
|
274 |
current_widget.session_id = user_id
|
|
|
275 |
current_widget.enabled = True
|
|
|
276 |
current_widget.type = WType.BROWSE_HISTORY
|
|
|
277 |
else:
|
|
|
278 |
query = Widget.query.filter_by(customer_id = user_id)
|
|
|
279 |
query = query.filter_by(type = WType.BROWSE_HISTORY)
|
|
|
280 |
try:
|
|
|
281 |
current_widget = query.one()
|
|
|
282 |
except:
|
|
|
283 |
current_widget = Widget()
|
|
|
284 |
current_widget.customer_id = user_id
|
|
|
285 |
current_widget.enabled = True
|
|
|
286 |
current_widget.type = WType.BROWSE_HISTORY
|
|
|
287 |
session.commit()
|
|
|
288 |
items = []
|
|
|
289 |
items.append(item_id)
|
|
|
290 |
add_items_to_widget(current_widget.id, items)
|
|
|
291 |
|
|
|
292 |
|