Subversion Repositories SmartDukaan

Rev

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