Subversion Repositories SmartDukaan

Rev

Rev 1491 | Rev 1596 | 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
1491 vikas 7
from shop2020.model.v1.user.impl.UserDataAccessors import create_user, update_user, get_user_by_id, get_user_by_email, update_password,\
557 chandransh 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,\
1273 varun.gupt 10
    create_anonymous_user, forgot_password, get_all_addresses_for_user, get_default_address_id, get_default_pincode,\
1583 varun.gupt 11
    create_user_communication, get_user_communication_by_id, get_user_communication_by_user, get_all_user_communications
557 chandransh 12
from shop2020.model.v1.user.impl.CartDataAccessors import create_cart, get_cart,\
13
    get_cart_by_id, get_cart_by_user_id_and_status, get_carts_by_status,\
14
    get_carts_between, change_cart_status, add_item_to_cart,\
643 chandransh 15
    change_item_status, add_address_to_cart, commit_cart,\
691 chandransh 16
    validate_cart, merge_cart, delete_item_from_cart, check_out, reset_cart
1583 varun.gupt 17
from shop2020.model.v1.user.impl.Converters import to_t_user, to_t_user_state, to_t_cart, to_t_address, to_t_user_communication
576 chandransh 18
 
557 chandransh 19
from shop2020.thriftpy.model.v1.user.ttypes import ShoppingCartException
130 ashish 20
 
772 rajveer 21
from shop2020.model.v1.user.impl.WidgetDataAccessor import update_my_research, get_browse_history,\
22
    update_browse_history, delete_item_from_my_research, merge_browse_history, get_my_research
23
from shop2020.model.v1.user.impl.WidgetConverters import to_t_widget
766 rajveer 24
from shop2020.model.v1.user.impl import UserDataAccessors, CartDataAccessors,\
25
    WidgetDataAccessor
557 chandransh 26
 
130 ashish 27
class UserContextServiceHandler:
28
 
404 rajveer 29
 
1249 chandransh 30
    def __init__(self, dbname='user'):
31
        initialize(dbname)
557 chandransh 32
 
130 ashish 33
    """
557 chandransh 34
    service
130 ashish 35
    """
557 chandransh 36
    def createAnonymousUser(self, jsessionId):
766 rajveer 37
        try:
38
            cart = create_cart(-1)
39
            return to_t_user(create_anonymous_user(jsessionId, cart))
40
        finally:
41
            UserDataAccessors.close_session()
557 chandransh 42
    def getUserById(self, userId):
130 ashish 43
        """
44
        Parameters:
557 chandransh 45
        - userId
130 ashish 46
        """
766 rajveer 47
        try:
48
            return to_t_user(get_user_by_id(userId))
49
        finally:
50
            UserDataAccessors.close_session()
1491 vikas 51
 
52
    def getUserByEmail(self, email):
53
        """
54
        Parameters:
55
         - email
56
        """
57
        try:
58
            return to_t_user(get_user_by_email(email))
59
        finally:
60
            UserDataAccessors.close_session()
61
 
62
 
557 chandransh 63
    def createUser(self, user):
130 ashish 64
        """
65
        Parameters:
557 chandransh 66
        - user
130 ashish 67
        """
766 rajveer 68
        try:
69
            cart = create_cart(-1)
70
            return to_t_user(create_user(user, cart))
71
        finally:
72
            UserDataAccessors.close_session()
73
 
557 chandransh 74
    def updateUser(self, user):
130 ashish 75
        """
76
        Parameters:
557 chandransh 77
        - user
130 ashish 78
        """
766 rajveer 79
        try:
80
            return to_t_user(update_user(user))
81
        finally:
82
            UserDataAccessors.close_session()
83
 
557 chandransh 84
    def deleteUser(self, userId):
130 ashish 85
        """
86
        Parameters:
557 chandransh 87
        - userId
130 ashish 88
        """
766 rajveer 89
        try:
90
            return delete_user(userId)
91
        finally:
92
            UserDataAccessors.close_session()
93
 
557 chandransh 94
    def getUserState(self, userId):
95
        """
96
        Parameters:
97
        - userId
98
        """
130 ashish 99
        log_entry(self, "get State")
766 rajveer 100
        try:
101
            return to_t_user_state(get_user_state(userId))
102
        finally:
103
            UserDataAccessors.close_session()
104
 
557 chandransh 105
    def authenticateUser(self, email, password):
130 ashish 106
        """
107
        Parameters:
557 chandransh 108
        - email
109
        - password
110
        """
111
        log_entry(self, "authenticate user")
766 rajveer 112
        try:
113
            return to_t_user(authenticate_user(email, password))
114
        finally:
115
            UserDataAccessors.close_session()
116
 
557 chandransh 117
    def userExists(self, email):
118
        """
119
        Parameters:
120
        - email
121
        """
122
        log_entry(self, "user exists")
766 rajveer 123
        try:
124
            return user_exists(email)
125
        finally:
126
            UserDataAccessors.close_session()
127
 
567 rajveer 128
    def addAddressForUser(self, userId, address, setDefault):
557 chandransh 129
        """
130
        Parameters:
131
        - userId
132
        - address
133
        - setDefault
134
        """
135
        log_entry(self, "add address for user")
766 rajveer 136
        try:
137
            return add_address_for_user(address, userId, setDefault)
138
        finally:
139
            UserDataAccessors.close_session()
140
 
557 chandransh 141
    def removeAddressForUser(self, userid, addressId):
142
        """
143
        Parameters:
144
        - userid
145
        - addressId
146
        """
147
        log_entry(self, "removing address")
766 rajveer 148
        try:
149
            return remove_address_for_user(userid, addressId)
150
        finally:
151
            UserDataAccessors.close_session()
152
 
557 chandransh 153
    def setUserAsLoggedIn(self, userId, timestamp):
154
        """
155
        Parameters:
156
        - userId
157
        - timestamp
158
        """
159
        log_entry(self, "set_user_as_logged_in")
766 rajveer 160
        try:
161
            return set_user_as_logged_in(userId, timestamp)
162
        finally:
163
            UserDataAccessors.close_session()
164
 
557 chandransh 165
    def setUserAsLoggedOut(self, userId, timestamp):
166
        """
167
        Parameters:
168
        - userId
169
        - timestamp
170
        """
171
        log_entry(self, "set user as logged out")
766 rajveer 172
        try:
173
            return set_user_as_logged_out(userId, timestamp)
174
        finally:
175
            UserDataAccessors.close_session()
176
 
557 chandransh 177
    def setDefaultAddress(self, userId, addressId):
178
        """
179
        Parameters:
180
        - userId
181
        - addressId
182
        """
766 rajveer 183
        try:
184
            return set_default_address(userId, addressId)
185
        finally:
186
            UserDataAccessors.close_session()
187
 
594 rajveer 188
    def updatePassword(self, userid, oldPassword, newPassword):
557 chandransh 189
        """
190
        Parameters:
594 rajveer 191
         - userid
192
         - oldPassword
193
         - newPassword
557 chandransh 194
        """
195
        log_entry(self, "updating password")
766 rajveer 196
        try:
197
            return update_password(userid, oldPassword, newPassword)
198
        finally:
199
            UserDataAccessors.close_session()
200
 
884 rajveer 201
    def forgotPassword(self, email, password):
581 rajveer 202
        """
203
        Parameters:
204
         - email
205
        """
206
        log_entry(self, "forgot password")
766 rajveer 207
        try:
884 rajveer 208
            return forgot_password(email, password)
766 rajveer 209
        finally:
210
            UserDataAccessors.close_session()
211
 
594 rajveer 212
    def getAllAddressesForUser(self, userId):
213
        """
214
        Parameters:
215
         - userId
216
        """
766 rajveer 217
        try:
218
            addresses = get_all_addresses_for_user(userId)
219
 
220
            t_addresses = list()
221
            for address in addresses:
222
                t_addresses.append(to_t_address(address))
223
            return t_addresses
224
        finally:
225
            UserDataAccessors.close_session()
594 rajveer 226
 
227
    def getDefaultAddressId(self, userId):
228
        """
229
        Parameters:
230
         - userId
231
        """
766 rajveer 232
        try:
785 rajveer 233
            return get_default_address_id(userId)
766 rajveer 234
        finally:
235
            UserDataAccessors.close_session()
785 rajveer 236
 
237
    def getDefaultPincode(self, userId):
238
        """
239
        Parameters:
240
         - userId
241
        """
242
        try:
243
            return get_default_pincode(userId)
244
        finally:
245
            UserDataAccessors.close_session()
246
 
557 chandransh 247
    def createCart(self, userId):
248
        """
249
        Parameters:
250
        - userId
251
        """
766 rajveer 252
        try:
253
            return create_cart(userId).id
254
        finally:
255
            UserDataAccessors.close_session()
256
 
557 chandransh 257
    def getCurrentCart(self, userId):
258
        """
259
        Parameters:
130 ashish 260
         - userId
261
        """
766 rajveer 262
        try:
263
            cart = get_cart(userId)
264
            if cart:
265
                return to_t_cart(cart)
266
            else:
267
                raise ShoppingCartException(101, "not cart attached to this user")
268
        finally:
269
            CartDataAccessors.close_session()
270
 
557 chandransh 271
    def getCart(self, cartId):
272
        """
273
        Parameters:
274
         - cartId
275
        """
766 rajveer 276
        try:
277
            cart = get_cart_by_id(cartId)
278
            if cart:
279
                return to_t_cart(cart)
280
            else:
281
                raise ShoppingCartException(101, "not cart attached to this id")
282
        finally:
283
            CartDataAccessors.close_session()
284
 
557 chandransh 285
    def getCartsForUser(self, userId, status):
130 ashish 286
        """
287
        Parameters:
288
         - userId
557 chandransh 289
         - status
130 ashish 290
        """
766 rajveer 291
        try:
292
            if not userId:
293
                raise ShoppingCartException(101, "user_id is not provided")
294
 
295
            carts = get_cart_by_user_id_and_status(userId, status)
296
            t_carts = []
297
            if carts:
298
                for cart in carts:
299
                    t_carts.append(to_t_cart(cart))
300
            return t_carts
301
        finally:
302
            CartDataAccessors.close_session()
303
 
557 chandransh 304
    def getCartsByStatus(self, status):
130 ashish 305
        """
306
        Parameters:
557 chandransh 307
         - status
130 ashish 308
        """
766 rajveer 309
        try:
310
            carts = get_carts_by_status(status)
311
            t_carts = []
312
            if carts:
313
                for cart in carts:
314
                    t_carts.append(to_t_cart(cart))
315
            return t_carts
316
        finally:
317
            CartDataAccessors.close_session()
318
 
557 chandransh 319
    def getCartsByTime(self, from_time, to_time, status):
130 ashish 320
        """
321
        Parameters:
557 chandransh 322
         - from_time
323
         - to_time
324
         - status
130 ashish 325
        """
766 rajveer 326
        try:
327
            carts = get_carts_between(from_time, to_time, status)
328
            t_carts = []
329
            if carts:
330
                for cart in carts:
331
                    t_carts.append(to_t_cart(cart))
332
            return t_carts
333
        finally:
334
            CartDataAccessors.close_session()
335
 
557 chandransh 336
    def changeCartStatus(self, cartId, status):
130 ashish 337
        """
338
        Parameters:
557 chandransh 339
         - cartId
340
         - status
130 ashish 341
        """
766 rajveer 342
        try:
343
            change_cart_status(cartId, status)
344
        finally:
345
            CartDataAccessors.close_session()
346
 
557 chandransh 347
    def addItemToCart(self, cartId, itemId, quantity):
130 ashish 348
        """
349
        Parameters:
557 chandransh 350
         - cartId
351
         - itemId
352
         - quantity
130 ashish 353
        """
766 rajveer 354
        try:
355
            add_item_to_cart(cartId, itemId, quantity)
356
        finally:
357
            CartDataAccessors.close_session()
358
 
557 chandransh 359
    def deleteItemFromCart(self, cartId, itemId):
130 ashish 360
        """
361
        Parameters:
557 chandransh 362
         - cartId
363
         - itemId
130 ashish 364
        """
766 rajveer 365
        try:
366
            delete_item_from_cart(cartId, itemId)
367
        finally:
368
            CartDataAccessors.close_session()
369
 
557 chandransh 370
    def changeQuantity(self, cartId, itemId, quantity):
371
        """
372
        Parameters:
373
         - cartId
374
         - itemId
375
         - quantity
376
        """
766 rajveer 377
        try:
378
            add_item_to_cart(cartId, itemId, quantity)
379
        finally:
380
            CartDataAccessors.close_session()
381
 
557 chandransh 382
    def changeItemStatus(self, cartId, itemId, status):
130 ashish 383
        """
384
        Parameters:
557 chandransh 385
         - cartId
386
         - itemId
387
        """
766 rajveer 388
        try:
389
            return change_item_status(cartId, itemId, status)
390
        finally:
391
            CartDataAccessors.close_session()
392
 
557 chandransh 393
    def addAddressToCart(self, cartId, addressId):
394
        """
395
        Parameters:
396
         - cartId
130 ashish 397
         - addressId
398
        """
766 rajveer 399
        try:
400
            return add_address_to_cart(cartId, addressId)
401
        finally:
402
            CartDataAccessors.close_session()
403
 
690 chandransh 404
    def createOrders(self, cartId):
130 ashish 405
        """
406
        Parameters:
557 chandransh 407
         - cartId
130 ashish 408
        """
766 rajveer 409
        try:
410
            return commit_cart(cartId)
411
        finally:
412
            CartDataAccessors.close_session()
1466 ankur.sing 413
 
557 chandransh 414
    def validateCart(self, cartId):
130 ashish 415
        """
1466 ankur.sing 416
        Validates that:
417
        1. The checkout timestamp is greater than the updatedOn timestamp.
418
        2. All of the lines in the cart are active items.
419
        3. The estimate for any of the lines in cart doesn't change.
420
        If all three are true, returns empty string; else returns appropriate message.
421
 
130 ashish 422
        Parameters:
557 chandransh 423
         - cartId
130 ashish 424
        """
766 rajveer 425
        try:
426
            return validate_cart(cartId)
427
        finally:
428
            CartDataAccessors.close_session()
429
 
557 chandransh 430
    def mergeCart(self, fromCartId, toCartId):
513 rajveer 431
        """
432
        Parameters:
557 chandransh 433
         - fromCartId
434
         - toCartId
513 rajveer 435
        """
766 rajveer 436
        try:
437
            merge_cart(fromCartId, toCartId)
438
        finally:
439
            CartDataAccessors.close_session()
440
 
691 chandransh 441
    def checkOut(self, cartId):
442
        """
443
        Sets the checkedOutOn timestamp of the cart. Raises an exception if the specified cart can't be found.
444
 
445
        Parameters:
446
         - cartId
447
        """
766 rajveer 448
        try:
449
            return check_out(cartId)
450
        finally:
451
            CartDataAccessors.close_session()
452
 
691 chandransh 453
    def resetCart(self, cartId, items):
454
        """
455
        The second parameter is a map of item ids and their quantities which have been successfully processed.
456
        This methods removes the specified quantiry of the specified item from the cart.
457
 
458
        Parameters:
459
         - cartId
460
         - items
461
        """
766 rajveer 462
        try:
463
            return reset_cart(cartId, items)
464
        finally:
465
            CartDataAccessors.close_session()
772 rajveer 466
 
467
    #Widget related methods
468
    def getMyResearch(self, userId):
469
        """
470
        Parameters:
471
         - user_id
472
        """
473
        try:
474
            return to_t_widget(get_my_research(userId))
475
        finally:
476
            WidgetDataAccessor.close_session()
477
 
478
    def updateMyResearch(self, userId, itemId):
479
        """
480
        Parameters:
481
         - user_id
482
         - item_id
483
        """
484
        try:
485
            return update_my_research(userId, itemId)
486
        finally:
487
            WidgetDataAccessor.close_session()
488
 
489
    def deleteItemFromMyResearch(self, userId, itemId):
490
        """
491
        Parameters:
492
         - user_id
493
         - item_id
494
        """
495
        try:
496
            return delete_item_from_my_research(userId, itemId)
497
        finally:
498
            WidgetDataAccessor.close_session()
766 rajveer 499
 
772 rajveer 500
    def updateBrowseHistory(self, userId, itemId):
501
        """
502
        Parameters: 
503
        - user_id
504
        - item_id
505
        """
506
        try:
507
            update_browse_history(userId, itemId)
508
        finally:
509
            WidgetDataAccessor.close_session()
510
 
511
    def getBrowseHistory(self, userId):
512
        """
513
        Parameters:
514
         - user_id
515
        """
516
        try:
517
            return to_t_widget(get_browse_history(userId))
518
        finally:
519
            WidgetDataAccessor.close_session()
520
 
521
    def mergeBrowseHistory(self, fromUserId, toUserId):
522
        """
523
        Parameters:
524
         - user_id
525
        """
526
        try:
527
            return merge_browse_history(fromUserId, toUserId)
528
        finally:
529
            WidgetDataAccessor.close_session()
530
 
1273 varun.gupt 531
    def saveUserCommunication(self, userId, email, communicationType, orderId, awb, product, subject, message):
532
        """
533
        Parameters:
534
         - userId
535
         - email
536
         - communicationType
537
         - orderId
538
         - awb
539
         - product
540
         - subject
541
         - message
542
        """
543
        try:
544
            return create_user_communication(userId, email, communicationType, orderId, awb, product, subject, message)
545
        finally:
546
            WidgetDataAccessor.close_session()
547
 
1583 varun.gupt 548
    def getUserCommunicationById(self, id):
549
        """
550
        Parameters:
551
         - id
552
        """
553
        try:
554
            return to_t_user_communication(get_user_communication_by_id(id))
555
        finally:
556
            WidgetDataAccessor.close_session()
557
 
558
    def getUserCommunicationByUser(self, userId):
559
        """
560
        Parameters:
561
         - userId
562
        """
563
        try:
564
            return [to_t_user_communication(user_communication) for user_communication in get_user_communication_by_user(userId)]
565
        finally:
566
            WidgetDataAccessor.close_session()
567
 
568
    def getAllUserCommunications(self):
569
        try:
570
            return [to_t_user_communication(user_communication) for user_communication in get_all_user_communications()]
571
        finally:
572
            WidgetDataAccessor.close_session()
573
 
772 rajveer 574
    def closeSession(self, ):
575
        CartDataAccessors.close_session()
576
        UserDataAccessors.close_session()
577
        WidgetDataAccessor.close_session()
578
 
579
    '''        
557 chandransh 580
    def addWidget(self, widget):
130 ashish 581
        """
582
        Parameters:
557 chandransh 583
         - widget
130 ashish 584
        """
766 rajveer 585
        try:
586
            add_widget(widget)
587
        finally:
588
            WidgetDataAccessor.close_session()
130 ashish 589
 
557 chandransh 590
    def addItemToWidget(self, widget_id, items):
130 ashish 591
        """
592
        Parameters:
557 chandransh 593
         - widget_id
594
         - items
130 ashish 595
        """
766 rajveer 596
        try:
597
            add_items_to_widget(widget_id, items)
598
        finally:
599
            WidgetDataAccessor.close_session()
600
 
557 chandransh 601
    def updateWidget(self, widgetId, enable):
130 ashish 602
        """
603
        Parameters:
557 chandransh 604
         - widgetId
605
         - enable
130 ashish 606
        """
766 rajveer 607
        try:
608
            update_widget(widgetId, enable)
609
        finally:
610
            WidgetDataAccessor.close_session()
611
 
557 chandransh 612
    def updateWidgetItem(self, widgetId, enable):
130 ashish 613
        """
614
        Parameters:
557 chandransh 615
         - widgetId
616
         - enable
130 ashish 617
        """
766 rajveer 618
        try:
619
            update_widget(widgetId, enable)
620
        finally:
621
            WidgetDataAccessor.close_session()
622
 
557 chandransh 623
    def deleteItemFromWidget(self, widget_id, item_id):
624
        """
625
        Parameters:
626
         - widget_id
627
         - item_id
628
        """
766 rajveer 629
        try:
630
            return delete_item_from_widget(widget_id, item_id)
631
        finally:
632
            WidgetDataAccessor.close_session()
633
 
557 chandransh 634
    def getWidget(self, type, userId, onlyEnabled):
130 ashish 635
        """
636
        Parameters:
557 chandransh 637
         - type
638
         - userId
639
         - onlyEnabled
130 ashish 640
        """
766 rajveer 641
        try:
642
            widget = get_widget_by_type(type, userId, onlyEnabled)
643
            return to_t_widget(widget)
644
        finally:
645
            WidgetDataAccessor.close_session()
646
 
557 chandransh 647
    def updateRatings(self, item_id, type, rating, user_id):
130 ashish 648
        """
649
        Parameters:
557 chandransh 650
        - item_id
651
        - type
652
        - rating
653
        - user_id
130 ashish 654
        """
766 rajveer 655
        try:
656
            update_ratings(user_id, item_id, rating, type)
657
        finally:
658
            WidgetDataAccessor.close_session()
557 chandransh 659
 
660
    def getRatings(self, item_id, user_id):
661
        """
662
        Parameters:
663
        - item_id
664
        - user_id
665
        """
766 rajveer 666
        try:
667
            all_ratings, user_ratings = get_ratings(user_id, item_id)
668
            return to_t_ratings(user_ratings, all_ratings, item_id, user_id)
669
        finally:
670
            WidgetDataAccessor.close_session()
671
 
772 rajveer 672
    '''
673