Subversion Repositories SmartDukaan

Rev

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