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