Subversion Repositories SmartDukaan

Rev

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