Subversion Repositories SmartDukaan

Rev

Rev 513 | Rev 567 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 513 Rev 557
Line 3... Line 3...
3
 
3
 
4
@author: ashish
4
@author: ashish
5
'''
5
'''
6
from shop2020.model.v1.user.impl import Dataservice
6
from shop2020.model.v1.user.impl import Dataservice
7
from shop2020.utils.Utils import log_entry, to_py_date, to_java_date
7
from shop2020.utils.Utils import log_entry, to_py_date, to_java_date
8
from shop2020.model.v1.user.impl.Dataservice import User, PrimaryInfo, IPMap,\
8
from shop2020.model.v1.user.impl.Dataservice import User, IPMap,\
9
    Address, SocialService, SocialHandle, State, InternalInfo
9
    Address, SocialHandle, UserState, InternalInfo, SocialService
10
from shop2020.thriftpy.model.v1.user.ttypes import UserContextException,\
10
from shop2020.thriftpy.model.v1.user.ttypes import UserContextException,\
11
    AuthenticationException, AccountStatus
11
    AuthenticationException, AccountStatus, Sex
12
from elixir import session
12
from elixir import session
13
import datetime
13
import datetime
14
from sqlalchemy.orm.session import Session
-
 
15
 
14
 
16
def initialize():
15
def initialize():
17
    log_entry("initialize@DataAccessor", "Initializing data service")
16
    log_entry("initialize@DataAccessor", "Initializing data service")
18
    Dataservice.initialize()
17
    Dataservice.initialize()
-
 
18
 
-
 
19
def create_anonymous_user(jsession_id, cart):
-
 
20
    user = User()
-
 
21
    anonymous_str = "anonymous"
-
 
22
    user.email = jsession_id + "anonymous.com"
-
 
23
    user.password = anonymous_str
-
 
24
    user.name = anonymous_str
-
 
25
    user.communication_email = jsession_id + "@anonymous.com"
-
 
26
    user.jsession_id = jsession_id
-
 
27
    user.is_anonymous = True
-
 
28
    user.sex = Sex.WONT_SAY
-
 
29
    user.active_cart_id = cart.id
-
 
30
    #initialize userState
-
 
31
    user_state = UserState()
-
 
32
    user_state.is_email_verified = False
-
 
33
    user_state.is_sms_verified = False
-
 
34
    user_state.account_status = AccountStatus.ACTIVE
-
 
35
    user_state.ip_list = []
-
 
36
    user_state.active_since = datetime.datetime.now()    
-
 
37
    user_state.user = user
-
 
38
    session.commit()
-
 
39
 
-
 
40
    cart.user_id = user.id    
-
 
41
    session.commit()
19
    
42
    
-
 
43
    return user
-
 
44
 
-
 
45
def get_user_by_id(user_id):
-
 
46
    return User.get_by(id=user_id)
-
 
47
 
20
def create_context(user_context):
48
def create_user(user_to_add, cart):
21
    if user_context.id:
49
    if user_to_add.userId:
22
        raise UserContextException(109, "id is set, user cannot be created")
50
        raise UserContextException(109, "id is set, user cannot be created")
23
    user_context_to_add = User()
51
    user = User()
24
    primary_info = PrimaryInfo()
52
    user.email = user_to_add.email
25
    primary_info.user = user_context_to_add
53
    user.password = user_to_add.password
26
    
-
 
27
    if user_context.primaryInfo:
54
    user.name = user_to_add.name
28
        
-
 
29
        if  user_context.primaryInfo.firstName:
-
 
30
            primary_info.first_name = user_context.primaryInfo.firstName
-
 
31
        
-
 
32
        if  user_context.primaryInfo.middleName:
-
 
33
            primary_info.middle_name = user_context.primaryInfo.middleName
-
 
34
        
-
 
35
        if  user_context.primaryInfo.lastName:
-
 
36
            primary_info.last_name = user_context.primaryInfo.lastName
-
 
37
            
-
 
38
        if  user_context.primaryInfo.title:
-
 
39
            primary_info.title = user_context.primaryInfo.title
-
 
40
            
-
 
41
        if  user_context.primaryInfo.pictureUrl:
-
 
42
            primary_info.picture_id = user_context.primaryInfo.pictureUrl
-
 
43
        
-
 
44
        if  user_context.primaryInfo.email:
-
 
45
            primary_info.email = user_context.primaryInfo.email
55
    user.communication_email = user_to_add.communicationEmail
46
            
-
 
47
        if  user_context.primaryInfo.userHandle:
-
 
48
            primary_info.user_handle = user_context.primaryInfo.userHandle
-
 
49
            
-
 
50
        if  user_context.primaryInfo.password:
56
    user.jsession_id = user_to_add.jsessionId
51
            primary_info.password = user_context.primaryInfo.password
-
 
52
            
-
 
53
        if  user_context.primaryInfo.occupation:
57
    user.is_anonymous = False
54
            primary_info.occupation = user_context.primaryInfo.occupation
-
 
55
            
-
 
56
        if  user_context.primaryInfo.hintQuestion:
-
 
57
            primary_info.hint_question = user_context.primaryInfo.hintQuestion
-
 
58
        
-
 
59
        if  user_context.primaryInfo.hintAnswer:
58
    user.sex = user_to_add.sex
60
            primary_info.hint_answer = user_context.primaryInfo.hintAnswer
59
    user.date_of_birth = to_py_date(user_to_add.dateOfBirth)
61
        
-
 
62
        if  user_context.primaryInfo.shipmentOption:
-
 
63
            primary_info.shipment_option = user_context.primaryInfo.shipmentOption
-
 
64
            
-
 
65
        if  user_context.primaryInfo.dateOfBirth:
60
    user.active_cart_id = cart.id
66
            user_context.primaryInfo.date_of_birth = user_context.primaryInfo.dateOfBirth  
-
 
67
 
61
 
68
        if  user_context.primaryInfo.communicationEmail:
-
 
69
            user_context.primaryInfo.communication_email = user_context.primaryInfo.communicationEmail
-
 
70
                
-
 
71
    if user_context.sessionid:
-
 
72
        user_context_to_add.session_id = user_context.sessionid
-
 
73
         
-
 
74
    internal_info = InternalInfo()
-
 
75
    user_state = State()
-
 
76
    
-
 
77
    #initialize internal info 
-
 
78
    
-
 
79
    internal_info.geo_zone = 0
-
 
80
    internal_info.shipment_zone = 0
-
 
81
    internal_info.tax_zone = 0
-
 
82
    internal_info.rank_score = 0.0
-
 
83
    internal_info.user = user_context_to_add
-
 
84
    
-
 
85
    #initialize userState
62
    #initialize userState
-
 
63
    user_state = UserState()
86
    user_state.is_email_verified = False
64
    user_state.is_email_verified = False
87
    user_state.is_sms_verified = False
65
    user_state.is_sms_verified = False
88
    user_state.is_logged_in = False
-
 
89
    user_state.account_status = AccountStatus.ACTIVE
66
    user_state.account_status = AccountStatus.ACTIVE
90
    user_state.is_enrolled = False
-
 
91
    #user_state.last_login_timestamp = 0
-
 
92
    #user_state.last_logout = 0
-
 
93
    user_state.ip_list = []
67
    user_state.ip_list = []
94
    user_state.active_since = datetime.datetime.now()
68
    user_state.active_since = datetime.datetime.now()
95
    if user_context.userState: 
-
 
96
        user_state.current_shopping_cart = user_context.userState.shoppingCartHandle
-
 
97
    user_state.user = user_context_to_add
69
    user_state.user = user
98
    
-
 
99
    session.commit()
70
    session.commit()
-
 
71
    
-
 
72
    #Now create the user phones. Mostly, this should only be a mobile number
100
 
73
 
101
    return user_context_to_add.id
74
    cart.user_id = user.id    
102
#===============================================================================
-
 
103
# All the validation rules prioir to adding a user to system go here
-
 
104
#===============================================================================
-
 
105
def validate_context_to_add(user_context):
-
 
106
    return True
75
    session.commit()
107
 
76
 
-
 
77
    return user
108
 
78
 
109
#===============================================================================
79
#===============================================================================
110
# Need to provide the update apis here for relevant fields in PrimaryInfo.
80
# Need to provide the update apis here for relevant fields in PrimaryInfo.
111
#===============================================================================
81
#===============================================================================
112
def update_context(user_context):
82
def update_user(user_to_update):
113
 
-
 
114
    if not user_context.id:
83
    if not user_to_update.id:
115
        raise UserContextException(110, "user does not exist")
84
        raise UserContextException(110, "user does not exist")
116
    user_context_to_add = get_context_by_id(user_context.id)
-
 
117
    primary_info = user_context_to_add.primary_info
-
 
118
    
-
 
119
    #if primary info does not exist
85
    session.add(user_to_update)    
120
    if not primary_info:
-
 
121
        primary_info = PrimaryInfo()
-
 
122
        primary_info.user = user_context_to_add
-
 
123
    
-
 
124
    if user_context.primaryInfo:
-
 
125
        
-
 
126
        if  user_context.primaryInfo.firstName:
-
 
127
            primary_info.first_name = user_context.primaryInfo.firstName
-
 
128
        
-
 
129
        if  user_context.primaryInfo.middleName:
-
 
130
            primary_info.middle_name = user_context.primaryInfo.middleName
-
 
131
        
-
 
132
        if  user_context.primaryInfo.lastName:
-
 
133
            primary_info.last_name = user_context.primaryInfo.lastName
-
 
134
            
-
 
135
        if  user_context.primaryInfo.title:
-
 
136
            primary_info.title = user_context.primaryInfo.title
-
 
137
            
-
 
138
        if  user_context.primaryInfo.pictureUrl:
-
 
139
            primary_info.picture_id = user_context.primaryInfo.pictureUrl
-
 
140
        
-
 
141
        if  user_context.primaryInfo.email:
-
 
142
            primary_info.email = user_context.primaryInfo.email
-
 
143
            
-
 
144
        if  user_context.primaryInfo.userHandle:
-
 
145
            primary_info.user_handle = user_context.primaryInfo.userHandle
-
 
146
            
-
 
147
        if  user_context.primaryInfo.password:
-
 
148
            primary_info.password = user_context.primaryInfo.password
-
 
149
            
-
 
150
        if  user_context.primaryInfo.occupation:
-
 
151
            primary_info.occupation = user_context.primaryInfo.occupation
-
 
152
            
-
 
153
        if  user_context.primaryInfo.hintQuestion:
-
 
154
            primary_info.hint_question = user_context.primaryInfo.hintQuestion
-
 
155
        
-
 
156
        if  user_context.primaryInfo.hintAnswer:
-
 
157
            primary_info.hint_answer = user_context.primaryInfo.hintAnswer
-
 
158
        
-
 
159
        if  user_context.primaryInfo.shipmentOption:
-
 
160
            primary_info.shipment_option = user_context.primaryInfo.shipmentOption
-
 
161
            
-
 
162
        if  user_context.primaryInfo.dateOfBirth:
-
 
163
            primary_info.date_of_birth = to_py_date(user_context.primaryInfo.dateOfBirth)  
-
 
164
 
-
 
165
        if  user_context.primaryInfo.communicationEmail:
-
 
166
            primary_info.communication_email = user_context.primaryInfo.communicationEmail  
-
 
167
         
-
 
168
   
-
 
169
    if user_context.sessionid:
-
 
170
        user_context_to_add.session_id = user_context.sessionid
-
 
171
         
-
 
172
    #userstate and internalinfo will not be updated here
-
 
173
    
-
 
174
    session.commit()
86
    session.commit()
175
    return user_context_to_add.id
87
    return user_to_update
176
 
-
 
177
 
-
 
178
 
-
 
179
def get_context_by_id(user_id):
-
 
180
    return User.get_by(id=user_id)
-
 
181
 
-
 
182
def get_context_by_session_id(session_id):
-
 
183
    return User.get_by(session_id=session_id)
-
 
184
 
88
 
185
def get_context_by_email_or_handle(email_or_handle, is_email):
-
 
186
    if is_email:
-
 
187
        primary_info = PrimaryInfo.get_by(email=email_or_handle)
-
 
188
    else:
-
 
189
        primary_info = PrimaryInfo.get_by(user_handle=email_or_handle)
-
 
190
    
-
 
191
    if primary_info:
-
 
192
        return primary_info.user
-
 
193
    else:
-
 
194
        raise UserContextException(101, "no such user in system")
-
 
195
    
-
 
196
def get_state(user_id, is_session_id):
89
def delete_user(user_id):
197
    if is_session_id:
-
 
198
        user = get_context_by_session_id(user_id)
-
 
199
    else:
-
 
200
        user = get_context_by_id(user_id)
90
    user = get_user_by_id(user_id)
201
    
91
    
202
    if not user:
92
    if not user:
203
        raise_user_exception(user_id)
93
        raise_user_exception(user_id)
-
 
94
        
-
 
95
    user.state.account_status = AccountStatus.DELETED
-
 
96
    session.commit()
-
 
97
    return True
204
    
98
    
205
    return user.state
99
def get_user_state(user_id):
-
 
100
    return UserState.get_by(user_id=user_id)
206
 
101
 
207
def get_primary_info(user_id, is_session_id):
102
def get_internal_info(user_id):
208
    if is_session_id:
-
 
209
        user = get_context_by_session_id(user_id)
103
    return InternalInfo.get_by(user_id=user_id)
210
    else:
-
 
211
        user = get_context_by_id(user_id)
-
 
212
    
-
 
213
    if not user:
-
 
214
        raise_user_exception(user_id)
-
 
215
    
-
 
216
    return user.primary_info
-
 
217
 
104
 
218
def get_internal_info(user_id, is_session_id):
105
def authenticate_user(user_handle, password):
219
    if is_session_id:
-
 
220
        user = get_context_by_session_id(user_id)
-
 
221
    else:
-
 
222
        user = get_context_by_id(user_id)
106
    user = User.get_by(email=user_handle)
223
    
-
 
224
    if not user:
107
    if not user:
225
        raise_user_exception(user_id)
108
        raise AuthenticationException("This email address is not registered.", 102)
226
    
109
    
227
    return user.internal_info
-
 
228
 
-
 
229
def get_context(user_handle, password):
-
 
230
    primary_info = PrimaryInfo.get_by(email=user_handle)
-
 
231
    if not primary_info:
-
 
232
        raise AuthenticationException("Wrong username", 102)
-
 
233
    
-
 
234
    db_password = get_db_password(password)
110
    if user.password == get_db_password(password):
235
    
-
 
236
    if primary_info.password == db_password:
-
 
237
        return primary_info.user
111
        return user
238
    else:
112
    else:
239
        raise AuthenticationException("Wrong username or password", 102)
113
        raise AuthenticationException("Wrong username or password", 102)
240
    
-
 
241
def get_address(address_id):
-
 
242
    address = Address.get_by(id=address_id)
-
 
243
    return address
-
 
244
 
-
 
245
def get_social_service(service_id):
-
 
246
    service = SocialService.get_by(service_id)
-
 
247
    return service
-
 
248
    
-
 
249
def authenticate_user(user_handle, password):
-
 
250
    try:
-
 
251
        get_context(user_handle, password)
-
 
252
    except:
-
 
253
        return False
-
 
254
    return True
-
 
255
 
114
 
256
def user_exists(email):
115
def user_exists(email):
257
    try:
116
    try:
258
        get_context_by_email_or_handle(email, True)
117
        user = User.get_by(email=email)
-
 
118
        if user:
-
 
119
            return True
-
 
120
        else:
-
 
121
            return False
259
    except:
122
    except:
260
        return False
123
        return False
261
    return True
-
 
262
 
-
 
263
 
124
 
264
def add_ip_address_for_user(ip_address, time_stamp, user_id):
-
 
265
    user = get_context_by_id(user_id)
-
 
266
    if not user:
-
 
267
        raise_user_exception(user_id)
-
 
268
    #user exists create an ipaddress object
-
 
269
    ip_address = IPMap()
-
 
270
    ip_address.ip = ip_address
-
 
271
    ip_address.timestamp = to_py_date(time_stamp)
-
 
272
    ip_address.user_info = user.state
-
 
273
    session.commit()
-
 
274
    return True
-
 
275
 
-
 
276
def add_address_for_user(address, user_id, time_stamp, setDefault):
125
def add_address_for_user(address, user_id, time_stamp, set_default):
277
    user = get_context_by_id(user_id)
126
    user = get_user_by_id(user_id)
278
    if not user:
127
    if not user:
279
        raise_user_exception(user_id)
128
        raise_user_exception(user_id)
280
    if not address:
129
    if not address:
281
        raise UserContextException(103,"Address cannot be null")
130
        raise UserContextException(103,"Address cannot be null")
282
    
131
    
Line 291... Line 140...
291
    address_to_add.type = address.type
140
    address_to_add.type = address.type
292
    address_to_add.name = address.name
141
    address_to_add.name = address.name
293
    address_to_add.phone = address.phone
142
    address_to_add.phone = address.phone
294
    address_to_add.added_on = to_py_date(address.addedOn)
143
    address_to_add.added_on = to_py_date(address.addedOn)
295
    address_to_add.enabled = True
144
    address_to_add.enabled = True
296
    address_to_add.primary_info = user.primary_info
145
    address_to_add.user = user
297
    session.commit()
146
    session.commit()
298
    
147
    
299
    if setDefault is True:
148
    if set_default is True:
300
        set_default_address(user_id, address_to_add.id)
149
        user.default_address_id = address_to_add.id
-
 
150
    session.commit()
301
    
151
    
302
    return True
152
    return True
303
 
-
 
304
def set_default_address(userid, addressId):
-
 
305
    primary_info = get_primary_info(userid, False)
-
 
306
    primary_info.default_address_id = addressId 
-
 
307
    session.commit()
-
 
308
    
153
    
309
def remove_address_for_user(user_id, address_id):
154
def remove_address_for_user(user_id, address_id):
310
    
-
 
311
    address = get_address(address_id) 
155
    address = get_address(address_id) 
312
        
156
        
313
    if not address:
157
    if not address:
314
        raise UserContextException(103,"Address not found")
158
        raise UserContextException(103, "Address not found")
315
    if address.primary_info.user.id != user_id:
159
    if address.user.id != user_id:
316
        raise UserContextException(104,"This address belongs to some other user")
160
        raise UserContextException(104, "This address belongs to some other user")
317
    
161
    
318
    #so far so good. Now disable the address
-
 
319
    address.enabled = False
162
    address.enabled = False
320
    session.commit()
163
    session.commit()
321
    return True
164
    return True
322
 
165
 
323
def set_user_as_logged_in(user_id, time_stamp):
166
def set_user_as_logged_in(user_id, time_stamp):
-
 
167
    user_state = UserState.get_by(user_id=user_id)
-
 
168
    #user = get_user_by_id(user_id)
324
    
169
    
325
    user = get_context_by_id(user_id)
-
 
326
    
-
 
327
    if not user:
170
    if not user_state:
328
        raise_user_exception(user_id)
171
        raise_user_exception(user_id)
329
        
172
    
330
    user.state.is_logged_in = True
-
 
331
    if not time_stamp:
173
    if not time_stamp:
332
        user.state.last_login_timestamp = datetime.datetime.now()
174
        user_state.last_login = datetime.datetime.now()
-
 
175
    else:
333
    user.state.last_login_timestamp = to_py_date(time_stamp)
176
        user_state.last_login = to_py_date(time_stamp)
334
    session.commit()
177
    session.commit()
335
    return True
178
    return True
336
    
179
    
337
def set_user_as_logged_out(user_id,time_stamp):
180
def set_user_as_logged_out(user_id, time_stamp):
338
    user = get_context_by_id(user_id)
181
    user_state = UserState.get_by(user_id=user_id)
339
    
182
    
340
    if not user:
183
    if not user_state:
341
        raise_user_exception(user_id)
184
        raise_user_exception(user_id)
342
        
-
 
343
    user.state.is_logged_in = False
-
 
344
    
185
    
345
    if not time_stamp:
186
    if not time_stamp:
346
        user.state.last_logout = datetime.datetime.now()
187
        user_state.last_logout = datetime.datetime.now()
-
 
188
    else:
347
    user.state.last_logout = to_py_date(time_stamp)
189
        user_state.last_logout = to_py_date(time_stamp)
348
    session.commit()
190
    session.commit()
349
    return True
191
    return True
350
 
192
 
-
 
193
def set_default_address(user_id, address_id):
-
 
194
    user = get_user_by_id(user_id)
-
 
195
    address = Address.get_by(id=address_id)
-
 
196
    if not user:
-
 
197
        raise_user_exception(user_id)
-
 
198
    if not address:
-
 
199
        raise UserContextException(103, "Address not found")
-
 
200
    if address.user.id != user.id:
-
 
201
        raise UserContextException(104, "This address belongs to some other user")
-
 
202
        
-
 
203
    user.default_address_id = address_id 
-
 
204
    session.commit()
-
 
205
    
351
def update_password(user_id, password):
206
def update_password(user_id, password):
352
    user = get_context_by_id(user_id)
207
    user = get_user_by_id(user_id)
353
    
208
    
354
    if not user:
209
    if not user:
355
        raise_user_exception(user_id)
210
        raise_user_exception(user_id)
356
    
211
    
357
    if check_for_valid_password(password):
212
    if check_for_valid_password(password):
358
        password_to_update = get_db_password(password)
213
        user.password = get_db_password(password)
359
        user.primary_info.password = password_to_update
-
 
360
        session.commit()
214
        session.commit()
361
        return True
215
        return True
362
    else:
216
    else:
363
        return False
217
        return False
364
    
218
    
365
def delete_user(user_id, is_session_id):
219
def get_address(address_id):
-
 
220
    address = Address.get_by(id=address_id)
366
    if is_session_id:
221
    return address
-
 
222
 
-
 
223
def get_social_service(service_id):
367
        user = get_context_by_session_id(user_id)
224
    service = SocialService.get_by(service_id)
368
    else:
225
    return service
-
 
226
 
-
 
227
def add_ip_address_for_user(ip_address, time_stamp, user_id):
369
        user = get_context_by_id(user_id)
228
    user = get_user_by_id(user_id)
370
    
-
 
371
    if not user:
229
    if not user:
372
        raise_user_exception(user_id)
230
        raise_user_exception(user_id)
-
 
231
    #user exists create an IP address object
373
        
232
    ip_address = IPMap()
-
 
233
    ip_address.ip = ip_address
374
    user.state.account_status = AccountStatus.DELETED
234
    ip_address.timestamp = to_py_date(time_stamp)
-
 
235
    ip_address.user_state = user.state
375
    session.commit()
236
    session.commit()
376
    return True
237
    return True
377
    
238
    
378
def get_social_service_by_name(service_name):
239
def get_social_service_by_name(service_name):
379
    service = SocialService.get_by(name=service_name)
240
    service = SocialService.get_by(name=service_name)
Line 381... Line 242...
381
    if not service:
242
    if not service:
382
        raise UserContextException(106, "No such social service exists")
243
        raise UserContextException(106, "No such social service exists")
383
    return service
244
    return service
384
    
245
    
385
def add_social_handle(user_id, social_service, handle):
246
def add_social_handle(user_id, social_service, handle):
386
    
-
 
387
    
-
 
388
    user = get_context_by_id(user_id)
247
    user = get_user_by_id(user_id)
389
    
248
    
390
    if not user:
249
    if not user:
391
        raise_user_exception(user_id)
250
        raise_user_exception(user_id)
392
    
251
    
393
    service = get_social_service_by_name(social_service)
252
    service = get_social_service_by_name(social_service)
394
    
253
    
395
    #get if use already has this service.
254
    #get if use already has this service.
396
    social_handle = SocialHandle.filter(service=service).filter(primary_info=user.primary_info).one()
255
    social_handle = SocialHandle.filter(service=service).filter(user=user).one()
397
    if not social_handle:
256
    if not social_handle:
398
        #create a new handle
257
        #create a new handle
399
        social_handle = SocialHandle()
258
        social_handle = SocialHandle()
400
        social_handle.service = service
259
        social_handle.service = service
401
        social_handle.primary_info = user.primary_info
260
        social_handle.user = user
402
        social_handle.handle = handle
261
        social_handle.handle = handle
403
        session.commit()
-
 
404
    else:
262
    else:
405
        social_handle.handle = handle
263
        social_handle.handle = handle
406
        session.commit()
264
    session.commit()
407
    return True
265
    return True
-
 
266
 
408
#=============================================================================
267
#=============================================================================
409
# Helper functions 
268
# Helper functions 
410
#=============================================================================
269
#=============================================================================
411
 
270
 
412
'''
271
'''