Subversion Repositories SmartDukaan

Rev

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