| Line 107... |
Line 107... |
| 107 |
'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
|
107 |
'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
|
| 108 |
'Accept-Language' : 'en-US,en;q=0.8',
|
108 |
'Accept-Language' : 'en-US,en;q=0.8',
|
| 109 |
'Accept-Charset' : 'ISO-8859-1,utf-8;q=0.7,*;q=0.3'
|
109 |
'Accept-Charset' : 'ISO-8859-1,utf-8;q=0.7,*;q=0.3'
|
| 110 |
}
|
110 |
}
|
| 111 |
SAHOLIC_ORDER_URL=PythonPropertyReader.getConfig('SAHOLIC_ORDER_URL')
|
111 |
SAHOLIC_ORDER_URL=PythonPropertyReader.getConfig('SAHOLIC_ORDER_URL')
|
| - |
|
112 |
SAHOLIC_ORDER_URL_TR=PythonPropertyReader.getConfig('SAHOLIC_ORDER_URL_TR')
|
| 112 |
class Store(MStore):
|
113 |
class Store(MStore):
|
| 113 |
'''
|
114 |
'''
|
| 114 |
This is to map order statuses of our system to order statuses of snapdeal.
|
115 |
This is to map order statuses of our system to order statuses of snapdeal.
|
| 115 |
And our statuses will change accordingly.
|
116 |
And our statuses will change accordingly.
|
| 116 |
|
117 |
|
| Line 129... |
Line 130... |
| 129 |
orderRequest = urllib2.Request(SAHOLIC_ORDER_URL %(paymentId), headers=headers)
|
130 |
orderRequest = urllib2.Request(SAHOLIC_ORDER_URL %(paymentId), headers=headers)
|
| 130 |
try:
|
131 |
try:
|
| 131 |
response = urllib2.urlopen(orderRequest).read()
|
132 |
response = urllib2.urlopen(orderRequest).read()
|
| 132 |
response = json.loads(response)['response']
|
133 |
response = json.loads(response)['response']
|
| 133 |
payment = response['payment']
|
134 |
payment = response['payment']
|
| - |
|
135 |
subOrders = []
|
| 134 |
orders = response['orders']
|
136 |
orders = response['orders']
|
| 135 |
items = response['itemsMap']
|
137 |
items = response['itemsMap']
|
| 136 |
orders[0]
|
- |
|
| 137 |
merchantOrder = Order(orderId, userId, subTagId, self.store_id, orderSuccessUrl)
|
138 |
merchantOrder = Order(orderId, userId, subTagId, self.store_id, orderSuccessUrl)
|
| 138 |
merchantOrder.merchantOrderId = payment['merchantTxnId']
|
139 |
merchantOrder.merchantOrderId = payment['merchantTxnId']
|
| 139 |
merchantOrder.createdOn = datetime.strftime(datetime.fromtimestamp(orders[0]['created_timestamp']/1000),"%d %B %Y")
|
- |
|
| 140 |
merchantOrder.paidAmount = payment['amount']
|
140 |
merchantOrder.paidAmount = payment['amount']
|
| 141 |
|
141 |
|
| 142 |
subOrders = []
|
- |
|
| 143 |
for o in orders:
|
142 |
for o in orders:
|
| 144 |
lineitem = o['lineitems'][0]
|
- |
|
| 145 |
|
- |
|
| 146 |
item = items[str(lineitem['item_id'])]
|
- |
|
| 147 |
brand = lineitem.get('brand')
|
- |
|
| 148 |
modelNumber = lineitem.get('model_number')
|
- |
|
| 149 |
modelName = lineitem.get('model_name')
|
- |
|
| 150 |
color = lineitem.get('color')
|
- |
|
| 151 |
productTitle = brand + (" " + modelName if modelName else "") + (" " + modelNumber if modelNumber is not None else "") + ("(" + color +")" if color else "")
|
- |
|
| 152 |
amountPaid = o['total_amount']- o['gvAmount']
|
- |
|
| 153 |
subOrder = SubOrder(productTitle, None, merchantOrder.createdOn, amountPaid)
|
- |
|
| 154 |
subOrder.merchantSubOrderId = str(o['id'])
|
- |
|
| 155 |
subOrder.orderDetailUrl = "http://m.saholic.com/order/" + subOrder.merchantSubOrderId
|
- |
|
| 156 |
|
- |
|
| 157 |
subOrder.estimatedDeliveryDate = datetime.strftime(datetime.fromtimestamp(o['promised_delivery_time']/1000),"%d %B %Y")
|
- |
|
| 158 |
subOrder.imgUrl = item['imgUrl']
|
- |
|
| 159 |
subOrder.productUrl = "http://m.saholic.com/" + item['url']
|
- |
|
| 160 |
subOrder.detailedStatus = o['statusDescription']
|
- |
|
| 161 |
(cashbackAmount, percentage) = self.getCashbackAmount(subOrder.productCode, amountPaid)
|
- |
|
| 162 |
cashbackStatus = Store.CB_PENDING
|
- |
|
| 163 |
if cashbackAmount <= 0:
|
- |
|
| 164 |
cashbackStatus = Store.CB_NA
|
- |
|
| 165 |
subOrder.cashBackStatus = cashbackStatus
|
- |
|
| 166 |
subOrder.cashBackAmount = cashbackAmount
|
- |
|
| 167 |
if percentage > 0:
|
- |
|
| 168 |
subOrder.cashBackPercentage = percentage
|
- |
|
| 169 |
subOrders.append(subOrder)
|
143 |
subOrders.append(self.createSubOrder(o, items))
|
| 170 |
|
144 |
|
| - |
|
145 |
merchantOrder.placedOn = merchantOrder.subOrders[0]['placedOn']
|
| 171 |
merchantOrder.subOrders = subOrders
|
146 |
merchantOrder.subOrders = subOrders
|
| 172 |
self._saveToOrder(todict(merchantOrder))
|
147 |
s = todict(merchantOrder)
|
| - |
|
148 |
s['sUserId'] = orders[0]['customer_id']
|
| - |
|
149 |
self._saveToOrder(s)
|
| 173 |
resp['result'] = 'ORDER_CREATED'
|
150 |
resp['result'] = 'ORDER_CREATED'
|
| 174 |
except:
|
151 |
except:
|
| 175 |
tprint("Could not fetch saholic id for user : " + str(userId))
|
152 |
tprint("Could not fetch saholic id for user : " + str(userId))
|
| 176 |
traceback.print_exc()
|
153 |
traceback.print_exc()
|
| 177 |
resp['result'] = 'PARSE_ERROR'
|
154 |
resp['result'] = 'PARSE_ERROR'
|
| 178 |
return resp
|
155 |
return resp
|
| 179 |
|
156 |
|
| - |
|
157 |
def createSubOrder(self, order, items):
|
| - |
|
158 |
lineitem = order['lineitems'][0]
|
| - |
|
159 |
|
| - |
|
160 |
item = items[str(lineitem['item_id'])]
|
| - |
|
161 |
brand = lineitem.get('brand')
|
| - |
|
162 |
modelNumber = lineitem.get('model_number')
|
| - |
|
163 |
modelName = lineitem.get('model_name')
|
| - |
|
164 |
color = lineitem.get('color')
|
| - |
|
165 |
productTitle = brand + (" " + modelName if modelName else "") + (" " + modelNumber if modelNumber is not None else "") + ("(" + color +")" if color else "")
|
| - |
|
166 |
amountPaid = order['total_amount']- order['gvAmount']
|
| - |
|
167 |
subOrder = SubOrder(productTitle, None, datetime.strftime(datetime.fromtimestamp(order['created_timestamp']/1000),"%d %B %Y"), amountPaid)
|
| - |
|
168 |
subOrder.merchantSubOrderId = str(order['id'])
|
| - |
|
169 |
subOrder.orderDetailUrl = "http://m.saholic.com/order/" + subOrder.merchantSubOrderId
|
| - |
|
170 |
|
| - |
|
171 |
subOrder.estimatedDeliveryDate = datetime.strftime(datetime.fromtimestamp(order['promised_delivery_time']/1000),"%d %B %Y")
|
| - |
|
172 |
subOrder.imgUrl = item['imgUrl']
|
| - |
|
173 |
subOrder.productUrl = "http://m.saholic.com/" + item['url']
|
| - |
|
174 |
subOrder.detailedStatus = order['statusDescription']
|
| - |
|
175 |
(cashbackAmount, percentage) = self.getCashbackAmount(subOrder.productCode, amountPaid)
|
| - |
|
176 |
cashbackStatus = Store.CB_PENDING
|
| - |
|
177 |
if cashbackAmount <= 0:
|
| - |
|
178 |
cashbackStatus = Store.CB_NA
|
| - |
|
179 |
subOrder.cashBackStatus = cashbackStatus
|
| - |
|
180 |
subOrder.cashBackAmount = cashbackAmount
|
| - |
|
181 |
if percentage > 0:
|
| - |
|
182 |
subOrder.cashBackPercentage = percentage
|
| - |
|
183 |
return subOrder
|
| 180 |
def _getStatusFromDetailedStatus(self, detailedStatus):
|
184 |
def _getStatusFromDetailedStatus(self, detailedStatus):
|
| 181 |
for key, value in Store.OrderStatusMap.iteritems():
|
185 |
for key, value in Store.OrderStatusMap.iteritems():
|
| 182 |
if detailedStatus in value:
|
186 |
if detailedStatus in value:
|
| 183 |
return key
|
187 |
return key
|
| 184 |
print "Detailed Status need to be mapped"
|
188 |
print "Detailed Status need to be mapped"
|
| 185 |
raise ParseException("_getStatusFromDetailedStatus", "Found new order status" + detailedStatus)
|
189 |
raise ParseException("_getStatusFromDetailedStatus", "Found new order status" + detailedStatus)
|
| 186 |
|
190 |
|
| 187 |
|
191 |
|
| 188 |
def scrapeStoreOrders(self,):
|
192 |
def scrapeStoreOrders(self,):
|
| - |
|
193 |
trs = self._getActiveOrders()
|
| - |
|
194 |
for tr in trs:
|
| - |
|
195 |
bulk = self.db.merchantOrder.initialize_ordered_bulk_op()
|
| - |
|
196 |
try:
|
| - |
|
197 |
orderRequest = urllib2.Request(SAHOLIC_ORDER_URL_TR %(tr['merchantOrderId'], tr['sUserId']), headers=headers)
|
| - |
|
198 |
response = urllib2.urlopen(orderRequest).read()
|
| - |
|
199 |
response = json.loads(response)['response']
|
| - |
|
200 |
items = response['itemsMap']
|
| - |
|
201 |
orders = response['orders']
|
| - |
|
202 |
closed = True
|
| - |
|
203 |
for order in orders:
|
| - |
|
204 |
subOrder = self._isSubOrderActive(tr, order['id'])
|
| - |
|
205 |
if subOrder:
|
| - |
|
206 |
if subOrder['closed']:
|
| - |
|
207 |
continue
|
| - |
|
208 |
else:
|
| - |
|
209 |
findMap = {"orderId": order['orderId'], "subOrders.merchantSubOrderId": order['id']}
|
| - |
|
210 |
updateMap = {}
|
| - |
|
211 |
updateMap["subOrders.$.detailedStatus"] = order['statusDescription']
|
| - |
|
212 |
status = self._getStatusFromDetailedStatus(ORDERSTATUS[order['status']])
|
| - |
|
213 |
closedStatus = status in [Store.ORDER_DELIVERED, Store.ORDER_CANCELLED]
|
| - |
|
214 |
updateMap["subOrders.$.status"] = status
|
| - |
|
215 |
if closedStatus:
|
| - |
|
216 |
#if status is closed then change the paybackStatus accordingly
|
| - |
|
217 |
updateMap["subOrders.$.closed"] = True
|
| - |
|
218 |
if status == Store.ORDER_DELIVERED:
|
| - |
|
219 |
deliveredOn = datetime.strftime(datetime.fromtimestamp(order['delivery_timestamp']/1000),"%A %d %B %Y")
|
| - |
|
220 |
updateMap['subOrders.$.deliveredOn'] = deliveredOn
|
| - |
|
221 |
if subOrder.get("cashBackStatus") == Store.CB_PENDING:
|
| - |
|
222 |
updateMap["subOrders.$.cashBackStatus"] = Store.CB_APPROVED
|
| - |
|
223 |
elif status == Store.ORDER_CANCELLED:
|
| - |
|
224 |
if subOrder.get("cashBackStatus") == Store.CB_PENDING:
|
| - |
|
225 |
updateMap["subOrders.$.cashBackStatus"] = Store.CB_CANCELLED
|
| - |
|
226 |
else:
|
| - |
|
227 |
expectedDelivery = datetime.strftime(datetime.fromtimestamp(order['promised_delivery_time']/1000),"%A %d %B %Y")
|
| - |
|
228 |
updateMap["subOrders.$.estimatedDeliveryDate"] = expectedDelivery
|
| - |
|
229 |
closed = False
|
| - |
|
230 |
bulk.find(findMap).update({'$set' : updateMap})
|
| - |
|
231 |
else:
|
| - |
|
232 |
subOrder = self.parseSubOrder(order, items)
|
| - |
|
233 |
self.db.merchantOrder.update({"orderId":order['orderId']},{'$push':{"subOrders":todict(subOrder)}})
|
| - |
|
234 |
print "Added new suborder with subOrder Id:", subOrder.merchantSubOrderId
|
| - |
|
235 |
closed = False
|
| - |
|
236 |
bulk.find({"order":order['id']}).update({'$set' : {'closed':closed}})
|
| - |
|
237 |
except:
|
| 189 |
pass
|
238 |
pass
|
| - |
|
239 |
|
| - |
|
240 |
|
| - |
|
241 |
|
| 190 |
|
242 |
|
| 191 |
def _saveToAffiliate(self, offers):
|
243 |
def _saveToAffiliate(self, offers):
|
| 192 |
if offers is None or len(offers)==0:
|
244 |
if offers is None or len(offers)==0:
|
| 193 |
print "no affiliate have been pushed"
|
245 |
print "no affiliate have been pushed"
|
| 194 |
return
|
246 |
return
|