Subversion Repositories SmartDukaan

Rev

Rev 636 | Rev 669 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 636 Rev 643
Line 33... Line 33...
33
#        raise ShoppingCartException(101, "Cart does not exist")
33
#        raise ShoppingCartException(101, "Cart does not exist")
34
    return cart
34
    return cart
35
 
35
 
36
def get_cart_by_id(id):
36
def get_cart_by_id(id):
37
    cart = Cart.get_by(id=id)
37
    cart = Cart.get_by(id=id)
38
    if cart:
-
 
39
        cart.accessed_on = datetime.datetime.now()
-
 
40
        session.commit()
-
 
41
    return cart
38
    return cart
42
 
39
 
43
def create_cart(user_id):
40
def create_cart(user_id):
44
    cart = get_cart(user_id)
41
    cart = get_cart(user_id)
45
    if not cart: 
42
    if not cart: 
Line 77... Line 74...
77
        query = query.filter(Cart.created_on <= finish_time)
74
        query = query.filter(Cart.created_on <= finish_time)
78
    
75
    
79
    carts = query.all()
76
    carts = query.all()
80
    return carts
77
    return carts
81
 
78
 
82
def get_item(item_id, cart_id, status, single):
79
def get_line(item_id, cart_id, status, single):
83
    
-
 
84
    #get cart first 
80
    #get cart first 
85
    try:
81
    try:
86
        found_cart = Cart.get_by(id=cart_id)
82
        found_cart = Cart.get_by(id=cart_id)
87
        
-
 
88
    except:
83
    except:
89
        raise ShoppingCartException(101, "cart not found ")
84
        raise ShoppingCartException(101, "cart not found ")
90
    query = Line.query.filter(Line.cart.has(id = cart_id))
-
 
91
    query = query.filter_by(item_id = item_id)
85
    query = Line.query.filter_by(cart = found_cart, item_id = item_id)
92
    
86
    
93
#    query = LineItem.query.filter_by(item_id = item_id)
-
 
94
#    query = query.filter(LineItem.cart.id == cart_id)
-
 
95
    if status:
87
    if status:
96
        query = query.filter_by(line_status = status)
88
        query = query.filter_by(line_status = status)
97
    else:
89
    else:
98
        query = query.filter_by(line_status = LineStatus.LINE_ACTIVE)
90
        query = query.filter_by(line_status = LineStatus.LINE_ACTIVE)
99
    try:
91
    try:
Line 102... Line 94...
102
        else:
94
        else:
103
            return query.all()
95
            return query.all()
104
    except:
96
    except:
105
        return None
97
        return None
106
 
98
 
107
def get_item_by_id(line_id):
-
 
108
    return Line.get_by(id=line_id)
-
 
109
 
-
 
110
def change_cart_status(cart_id, status):
99
def change_cart_status(cart_id, status):
111
    cart = get_cart_by_id(id)
100
    cart = get_cart_by_id(id)
112
    if not cart:
101
    if not cart:
113
        raise ShoppingCartException(101, "no cart attached to this id")
102
        raise ShoppingCartException(101, "no cart attached to this id")
114
    if not status:
103
    if not status:
Line 122... Line 111...
122
    if not item_id:
111
    if not item_id:
123
        raise ShoppingCartException(101, "item_id cannot be null")
112
        raise ShoppingCartException(101, "item_id cannot be null")
124
    
113
    
125
    if not quantity:
114
    if not quantity:
126
        raise ShoppingCartException(101, "quantity cannot be null")    
115
        raise ShoppingCartException(101, "quantity cannot be null")    
127
    
-
 
128
    item = get_item(item_id, cart_id, None,True)
-
 
129
    if item:
-
 
130
        #change the quantity only
-
 
131
        item.quantity = quantity
-
 
132
        session.commit()
-
 
133
        return item.id
-
 
134
    
116
 
135
    cart = get_cart_by_id(cart_id)
117
    cart = Cart.get_by(id = cart_id)
136
    if not cart:
118
    if not cart:
137
        raise ShoppingCartException(101, "no cart attached to this id")
119
        raise ShoppingCartException(101, "no cart attached to this id")
138
    
120
    
-
 
121
    current_time = datetime.datetime.now()
-
 
122
    cart.updated_on = current_time
-
 
123
    line = get_line(item_id, cart_id, None,True)
-
 
124
    if line:
-
 
125
        #change the quantity only
-
 
126
        line.quantity = quantity
-
 
127
    else:
139
    line = Line()
128
        line = Line()
-
 
129
        line.cart = cart
140
    line.item_id = item_id
130
        line.item_id = item_id
141
    line.quantity = quantity
131
        line.quantity = quantity
142
    line.created_on = datetime.datetime.now()
132
        line.created_on = current_time
143
    line.updated_on = datetime.datetime.now()
133
        line.updated_on = current_time
144
    line.line_status = LineStatus.LINE_ACTIVE
134
        line.line_status = LineStatus.LINE_ACTIVE
145
    
-
 
146
    cart.lines.append(line)
-
 
147
    session.commit()
135
    session.commit()
148
    return line.id
-
 
149
 
136
 
150
def delete_item_from_cart(cart_id, item_id):
137
def delete_item_from_cart(cart_id, item_id):
151
    if not item_id:
138
    if not item_id:
152
        raise ShoppingCartException(101, "item_id cannot be null")
139
        raise ShoppingCartException(101, "item_id cannot be null")
153
    item = get_item(item_id, cart_id, None, True)
140
    item = get_line(item_id, cart_id, None, True)
154
    item.delete()
141
    item.delete()
155
    session.commit()
142
    session.commit()
156
    return
-
 
157
 
-
 
158
def change_item_quantity(cart_id, item_id, quantity):
-
 
159
    return add_item_to_cart(cart_id, item_id, quantity)
-
 
160
 
143
 
161
def change_item_status(cart_id, item_id, status):
144
def change_item_status(cart_id, item_id, status):
162
    if not status:
145
    if not status:
163
        raise ShoppingCartException(101, "Status cannot be made null")
146
        raise ShoppingCartException(101, "Status cannot be made null")
164
    item = get_item(item_id, cart_id, None, True)
147
    item = get_line(item_id, cart_id, None, True)
165
    if item:
148
    if item:
166
        item.line_status = status
149
        item.line_status = status
167
        session.commit()
150
        session.commit()
168
    else:
151
    else:
169
        raise ShoppingCartException(101, "Unable to probe the item you desired")
152
        raise ShoppingCartException(101, "Unable to probe the item you desired")
Line 295... Line 278...
295
        item_id = line.item_id
278
        item_id = line.item_id
296
        if inventory_client.isActive(item_id):
279
        if inventory_client.isActive(item_id):
297
            if customer_pincode:
280
            if customer_pincode:
298
                #FIXME: Provider ID should be returned by the same call and shouldn't be hard-coded as 1
281
                #FIXME: Provider ID should be returned by the same call and shouldn't be hard-coded as 1
299
                try:
282
                try:
300
                    item_delivery_estimate = logistics_client.getLogisticsEstimation(item_id, customer_pincode, 1).shippingTime
283
                    item_delivery_estimate = logistics_client.getLogisticsEstimation(item_id, customer_pincode).deliveryTime
301
                except:
284
                except:
302
                    item_delivery_estimate = 72
285
                    item_delivery_estimate = 3
303
                if old_estimate != item_delivery_estimate:
286
                if old_estimate != item_delivery_estimate:
304
                    line.estimate = item_delivery_estimate
287
                    line.estimate = item_delivery_estimate
305
                    retval = False
288
                    retval = False
306
        else:
289
        else:
307
            line.delete()
290
            line.delete()
Line 327... Line 310...
327
 
310
 
328
    toCart.updatedOn = datetime.datetime.now() 
311
    toCart.updatedOn = datetime.datetime.now() 
329
    
312
    
330
    fromCart.expired_on = datetime.datetime.now()
313
    fromCart.expired_on = datetime.datetime.now()
331
    fromCart.cart_status = CartStatus.INACTIVE
314
    fromCart.cart_status = CartStatus.INACTIVE
332
    session.commit()
-
 
333
315
    session.commit()
-
 
316