Subversion Repositories SmartDukaan

Rev

Rev 1891 | Rev 1996 | 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():
1863 vikas 273
    return UserCommunication.query.order_by(-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
 
1899 vikas 282
def get_all_master_affiliates():
283
    return MasterAffiliate.query.all()
284
 
1845 vikas 285
def get_master_affiliate_by_id(id):
286
    return MasterAffiliate.get_by(id = id)
287
 
288
def get_master_affiliate_by_name(name):
289
    return MasterAffiliate.get_by(name = name)
290
 
1859 vikas 291
def create_affiliate(name, url, master_affiliate_id, added_on):
1845 vikas 292
    affiliate = Affiliate()
293
    affiliate.name = name
294
    if url is not None:
295
        affiliate.url = url
296
    affiliate.master_affiliate_id = master_affiliate_id
1859 vikas 297
    affiliate.added_on = to_py_date(added_on)
1845 vikas 298
    session.commit()
299
    return affiliate
300
 
301
def get_affiliate_by_id(id):
302
    return Affiliate.get_by(id = id)
303
 
304
def get_affiliate_by_name(name):
305
    return Affiliate.get_by(name = name)
306
 
307
def get_affiliates_by_master_affiliate(master_affiliate_id):
308
    return MasterAffiliate.get_by(id =  master_affiliate_id).affiliates
309
 
1859 vikas 310
def create_tracker(affiliate_id, added_on):
1845 vikas 311
    tracker = Tracker()
312
    tracker.affiliate_id = affiliate_id
1859 vikas 313
    tracker.added_on = to_py_date(added_on)
1845 vikas 314
    session.commit()
315
    return tracker
316
 
317
def get_tracker_by_id(id):
318
    return Tracker.get_by(id = id)
319
 
320
def get_trackers_by_affiliate(affiliate_id):
321
    return Affiliate.get_by(id = affiliate_id).trackers
322
 
1859 vikas 323
def add_track_log(tracker_id, user_id, event, url, data, added_on):
1845 vikas 324
    track_log = TrackLog()
325
    track_log.tracker_id = tracker_id
326
    if user_id:
327
        track_log.user_id = user_id
328
    track_log.event = event
329
    if url:
330
        track_log.url = url
331
    if data:
1859 vikas 332
        track_log.data = data
333
    track_log.added_on = to_py_date(added_on)
1845 vikas 334
    session.commit()
335
    return track_log.id
336
 
337
def get_track_log_by_id(id):
338
    return TrackLog.get_by(id = id)
339
 
340
def get_track_logs_by_tracker(tracker_id):
341
    return Tracker.get_by(id = tracker_id).tracklogs
342
 
343
def get_track_logs_by_user(user_id):
344
    return TrackLog.query.filter(TrackLog.user_id == user_id).all()
345
 
346
def get_track_logs(trackerId, userId, event, url):
347
    query = TrackLog.query
348
 
349
    if trackerId:
350
        query = query.filter(TrackLog.tracker_id == trackerId)
351
    if userId:
352
        query = query.filter(TrackLog.user_id == userId)
353
    if event:
354
        query = query.filter(TrackLog.event == event)
355
    if url:
356
        query = query.filter(TrackLog.url == url)
357
    return query.all()
358
 
359
 
557 chandransh 360
def get_address(address_id):
361
    address = Address.get_by(id=address_id)
362
    return address
363
 
364
def get_social_service(service_id):
365
    service = SocialService.get_by(service_id)
366
    return service
367
 
368
def add_ip_address_for_user(ip_address, time_stamp, user_id):
369
    user = get_user_by_id(user_id)
130 ashish 370
    if not user:
371
        raise_user_exception(user_id)
557 chandransh 372
    #user exists create an IP address object
373
    ip_address = IPMap()
374
    ip_address.ip = ip_address
375
    ip_address.timestamp = to_py_date(time_stamp)
376
    ip_address.user_state = user.state
130 ashish 377
    session.commit()
378
    return True
379
 
380
def get_social_service_by_name(service_name):
381
    service = SocialService.get_by(name=service_name)
382
 
383
    if not service:
384
        raise UserContextException(106, "No such social service exists")
385
    return service
386
 
387
def add_social_handle(user_id, social_service, handle):
557 chandransh 388
    user = get_user_by_id(user_id)
130 ashish 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.
557 chandransh 396
    social_handle = SocialHandle.filter(service=service).filter(user=user).one()
130 ashish 397
    if not social_handle:
398
        #create a new handle
399
        social_handle = SocialHandle()
400
        social_handle.service = service
557 chandransh 401
        social_handle.user = user
130 ashish 402
        social_handle.handle = handle
403
    else:
404
        social_handle.handle = handle
557 chandransh 405
    session.commit()
130 ashish 406
    return True
557 chandransh 407
 
884 rajveer 408
 
409
def forgot_password(email, password):
410
    try:
411
        user = User.get_by(email=email)
412
        if user:
413
            user.password = password
900 rajveer 414
            session.commit()
884 rajveer 415
            return True
416
        else:
417
            return False
418
    except:
419
        return False
420
 
421
 
422
'''
581 rajveer 423
def forgot_password(email):
424
    try:
425
        user = User.get_by(email=email)
426
        if user:
873 rajveer 427
            new_password = ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(8))
581 rajveer 428
            mail = Mail()
429
            mail.to = []
430
            mail.to.append(email)
431
            mail.sender = ""
432
            mail.password = ""
433
            mail.data = "Your new password is " + user.password
434
            helper_client = HelperClient()
435
            helper_client.__start__()
436
            client = helper_client.get_client()
437
            client.sendMail(mail);
873 rajveer 438
 
581 rajveer 439
            return True
440
        else:
441
            return False
442
    except:
443
        return False
884 rajveer 444
'''    
581 rajveer 445
 
594 rajveer 446
def get_all_addresses_for_user(userId):
447
    query = Address.query.filter(Address.user.has(id = userId))
448
    query = query.filter(Address.enabled == True)
449
    return query.all()
581 rajveer 450
 
785 rajveer 451
def get_default_address_id(userId):
594 rajveer 452
    user = get_user_by_id(userId);
840 chandransh 453
    if user.default_address_id is None:
454
        return 0
594 rajveer 455
    return user.default_address_id
581 rajveer 456
 
785 rajveer 457
def get_default_pincode(user_id):
458
    user = get_user_by_id(user_id)
459
    if not user:
460
        raise_user_exception(user_id)
461
    default_address_id = user.default_address_id
462
    if default_address_id:
463
        address = Address.get_by(id=default_address_id)
464
        default_pincode = address.pin 
465
    else:
466
        default_pincode = '110001' 
467
    return default_pincode     
1596 ankur.sing 468
 
469
def get_user_count(user_type):
470
    if user_type is None:
471
        return User.query.count()
472
    else:
473
        return User.query.filter_by(is_anonymous = user_type).count()
785 rajveer 474
 
1891 ankur.sing 475
def get_users(user_type, start_date, end_date):
476
    query = session.query(User).join(UserState)
477
    if start_date != -1:
478
        query = query.filter(UserState.active_since >= to_py_date(start_date))
479
    if end_date != -1:
480
        query = query.filter(UserState.active_since <= to_py_date(end_date))
481
    if user_type is not None:
482
        query = query.filter(User.is_anonymous == user_type)
483
    return query.all()
1673 ankur.sing 484
 
130 ashish 485
#=============================================================================
486
# Helper functions 
487
#=============================================================================
488
 
489
'''
490
This function returns the password as stored in the db
491
'''    
492
def get_db_password(password):
493
    return password
494
 
495
def check_for_valid_password(password):
496
    if not password:
497
        raise UserContextException(105,"password cannot be null")
498
    return True
499
#------------------------------------------------------------------------------ 
500
 
501
#===============================================================================
502
# raises the UserContextException
503
#===============================================================================
504
def raise_user_exception(user_id):
766 rajveer 505
    raise UserContextException(101, "no such user in system %d" %(user_id))
506
 
507
def close_session():
508
    if session.is_active:
509
        print "session is active. closing it."
510
        session.close()