Subversion Repositories SmartDukaan

Rev

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