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()
23
    if user_context.primaryInfo:
24
        primary_info = PrimaryInfo()
25
        if  user_context.primaryInfo.firstName:
26
            primary_info.first_name = user_context.primaryInfo.firstName
27
 
28
        if  user_context.primaryInfo.middleName:
29
            primary_info.middle_name = user_context.primaryInfo.middleName
30
 
31
        if  user_context.primaryInfo.lastName:
32
            primary_info.last_name = user_context.primaryInfo.lastName
33
 
34
        if  user_context.primaryInfo.title:
35
            primary_info.title = user_context.primaryInfo.title
36
 
37
        if  user_context.primaryInfo.pictureUrl:
38
            primary_info.picture_id = user_context.primaryInfo.pictureUrl
39
 
40
        if  user_context.primaryInfo.email:
41
            primary_info.email = user_context.primaryInfo.email
42
 
43
        if  user_context.primaryInfo.userHandle:
44
            primary_info.user_handle = user_context.primaryInfo.userHandle
45
 
46
        if  user_context.primaryInfo.password:
47
            primary_info.password = user_context.primaryInfo.password
48
 
49
        if  user_context.primaryInfo.occupation:
50
            primary_info.occupation = user_context.primaryInfo.occupation
51
 
52
        if  user_context.primaryInfo.hintQuestion:
53
            primary_info.hint_question = user_context.primaryInfo.hintQuestion
54
 
55
        if  user_context.primaryInfo.hintAnswer:
56
            primary_info.hint_answer = user_context.primaryInfo.hintAnswer
57
 
58
        if  user_context.primaryInfo.shipmentOption:
59
            primary_info.shipment_option = user_context.primaryInfo.shipmentOption
60
 
61
        if  user_context.primaryInfo.firstName:
62
            primary_info.first_name = user_context.primaryInfo.firstName
63
 
64
        primary_info.user = user_context_to_add 
65
 
66
    if user_context.sessionId:
67
        user_context_to_add.session_id = user_context.sessionId
68
 
69
    primary_info = PrimaryInfo()
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
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()
91
    user_state.current_shopping_cart = user_context.userState.shoppingCartHandle
92
    user_state.user = user_context_to_add
93
 
94
    session.commit()
95
 
96
#===============================================================================
97
# All the validation rules prioir to adding a user to system go here
98
#===============================================================================
99
def validate_context_to_add(user_context):
100
    return True
101
 
102
 
103
#===============================================================================
104
# Need to provide the update apis here for relevant fields in PrimaryInfo.
105
#===============================================================================
106
def update_context(user_context):
107
    pass
108
 
109
def get_context_by_id(user_id):
110
    return User.get_by(id=user_id)
111
 
112
def get_context_by_session_id(session_id):
113
    return User.get_by(session_id=session_id)
114
 
115
def get_context_by_email_or_handle(email_or_handle, is_email):
116
    if is_email:
117
        primary_info = PrimaryInfo.get_by(email=email_or_handle)
118
    else:
119
        primary_info = PrimaryInfo.get_by(user_handle=email_or_handle)
120
 
121
    if primary_info:
122
        return primary_info.user
123
    else:
124
        raise UserContextException(101, "no such user in system")
125
 
126
def get_state(user_id, is_session_id):
127
    if is_session_id:
128
        user = get_context_by_session_id(user_id)
129
    else:
130
        user = get_context_by_id(user_id)
131
 
132
    if not user:
133
        raise_user_exception(user_id)
134
 
135
    return user.state
136
 
137
def get_primary_info(user_id, is_session_id):
138
    if is_session_id:
139
        user = get_context_by_session_id(user_id)
140
    else:
141
        user = get_context_by_id(user_id)
142
 
143
    if not user:
144
        raise_user_exception(user_id)
145
 
146
    return user.primary_info
147
 
148
def get_internal_info(user_id, is_session_id):
149
    if is_session_id:
150
        user = get_context_by_session_id(user_id)
151
    else:
152
        user = get_context_by_id(user_id)
153
 
154
    if not user:
155
        raise_user_exception(user_id)
156
 
157
    return user.internal_info
158
 
159
def get_context(user_handle, password):
160
    primary_info = PrimaryInfo.get_by(email=user_handle)
161
    if not primary_info:
162
        raise AuthenticationException("Wrong username", 102)
163
 
164
    db_password = get_db_password(password)
165
 
166
    if primary_info.password == db_password:
167
        return primary_info.user
168
    else:
169
        raise AuthenticationException("Wrong username or password", 102)
170
 
171
def get_address(address_id):
172
    address = Address.get_by(id=address_id)
173
    return address
174
 
175
def get_social_service(service_id):
176
    service = SocialService.get_by(service_id)
177
    return service
178
 
179
def authenticate_user(user_handle, password):
180
    try:
181
        get_context(user_handle, password)
182
    except:
183
        return False
184
    return True
185
 
186
def user_exists(email):
187
    user = get_context_by_email_or_handle(email, True)
188
    if user:
189
        return True
190
    else:
191
        return False
192
 
193
def add_ip_address_for_user(ip_address, time_stamp, user_id):
194
    user = get_context_by_id(user_id)
195
    if not user:
196
        raise_user_exception(user_id)
197
    #user exists create an ipaddress object
198
    ip_address = IPMap()
199
    ip_address.ip = ip_address
200
    ip_address.timestamp = to_py_date(time_stamp)
201
    ip_address.user_info = user.state
202
    session.commit()
203
    return True
204
 
205
def add_address_for_user(address, user_id, time_stamp):
206
    user = get_context_by_id(user_id)
207
    if not user:
208
        raise_user_exception(user_id)
209
    if not address:
210
        raise UserContextException(103,"Address cannot be null")
211
 
212
    address_to_add = Address()
213
    address_to_add.line_1 = address.line1
214
    address_to_add.line_2 = address.line2
215
    address_to_add.landmark = address.landmark
216
    address_to_add.city = address.city
217
    address_to_add.country = address.country
218
    address_to_add.state = address.state
219
    address_to_add.pin = address.pin
220
    address_to_add.type = address.type
221
    address_to_add.added_on = to_py_date(address.addedOn)
222
    address_to_add.enabled = True
223
    address_to_add.primary_info = user.primary_info
224
    session.commit()
225
    return True
226
 
227
def remove_address_for_user(user_id, address_id):
228
 
229
    address = get_address(address_id) 
230
 
231
    if not address:
232
        raise UserContextException(103,"Address not found")
233
    if address.primary_info.user.id != user_id:
234
        raise UserContextException(104,"This address belongs to some other user")
235
 
236
    #so far so good. Now diable the address
237
    address.enabled = False
238
    session.commit()
239
    return True
240
 
241
def set_user_as_logged_in(user_id, time_stamp):
242
 
243
    user = get_context_by_id(user_id)
244
 
245
    if not user:
246
        raise_user_exception(user_id)
247
 
248
    user.state.is_logged_in = True
249
    if not time_stamp:
250
        user.state.last_login_timestamp = datetime.datetime.now()
251
    user.state.last_login_timestamp = to_py_date(time_stamp)
252
    session.commit()
253
    return True
254
 
255
def set_user_as_logged_out(user_id,time_stamp):
256
    user = get_context_by_id(user_id)
257
 
258
    if not user:
259
        raise_user_exception(user_id)
260
 
261
    user.state.is_logged_in = True
262
 
263
    if not time_stamp:
264
        user.state.last_logout = datetime.datetime.now()
265
    user.state.last_logout = to_py_date(time_stamp)
266
    session.commit()
267
    return True
268
 
269
def update_password(user_id, password):
270
    user = get_context_by_id(user_id)
271
 
272
    if not user:
273
        raise_user_exception(user_id)
274
 
275
    if check_for_valid_password(password):
276
        password_to_update = get_db_password(password)
277
        user.primary_info.password = password_to_update
278
        session.commit()
279
        return True
280
    else:
281
        return False
282
 
283
def delete_user(user_id, is_session_id):
284
    if is_session_id:
285
        user = get_context_by_session_id(user_id)
286
    else:
287
        user = get_context_by_id(user_id)
288
 
289
    if not user:
290
        raise_user_exception(user_id)
291
 
292
    user.state.account_status = AccountStatus.DELETED
293
    session.commit()
294
    return True
295
 
296
def get_social_service_by_name(service_name):
297
    service = SocialService.get_by(name=service_name)
298
 
299
    if not service:
300
        raise UserContextException(106, "No such social service exists")
301
    return service
302
 
303
def add_social_handle(user_id, social_service, handle):
304
 
305
 
306
    user = get_context_by_id(user_id)
307
 
308
    if not user:
309
        raise_user_exception(user_id)
310
 
311
    service = get_social_service_by_name(social_service)
312
 
313
    #get if use already has this service.
314
    social_handle = SocialHandle.filter(service=service).filter(primary_info=user.primary_info).one()
315
    if not social_handle:
316
        #create a new handle
317
        social_handle = SocialHandle()
318
        social_handle.service = service
319
        social_handle.primary_info = user.primary_info
320
        social_handle.handle = handle
321
        session.commit()
322
    else:
323
        social_handle.handle = handle
324
        session.commit()
325
    return True
326
#=============================================================================
327
# Helper functions 
328
#=============================================================================
329
 
330
'''
331
This function returns the password as stored in the db
332
'''    
333
def get_db_password(password):
334
    return password
335
 
336
def check_for_valid_password(password):
337
    if not password:
338
        raise UserContextException(105,"password cannot be null")
339
    return True
340
#------------------------------------------------------------------------------ 
341
 
342
#===============================================================================
343
# raises the UserContextException
344
#===============================================================================
345
def raise_user_exception(user_id):
346
    raise UserContextException(101, "no such user in system %d" %(user_id))