| 130 |
ashish |
1 |
'''
|
|
|
2 |
Created on 28-Apr-2010
|
|
|
3 |
|
|
|
4 |
@author: ashish
|
|
|
5 |
'''
|
|
|
6 |
from shop2020.model.v1.user.impl import Dataservice
|
|
|
7 |
from shop2020.utils.Utils import log_entry, to_py_date, to_java_date
|
|
|
8 |
from shop2020.model.v1.user.impl.Dataservice import User, PrimaryInfo, IPMap,\
|
|
|
9 |
Address, SocialService, SocialHandle, State, InternalInfo
|
|
|
10 |
from shop2020.thriftpy.model.v1.user.ttypes import UserContextException,\
|
|
|
11 |
AuthenticationException, AccountStatus
|
|
|
12 |
from elixir import session
|
|
|
13 |
import datetime
|
| 513 |
rajveer |
14 |
from sqlalchemy.orm.session import Session
|
| 130 |
ashish |
15 |
|
|
|
16 |
def initialize():
|
|
|
17 |
log_entry("initialize@DataAccessor", "Initializing data service")
|
|
|
18 |
Dataservice.initialize()
|
|
|
19 |
|
|
|
20 |
def create_context(user_context):
|
|
|
21 |
if user_context.id:
|
|
|
22 |
raise UserContextException(109, "id is set, user cannot be created")
|
|
|
23 |
user_context_to_add = User()
|
| 413 |
rajveer |
24 |
primary_info = PrimaryInfo()
|
|
|
25 |
primary_info.user = user_context_to_add
|
|
|
26 |
|
| 130 |
ashish |
27 |
if user_context.primaryInfo:
|
| 413 |
rajveer |
28 |
|
| 130 |
ashish |
29 |
if user_context.primaryInfo.firstName:
|
|
|
30 |
primary_info.first_name = user_context.primaryInfo.firstName
|
|
|
31 |
|
|
|
32 |
if user_context.primaryInfo.middleName:
|
|
|
33 |
primary_info.middle_name = user_context.primaryInfo.middleName
|
|
|
34 |
|
|
|
35 |
if user_context.primaryInfo.lastName:
|
|
|
36 |
primary_info.last_name = user_context.primaryInfo.lastName
|
|
|
37 |
|
|
|
38 |
if user_context.primaryInfo.title:
|
|
|
39 |
primary_info.title = user_context.primaryInfo.title
|
|
|
40 |
|
|
|
41 |
if user_context.primaryInfo.pictureUrl:
|
|
|
42 |
primary_info.picture_id = user_context.primaryInfo.pictureUrl
|
|
|
43 |
|
|
|
44 |
if user_context.primaryInfo.email:
|
|
|
45 |
primary_info.email = user_context.primaryInfo.email
|
|
|
46 |
|
|
|
47 |
if user_context.primaryInfo.userHandle:
|
|
|
48 |
primary_info.user_handle = user_context.primaryInfo.userHandle
|
|
|
49 |
|
|
|
50 |
if user_context.primaryInfo.password:
|
|
|
51 |
primary_info.password = user_context.primaryInfo.password
|
|
|
52 |
|
|
|
53 |
if user_context.primaryInfo.occupation:
|
|
|
54 |
primary_info.occupation = user_context.primaryInfo.occupation
|
|
|
55 |
|
|
|
56 |
if user_context.primaryInfo.hintQuestion:
|
|
|
57 |
primary_info.hint_question = user_context.primaryInfo.hintQuestion
|
|
|
58 |
|
|
|
59 |
if user_context.primaryInfo.hintAnswer:
|
|
|
60 |
primary_info.hint_answer = user_context.primaryInfo.hintAnswer
|
|
|
61 |
|
|
|
62 |
if user_context.primaryInfo.shipmentOption:
|
|
|
63 |
primary_info.shipment_option = user_context.primaryInfo.shipmentOption
|
|
|
64 |
|
| 509 |
rajveer |
65 |
if user_context.primaryInfo.dateOfBirth:
|
|
|
66 |
user_context.primaryInfo.date_of_birth = user_context.primaryInfo.dateOfBirth
|
| 513 |
rajveer |
67 |
|
|
|
68 |
if user_context.primaryInfo.communicationEmail:
|
|
|
69 |
user_context.primaryInfo.communication_email = user_context.primaryInfo.communicationEmail
|
|
|
70 |
|
| 404 |
rajveer |
71 |
if user_context.sessionid:
|
|
|
72 |
user_context_to_add.session_id = user_context.sessionid
|
| 130 |
ashish |
73 |
|
|
|
74 |
internal_info = InternalInfo()
|
|
|
75 |
user_state = State()
|
|
|
76 |
|
|
|
77 |
#initialize internal info
|
|
|
78 |
|
|
|
79 |
internal_info.geo_zone = 0
|
|
|
80 |
internal_info.shipment_zone = 0
|
|
|
81 |
internal_info.tax_zone = 0
|
|
|
82 |
internal_info.rank_score = 0.0
|
|
|
83 |
internal_info.user = user_context_to_add
|
|
|
84 |
|
|
|
85 |
#initialize userState
|
|
|
86 |
user_state.is_email_verified = False
|
|
|
87 |
user_state.is_sms_verified = False
|
|
|
88 |
user_state.is_logged_in = False
|
|
|
89 |
user_state.account_status = AccountStatus.ACTIVE
|
|
|
90 |
user_state.is_enrolled = False
|
| 404 |
rajveer |
91 |
#user_state.last_login_timestamp = 0
|
|
|
92 |
#user_state.last_logout = 0
|
| 132 |
ashish |
93 |
user_state.ip_list = []
|
| 130 |
ashish |
94 |
user_state.active_since = datetime.datetime.now()
|
| 413 |
rajveer |
95 |
if user_context.userState:
|
|
|
96 |
user_state.current_shopping_cart = user_context.userState.shoppingCartHandle
|
| 130 |
ashish |
97 |
user_state.user = user_context_to_add
|
|
|
98 |
|
|
|
99 |
session.commit()
|
| 404 |
rajveer |
100 |
|
|
|
101 |
return user_context_to_add.id
|
| 130 |
ashish |
102 |
#===============================================================================
|
|
|
103 |
# All the validation rules prioir to adding a user to system go here
|
|
|
104 |
#===============================================================================
|
|
|
105 |
def validate_context_to_add(user_context):
|
|
|
106 |
return True
|
|
|
107 |
|
|
|
108 |
|
|
|
109 |
#===============================================================================
|
|
|
110 |
# Need to provide the update apis here for relevant fields in PrimaryInfo.
|
|
|
111 |
#===============================================================================
|
|
|
112 |
def update_context(user_context):
|
|
|
113 |
|
| 415 |
ashish |
114 |
if not user_context.id:
|
|
|
115 |
raise UserContextException(110, "user does not exist")
|
|
|
116 |
user_context_to_add = get_context_by_id(user_context.id)
|
|
|
117 |
primary_info = user_context_to_add.primary_info
|
|
|
118 |
|
|
|
119 |
#if primary info does not exist
|
|
|
120 |
if not primary_info:
|
|
|
121 |
primary_info = PrimaryInfo()
|
|
|
122 |
primary_info.user = user_context_to_add
|
|
|
123 |
|
|
|
124 |
if user_context.primaryInfo:
|
|
|
125 |
|
|
|
126 |
if user_context.primaryInfo.firstName:
|
|
|
127 |
primary_info.first_name = user_context.primaryInfo.firstName
|
|
|
128 |
|
|
|
129 |
if user_context.primaryInfo.middleName:
|
|
|
130 |
primary_info.middle_name = user_context.primaryInfo.middleName
|
|
|
131 |
|
|
|
132 |
if user_context.primaryInfo.lastName:
|
|
|
133 |
primary_info.last_name = user_context.primaryInfo.lastName
|
|
|
134 |
|
|
|
135 |
if user_context.primaryInfo.title:
|
|
|
136 |
primary_info.title = user_context.primaryInfo.title
|
|
|
137 |
|
|
|
138 |
if user_context.primaryInfo.pictureUrl:
|
|
|
139 |
primary_info.picture_id = user_context.primaryInfo.pictureUrl
|
|
|
140 |
|
|
|
141 |
if user_context.primaryInfo.email:
|
|
|
142 |
primary_info.email = user_context.primaryInfo.email
|
|
|
143 |
|
|
|
144 |
if user_context.primaryInfo.userHandle:
|
|
|
145 |
primary_info.user_handle = user_context.primaryInfo.userHandle
|
|
|
146 |
|
|
|
147 |
if user_context.primaryInfo.password:
|
|
|
148 |
primary_info.password = user_context.primaryInfo.password
|
|
|
149 |
|
|
|
150 |
if user_context.primaryInfo.occupation:
|
|
|
151 |
primary_info.occupation = user_context.primaryInfo.occupation
|
|
|
152 |
|
|
|
153 |
if user_context.primaryInfo.hintQuestion:
|
|
|
154 |
primary_info.hint_question = user_context.primaryInfo.hintQuestion
|
|
|
155 |
|
|
|
156 |
if user_context.primaryInfo.hintAnswer:
|
|
|
157 |
primary_info.hint_answer = user_context.primaryInfo.hintAnswer
|
|
|
158 |
|
|
|
159 |
if user_context.primaryInfo.shipmentOption:
|
|
|
160 |
primary_info.shipment_option = user_context.primaryInfo.shipmentOption
|
|
|
161 |
|
| 509 |
rajveer |
162 |
if user_context.primaryInfo.dateOfBirth:
|
| 513 |
rajveer |
163 |
primary_info.date_of_birth = to_py_date(user_context.primaryInfo.dateOfBirth)
|
| 415 |
ashish |
164 |
|
| 513 |
rajveer |
165 |
if user_context.primaryInfo.communicationEmail:
|
|
|
166 |
primary_info.communication_email = user_context.primaryInfo.communicationEmail
|
| 415 |
ashish |
167 |
|
| 513 |
rajveer |
168 |
|
| 415 |
ashish |
169 |
if user_context.sessionid:
|
|
|
170 |
user_context_to_add.session_id = user_context.sessionid
|
|
|
171 |
|
|
|
172 |
#userstate and internalinfo will not be updated here
|
|
|
173 |
|
|
|
174 |
session.commit()
|
|
|
175 |
return user_context_to_add.id
|
|
|
176 |
|
|
|
177 |
|
|
|
178 |
|
| 130 |
ashish |
179 |
def get_context_by_id(user_id):
|
|
|
180 |
return User.get_by(id=user_id)
|
|
|
181 |
|
|
|
182 |
def get_context_by_session_id(session_id):
|
|
|
183 |
return User.get_by(session_id=session_id)
|
|
|
184 |
|
|
|
185 |
def get_context_by_email_or_handle(email_or_handle, is_email):
|
|
|
186 |
if is_email:
|
|
|
187 |
primary_info = PrimaryInfo.get_by(email=email_or_handle)
|
|
|
188 |
else:
|
|
|
189 |
primary_info = PrimaryInfo.get_by(user_handle=email_or_handle)
|
|
|
190 |
|
|
|
191 |
if primary_info:
|
|
|
192 |
return primary_info.user
|
|
|
193 |
else:
|
|
|
194 |
raise UserContextException(101, "no such user in system")
|
|
|
195 |
|
|
|
196 |
def get_state(user_id, is_session_id):
|
|
|
197 |
if is_session_id:
|
|
|
198 |
user = get_context_by_session_id(user_id)
|
|
|
199 |
else:
|
|
|
200 |
user = get_context_by_id(user_id)
|
|
|
201 |
|
|
|
202 |
if not user:
|
|
|
203 |
raise_user_exception(user_id)
|
|
|
204 |
|
|
|
205 |
return user.state
|
|
|
206 |
|
|
|
207 |
def get_primary_info(user_id, is_session_id):
|
|
|
208 |
if is_session_id:
|
|
|
209 |
user = get_context_by_session_id(user_id)
|
|
|
210 |
else:
|
|
|
211 |
user = get_context_by_id(user_id)
|
|
|
212 |
|
|
|
213 |
if not user:
|
|
|
214 |
raise_user_exception(user_id)
|
|
|
215 |
|
|
|
216 |
return user.primary_info
|
|
|
217 |
|
|
|
218 |
def get_internal_info(user_id, is_session_id):
|
|
|
219 |
if is_session_id:
|
|
|
220 |
user = get_context_by_session_id(user_id)
|
|
|
221 |
else:
|
|
|
222 |
user = get_context_by_id(user_id)
|
|
|
223 |
|
|
|
224 |
if not user:
|
|
|
225 |
raise_user_exception(user_id)
|
|
|
226 |
|
|
|
227 |
return user.internal_info
|
|
|
228 |
|
|
|
229 |
def get_context(user_handle, password):
|
|
|
230 |
primary_info = PrimaryInfo.get_by(email=user_handle)
|
|
|
231 |
if not primary_info:
|
|
|
232 |
raise AuthenticationException("Wrong username", 102)
|
|
|
233 |
|
|
|
234 |
db_password = get_db_password(password)
|
|
|
235 |
|
|
|
236 |
if primary_info.password == db_password:
|
|
|
237 |
return primary_info.user
|
|
|
238 |
else:
|
|
|
239 |
raise AuthenticationException("Wrong username or password", 102)
|
|
|
240 |
|
|
|
241 |
def get_address(address_id):
|
|
|
242 |
address = Address.get_by(id=address_id)
|
|
|
243 |
return address
|
|
|
244 |
|
|
|
245 |
def get_social_service(service_id):
|
|
|
246 |
service = SocialService.get_by(service_id)
|
|
|
247 |
return service
|
|
|
248 |
|
|
|
249 |
def authenticate_user(user_handle, password):
|
|
|
250 |
try:
|
|
|
251 |
get_context(user_handle, password)
|
|
|
252 |
except:
|
|
|
253 |
return False
|
|
|
254 |
return True
|
|
|
255 |
|
|
|
256 |
def user_exists(email):
|
| 413 |
rajveer |
257 |
try:
|
|
|
258 |
get_context_by_email_or_handle(email, True)
|
|
|
259 |
except:
|
| 130 |
ashish |
260 |
return False
|
| 413 |
rajveer |
261 |
return True
|
| 130 |
ashish |
262 |
|
| 413 |
rajveer |
263 |
|
| 130 |
ashish |
264 |
def add_ip_address_for_user(ip_address, time_stamp, user_id):
|
|
|
265 |
user = get_context_by_id(user_id)
|
|
|
266 |
if not user:
|
|
|
267 |
raise_user_exception(user_id)
|
|
|
268 |
#user exists create an ipaddress object
|
|
|
269 |
ip_address = IPMap()
|
|
|
270 |
ip_address.ip = ip_address
|
|
|
271 |
ip_address.timestamp = to_py_date(time_stamp)
|
|
|
272 |
ip_address.user_info = user.state
|
|
|
273 |
session.commit()
|
|
|
274 |
return True
|
|
|
275 |
|
| 513 |
rajveer |
276 |
def add_address_for_user(address, user_id, time_stamp, setDefault):
|
| 130 |
ashish |
277 |
user = get_context_by_id(user_id)
|
|
|
278 |
if not user:
|
|
|
279 |
raise_user_exception(user_id)
|
|
|
280 |
if not address:
|
|
|
281 |
raise UserContextException(103,"Address cannot be null")
|
|
|
282 |
|
|
|
283 |
address_to_add = Address()
|
|
|
284 |
address_to_add.line_1 = address.line1
|
|
|
285 |
address_to_add.line_2 = address.line2
|
|
|
286 |
address_to_add.landmark = address.landmark
|
|
|
287 |
address_to_add.city = address.city
|
|
|
288 |
address_to_add.country = address.country
|
|
|
289 |
address_to_add.state = address.state
|
|
|
290 |
address_to_add.pin = address.pin
|
|
|
291 |
address_to_add.type = address.type
|
| 414 |
ashish |
292 |
address_to_add.name = address.name
|
|
|
293 |
address_to_add.phone = address.phone
|
| 130 |
ashish |
294 |
address_to_add.added_on = to_py_date(address.addedOn)
|
|
|
295 |
address_to_add.enabled = True
|
|
|
296 |
address_to_add.primary_info = user.primary_info
|
|
|
297 |
session.commit()
|
| 509 |
rajveer |
298 |
|
| 513 |
rajveer |
299 |
if setDefault is True:
|
|
|
300 |
set_default_address(user_id, address_to_add.id)
|
|
|
301 |
|
| 130 |
ashish |
302 |
return True
|
|
|
303 |
|
| 513 |
rajveer |
304 |
def set_default_address(userid, addressId):
|
|
|
305 |
primary_info = get_primary_info(userid, False)
|
|
|
306 |
primary_info.default_address_id = addressId
|
|
|
307 |
session.commit()
|
|
|
308 |
|
| 130 |
ashish |
309 |
def remove_address_for_user(user_id, address_id):
|
|
|
310 |
|
|
|
311 |
address = get_address(address_id)
|
|
|
312 |
|
|
|
313 |
if not address:
|
|
|
314 |
raise UserContextException(103,"Address not found")
|
|
|
315 |
if address.primary_info.user.id != user_id:
|
|
|
316 |
raise UserContextException(104,"This address belongs to some other user")
|
|
|
317 |
|
| 509 |
rajveer |
318 |
#so far so good. Now disable the address
|
| 130 |
ashish |
319 |
address.enabled = False
|
|
|
320 |
session.commit()
|
|
|
321 |
return True
|
|
|
322 |
|
|
|
323 |
def set_user_as_logged_in(user_id, time_stamp):
|
|
|
324 |
|
|
|
325 |
user = get_context_by_id(user_id)
|
|
|
326 |
|
|
|
327 |
if not user:
|
|
|
328 |
raise_user_exception(user_id)
|
|
|
329 |
|
|
|
330 |
user.state.is_logged_in = True
|
|
|
331 |
if not time_stamp:
|
|
|
332 |
user.state.last_login_timestamp = datetime.datetime.now()
|
|
|
333 |
user.state.last_login_timestamp = to_py_date(time_stamp)
|
|
|
334 |
session.commit()
|
|
|
335 |
return True
|
|
|
336 |
|
|
|
337 |
def set_user_as_logged_out(user_id,time_stamp):
|
|
|
338 |
user = get_context_by_id(user_id)
|
|
|
339 |
|
|
|
340 |
if not user:
|
|
|
341 |
raise_user_exception(user_id)
|
|
|
342 |
|
| 417 |
rajveer |
343 |
user.state.is_logged_in = False
|
| 130 |
ashish |
344 |
|
|
|
345 |
if not time_stamp:
|
|
|
346 |
user.state.last_logout = datetime.datetime.now()
|
|
|
347 |
user.state.last_logout = to_py_date(time_stamp)
|
|
|
348 |
session.commit()
|
|
|
349 |
return True
|
|
|
350 |
|
|
|
351 |
def update_password(user_id, password):
|
|
|
352 |
user = get_context_by_id(user_id)
|
|
|
353 |
|
|
|
354 |
if not user:
|
|
|
355 |
raise_user_exception(user_id)
|
|
|
356 |
|
|
|
357 |
if check_for_valid_password(password):
|
|
|
358 |
password_to_update = get_db_password(password)
|
|
|
359 |
user.primary_info.password = password_to_update
|
|
|
360 |
session.commit()
|
|
|
361 |
return True
|
|
|
362 |
else:
|
|
|
363 |
return False
|
|
|
364 |
|
|
|
365 |
def delete_user(user_id, is_session_id):
|
|
|
366 |
if is_session_id:
|
|
|
367 |
user = get_context_by_session_id(user_id)
|
|
|
368 |
else:
|
|
|
369 |
user = get_context_by_id(user_id)
|
|
|
370 |
|
|
|
371 |
if not user:
|
|
|
372 |
raise_user_exception(user_id)
|
|
|
373 |
|
|
|
374 |
user.state.account_status = AccountStatus.DELETED
|
|
|
375 |
session.commit()
|
|
|
376 |
return True
|
|
|
377 |
|
|
|
378 |
def get_social_service_by_name(service_name):
|
|
|
379 |
service = SocialService.get_by(name=service_name)
|
|
|
380 |
|
|
|
381 |
if not service:
|
|
|
382 |
raise UserContextException(106, "No such social service exists")
|
|
|
383 |
return service
|
|
|
384 |
|
|
|
385 |
def add_social_handle(user_id, social_service, handle):
|
|
|
386 |
|
|
|
387 |
|
|
|
388 |
user = get_context_by_id(user_id)
|
|
|
389 |
|
|
|
390 |
if not user:
|
|
|
391 |
raise_user_exception(user_id)
|
|
|
392 |
|
|
|
393 |
service = get_social_service_by_name(social_service)
|
|
|
394 |
|
|
|
395 |
#get if use already has this service.
|
|
|
396 |
social_handle = SocialHandle.filter(service=service).filter(primary_info=user.primary_info).one()
|
|
|
397 |
if not social_handle:
|
|
|
398 |
#create a new handle
|
|
|
399 |
social_handle = SocialHandle()
|
|
|
400 |
social_handle.service = service
|
|
|
401 |
social_handle.primary_info = user.primary_info
|
|
|
402 |
social_handle.handle = handle
|
|
|
403 |
session.commit()
|
|
|
404 |
else:
|
|
|
405 |
social_handle.handle = handle
|
|
|
406 |
session.commit()
|
|
|
407 |
return True
|
|
|
408 |
#=============================================================================
|
|
|
409 |
# Helper functions
|
|
|
410 |
#=============================================================================
|
|
|
411 |
|
|
|
412 |
'''
|
|
|
413 |
This function returns the password as stored in the db
|
|
|
414 |
'''
|
|
|
415 |
def get_db_password(password):
|
|
|
416 |
return password
|
|
|
417 |
|
|
|
418 |
def check_for_valid_password(password):
|
|
|
419 |
if not password:
|
|
|
420 |
raise UserContextException(105,"password cannot be null")
|
|
|
421 |
return True
|
|
|
422 |
#------------------------------------------------------------------------------
|
|
|
423 |
|
|
|
424 |
#===============================================================================
|
|
|
425 |
# raises the UserContextException
|
|
|
426 |
#===============================================================================
|
|
|
427 |
def raise_user_exception(user_id):
|
|
|
428 |
raise UserContextException(101, "no such user in system %d" %(user_id))
|