Subversion Repositories SmartDukaan

Rev

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