Subversion Repositories SmartDukaan

Rev

Rev 515 | Rev 563 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 515 Rev 557
Line 2... Line 2...
2
Created on 28-Apr-2010
2
Created on 28-Apr-2010
3
 
3
 
4
@author: ashish
4
@author: ashish
5
'''
5
'''
6
from shop2020.utils.Utils import log_entry
6
from shop2020.utils.Utils import log_entry
7
from shop2020.model.v1.user.impl.DataAccessors import add_social_handle,\
7
from shop2020.model.v1.user.impl.UserDataAccessors import create_user, update_user, get_user_by_id, update_password,\
8
    delete_user, update_password, set_user_as_logged_out, set_user_as_logged_in,\
8
    set_user_as_logged_out, set_user_as_logged_in, remove_address_for_user,\
9
    remove_address_for_user, add_address_for_user, add_ip_address_for_user,\
9
    add_address_for_user, delete_user, authenticate_user, user_exists, get_user_state, initialize, set_default_address,\
-
 
10
    create_anonymous_user
-
 
11
from shop2020.model.v1.user.impl.CartDataAccessors import create_cart, get_cart,\
10
    user_exists, authenticate_user, get_context, get_internal_info,\
12
    get_cart_by_id, get_cart_by_user_id_and_status, get_carts_by_status,\
11
    get_primary_info, get_state, get_context_by_email_or_handle,\
13
    get_carts_between, change_cart_status, add_item_to_cart,\
12
    get_context_by_session_id, get_context_by_id, create_context, update_context,\
14
    change_item_quantity, change_item_status, add_address_to_cart, commit_cart,\
13
    initialize, set_default_address
15
    validate_cart, merge_cart, delete_item_from_cart
14
from shop2020.model.v1.user.impl.Converters import to_t_user_context,\
16
from shop2020.model.v1.user.impl.Converters import to_t_user, to_t_user_state, to_t_cart
-
 
17
from shop2020.clients.TransactionClient import TransactionClient
-
 
18
from shop2020.thriftpy.model.v1.user.ttypes import ShoppingCartException
-
 
19
 
-
 
20
from shop2020.model.v1.user.impl.WidgetDataAccessor import add_widget,\
-
 
21
    add_items_to_widget, update_my_research, update_widget, get_widget_by_type,\
15
    to_t_internal_info, to_t_primary_info, to_t_user_state
22
    get_my_research, get_ratings, update_ratings, get_browse_history,\
-
 
23
    update_browse_history, delete_item_from_my_research, delete_item_from_widget
-
 
24
from shop2020.model.v1.user.impl.WidgetConverters import to_t_widget, to_t_ratings
16
 
25
 
17
class UserContextServiceHandler:
26
class UserContextServiceHandler:
18
    
27
    
19
    
28
    
20
    def __init__(self):
29
    def __init__(self):
21
        initialize()
30
        initialize()
-
 
31
        self.transaction_client = TransactionClient()
22
    
32
 
23
    """
33
    """
24
    User session context service handler
34
    service
25
    """
35
    """
26
    def createContext(self, context, updateExisting):
36
    def createAnonymousUser(self, jsessionId):
-
 
37
        cart = create_cart(-1)
-
 
38
        return to_t_user(create_anonymous_user(jsessionId, cart))
-
 
39
    
-
 
40
    def getUserById(self, userId):
27
        """
41
        """
28
        Parameters:
42
        Parameters:
29
         - context
43
        - userId
30
         - updateExisting
-
 
31
        """
44
        """
32
        log_entry(self, "create or update context")
-
 
33
        if not updateExisting:
-
 
34
            id = create_context(context)
-
 
35
        else:
-
 
36
            id = update_context(context)
-
 
37
        
-
 
38
        return to_t_user_context(get_context_by_id(id))
45
        return to_t_user(get_user_by_id(userId))
39
        pass
-
 
40
    
46
    
41
    def getContextFromId(self, userId, isSessionId):
47
    def createUser(self, user):
42
        """
48
        """
43
        Parameters:
49
        Parameters:
44
         - userId
50
        - user
45
         - isSessionId
-
 
46
        """
51
        """
47
        log_entry(self, "get context from id")
-
 
48
        if isSessionId:
52
        cart = create_cart(-1)
49
            return to_t_user_context(get_context_by_session_id(userId))
-
 
50
        else:
-
 
51
            return to_t_user_context(get_context_by_id(userId))
53
        return to_t_user(create_user(user, cart))
52
        pass
-
 
53
    
54
    
54
    def getContextFromEmailOrHandle(self, emailorhandle, isEmail):
55
    def updateUser(self, user):
55
        """
56
        """
56
        Parameters:
57
        Parameters:
57
         - emailorhandle
-
 
58
         - isEmail
58
        - user
59
        """
59
        """
60
        log_entry(self, "get context from email or handle")
60
        return to_t_user(update_user(user))
61
        return to_t_user_context(get_context_by_email_or_handle(emailorhandle, isEmail))
-
 
62
        pass
-
 
63
    
61
    
64
    def getState(self, userId, isSessionId):
62
    def deleteUser(self, userId):
65
        """
63
        """
66
        Parameters:
64
        Parameters:
67
         - userId
65
        - userId
-
 
66
        """
-
 
67
        return delete_user(userId)
-
 
68
    
-
 
69
    def getUserState(self, userId):
-
 
70
        """
-
 
71
        Parameters:
68
         - isSessionId
72
        - userId
69
        """
73
        """
70
        log_entry(self, "get State")
74
        log_entry(self, "get State")
71
        return to_t_user_state(get_state(userId, isSessionId))
75
        return to_t_user_state(get_user_state(userId))
-
 
76
    
-
 
77
    def authenticateUser(self, email, password):
-
 
78
        """
-
 
79
        Parameters:
-
 
80
        - email
-
 
81
        - password
-
 
82
        """
-
 
83
        log_entry(self, "authenticate user")
-
 
84
        return to_t_user(authenticate_user(email, password))
-
 
85
    
-
 
86
    def userExists(self, email):
-
 
87
        """
-
 
88
        Parameters:
-
 
89
        - email
-
 
90
        """
-
 
91
        log_entry(self, "user exists")
-
 
92
        return user_exists(email)
-
 
93
    
-
 
94
    def addAddressForUser(self, userId, address, timestamp, setDefault):
-
 
95
        """
-
 
96
        Parameters:
-
 
97
        - userId
-
 
98
        - address
-
 
99
        - timestamp
-
 
100
        - setDefault
-
 
101
        """
-
 
102
        log_entry(self, "add address for user")
-
 
103
        return add_address_for_user(address, userId, timestamp, setDefault)
-
 
104
    
-
 
105
    def removeAddressForUser(self, userid, addressId):
-
 
106
        """
-
 
107
        Parameters:
-
 
108
        - userid
-
 
109
        - addressId
-
 
110
        """
-
 
111
        log_entry(self, "removing address")
-
 
112
        return remove_address_for_user(userid, addressId)
-
 
113
    
-
 
114
    def setUserAsLoggedIn(self, userId, timestamp):
-
 
115
        """
-
 
116
        Parameters:
-
 
117
        - userId
-
 
118
        - timestamp
-
 
119
        """
-
 
120
        log_entry(self, "set_user_as_logged_in")
-
 
121
        return set_user_as_logged_in(userId, timestamp)
-
 
122
    
-
 
123
    def setUserAsLoggedOut(self, userId, timestamp):
-
 
124
        """
-
 
125
        Parameters:
-
 
126
        - userId
-
 
127
        - timestamp
72
        pass
128
        """
-
 
129
        log_entry(self, "set user as logged out")
-
 
130
        return set_user_as_logged_out(userId, timestamp)
73
    
131
    
-
 
132
    def setDefaultAddress(self, userId, addressId):
-
 
133
        """
-
 
134
        Parameters:
-
 
135
        - userId
-
 
136
        - addressId
-
 
137
        """
-
 
138
        return set_default_address(userId, addressId)
-
 
139
    
74
    def getPrimaryInfo(self, userId, isSessionId):
140
    def updatePassword(self, userId, password):
-
 
141
        """
-
 
142
        Parameters:
-
 
143
        - userId
-
 
144
        - password
-
 
145
        """
-
 
146
        log_entry(self, "updating password")
-
 
147
        return update_password(userId, password)
-
 
148
 
-
 
149
 
-
 
150
    def createCart(self, userId):
-
 
151
        """
-
 
152
        Parameters:
-
 
153
        - userId
-
 
154
        """
-
 
155
        return create_cart(userId).id
-
 
156
 
-
 
157
    def getCurrentCart(self, userId):
75
        """
158
        """
76
        Parameters:
159
        Parameters:
77
         - userId
160
         - userId
78
         - isSessionId
-
 
79
        """
161
        """
-
 
162
        cart = get_cart(userId)
-
 
163
        if cart:
80
        log_entry(self, "get primary info")
164
            return to_t_cart(cart)
-
 
165
        else:
81
        return to_t_primary_info(get_primary_info(userId, isSessionId))
166
            raise ShoppingCartException(101, "not cart attached to this user")
-
 
167
 
-
 
168
    def getCart(self, cartId):
-
 
169
        """
-
 
170
        Parameters:
-
 
171
         - cartId
-
 
172
        """
-
 
173
        cart = get_cart_by_id(cartId)
-
 
174
        if cart:
-
 
175
            return to_t_cart(cart)
82
        pass
176
        else:
-
 
177
            raise ShoppingCartException(101, "not cart attached to this id")
83
    
178
    
84
    def getInternalInfo(self, userId, isSessionId):
179
    def getCartsForUser(self, userId, status):
85
        """
180
        """
86
        Parameters:
181
        Parameters:
87
         - userId
182
         - userId
88
         - isSessionId
183
         - status
89
        """
184
        """
-
 
185
        if not userId:
90
        log_entry(self, "get internal info")
186
            raise ShoppingCartException(101, "user_id is not provided")
-
 
187
        
91
        return to_t_internal_info(get_internal_info(userId, isSessionId))
188
        carts = get_cart_by_user_id_and_status(userId, status)
-
 
189
        t_carts = []
92
        pass
190
        if carts:
-
 
191
            for cart in carts:
-
 
192
                t_carts.append(to_t_cart(cart))
-
 
193
        return t_carts
93
    
194
    
94
    def getContext(self, email, password):
195
    def getCartsByStatus(self, status):
95
        """
196
        """
96
        Parameters:
197
        Parameters:
97
         - email
198
         - status
98
         - password
-
 
99
        """
199
        """
-
 
200
        carts = get_carts_by_status(status)
-
 
201
        t_carts = []
-
 
202
        if carts:
100
        log_entry(self, "get context")
203
            for cart in carts:
101
        return to_t_user_context(get_context(email, password))
204
                t_carts.append(to_t_cart(cart))
102
        pass
205
        return t_carts
103
    
206
    
104
    def authenticateUser(self, handle, password, isEmail):
207
    def getCartsByTime(self, from_time, to_time, status):
105
        """
208
        """
106
        Parameters:
209
        Parameters:
107
         - handle
210
         - from_time
108
         - password
211
         - to_time
109
         - isEmail
212
         - status
110
        """
213
        """
-
 
214
        carts = get_carts_between(from_time, to_time, status)
-
 
215
        t_carts = []
-
 
216
        if carts:
111
        log_entry(self, "authenticate user")
217
            for cart in carts:
112
        return authenticate_user(handle, password)
218
                t_carts.append(to_t_cart(cart))
113
        pass
219
        return t_carts
114
    
220
    
115
    def userExists(self, email):
221
    def changeCartStatus(self, cartId, status):
116
        """
222
        """
117
        Parameters:
223
        Parameters:
-
 
224
         - cartId
118
         - email
225
         - status
119
        """
226
        """
120
        log_entry(self, "user exists")
227
        change_cart_status(cartId, status)
121
        return user_exists(email)
-
 
122
        pass
-
 
123
    
228
    
124
    def addIpAdressForUser(self, ip, timestamp, userId):
229
    def addItemToCart(self, cartId, itemId, quantity):
125
        """
230
        """
126
        Parameters:
231
        Parameters:
127
         - ip
232
         - cartId
128
         - timestamp
233
         - itemId
129
         - userId
234
         - quantity
130
        """
235
        """
131
        log_entry(self, "add ipaddress for user")
-
 
132
        return add_ip_address_for_user(ip, timestamp, userId)
236
        return add_item_to_cart(cartId, itemId, quantity)
133
        pass
-
 
134
    
237
    
135
    def addAddressForUser(self, address, userid, timestamp, setDefault):
238
    def deleteItemFromCart(self, cartId, itemId):
136
        """
239
        """
137
        Parameters:
240
        Parameters:
138
         - address
241
         - cartId
139
         - userid
242
         - itemId
140
         - timestamp
-
 
141
         - setDefault
-
 
142
        """
243
        """
143
        log_entry(self, "add address for user")
244
        delete_item_from_cart(cartId, itemId)
-
 
245
 
144
        return add_address_for_user(address, userid, timestamp, setDefault)
246
    def changeQuantity(self, cartId, itemId, quantity):
-
 
247
        """
-
 
248
        Parameters:
-
 
249
         - cartId
-
 
250
         - itemId
-
 
251
         - quantity
145
        pass
252
        """
-
 
253
        return change_item_quantity(cartId, itemId, quantity)
146
    
254
    
-
 
255
    def changeItemStatus(self, cartId, itemId, status):
-
 
256
        """
-
 
257
        Parameters:
-
 
258
         - cartId
-
 
259
         - itemId
-
 
260
        """
-
 
261
        return change_item_status(cartId, itemId, status)
-
 
262
    
147
    def removeAddressForUser(self, userid, addressId):
263
    def addAddressToCart(self, cartId, addressId):
148
        """
264
        """
149
        Parameters:
265
        Parameters:
150
         - userid
266
         - cartId
151
         - addressId
267
         - addressId
152
        """
268
        """
153
        log_entry(self, "removing address")
-
 
154
        return remove_address_for_user(userid, addressId)
269
        return add_address_to_cart(cartId, addressId)
155
        pass
-
 
156
    
270
    
157
    def setUserAsLoggedIn(self, userId, timestamp):
271
    def commitCart(self, cartId):
158
        """
272
        """
159
        Parameters:
273
        Parameters:
160
         - userId
274
         - cartId
161
         - timestamp
-
 
162
        """
275
        """
163
        log_entry(self, "set_user_as_logged_in")
-
 
164
        return set_user_as_logged_in(userId, timestamp)
276
        return commit_cart(cartId, self.transaction_client)
165
        pass
-
 
166
    
277
    
167
    def setUserAsLoggedOut(self, userid, timestamp):
278
    def validateCart(self, cartId):
168
        """
279
        """
169
        Parameters:
280
        Parameters:
170
         - userid
281
         - cartId
171
         - timestamp
-
 
172
        """
282
        """
173
        log_entry(self, "set user as logged out")
-
 
174
        return set_user_as_logged_out(userid, timestamp)
283
        return validate_cart(cartId)
175
        pass
-
 
176
    
284
    
177
    def setDefaultAddress(self, userid, addressId):
285
    def mergeCart(self, fromCartId, toCartId):
178
        """
286
        """
179
        Parameters:
287
        Parameters:
180
         - userid
288
         - fromCartId
181
         - addressId
289
         - toCartId
182
        """
290
        """
183
        return set_default_address(userid, addressId)
291
        merge_cart(fromCartId, toCartId)
184
        pass
-
 
185
 
292
 
186
 
-
 
187
    def updatePassword(self, userid, password):
293
    def addWidget(self, widget):
188
        """
294
        """
189
        Parameters:
295
        Parameters:
190
         - userid
296
         - widget
191
         - password
-
 
192
        """
297
        """
193
        log_entry(self, "updating password")
298
        add_widget(widget)
194
        return update_password(userid, password)
-
 
195
        pass
-
 
196
    
299
    
197
    def deleteUser(self, userid, isSessionId):
300
    def addItemToWidget(self, widget_id, items):
198
        """
301
        """
199
        Parameters:
302
        Parameters:
200
         - userid
303
         - widget_id
201
         - isSessionId
304
         - items
202
        """
305
        """
203
        log_entry(self, "Deleting user")
306
        add_items_to_widget(widget_id, items)
204
        return delete_user(userid, isSessionId)
-
 
205
    
307
    
206
    def sendEmailVerification(self, userid):
308
    def updateMyResearch(self, user_id, item_id):
207
        """
309
        """
208
        Parameters:
310
        Parameters:
209
         - userid
311
         - user_id
-
 
312
         - item_id
210
        """
313
        """
211
        pass
314
        return update_my_research(user_id, item_id)
212
    
315
    
213
    def sendSMSVerification(self, userid):
316
    def updateWidget(self, widgetId, enable):
214
        """
317
        """
215
        Parameters:
318
        Parameters:
216
         - userid
319
         - widgetId
-
 
320
         - enable
217
        """
321
        """
218
        pass
322
        update_widget(widgetId, enable)
219
    
323
    
-
 
324
    def updateWidgetItem(self, widgetId, enable):
-
 
325
        """
-
 
326
        Parameters:
-
 
327
         - widgetId
-
 
328
         - enable
-
 
329
        """
-
 
330
        update_widget(widgetId, enable)
-
 
331
        
220
    def confirmEmailVerification(self, userid):
332
    def deleteItemFromWidget(self, widget_id, item_id):
221
        """
333
        """
222
        Parameters:
334
        Parameters:
-
 
335
         - widget_id
223
         - userid
336
         - item_id
224
        """
337
        """
225
        pass
338
        return delete_item_from_widget(widget_id, item_id)
226
    
339
    
227
    def confirmSMSVerification(self, userid):
340
    def deleteItemFromMyResearch(self, user_id, item_id):
228
        """
341
        """
229
        Parameters:
342
        Parameters:
230
         - userid
343
         - user_id
-
 
344
         - item_id
231
        """
345
        """
232
        pass
346
        return delete_item_from_my_research(user_id, item_id)
233
    
347
    
234
    def addSocialhandle(self, userid, socialService, handle):
348
    def getWidget(self, type, userId, onlyEnabled):
235
        """
349
        """
236
        Parameters:
350
        Parameters:
237
         - userid
351
         - type
238
         - socialService
352
         - userId
239
         - handle
353
         - onlyEnabled
240
        """
354
        """
241
        log_entry(self, "adding social handle")
355
        widget = get_widget_by_type(type, userId, onlyEnabled)
242
        return add_social_handle(userid, socialService, handle)
356
        return to_t_widget(widget)
243
 
-
 
244
    
357
    
245
    def sendNewPasswordById(self, userid):
358
    def getMyResearch(self, user_id):
246
        """
359
        """
247
        Parameters:
360
        Parameters:
248
         - userid
361
         - user_id
249
        """
362
        """
250
        pass
363
        return to_t_widget(get_my_research(user_id))
251
    
364
    
-
 
365
    def updateRatings(self, item_id, type, rating, user_id):
-
 
366
        """
-
 
367
        Parameters:
-
 
368
        - item_id
-
 
369
        - type
-
 
370
        - rating
-
 
371
        - user_id
-
 
372
        """
-
 
373
        update_ratings(user_id, item_id, rating, type)
-
 
374
        
-
 
375
            
-
 
376
    def getRatings(self, item_id, user_id):
-
 
377
        """
-
 
378
        Parameters:
-
 
379
        - item_id
-
 
380
        - user_id
-
 
381
        """
-
 
382
        all_ratings, user_ratings = get_ratings(user_id, item_id)
-
 
383
        return to_t_ratings(user_ratings, all_ratings, item_id, user_id)
-
 
384
 
-
 
385
    def updateBrowseHistory(self, user_id, item_id, is_session_id):
-
 
386
        """
-
 
387
        Parameters: 
-
 
388
        - user_id
-
 
389
        - item_id
-
 
390
        - isSessionId
-
 
391
        """
-
 
392
        update_browse_history(user_id, item_id, is_session_id)
-
 
393
        
252
    def sendNewPasswordByHandle(self, emailOrHandle, isEmail):
394
    def getBrowseHistory(self, user_id, is_session_id):
253
        """
395
        """
254
        Parameters:
396
        Parameters:
255
         - emailOrHandle
397
         - user_id
256
         - isEmail
398
         - is_session_id
257
        """
399
        """
258
        pass
400
        return to_t_widget(get_browse_history(user_id, is_session_id))
259
401