Subversion Repositories SmartDukaan

Rev

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