Subversion Repositories SmartDukaan

Rev

Rev 1845 | Rev 1863 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
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
1273 varun.gupt 8
from shop2020.model.v1.user.impl.Dataservice import User, UserCommunication, IPMap,\
1845 vikas 9
    Address, SocialHandle, UserState, InternalInfo, SocialService, Affiliate, Tracker, TrackLog,\
10
    MasterAffiliate
130 ashish 11
from shop2020.thriftpy.model.v1.user.ttypes import UserContextException,\
557 chandransh 12
    AuthenticationException, AccountStatus, Sex
581 rajveer 13
from shop2020.clients.HelperClient import HelperClient
130 ashish 14
from elixir import session
873 rajveer 15
import datetime, random, string
581 rajveer 16
from shop2020.thriftpy.utils.ttypes import Mail
130 ashish 17
 
1249 chandransh 18
def initialize(dbname='user'):
130 ashish 19
    log_entry("initialize@DataAccessor", "Initializing data service")
1249 chandransh 20
    Dataservice.initialize(dbname)
557 chandransh 21
 
22
def create_anonymous_user(jsession_id, cart):
576 chandransh 23
    user=User.get_by(jsession_id=jsession_id)
24
    if not user is None:
25
        return user
557 chandransh 26
    user = User()
27
    anonymous_str = "anonymous"
576 chandransh 28
    user.email = jsession_id + "@anonymous.com"
557 chandransh 29
    user.password = anonymous_str
30
    user.name = anonymous_str
31
    user.communication_email = jsession_id + "@anonymous.com"
32
    user.jsession_id = jsession_id
33
    user.is_anonymous = True
34
    user.sex = Sex.WONT_SAY
35
    user.active_cart_id = cart.id
36
    #initialize userState
37
    user_state = UserState()
38
    user_state.is_email_verified = False
39
    user_state.is_sms_verified = False
40
    user_state.account_status = AccountStatus.ACTIVE
41
    user_state.ip_list = []
42
    user_state.active_since = datetime.datetime.now()    
43
    user_state.user = user
1607 vikas 44
    session.commit();
557 chandransh 45
 
46
    cart.user_id = user.id    
47
    session.commit()
130 ashish 48
 
557 chandransh 49
    return user
50
 
51
def get_user_by_id(user_id):
52
    return User.get_by(id=user_id)
53
 
1491 vikas 54
def get_user_by_email(email):
55
    return User.get_by(email=email)
56
 
557 chandransh 57
def create_user(user_to_add, cart):
58
    if user_to_add.userId:
130 ashish 59
        raise UserContextException(109, "id is set, user cannot be created")
557 chandransh 60
    user = User()
61
    user.email = user_to_add.email
62
    user.password = user_to_add.password
63
    user.name = user_to_add.name
64
    user.communication_email = user_to_add.communicationEmail
65
    user.jsession_id = user_to_add.jsessionId
66
    user.is_anonymous = False
67
    user.sex = user_to_add.sex
567 rajveer 68
    user.date_of_birth = user_to_add.dateOfBirth
557 chandransh 69
    user.active_cart_id = cart.id
567 rajveer 70
    user.mobile_number = user_to_add.mobileNumber
513 rajveer 71
 
130 ashish 72
    #initialize userState
557 chandransh 73
    user_state = UserState()
130 ashish 74
    user_state.is_email_verified = False
75
    user_state.is_sms_verified = False
76
    user_state.account_status = AccountStatus.ACTIVE
132 ashish 77
    user_state.ip_list = []
130 ashish 78
    user_state.active_since = datetime.datetime.now()
557 chandransh 79
    user_state.user = user
1607 vikas 80
    session.commit();
130 ashish 81
 
557 chandransh 82
    #Now create the user phones. Mostly, this should only be a mobile number
83
 
84
    cart.user_id = user.id    
130 ashish 85
    session.commit()
404 rajveer 86
 
557 chandransh 87
    return user
130 ashish 88
 
89
#===============================================================================
90
# Need to provide the update apis here for relevant fields in PrimaryInfo.
91
#===============================================================================
557 chandransh 92
def update_user(user_to_update):
594 rajveer 93
    if not user_to_update.userId:
415 ashish 94
        raise UserContextException(110, "user does not exist")
594 rajveer 95
    user = get_user_by_id(user_to_update.userId)
96
    user.email = user_to_update.email
97
    user.password = user_to_update.password
98
    user.name = user_to_update.name
99
    user.communication_email = user_to_update.communicationEmail
100
    user.jsession_id = user_to_update.jsessionId
101
    user.is_anonymous = user_to_update.isAnonymous
102
    user.sex = user_to_update.sex
103
    user.date_of_birth = user_to_update.dateOfBirth
104
    user.active_cart_id = user_to_update.activeCartId
105
    user.mobile_number = user_to_update.mobileNumber
415 ashish 106
    session.commit()
594 rajveer 107
    return user
415 ashish 108
 
557 chandransh 109
def delete_user(user_id):
110
    user = get_user_by_id(user_id)
130 ashish 111
 
112
    if not user:
113
        raise_user_exception(user_id)
557 chandransh 114
 
115
    user.state.account_status = AccountStatus.DELETED
116
    session.commit()
117
    return True
130 ashish 118
 
557 chandransh 119
def get_user_state(user_id):
766 rajveer 120
    userstate = UserState.get_by(user_id=user_id)
121
    return userstate
130 ashish 122
 
557 chandransh 123
def get_internal_info(user_id):
766 rajveer 124
    info = InternalInfo.get_by(user_id=user_id)
125
    return info
130 ashish 126
 
557 chandransh 127
def authenticate_user(user_handle, password):
128
    user = User.get_by(email=user_handle)
130 ashish 129
    if not user:
557 chandransh 130
        raise AuthenticationException("This email address is not registered.", 102)
130 ashish 131
 
557 chandransh 132
    if user.password == get_db_password(password):
133
        return user
130 ashish 134
    else:
135
        raise AuthenticationException("Wrong username or password", 102)
136
 
137
def user_exists(email):
413 rajveer 138
    try:
557 chandransh 139
        user = User.get_by(email=email)
140
        if user:
141
            return True
142
        else:
143
            return False
413 rajveer 144
    except:
130 ashish 145
        return False
146
 
567 rajveer 147
def add_address_for_user(address, user_id, set_default):
557 chandransh 148
    user = get_user_by_id(user_id)
766 rajveer 149
 
130 ashish 150
    if not user:
151
        raise_user_exception(user_id)
152
    if not address:
153
        raise UserContextException(103,"Address cannot be null")
154
 
155
    address_to_add = Address()
156
    address_to_add.line_1 = address.line1
157
    address_to_add.line_2 = address.line2
158
    address_to_add.landmark = address.landmark
159
    address_to_add.city = address.city
160
    address_to_add.country = address.country
161
    address_to_add.state = address.state
162
    address_to_add.pin = address.pin
163
    address_to_add.type = address.type
414 ashish 164
    address_to_add.name = address.name
165
    address_to_add.phone = address.phone
130 ashish 166
    address_to_add.added_on = to_py_date(address.addedOn)
167
    address_to_add.enabled = True
557 chandransh 168
    address_to_add.user = user
130 ashish 169
    session.commit()
509 rajveer 170
 
557 chandransh 171
    if set_default is True:
172
        user.default_address_id = address_to_add.id
567 rajveer 173
    #set default address if nothing is default    
174
    if user.default_address_id is None:
175
        user.default_address_id = address_to_add.id
176
 
557 chandransh 177
    session.commit()
513 rajveer 178
 
567 rajveer 179
    return address_to_add.id
513 rajveer 180
 
130 ashish 181
def remove_address_for_user(user_id, address_id):
182
    address = get_address(address_id) 
183
 
184
    if not address:
557 chandransh 185
        raise UserContextException(103, "Address not found")
186
    if address.user.id != user_id:
187
        raise UserContextException(104, "This address belongs to some other user")
130 ashish 188
 
189
    address.enabled = False
190
    session.commit()
191
    return True
192
 
193
def set_user_as_logged_in(user_id, time_stamp):
557 chandransh 194
    user_state = UserState.get_by(user_id=user_id)
195
    #user = get_user_by_id(user_id)
130 ashish 196
 
557 chandransh 197
    if not user_state:
198
        raise_user_exception(user_id)
130 ashish 199
 
200
    if not time_stamp:
557 chandransh 201
        user_state.last_login = datetime.datetime.now()
202
    else:
203
        user_state.last_login = to_py_date(time_stamp)
130 ashish 204
    session.commit()
205
    return True
206
 
557 chandransh 207
def set_user_as_logged_out(user_id, time_stamp):
208
    user_state = UserState.get_by(user_id=user_id)
130 ashish 209
 
557 chandransh 210
    if not user_state:
130 ashish 211
        raise_user_exception(user_id)
212
 
213
    if not time_stamp:
557 chandransh 214
        user_state.last_logout = datetime.datetime.now()
215
    else:
216
        user_state.last_logout = to_py_date(time_stamp)
130 ashish 217
    session.commit()
218
    return True
219
 
557 chandransh 220
def set_default_address(user_id, address_id):
221
    user = get_user_by_id(user_id)
222
    address = Address.get_by(id=address_id)
223
    if not user:
224
        raise_user_exception(user_id)
225
    if not address:
226
        raise UserContextException(103, "Address not found")
227
    if address.user.id != user.id:
228
        raise UserContextException(104, "This address belongs to some other user")
229
 
230
    user.default_address_id = address_id 
231
    session.commit()
232
 
594 rajveer 233
def update_password(user_id, old_password, new_password):
557 chandransh 234
    user = get_user_by_id(user_id)
130 ashish 235
 
236
    if not user:
237
        raise_user_exception(user_id)
238
 
594 rajveer 239
    if user.password != old_password:
240
        return False
241
 
242
    if check_for_valid_password(new_password):
243
        user.password = get_db_password(new_password)
130 ashish 244
        session.commit()
245
        return True
246
    else:
247
        return False
1273 varun.gupt 248
 
249
 
250
def create_user_communication(user_id, email, communication_type, order_id, awb, product, subject, message):
251
 
252
    user_communication = UserCommunication()
253
    user_communication.user_id = user_id
254
    user_communication.communication_type = communication_type
1743 varun.gupt 255
 
256
    if order_id > 0:
257
        user_communication.order_id = order_id
1273 varun.gupt 258
    user_communication.airwaybill_no = awb
259
    user_communication.reply_to = email
260
    user_communication.product_name = product
261
    user_communication.subject = subject
262
    user_communication.message = message
1301 varun.gupt 263
    user_communication.communication_timestamp = datetime.datetime.now()
1273 varun.gupt 264
    session.commit()
130 ashish 265
 
1583 varun.gupt 266
def get_user_communication_by_id(user_communication_id):
267
    return UserCommunication.get_by(id = user_communication_id)
268
 
269
def get_user_communication_by_user(user_communication_user_id):
1607 vikas 270
    return UserCommunication.query.filter_by(user_id = user_communication_user_id).order_by(-UserCommunication.id).all()
1583 varun.gupt 271
 
272
def get_all_user_communications():
1607 vikas 273
    return UserCommunication.query.order_by(UserCommunication.user_id, -UserCommunication.id).all()
1583 varun.gupt 274
 
1859 vikas 275
def create_master_affiliate(name, added_on):
1845 vikas 276
    master_affiliate = MasterAffiliate()
277
    master_affiliate.name = name
1859 vikas 278
    master_affiliate.added_on = to_py_date(added_on)
1845 vikas 279
    session.commit()
280
    return master_affiliate
281
 
282
def get_master_affiliate_by_id(id):
283
    return MasterAffiliate.get_by(id = id)
284
 
285
def get_master_affiliate_by_name(name):
286
    return MasterAffiliate.get_by(name = name)
287
 
1859 vikas 288
def create_affiliate(name, url, master_affiliate_id, added_on):
1845 vikas 289
    affiliate = Affiliate()
290
    affiliate.name = name
291
    if url is not None:
292
        affiliate.url = url
293
    affiliate.master_affiliate_id = master_affiliate_id
1859 vikas 294
    affiliate.added_on = to_py_date(added_on)
1845 vikas 295
    session.commit()
296
    return affiliate
297
 
298
def get_affiliate_by_id(id):
299
    return Affiliate.get_by(id = id)
300
 
301
def get_affiliate_by_name(name):
302
    return Affiliate.get_by(name = name)
303
 
304
def get_affiliates_by_master_affiliate(master_affiliate_id):
305
    return MasterAffiliate.get_by(id =  master_affiliate_id).affiliates
306
 
1859 vikas 307
def create_tracker(affiliate_id, added_on):
1845 vikas 308
    tracker = Tracker()
309
    tracker.affiliate_id = affiliate_id
1859 vikas 310
    tracker.added_on = to_py_date(added_on)
1845 vikas 311
    session.commit()
312
    return tracker
313
 
314
def get_tracker_by_id(id):
315
    return Tracker.get_by(id = id)
316
 
317
def get_trackers_by_affiliate(affiliate_id):
318
    return Affiliate.get_by(id = affiliate_id).trackers
319
 
1859 vikas 320
def add_track_log(tracker_id, user_id, event, url, data, added_on):
1845 vikas 321
    track_log = TrackLog()
322
    track_log.tracker_id = tracker_id
323
    if user_id:
324
        track_log.user_id = user_id
325
    track_log.event = event
326
    if url:
327
        track_log.url = url
328
    if data:
1859 vikas 329
        track_log.data = data
330
    track_log.added_on = to_py_date(added_on)
1845 vikas 331
    session.commit()
332
    return track_log.id
333
 
334
def get_track_log_by_id(id):
335
    return TrackLog.get_by(id = id)
336
 
337
def get_track_logs_by_tracker(tracker_id):
338
    return Tracker.get_by(id = tracker_id).tracklogs
339
 
340
def get_track_logs_by_user(user_id):
341
    return TrackLog.query.filter(TrackLog.user_id == user_id).all()
342
 
343
def get_track_logs(trackerId, userId, event, url):
344
    query = TrackLog.query
345
 
346
    if trackerId:
347
        query = query.filter(TrackLog.tracker_id == trackerId)
348
    if userId:
349
        query = query.filter(TrackLog.user_id == userId)
350
    if event:
351
        query = query.filter(TrackLog.event == event)
352
    if url:
353
        query = query.filter(TrackLog.url == url)
354
    return query.all()
355
 
356
 
557 chandransh 357
def get_address(address_id):
358
    address = Address.get_by(id=address_id)
359
    return address
360
 
361
def get_social_service(service_id):
362
    service = SocialService.get_by(service_id)
363
    return service
364
 
365
def add_ip_address_for_user(ip_address, time_stamp, user_id):
366
    user = get_user_by_id(user_id)
130 ashish 367
    if not user:
368
        raise_user_exception(user_id)
557 chandransh 369
    #user exists create an IP address object
370
    ip_address = IPMap()
371
    ip_address.ip = ip_address
372
    ip_address.timestamp = to_py_date(time_stamp)
373
    ip_address.user_state = user.state
130 ashish 374
    session.commit()
375
    return True
376
 
377
def get_social_service_by_name(service_name):
378
    service = SocialService.get_by(name=service_name)
379
 
380
    if not service:
381
        raise UserContextException(106, "No such social service exists")
382
    return service
383
 
384
def add_social_handle(user_id, social_service, handle):
557 chandransh 385
    user = get_user_by_id(user_id)
130 ashish 386
 
387
    if not user:
388
        raise_user_exception(user_id)
389
 
390
    service = get_social_service_by_name(social_service)
391
 
392
    #get if use already has this service.
557 chandransh 393
    social_handle = SocialHandle.filter(service=service).filter(user=user).one()
130 ashish 394
    if not social_handle:
395
        #create a new handle
396
        social_handle = SocialHandle()
397
        social_handle.service = service
557 chandransh 398
        social_handle.user = user
130 ashish 399
        social_handle.handle = handle
400
    else:
401
        social_handle.handle = handle
557 chandransh 402
    session.commit()
130 ashish 403
    return True
557 chandransh 404
 
884 rajveer 405
 
406
def forgot_password(email, password):
407
    try:
408
        user = User.get_by(email=email)
409
        if user:
410
            user.password = password
900 rajveer 411
            session.commit()
884 rajveer 412
            return True
413
        else:
414
            return False
415
    except:
416
        return False
417
 
418
 
419
'''
581 rajveer 420
def forgot_password(email):
421
    try:
422
        user = User.get_by(email=email)
423
        if user:
873 rajveer 424
            new_password = ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(8))
581 rajveer 425
            mail = Mail()
426
            mail.to = []
427
            mail.to.append(email)
428
            mail.sender = ""
429
            mail.password = ""
430
            mail.data = "Your new password is " + user.password
431
            helper_client = HelperClient()
432
            helper_client.__start__()
433
            client = helper_client.get_client()
434
            client.sendMail(mail);
873 rajveer 435
 
581 rajveer 436
            return True
437
        else:
438
            return False
439
    except:
440
        return False
884 rajveer 441
'''    
581 rajveer 442
 
594 rajveer 443
def get_all_addresses_for_user(userId):
444
    query = Address.query.filter(Address.user.has(id = userId))
445
    query = query.filter(Address.enabled == True)
446
    return query.all()
581 rajveer 447
 
785 rajveer 448
def get_default_address_id(userId):
594 rajveer 449
    user = get_user_by_id(userId);
840 chandransh 450
    if user.default_address_id is None:
451
        return 0
594 rajveer 452
    return user.default_address_id
581 rajveer 453
 
785 rajveer 454
def get_default_pincode(user_id):
455
    user = get_user_by_id(user_id)
456
    if not user:
457
        raise_user_exception(user_id)
458
    default_address_id = user.default_address_id
459
    if default_address_id:
460
        address = Address.get_by(id=default_address_id)
461
        default_pincode = address.pin 
462
    else:
463
        default_pincode = '110001' 
464
    return default_pincode     
1596 ankur.sing 465
 
466
def get_user_count(user_type):
467
    if user_type is None:
468
        return User.query.count()
469
    else:
470
        return User.query.filter_by(is_anonymous = user_type).count()
785 rajveer 471
 
1673 ankur.sing 472
def get_users(user_type):
473
    if user_type is None:
474
        return User.query.all()
475
    else:
476
        return User.query.filter_by(is_anonymous = user_type).all()    
477
 
130 ashish 478
#=============================================================================
479
# Helper functions 
480
#=============================================================================
481
 
482
'''
483
This function returns the password as stored in the db
484
'''    
485
def get_db_password(password):
486
    return password
487
 
488
def check_for_valid_password(password):
489
    if not password:
490
        raise UserContextException(105,"password cannot be null")
491
    return True
492
#------------------------------------------------------------------------------ 
493
 
494
#===============================================================================
495
# raises the UserContextException
496
#===============================================================================
497
def raise_user_exception(user_id):
766 rajveer 498
    raise UserContextException(101, "no such user in system %d" %(user_id))
499
 
500
def close_session():
501
    if session.is_active:
502
        print "session is active. closing it."
503
        session.close()