Subversion Repositories SmartDukaan

Rev

Rev 690 | Rev 766 | Go to most recent revision | Details | Compare with Previous | 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.utils.Utils import log_entry
557 chandransh 7
from shop2020.model.v1.user.impl.UserDataAccessors import create_user, update_user, get_user_by_id, update_password,\
8
    set_user_as_logged_out, set_user_as_logged_in, remove_address_for_user,\
9
    add_address_for_user, delete_user, authenticate_user, user_exists, get_user_state, initialize, set_default_address,\
594 rajveer 10
    create_anonymous_user, forgot_password, get_all_addresses_for_user, get_default_addredd_id
557 chandransh 11
from shop2020.model.v1.user.impl.CartDataAccessors import create_cart, get_cart,\
12
    get_cart_by_id, get_cart_by_user_id_and_status, get_carts_by_status,\
13
    get_carts_between, change_cart_status, add_item_to_cart,\
643 chandransh 14
    change_item_status, add_address_to_cart, commit_cart,\
691 chandransh 15
    validate_cart, merge_cart, delete_item_from_cart, check_out, reset_cart
594 rajveer 16
from shop2020.model.v1.user.impl.Converters import to_t_user, to_t_user_state, to_t_cart, to_t_address
576 chandransh 17
 
557 chandransh 18
from shop2020.thriftpy.model.v1.user.ttypes import ShoppingCartException
130 ashish 19
 
557 chandransh 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,\
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
25
 
130 ashish 26
class UserContextServiceHandler:
27
 
404 rajveer 28
 
29
    def __init__(self):
30
        initialize()
557 chandransh 31
 
130 ashish 32
    """
557 chandransh 33
    service
130 ashish 34
    """
557 chandransh 35
    def createAnonymousUser(self, jsessionId):
36
        cart = create_cart(-1)
37
        return to_t_user(create_anonymous_user(jsessionId, cart))
38
 
39
    def getUserById(self, userId):
130 ashish 40
        """
41
        Parameters:
557 chandransh 42
        - userId
130 ashish 43
        """
557 chandransh 44
        return to_t_user(get_user_by_id(userId))
130 ashish 45
 
557 chandransh 46
    def createUser(self, user):
130 ashish 47
        """
48
        Parameters:
557 chandransh 49
        - user
130 ashish 50
        """
557 chandransh 51
        cart = create_cart(-1)
52
        return to_t_user(create_user(user, cart))
130 ashish 53
 
557 chandransh 54
    def updateUser(self, user):
130 ashish 55
        """
56
        Parameters:
557 chandransh 57
        - user
130 ashish 58
        """
557 chandransh 59
        return to_t_user(update_user(user))
130 ashish 60
 
557 chandransh 61
    def deleteUser(self, userId):
130 ashish 62
        """
63
        Parameters:
557 chandransh 64
        - userId
130 ashish 65
        """
557 chandransh 66
        return delete_user(userId)
67
 
68
    def getUserState(self, userId):
69
        """
70
        Parameters:
71
        - userId
72
        """
130 ashish 73
        log_entry(self, "get State")
557 chandransh 74
        return to_t_user_state(get_user_state(userId))
130 ashish 75
 
557 chandransh 76
    def authenticateUser(self, email, password):
130 ashish 77
        """
78
        Parameters:
557 chandransh 79
        - email
80
        - password
81
        """
82
        log_entry(self, "authenticate user")
83
        return to_t_user(authenticate_user(email, password))
84
 
85
    def userExists(self, email):
86
        """
87
        Parameters:
88
        - email
89
        """
90
        log_entry(self, "user exists")
91
        return user_exists(email)
92
 
567 rajveer 93
    def addAddressForUser(self, userId, address, setDefault):
557 chandransh 94
        """
95
        Parameters:
96
        - userId
97
        - address
98
        - setDefault
99
        """
100
        log_entry(self, "add address for user")
567 rajveer 101
        return add_address_for_user(address, userId, setDefault)
557 chandransh 102
 
103
    def removeAddressForUser(self, userid, addressId):
104
        """
105
        Parameters:
106
        - userid
107
        - addressId
108
        """
109
        log_entry(self, "removing address")
110
        return remove_address_for_user(userid, addressId)
111
 
112
    def setUserAsLoggedIn(self, userId, timestamp):
113
        """
114
        Parameters:
115
        - userId
116
        - timestamp
117
        """
118
        log_entry(self, "set_user_as_logged_in")
119
        return set_user_as_logged_in(userId, timestamp)
120
 
121
    def setUserAsLoggedOut(self, userId, timestamp):
122
        """
123
        Parameters:
124
        - userId
125
        - timestamp
126
        """
127
        log_entry(self, "set user as logged out")
128
        return set_user_as_logged_out(userId, timestamp)
129
 
130
    def setDefaultAddress(self, userId, addressId):
131
        """
132
        Parameters:
133
        - userId
134
        - addressId
135
        """
136
        return set_default_address(userId, addressId)
594 rajveer 137
    def updatePassword(self, userid, oldPassword, newPassword):
557 chandransh 138
        """
139
        Parameters:
594 rajveer 140
         - userid
141
         - oldPassword
142
         - newPassword
557 chandransh 143
        """
144
        log_entry(self, "updating password")
594 rajveer 145
        return update_password(userid, oldPassword, newPassword)
557 chandransh 146
 
581 rajveer 147
    def forgotPassword(self, email):
148
        """
149
        Parameters:
150
         - email
151
        """
152
        log_entry(self, "forgot password")
153
        return forgot_password(email)
557 chandransh 154
 
594 rajveer 155
    def getAllAddressesForUser(self, userId):
156
        """
157
        Parameters:
158
         - userId
159
        """
160
        addresses = get_all_addresses_for_user(userId)
161
 
162
        t_addresses = list()
163
        for address in addresses:
164
            t_addresses.append(to_t_address(address))
165
        return t_addresses
166
 
167
    def getDefaultAddressId(self, userId):
168
        """
169
        Parameters:
170
         - userId
171
        """
172
        return get_default_addredd_id(userId)
173
 
557 chandransh 174
    def createCart(self, userId):
175
        """
176
        Parameters:
177
        - userId
178
        """
179
        return create_cart(userId).id
180
 
181
    def getCurrentCart(self, userId):
182
        """
183
        Parameters:
130 ashish 184
         - userId
185
        """
557 chandransh 186
        cart = get_cart(userId)
187
        if cart:
188
            return to_t_cart(cart)
189
        else:
190
            raise ShoppingCartException(101, "not cart attached to this user")
191
 
192
    def getCart(self, cartId):
193
        """
194
        Parameters:
195
         - cartId
196
        """
197
        cart = get_cart_by_id(cartId)
198
        if cart:
199
            return to_t_cart(cart)
200
        else:
201
            raise ShoppingCartException(101, "not cart attached to this id")
130 ashish 202
 
557 chandransh 203
    def getCartsForUser(self, userId, status):
130 ashish 204
        """
205
        Parameters:
206
         - userId
557 chandransh 207
         - status
130 ashish 208
        """
557 chandransh 209
        if not userId:
210
            raise ShoppingCartException(101, "user_id is not provided")
211
 
212
        carts = get_cart_by_user_id_and_status(userId, status)
213
        t_carts = []
214
        if carts:
215
            for cart in carts:
216
                t_carts.append(to_t_cart(cart))
217
        return t_carts
130 ashish 218
 
557 chandransh 219
    def getCartsByStatus(self, status):
130 ashish 220
        """
221
        Parameters:
557 chandransh 222
         - status
130 ashish 223
        """
557 chandransh 224
        carts = get_carts_by_status(status)
225
        t_carts = []
226
        if carts:
227
            for cart in carts:
228
                t_carts.append(to_t_cart(cart))
229
        return t_carts
130 ashish 230
 
557 chandransh 231
    def getCartsByTime(self, from_time, to_time, status):
130 ashish 232
        """
233
        Parameters:
557 chandransh 234
         - from_time
235
         - to_time
236
         - status
130 ashish 237
        """
557 chandransh 238
        carts = get_carts_between(from_time, to_time, status)
239
        t_carts = []
240
        if carts:
241
            for cart in carts:
242
                t_carts.append(to_t_cart(cart))
243
        return t_carts
130 ashish 244
 
557 chandransh 245
    def changeCartStatus(self, cartId, status):
130 ashish 246
        """
247
        Parameters:
557 chandransh 248
         - cartId
249
         - status
130 ashish 250
        """
557 chandransh 251
        change_cart_status(cartId, status)
130 ashish 252
 
557 chandransh 253
    def addItemToCart(self, cartId, itemId, quantity):
130 ashish 254
        """
255
        Parameters:
557 chandransh 256
         - cartId
257
         - itemId
258
         - quantity
130 ashish 259
        """
643 chandransh 260
        add_item_to_cart(cartId, itemId, quantity)
130 ashish 261
 
557 chandransh 262
    def deleteItemFromCart(self, cartId, itemId):
130 ashish 263
        """
264
        Parameters:
557 chandransh 265
         - cartId
266
         - itemId
130 ashish 267
        """
557 chandransh 268
        delete_item_from_cart(cartId, itemId)
269
 
270
    def changeQuantity(self, cartId, itemId, quantity):
271
        """
272
        Parameters:
273
         - cartId
274
         - itemId
275
         - quantity
276
        """
643 chandransh 277
        add_item_to_cart(cartId, itemId, quantity)
130 ashish 278
 
557 chandransh 279
    def changeItemStatus(self, cartId, itemId, status):
130 ashish 280
        """
281
        Parameters:
557 chandransh 282
         - cartId
283
         - itemId
284
        """
285
        return change_item_status(cartId, itemId, status)
286
 
287
    def addAddressToCart(self, cartId, addressId):
288
        """
289
        Parameters:
290
         - cartId
130 ashish 291
         - addressId
292
        """
557 chandransh 293
        return add_address_to_cart(cartId, addressId)
130 ashish 294
 
690 chandransh 295
    def createOrders(self, cartId):
130 ashish 296
        """
297
        Parameters:
557 chandransh 298
         - cartId
130 ashish 299
        """
576 chandransh 300
        return commit_cart(cartId)
130 ashish 301
 
557 chandransh 302
    def validateCart(self, cartId):
130 ashish 303
        """
304
        Parameters:
557 chandransh 305
         - cartId
130 ashish 306
        """
576 chandransh 307
        return validate_cart(cartId)
130 ashish 308
 
557 chandransh 309
    def mergeCart(self, fromCartId, toCartId):
513 rajveer 310
        """
311
        Parameters:
557 chandransh 312
         - fromCartId
313
         - toCartId
513 rajveer 314
        """
557 chandransh 315
        merge_cart(fromCartId, toCartId)
513 rajveer 316
 
691 chandransh 317
    def checkOut(self, cartId):
318
        """
319
        Sets the checkedOutOn timestamp of the cart. Raises an exception if the specified cart can't be found.
320
 
321
        Parameters:
322
         - cartId
323
        """
324
        return check_out(cartId)
325
 
326
    def resetCart(self, cartId, items):
327
        """
328
        The second parameter is a map of item ids and their quantities which have been successfully processed.
329
        This methods removes the specified quantiry of the specified item from the cart.
330
 
331
        Parameters:
332
         - cartId
333
         - items
334
        """
335
        return reset_cart(cartId, items)
336
 
557 chandransh 337
    def addWidget(self, widget):
130 ashish 338
        """
339
        Parameters:
557 chandransh 340
         - widget
130 ashish 341
        """
557 chandransh 342
        add_widget(widget)
130 ashish 343
 
557 chandransh 344
    def addItemToWidget(self, widget_id, items):
130 ashish 345
        """
346
        Parameters:
557 chandransh 347
         - widget_id
348
         - items
130 ashish 349
        """
557 chandransh 350
        add_items_to_widget(widget_id, items)
130 ashish 351
 
557 chandransh 352
    def updateMyResearch(self, user_id, item_id):
130 ashish 353
        """
354
        Parameters:
557 chandransh 355
         - user_id
356
         - item_id
130 ashish 357
        """
557 chandransh 358
        return update_my_research(user_id, item_id)
130 ashish 359
 
557 chandransh 360
    def updateWidget(self, widgetId, enable):
130 ashish 361
        """
362
        Parameters:
557 chandransh 363
         - widgetId
364
         - enable
130 ashish 365
        """
557 chandransh 366
        update_widget(widgetId, enable)
130 ashish 367
 
557 chandransh 368
    def updateWidgetItem(self, widgetId, enable):
130 ashish 369
        """
370
        Parameters:
557 chandransh 371
         - widgetId
372
         - enable
130 ashish 373
        """
557 chandransh 374
        update_widget(widgetId, enable)
375
 
376
    def deleteItemFromWidget(self, widget_id, item_id):
377
        """
378
        Parameters:
379
         - widget_id
380
         - item_id
381
        """
382
        return delete_item_from_widget(widget_id, item_id)
130 ashish 383
 
557 chandransh 384
    def deleteItemFromMyResearch(self, user_id, item_id):
130 ashish 385
        """
386
        Parameters:
557 chandransh 387
         - user_id
388
         - item_id
130 ashish 389
        """
557 chandransh 390
        return delete_item_from_my_research(user_id, item_id)
130 ashish 391
 
557 chandransh 392
    def getWidget(self, type, userId, onlyEnabled):
130 ashish 393
        """
394
        Parameters:
557 chandransh 395
         - type
396
         - userId
397
         - onlyEnabled
130 ashish 398
        """
557 chandransh 399
        widget = get_widget_by_type(type, userId, onlyEnabled)
400
        return to_t_widget(widget)
130 ashish 401
 
557 chandransh 402
    def getMyResearch(self, user_id):
130 ashish 403
        """
404
        Parameters:
557 chandransh 405
         - user_id
130 ashish 406
        """
557 chandransh 407
        return to_t_widget(get_my_research(user_id))
130 ashish 408
 
557 chandransh 409
    def updateRatings(self, item_id, type, rating, user_id):
130 ashish 410
        """
411
        Parameters:
557 chandransh 412
        - item_id
413
        - type
414
        - rating
415
        - user_id
130 ashish 416
        """
557 chandransh 417
        update_ratings(user_id, item_id, rating, type)
418
 
419
 
420
    def getRatings(self, item_id, user_id):
421
        """
422
        Parameters:
423
        - item_id
424
        - user_id
425
        """
426
        all_ratings, user_ratings = get_ratings(user_id, item_id)
427
        return to_t_ratings(user_ratings, all_ratings, item_id, user_id)
428
 
429
    def updateBrowseHistory(self, user_id, item_id, is_session_id):
430
        """
431
        Parameters: 
432
        - user_id
433
        - item_id
434
        - isSessionId
435
        """
436
        update_browse_history(user_id, item_id, is_session_id)
437
 
438
    def getBrowseHistory(self, user_id, is_session_id):
439
        """
440
        Parameters:
441
         - user_id
442
         - is_session_id
443
        """
444
        return to_t_widget(get_browse_history(user_id, is_session_id))