| 557 |
chandransh |
1 |
'''
|
|
|
2 |
Created on 10-May-2010
|
|
|
3 |
|
|
|
4 |
@author: ashish
|
|
|
5 |
'''
|
|
|
6 |
from elixir import *
|
|
|
7 |
from shop2020.model.v1.user.impl.Dataservice import Cart, Line
|
|
|
8 |
from shop2020.thriftpy.model.v1.user.ttypes import CartStatus, LineStatus, ShoppingCartException
|
|
|
9 |
import datetime
|
|
|
10 |
from shop2020.utils.Utils import to_py_date
|
|
|
11 |
from shop2020.thriftpy.model.v1.order.ttypes import Transaction,\
|
|
|
12 |
TransactionStatus
|
|
|
13 |
|
|
|
14 |
|
|
|
15 |
def get_cart(user_id):
|
|
|
16 |
query = Cart.query.filter_by(user_id=user_id)
|
|
|
17 |
query = query.filter_by(cart_status = CartStatus.ACTIVE)
|
|
|
18 |
try:
|
|
|
19 |
cart = query.one()
|
|
|
20 |
cart.accessed_on = datetime.datetime.now()
|
|
|
21 |
session.commit()
|
|
|
22 |
except:
|
|
|
23 |
return None
|
|
|
24 |
# raise ShoppingCartException(101, "Cart does not exist")
|
|
|
25 |
return cart
|
|
|
26 |
|
|
|
27 |
def get_cart_by_id(id):
|
|
|
28 |
cart = Cart.get_by(id=id)
|
|
|
29 |
if cart:
|
|
|
30 |
cart.accessed_on = datetime.datetime.now()
|
|
|
31 |
session.commit()
|
|
|
32 |
return cart
|
|
|
33 |
|
|
|
34 |
def create_cart(user_id):
|
|
|
35 |
cart = get_cart(user_id)
|
|
|
36 |
if not cart:
|
|
|
37 |
cart = Cart()
|
|
|
38 |
cart.user_id = user_id
|
|
|
39 |
cart.created_on = datetime.datetime.now()
|
|
|
40 |
cart.updated_on = datetime.datetime.now()
|
|
|
41 |
cart.cart_status = CartStatus.ACTIVE
|
|
|
42 |
session.commit()
|
|
|
43 |
|
|
|
44 |
return cart
|
|
|
45 |
|
|
|
46 |
def get_cart_by_user_id_and_status(user_id, status):
|
|
|
47 |
|
|
|
48 |
query = Cart.query.filter_by(user_id=user_id)
|
|
|
49 |
if status:
|
|
|
50 |
query = query.filter_by(cart_status=status)
|
|
|
51 |
carts = query.all()
|
|
|
52 |
return carts
|
|
|
53 |
|
|
|
54 |
def get_carts_by_status(status):
|
|
|
55 |
return Cart.query.filter_by(cart_status=status).all()
|
|
|
56 |
|
|
|
57 |
|
|
|
58 |
def get_carts_between(start_time, end_time, status):
|
|
|
59 |
init_time = to_py_date(start_time)
|
|
|
60 |
finish_time = to_py_date(end_time)
|
|
|
61 |
|
|
|
62 |
query = Cart.query
|
|
|
63 |
if status:
|
|
|
64 |
query = query.filter(Cart.cart_status==status)
|
|
|
65 |
if init_time:
|
|
|
66 |
query = query.filter(Cart.created_on >= init_time)
|
|
|
67 |
if finish_time:
|
|
|
68 |
query = query.filter(Cart.created_on <= finish_time)
|
|
|
69 |
|
|
|
70 |
carts = query.all()
|
|
|
71 |
return carts
|
|
|
72 |
|
|
|
73 |
def get_item(item_id, cart_id, status, single):
|
|
|
74 |
|
|
|
75 |
#get cart first
|
|
|
76 |
try:
|
|
|
77 |
found_cart = Cart.get_by(id=cart_id)
|
|
|
78 |
|
|
|
79 |
except:
|
|
|
80 |
raise ShoppingCartException(101, "cart not found ")
|
|
|
81 |
query = Line.query.filter(Line.cart.has(id = cart_id))
|
|
|
82 |
query = query.filter_by(item_id = item_id)
|
|
|
83 |
|
|
|
84 |
# query = LineItem.query.filter_by(item_id = item_id)
|
|
|
85 |
# query = query.filter(LineItem.cart.id == cart_id)
|
|
|
86 |
if status:
|
|
|
87 |
query = query.filter_by(line_status = status)
|
|
|
88 |
else:
|
|
|
89 |
query = query.filter_by(line_status = LineStatus.LINE_ACTIVE)
|
|
|
90 |
try:
|
|
|
91 |
if single:
|
|
|
92 |
return query.one()
|
|
|
93 |
else:
|
|
|
94 |
return query.all()
|
|
|
95 |
except:
|
|
|
96 |
return None
|
|
|
97 |
|
|
|
98 |
def get_item_by_id(line_id):
|
|
|
99 |
return Line.get_by(id=line_id)
|
|
|
100 |
|
|
|
101 |
def change_cart_status(cart_id, status):
|
|
|
102 |
cart = get_cart_by_id(id)
|
|
|
103 |
if not cart:
|
|
|
104 |
raise ShoppingCartException(101, "no cart attached to this id")
|
|
|
105 |
if not status:
|
|
|
106 |
raise ShoppingCartException(101, "invalid status")
|
|
|
107 |
|
|
|
108 |
cart.cart_status = status
|
|
|
109 |
|
|
|
110 |
session.commit()
|
|
|
111 |
|
|
|
112 |
def add_item_to_cart(cart_id, item_id, quantity):
|
|
|
113 |
if not item_id:
|
|
|
114 |
raise ShoppingCartException(101, "item_id cannot be null")
|
|
|
115 |
|
|
|
116 |
if not quantity:
|
|
|
117 |
raise ShoppingCartException(101, "quantity cannot be null")
|
|
|
118 |
|
|
|
119 |
item = get_item(item_id, cart_id, None,True)
|
|
|
120 |
if item:
|
|
|
121 |
#change the quantity only
|
|
|
122 |
item.quantity = quantity
|
|
|
123 |
session.commit()
|
|
|
124 |
return item.id
|
|
|
125 |
|
|
|
126 |
cart = get_cart_by_id(cart_id)
|
|
|
127 |
if not cart:
|
|
|
128 |
raise ShoppingCartException(101, "no cart attached to this id")
|
|
|
129 |
|
|
|
130 |
line = Line()
|
|
|
131 |
line.item_id = item_id
|
|
|
132 |
line.quantity = quantity
|
|
|
133 |
line.created_on = datetime.datetime.now()
|
|
|
134 |
line.updated_on = datetime.datetime.now()
|
|
|
135 |
line.line_status = LineStatus.LINE_ACTIVE
|
|
|
136 |
|
|
|
137 |
cart.lines.append(line)
|
|
|
138 |
session.commit()
|
|
|
139 |
return line.id
|
|
|
140 |
|
|
|
141 |
def delete_item_from_cart(cart_id, item_id):
|
|
|
142 |
if not item_id:
|
|
|
143 |
raise ShoppingCartException(101, "item_id cannot be null")
|
|
|
144 |
item = get_item(item_id, cart_id, None, True)
|
|
|
145 |
item.delete()
|
|
|
146 |
session.commit()
|
|
|
147 |
return
|
|
|
148 |
|
|
|
149 |
def change_item_quantity(cart_id, item_id, quantity):
|
|
|
150 |
return add_item_to_cart(cart_id, item_id, quantity)
|
|
|
151 |
|
|
|
152 |
def change_item_status(cart_id, item_id, status):
|
|
|
153 |
if not status:
|
|
|
154 |
raise ShoppingCartException(101, "Status cannot be made null")
|
|
|
155 |
item = get_item(item_id, cart_id, None, True)
|
|
|
156 |
if item:
|
|
|
157 |
item.line_status = status
|
|
|
158 |
session.commit()
|
|
|
159 |
else:
|
|
|
160 |
raise ShoppingCartException(101, "Unable to probe the item you desired")
|
|
|
161 |
|
|
|
162 |
def add_address_to_cart(cart_id, address_id):
|
|
|
163 |
if not cart_id:
|
|
|
164 |
raise ShoppingCartException(101, "cart id cannot be made null")
|
|
|
165 |
|
|
|
166 |
if not address_id:
|
|
|
167 |
raise ShoppingCartException(101, "address id cannot be made null")
|
|
|
168 |
|
|
|
169 |
cart = get_cart_by_id(cart_id)
|
|
|
170 |
if not cart:
|
|
|
171 |
raise ShoppingCartException(101, "no cart for this id")
|
|
|
172 |
cart.address_id = address_id
|
|
|
173 |
session.commit()
|
|
|
174 |
|
|
|
175 |
def commit_cart(cart_id, transaction_client):
|
|
|
176 |
|
|
|
177 |
cart = get_cart_by_id(cart_id)
|
|
|
178 |
cart.cart_status = CartStatus.COMMITTED
|
|
|
179 |
cart.committed_on = datetime.datetime.now()
|
|
|
180 |
session.commit()
|
|
|
181 |
#now we have a cart. Need to create a transaction with it
|
|
|
182 |
#transaction = transaction_client.create_transaction(cart)
|
|
|
183 |
return True
|
|
|
184 |
"""
|
|
|
185 |
transaction.shoppingCartid = cart_id
|
|
|
186 |
transaction.customer_id = cart.user_id
|
|
|
187 |
transaction.transactionStatus = TransactionStatus.INIT
|
|
|
188 |
transaction.statusDescription = "Transaction submitted to warehouse"
|
|
|
189 |
transaction.created_on = datetime.datetime.now()
|
|
|
190 |
transaction.warehouse_id = 1;
|
|
|
191 |
|
|
|
192 |
cart_lines = cart.lines
|
|
|
193 |
for line in cart_lines:
|
|
|
194 |
if line.status == LineStatus.LINE_ACTIVE:
|
|
|
195 |
#line is active
|
|
|
196 |
line_item = get_line_item(cartLine.getItemId());
|
|
|
197 |
// create orders equivalent to quantity. Create one order per item.
|
|
|
198 |
for(int i= 0; i<cartLine.getQuantity(); i++){
|
|
|
199 |
// set order
|
|
|
200 |
Order order = getOrder(c.getAddressId(), lineItem);
|
|
|
201 |
order.addToLineitems(lineItem);
|
|
|
202 |
orders.add(order);
|
|
|
203 |
"""
|
|
|
204 |
|
| 563 |
chandransh |
205 |
def validate_cart(cartId, inventory_client):
|
|
|
206 |
retval = True
|
| 557 |
chandransh |
207 |
# No need to validate duplicate items since there are only two ways
|
|
|
208 |
# to add items to a cart and both of them check whether the item being
|
|
|
209 |
# added is a duplicate of an already existing item.
|
| 563 |
chandransh |
210 |
cart = Cart.get_by(id=cartId)
|
|
|
211 |
cart_lines = cart.lines
|
|
|
212 |
for line in cart_lines:
|
|
|
213 |
item_id = line.item_id
|
|
|
214 |
if not inventory_client.is_active(item_id):
|
|
|
215 |
line.delete()
|
|
|
216 |
retval = False
|
|
|
217 |
return retval
|
| 557 |
chandransh |
218 |
|
|
|
219 |
def merge_cart(fromCartId, toCartId):
|
|
|
220 |
fromCart = Cart.get_by(id=fromCartId)
|
|
|
221 |
toCart = Cart.get_by(id=toCartId)
|
|
|
222 |
|
|
|
223 |
old_lines = fromCart.lines
|
|
|
224 |
new_lines = toCart.lines
|
|
|
225 |
|
|
|
226 |
for line in old_lines:
|
|
|
227 |
flag = True
|
|
|
228 |
for new_line in new_lines:
|
|
|
229 |
if line.item_id == new_line.item_id:
|
|
|
230 |
flag = False
|
|
|
231 |
if flag:
|
|
|
232 |
toCart.lines.append(line)
|
|
|
233 |
|
|
|
234 |
toCart.updatedOn = datetime.datetime.now()
|
|
|
235 |
|
|
|
236 |
fromCart.expired_on = datetime.datetime.now()
|
|
|
237 |
fromCart.cart_status = CartStatus.INACTIVE
|
|
|
238 |
session.commit()
|
|
|
239 |
"""
|
|
|
240 |
t_line.id = line.id
|
|
|
241 |
t_line.itemId = line.item_id
|
|
|
242 |
t_line.quantity = line.quantity
|
|
|
243 |
t_line.createdOn = to_java_date(line.created_on)
|
|
|
244 |
t_line.updatedOn = to_java_date(line.updated_on)
|
|
|
245 |
t_line.lineStatus = line.line_status
|
|
|
246 |
"""
|