Subversion Repositories SmartDukaan

Rev

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