Subversion Repositories SmartDukaan

Rev

Rev 1596 | Rev 1673 | 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
254
    user_communication.order_id = order_id
255
    user_communication.airwaybill_no = awb
256
    user_communication.reply_to = email
257
    user_communication.product_name = product
258
    user_communication.subject = subject
259
    user_communication.message = message
1301 varun.gupt 260
    user_communication.communication_timestamp = datetime.datetime.now()
1273 varun.gupt 261
    session.commit()
130 ashish 262
 
1583 varun.gupt 263
def get_user_communication_by_id(user_communication_id):
264
    return UserCommunication.get_by(id = user_communication_id)
265
 
266
def get_user_communication_by_user(user_communication_user_id):
1607 vikas 267
    return UserCommunication.query.filter_by(user_id = user_communication_user_id).order_by(-UserCommunication.id).all()
1583 varun.gupt 268
 
269
def get_all_user_communications():
1607 vikas 270
    return UserCommunication.query.order_by(UserCommunication.user_id, -UserCommunication.id).all()
1583 varun.gupt 271
 
557 chandransh 272
def get_address(address_id):
273
    address = Address.get_by(id=address_id)
274
    return address
275
 
276
def get_social_service(service_id):
277
    service = SocialService.get_by(service_id)
278
    return service
279
 
280
def add_ip_address_for_user(ip_address, time_stamp, user_id):
281
    user = get_user_by_id(user_id)
130 ashish 282
    if not user:
283
        raise_user_exception(user_id)
557 chandransh 284
    #user exists create an IP address object
285
    ip_address = IPMap()
286
    ip_address.ip = ip_address
287
    ip_address.timestamp = to_py_date(time_stamp)
288
    ip_address.user_state = user.state
130 ashish 289
    session.commit()
290
    return True
291
 
292
def get_social_service_by_name(service_name):
293
    service = SocialService.get_by(name=service_name)
294
 
295
    if not service:
296
        raise UserContextException(106, "No such social service exists")
297
    return service
298
 
299
def add_social_handle(user_id, social_service, handle):
557 chandransh 300
    user = get_user_by_id(user_id)
130 ashish 301
 
302
    if not user:
303
        raise_user_exception(user_id)
304
 
305
    service = get_social_service_by_name(social_service)
306
 
307
    #get if use already has this service.
557 chandransh 308
    social_handle = SocialHandle.filter(service=service).filter(user=user).one()
130 ashish 309
    if not social_handle:
310
        #create a new handle
311
        social_handle = SocialHandle()
312
        social_handle.service = service
557 chandransh 313
        social_handle.user = user
130 ashish 314
        social_handle.handle = handle
315
    else:
316
        social_handle.handle = handle
557 chandransh 317
    session.commit()
130 ashish 318
    return True
557 chandransh 319
 
884 rajveer 320
 
321
def forgot_password(email, password):
322
    try:
323
        user = User.get_by(email=email)
324
        if user:
325
            user.password = password
900 rajveer 326
            session.commit()
884 rajveer 327
            return True
328
        else:
329
            return False
330
    except:
331
        return False
332
 
333
 
334
'''
581 rajveer 335
def forgot_password(email):
336
    try:
337
        user = User.get_by(email=email)
338
        if user:
873 rajveer 339
            new_password = ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(8))
581 rajveer 340
            mail = Mail()
341
            mail.to = []
342
            mail.to.append(email)
343
            mail.sender = ""
344
            mail.password = ""
345
            mail.data = "Your new password is " + user.password
346
            helper_client = HelperClient()
347
            helper_client.__start__()
348
            client = helper_client.get_client()
349
            client.sendMail(mail);
873 rajveer 350
 
581 rajveer 351
            return True
352
        else:
353
            return False
354
    except:
355
        return False
884 rajveer 356
'''    
581 rajveer 357
 
594 rajveer 358
def get_all_addresses_for_user(userId):
359
    query = Address.query.filter(Address.user.has(id = userId))
360
    query = query.filter(Address.enabled == True)
361
    return query.all()
581 rajveer 362
 
785 rajveer 363
def get_default_address_id(userId):
594 rajveer 364
    user = get_user_by_id(userId);
840 chandransh 365
    if user.default_address_id is None:
366
        return 0
594 rajveer 367
    return user.default_address_id
581 rajveer 368
 
785 rajveer 369
def get_default_pincode(user_id):
370
    user = get_user_by_id(user_id)
371
    if not user:
372
        raise_user_exception(user_id)
373
    default_address_id = user.default_address_id
374
    if default_address_id:
375
        address = Address.get_by(id=default_address_id)
376
        default_pincode = address.pin 
377
    else:
378
        default_pincode = '110001' 
379
    return default_pincode     
1596 ankur.sing 380
 
381
def get_user_count(user_type):
382
    if user_type is None:
383
        return User.query.count()
384
    else:
385
        return User.query.filter_by(is_anonymous = user_type).count()
785 rajveer 386
 
130 ashish 387
#=============================================================================
388
# Helper functions 
389
#=============================================================================
390
 
391
'''
392
This function returns the password as stored in the db
393
'''    
394
def get_db_password(password):
395
    return password
396
 
397
def check_for_valid_password(password):
398
    if not password:
399
        raise UserContextException(105,"password cannot be null")
400
    return True
401
#------------------------------------------------------------------------------ 
402
 
403
#===============================================================================
404
# raises the UserContextException
405
#===============================================================================
406
def raise_user_exception(user_id):
766 rajveer 407
    raise UserContextException(101, "no such user in system %d" %(user_id))
408
 
409
def close_session():
410
    if session.is_active:
411
        print "session is active. closing it."
412
        session.close()