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