Subversion Repositories SmartDukaan

Rev

Rev 515 | Rev 563 | 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
25
 
130 ashish 26
class UserContextServiceHandler:
27
 
404 rajveer 28
 
29
    def __init__(self):
30
        initialize()
557 chandransh 31
        self.transaction_client = TransactionClient()
32
 
130 ashish 33
    """
557 chandransh 34
    service
130 ashish 35
    """
557 chandransh 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):
130 ashish 41
        """
42
        Parameters:
557 chandransh 43
        - userId
130 ashish 44
        """
557 chandransh 45
        return to_t_user(get_user_by_id(userId))
130 ashish 46
 
557 chandransh 47
    def createUser(self, user):
130 ashish 48
        """
49
        Parameters:
557 chandransh 50
        - user
130 ashish 51
        """
557 chandransh 52
        cart = create_cart(-1)
53
        return to_t_user(create_user(user, cart))
130 ashish 54
 
557 chandransh 55
    def updateUser(self, user):
130 ashish 56
        """
57
        Parameters:
557 chandransh 58
        - user
130 ashish 59
        """
557 chandransh 60
        return to_t_user(update_user(user))
130 ashish 61
 
557 chandransh 62
    def deleteUser(self, userId):
130 ashish 63
        """
64
        Parameters:
557 chandransh 65
        - userId
130 ashish 66
        """
557 chandransh 67
        return delete_user(userId)
68
 
69
    def getUserState(self, userId):
70
        """
71
        Parameters:
72
        - userId
73
        """
130 ashish 74
        log_entry(self, "get State")
557 chandransh 75
        return to_t_user_state(get_user_state(userId))
130 ashish 76
 
557 chandransh 77
    def authenticateUser(self, email, password):
130 ashish 78
        """
79
        Parameters:
557 chandransh 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
128
        """
129
        log_entry(self, "set user as logged out")
130
        return set_user_as_logged_out(userId, timestamp)
131
 
132
    def setDefaultAddress(self, userId, addressId):
133
        """
134
        Parameters:
135
        - userId
136
        - addressId
137
        """
138
        return set_default_address(userId, addressId)
139
 
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):
158
        """
159
        Parameters:
130 ashish 160
         - userId
161
        """
557 chandransh 162
        cart = get_cart(userId)
163
        if cart:
164
            return to_t_cart(cart)
165
        else:
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)
176
        else:
177
            raise ShoppingCartException(101, "not cart attached to this id")
130 ashish 178
 
557 chandransh 179
    def getCartsForUser(self, userId, status):
130 ashish 180
        """
181
        Parameters:
182
         - userId
557 chandransh 183
         - status
130 ashish 184
        """
557 chandransh 185
        if not userId:
186
            raise ShoppingCartException(101, "user_id is not provided")
187
 
188
        carts = get_cart_by_user_id_and_status(userId, status)
189
        t_carts = []
190
        if carts:
191
            for cart in carts:
192
                t_carts.append(to_t_cart(cart))
193
        return t_carts
130 ashish 194
 
557 chandransh 195
    def getCartsByStatus(self, status):
130 ashish 196
        """
197
        Parameters:
557 chandransh 198
         - status
130 ashish 199
        """
557 chandransh 200
        carts = get_carts_by_status(status)
201
        t_carts = []
202
        if carts:
203
            for cart in carts:
204
                t_carts.append(to_t_cart(cart))
205
        return t_carts
130 ashish 206
 
557 chandransh 207
    def getCartsByTime(self, from_time, to_time, status):
130 ashish 208
        """
209
        Parameters:
557 chandransh 210
         - from_time
211
         - to_time
212
         - status
130 ashish 213
        """
557 chandransh 214
        carts = get_carts_between(from_time, to_time, status)
215
        t_carts = []
216
        if carts:
217
            for cart in carts:
218
                t_carts.append(to_t_cart(cart))
219
        return t_carts
130 ashish 220
 
557 chandransh 221
    def changeCartStatus(self, cartId, status):
130 ashish 222
        """
223
        Parameters:
557 chandransh 224
         - cartId
225
         - status
130 ashish 226
        """
557 chandransh 227
        change_cart_status(cartId, status)
130 ashish 228
 
557 chandransh 229
    def addItemToCart(self, cartId, itemId, quantity):
130 ashish 230
        """
231
        Parameters:
557 chandransh 232
         - cartId
233
         - itemId
234
         - quantity
130 ashish 235
        """
557 chandransh 236
        return add_item_to_cart(cartId, itemId, quantity)
130 ashish 237
 
557 chandransh 238
    def deleteItemFromCart(self, cartId, itemId):
130 ashish 239
        """
240
        Parameters:
557 chandransh 241
         - cartId
242
         - itemId
130 ashish 243
        """
557 chandransh 244
        delete_item_from_cart(cartId, itemId)
245
 
246
    def changeQuantity(self, cartId, itemId, quantity):
247
        """
248
        Parameters:
249
         - cartId
250
         - itemId
251
         - quantity
252
        """
253
        return change_item_quantity(cartId, itemId, quantity)
130 ashish 254
 
557 chandransh 255
    def changeItemStatus(self, cartId, itemId, status):
130 ashish 256
        """
257
        Parameters:
557 chandransh 258
         - cartId
259
         - itemId
260
        """
261
        return change_item_status(cartId, itemId, status)
262
 
263
    def addAddressToCart(self, cartId, addressId):
264
        """
265
        Parameters:
266
         - cartId
130 ashish 267
         - addressId
268
        """
557 chandransh 269
        return add_address_to_cart(cartId, addressId)
130 ashish 270
 
557 chandransh 271
    def commitCart(self, cartId):
130 ashish 272
        """
273
        Parameters:
557 chandransh 274
         - cartId
130 ashish 275
        """
557 chandransh 276
        return commit_cart(cartId, self.transaction_client)
130 ashish 277
 
557 chandransh 278
    def validateCart(self, cartId):
130 ashish 279
        """
280
        Parameters:
557 chandransh 281
         - cartId
130 ashish 282
        """
557 chandransh 283
        return validate_cart(cartId)
130 ashish 284
 
557 chandransh 285
    def mergeCart(self, fromCartId, toCartId):
513 rajveer 286
        """
287
        Parameters:
557 chandransh 288
         - fromCartId
289
         - toCartId
513 rajveer 290
        """
557 chandransh 291
        merge_cart(fromCartId, toCartId)
513 rajveer 292
 
557 chandransh 293
    def addWidget(self, widget):
130 ashish 294
        """
295
        Parameters:
557 chandransh 296
         - widget
130 ashish 297
        """
557 chandransh 298
        add_widget(widget)
130 ashish 299
 
557 chandransh 300
    def addItemToWidget(self, widget_id, items):
130 ashish 301
        """
302
        Parameters:
557 chandransh 303
         - widget_id
304
         - items
130 ashish 305
        """
557 chandransh 306
        add_items_to_widget(widget_id, items)
130 ashish 307
 
557 chandransh 308
    def updateMyResearch(self, user_id, item_id):
130 ashish 309
        """
310
        Parameters:
557 chandransh 311
         - user_id
312
         - item_id
130 ashish 313
        """
557 chandransh 314
        return update_my_research(user_id, item_id)
130 ashish 315
 
557 chandransh 316
    def updateWidget(self, widgetId, enable):
130 ashish 317
        """
318
        Parameters:
557 chandransh 319
         - widgetId
320
         - enable
130 ashish 321
        """
557 chandransh 322
        update_widget(widgetId, enable)
130 ashish 323
 
557 chandransh 324
    def updateWidgetItem(self, widgetId, enable):
130 ashish 325
        """
326
        Parameters:
557 chandransh 327
         - widgetId
328
         - enable
130 ashish 329
        """
557 chandransh 330
        update_widget(widgetId, enable)
331
 
332
    def deleteItemFromWidget(self, widget_id, item_id):
333
        """
334
        Parameters:
335
         - widget_id
336
         - item_id
337
        """
338
        return delete_item_from_widget(widget_id, item_id)
130 ashish 339
 
557 chandransh 340
    def deleteItemFromMyResearch(self, user_id, item_id):
130 ashish 341
        """
342
        Parameters:
557 chandransh 343
         - user_id
344
         - item_id
130 ashish 345
        """
557 chandransh 346
        return delete_item_from_my_research(user_id, item_id)
130 ashish 347
 
557 chandransh 348
    def getWidget(self, type, userId, onlyEnabled):
130 ashish 349
        """
350
        Parameters:
557 chandransh 351
         - type
352
         - userId
353
         - onlyEnabled
130 ashish 354
        """
557 chandransh 355
        widget = get_widget_by_type(type, userId, onlyEnabled)
356
        return to_t_widget(widget)
130 ashish 357
 
557 chandransh 358
    def getMyResearch(self, user_id):
130 ashish 359
        """
360
        Parameters:
557 chandransh 361
         - user_id
130 ashish 362
        """
557 chandransh 363
        return to_t_widget(get_my_research(user_id))
130 ashish 364
 
557 chandransh 365
    def updateRatings(self, item_id, type, rating, user_id):
130 ashish 366
        """
367
        Parameters:
557 chandransh 368
        - item_id
369
        - type
370
        - rating
371
        - user_id
130 ashish 372
        """
557 chandransh 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
 
394
    def getBrowseHistory(self, user_id, is_session_id):
395
        """
396
        Parameters:
397
         - user_id
398
         - is_session_id
399
        """
400
        return to_t_widget(get_browse_history(user_id, is_session_id))