Subversion Repositories SmartDukaan

Rev

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