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
226
    address_to_add.added_on = to_py_date(address.addedOn)
227
    address_to_add.enabled = True
228
    address_to_add.primary_info = user.primary_info
229
    session.commit()
230
    return True
231
 
232
def remove_address_for_user(user_id, address_id):
233
 
234
    address = get_address(address_id) 
235
 
236
    if not address:
237
        raise UserContextException(103,"Address not found")
238
    if address.primary_info.user.id != user_id:
239
        raise UserContextException(104,"This address belongs to some other user")
240
 
241
    #so far so good. Now diable the address
242
    address.enabled = False
243
    session.commit()
244
    return True
245
 
246
def set_user_as_logged_in(user_id, time_stamp):
247
 
248
    user = get_context_by_id(user_id)
249
 
250
    if not user:
251
        raise_user_exception(user_id)
252
 
253
    user.state.is_logged_in = True
254
    if not time_stamp:
255
        user.state.last_login_timestamp = datetime.datetime.now()
256
    user.state.last_login_timestamp = to_py_date(time_stamp)
257
    session.commit()
258
    return True
259
 
260
def set_user_as_logged_out(user_id,time_stamp):
261
    user = get_context_by_id(user_id)
262
 
263
    if not user:
264
        raise_user_exception(user_id)
265
 
266
    user.state.is_logged_in = True
267
 
268
    if not time_stamp:
269
        user.state.last_logout = datetime.datetime.now()
270
    user.state.last_logout = to_py_date(time_stamp)
271
    session.commit()
272
    return True
273
 
274
def update_password(user_id, password):
275
    user = get_context_by_id(user_id)
276
 
277
    if not user:
278
        raise_user_exception(user_id)
279
 
280
    if check_for_valid_password(password):
281
        password_to_update = get_db_password(password)
282
        user.primary_info.password = password_to_update
283
        session.commit()
284
        return True
285
    else:
286
        return False
287
 
288
def delete_user(user_id, is_session_id):
289
    if is_session_id:
290
        user = get_context_by_session_id(user_id)
291
    else:
292
        user = get_context_by_id(user_id)
293
 
294
    if not user:
295
        raise_user_exception(user_id)
296
 
297
    user.state.account_status = AccountStatus.DELETED
298
    session.commit()
299
    return True
300
 
301
def get_social_service_by_name(service_name):
302
    service = SocialService.get_by(name=service_name)
303
 
304
    if not service:
305
        raise UserContextException(106, "No such social service exists")
306
    return service
307
 
308
def add_social_handle(user_id, social_service, handle):
309
 
310
 
311
    user = get_context_by_id(user_id)
312
 
313
    if not user:
314
        raise_user_exception(user_id)
315
 
316
    service = get_social_service_by_name(social_service)
317
 
318
    #get if use already has this service.
319
    social_handle = SocialHandle.filter(service=service).filter(primary_info=user.primary_info).one()
320
    if not social_handle:
321
        #create a new handle
322
        social_handle = SocialHandle()
323
        social_handle.service = service
324
        social_handle.primary_info = user.primary_info
325
        social_handle.handle = handle
326
        session.commit()
327
    else:
328
        social_handle.handle = handle
329
        session.commit()
330
    return True
331
#=============================================================================
332
# Helper functions 
333
#=============================================================================
334
 
335
'''
336
This function returns the password as stored in the db
337
'''    
338
def get_db_password(password):
339
    return password
340
 
341
def check_for_valid_password(password):
342
    if not password:
343
        raise UserContextException(105,"password cannot be null")
344
    return True
345
#------------------------------------------------------------------------------ 
346
 
347
#===============================================================================
348
# raises the UserContextException
349
#===============================================================================
350
def raise_user_exception(user_id):
351
    raise UserContextException(101, "no such user in system %d" %(user_id))