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
    pass
112
 
113
def get_context_by_id(user_id):
114
    return User.get_by(id=user_id)
115
 
116
def get_context_by_session_id(session_id):
117
    return User.get_by(session_id=session_id)
118
 
119
def get_context_by_email_or_handle(email_or_handle, is_email):
120
    if is_email:
121
        primary_info = PrimaryInfo.get_by(email=email_or_handle)
122
    else:
123
        primary_info = PrimaryInfo.get_by(user_handle=email_or_handle)
124
 
125
    if primary_info:
126
        return primary_info.user
127
    else:
128
        raise UserContextException(101, "no such user in system")
129
 
130
def get_state(user_id, is_session_id):
131
    if is_session_id:
132
        user = get_context_by_session_id(user_id)
133
    else:
134
        user = get_context_by_id(user_id)
135
 
136
    if not user:
137
        raise_user_exception(user_id)
138
 
139
    return user.state
140
 
141
def get_primary_info(user_id, is_session_id):
142
    if is_session_id:
143
        user = get_context_by_session_id(user_id)
144
    else:
145
        user = get_context_by_id(user_id)
146
 
147
    if not user:
148
        raise_user_exception(user_id)
149
 
150
    return user.primary_info
151
 
152
def get_internal_info(user_id, is_session_id):
153
    if is_session_id:
154
        user = get_context_by_session_id(user_id)
155
    else:
156
        user = get_context_by_id(user_id)
157
 
158
    if not user:
159
        raise_user_exception(user_id)
160
 
161
    return user.internal_info
162
 
163
def get_context(user_handle, password):
164
    primary_info = PrimaryInfo.get_by(email=user_handle)
165
    if not primary_info:
166
        raise AuthenticationException("Wrong username", 102)
167
 
168
    db_password = get_db_password(password)
169
 
170
    if primary_info.password == db_password:
171
        return primary_info.user
172
    else:
173
        raise AuthenticationException("Wrong username or password", 102)
174
 
175
def get_address(address_id):
176
    address = Address.get_by(id=address_id)
177
    return address
178
 
179
def get_social_service(service_id):
180
    service = SocialService.get_by(service_id)
181
    return service
182
 
183
def authenticate_user(user_handle, password):
184
    try:
185
        get_context(user_handle, password)
186
    except:
187
        return False
188
    return True
189
 
190
def user_exists(email):
413 rajveer 191
    try:
192
        get_context_by_email_or_handle(email, True)
193
    except:
130 ashish 194
        return False
413 rajveer 195
    return True
130 ashish 196
 
413 rajveer 197
 
130 ashish 198
def add_ip_address_for_user(ip_address, time_stamp, user_id):
199
    user = get_context_by_id(user_id)
200
    if not user:
201
        raise_user_exception(user_id)
202
    #user exists create an ipaddress object
203
    ip_address = IPMap()
204
    ip_address.ip = ip_address
205
    ip_address.timestamp = to_py_date(time_stamp)
206
    ip_address.user_info = user.state
207
    session.commit()
208
    return True
209
 
210
def add_address_for_user(address, user_id, time_stamp):
211
    user = get_context_by_id(user_id)
212
    if not user:
213
        raise_user_exception(user_id)
214
    if not address:
215
        raise UserContextException(103,"Address cannot be null")
216
 
217
    address_to_add = Address()
218
    address_to_add.line_1 = address.line1
219
    address_to_add.line_2 = address.line2
220
    address_to_add.landmark = address.landmark
221
    address_to_add.city = address.city
222
    address_to_add.country = address.country
223
    address_to_add.state = address.state
224
    address_to_add.pin = address.pin
225
    address_to_add.type = address.type
414 ashish 226
    address_to_add.name = address.name
227
    address_to_add.phone = address.phone
130 ashish 228
    address_to_add.added_on = to_py_date(address.addedOn)
229
    address_to_add.enabled = True
230
    address_to_add.primary_info = user.primary_info
231
    session.commit()
232
    return True
233
 
234
def remove_address_for_user(user_id, address_id):
235
 
236
    address = get_address(address_id) 
237
 
238
    if not address:
239
        raise UserContextException(103,"Address not found")
240
    if address.primary_info.user.id != user_id:
241
        raise UserContextException(104,"This address belongs to some other user")
242
 
243
    #so far so good. Now diable the address
244
    address.enabled = False
245
    session.commit()
246
    return True
247
 
248
def set_user_as_logged_in(user_id, time_stamp):
249
 
250
    user = get_context_by_id(user_id)
251
 
252
    if not user:
253
        raise_user_exception(user_id)
254
 
255
    user.state.is_logged_in = True
256
    if not time_stamp:
257
        user.state.last_login_timestamp = datetime.datetime.now()
258
    user.state.last_login_timestamp = to_py_date(time_stamp)
259
    session.commit()
260
    return True
261
 
262
def set_user_as_logged_out(user_id,time_stamp):
263
    user = get_context_by_id(user_id)
264
 
265
    if not user:
266
        raise_user_exception(user_id)
267
 
268
    user.state.is_logged_in = True
269
 
270
    if not time_stamp:
271
        user.state.last_logout = datetime.datetime.now()
272
    user.state.last_logout = to_py_date(time_stamp)
273
    session.commit()
274
    return True
275
 
276
def update_password(user_id, password):
277
    user = get_context_by_id(user_id)
278
 
279
    if not user:
280
        raise_user_exception(user_id)
281
 
282
    if check_for_valid_password(password):
283
        password_to_update = get_db_password(password)
284
        user.primary_info.password = password_to_update
285
        session.commit()
286
        return True
287
    else:
288
        return False
289
 
290
def delete_user(user_id, is_session_id):
291
    if is_session_id:
292
        user = get_context_by_session_id(user_id)
293
    else:
294
        user = get_context_by_id(user_id)
295
 
296
    if not user:
297
        raise_user_exception(user_id)
298
 
299
    user.state.account_status = AccountStatus.DELETED
300
    session.commit()
301
    return True
302
 
303
def get_social_service_by_name(service_name):
304
    service = SocialService.get_by(name=service_name)
305
 
306
    if not service:
307
        raise UserContextException(106, "No such social service exists")
308
    return service
309
 
310
def add_social_handle(user_id, social_service, handle):
311
 
312
 
313
    user = get_context_by_id(user_id)
314
 
315
    if not user:
316
        raise_user_exception(user_id)
317
 
318
    service = get_social_service_by_name(social_service)
319
 
320
    #get if use already has this service.
321
    social_handle = SocialHandle.filter(service=service).filter(primary_info=user.primary_info).one()
322
    if not social_handle:
323
        #create a new handle
324
        social_handle = SocialHandle()
325
        social_handle.service = service
326
        social_handle.primary_info = user.primary_info
327
        social_handle.handle = handle
328
        session.commit()
329
    else:
330
        social_handle.handle = handle
331
        session.commit()
332
    return True
333
#=============================================================================
334
# Helper functions 
335
#=============================================================================
336
 
337
'''
338
This function returns the password as stored in the db
339
'''    
340
def get_db_password(password):
341
    return password
342
 
343
def check_for_valid_password(password):
344
    if not password:
345
        raise UserContextException(105,"password cannot be null")
346
    return True
347
#------------------------------------------------------------------------------ 
348
 
349
#===============================================================================
350
# raises the UserContextException
351
#===============================================================================
352
def raise_user_exception(user_id):
353
    raise UserContextException(101, "no such user in system %d" %(user_id))