Subversion Repositories SmartDukaan

Rev

Rev 594 | Rev 785 | 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
14
import datetime
581 rajveer 15
from shop2020.thriftpy.utils.ttypes import Mail
130 ashish 16
 
17
def initialize():
18
    log_entry("initialize@DataAccessor", "Initializing data service")
19
    Dataservice.initialize()
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
 
581 rajveer 293
def forgot_password(email):
294
    try:
295
        user = User.get_by(email=email)
296
        if user:
297
            mail = Mail()
298
            mail.to = []
299
            mail.to.append(email)
300
            mail.sender = ""
301
            mail.password = ""
302
            mail.data = "Your new password is " + user.password
303
            helper_client = HelperClient()
304
            helper_client.__start__()
305
            client = helper_client.get_client()
306
            client.sendMail(mail);
307
            return True
308
        else:
309
            return False
310
    except:
311
        return False
312
 
313
 
594 rajveer 314
def get_all_addresses_for_user(userId):
315
    query = Address.query.filter(Address.user.has(id = userId))
316
    query = query.filter(Address.enabled == True)
317
    return query.all()
581 rajveer 318
 
594 rajveer 319
def get_default_addredd_id(userId):
320
    user = get_user_by_id(userId);
321
    return user.default_address_id
581 rajveer 322
 
130 ashish 323
#=============================================================================
324
# Helper functions 
325
#=============================================================================
326
 
327
'''
328
This function returns the password as stored in the db
329
'''    
330
def get_db_password(password):
331
    return password
332
 
333
def check_for_valid_password(password):
334
    if not password:
335
        raise UserContextException(105,"password cannot be null")
336
    return True
337
#------------------------------------------------------------------------------ 
338
 
339
#===============================================================================
340
# raises the UserContextException
341
#===============================================================================
342
def raise_user_exception(user_id):
766 rajveer 343
    raise UserContextException(101, "no such user in system %d" %(user_id))
344
 
345
def close_session():
346
    if session.is_active:
347
        print "session is active. closing it."
348
        session.close()