class BaseShippingListResource(BaseXmlResource): template = "shipping_list.xml" def _on_get(self, req, resp, conn, **kwargs): try: self._order_check(conn) except UserError, e: conn.rollback() logging.error("%s" % str(e), exc_info=True) return {"error": e.code} id_order = req._params.get('id_order') order_status = get_order_status(conn, id_order) order = get_order(conn, id_order) order_create_date = order['confirmation_time'].date() id_user = order['id_user'] id_shipaddr = order['id_shipaddr'] shipping_dest = get_user_dest_addr(conn, id_user, id_shipaddr) shipment_list = get_shipments_by_order(conn, id_order) object_list = [] for shipment in shipment_list: if shipment['status'] == SHIPMENT_STATUS.DELETED: continue id_shipment = shipment['id'] shipment['carriers'] = self._get_supported_services(conn, shipment) shipment['tracking_info'] = self._get_tracking_info(id_shipment) shipment['fee_info'] = self._get_shipping_fee(conn, shipment['id']) shipment['shipping_list'] = self._get_shipping_list( conn, id_shipment) shipment['postage'] = get_shipping_postage(conn, id_shipment) if int(order_status) == ORDER_STATUS.AWAITING_SHIPPING: paid_time = get_shipment_paid_time(conn, id_shipment)['paid_time'] shipment['paid_date'] = paid_time.date() object_list.append(shipment) unpacking_list = self._get_unpacking_shipment(conn, id_order) object_list.extend(unpacking_list) return { 'object_list': object_list, 'order_status': order_status, 'order_create_date': order_create_date, 'shipping_currency': SHIPPING_CURRENCY, 'shipping_weight_unit': SHIPPING_WEIGHT_UNIT, 'shipping_dest': shipping_dest }
def get_and_save_invoices(self, conn, req, id_user, id_shipments=None): id_order = req.get_param('order') if not id_order: raise ValidationError("Miss param in request") order = get_order(conn, id_order, id_user=id_user) if order is None: raise ValidationError("Order not exist") # address id_shipaddr = order['id_shipaddr'] dest = get_user_dest_addr(conn, id_user, id_shipaddr) dest = ujson.dumps(dest) # customer user = get_user_profile(conn, id_user) if id_shipments is not None: shipments = get_shipments_by_id(conn, id_shipments) else: shipments = get_shipments_by_order(conn, id_order) invoices = {} for sp in shipments: invoice_xml = self.get_shipment_invoice(conn, dest, user, sp) if invoice_xml is None: continue invoices[sp['id']] = invoice_xml shipments_id = invoices.keys() shipments_id.sort() content = self.gen_invoices_content([ invoices[id_shipment] for id_shipment in shipments_id if invoices.get(id_shipment) ]) return content
class OrderResource(BaseJsonResource): login_required = {'get': False, 'post': True} post_action_func_map = {'create': 'order_create', 'modify': 'order_modify'} users_id = None def _on_post(self, req, resp, conn, **kwargs): self.users_id = kwargs.get('users_id') action = req.get_param('action') if action is None or action not in self.post_action_func_map: logging.error('Invalid Order: %s', req.query_string) raise ValidationError('ORDER_ERR_INVALID_ACTION') func = getattr(self, self.post_action_func_map[action], None) assert hasattr(func, '__call__') return func(req, resp, conn) def order_modify(self, req, resp, conn): ## check params try: assert req.get_param('id_order') assert req.get_param('telephone') assert req.get_param('shipaddr'), 'shipaddr' assert req.get_param('billaddr'), 'billaddr' self._addressValidCheck(conn, req.get_param('shipaddr')) self._addressValidCheck(conn, req.get_param('billaddr')) self._telephoneValidCheck(conn, req.get_param('telephone')) except AssertionError, e: logging.error('Invalid Order request: %s', req.query_string) raise ValidationError('ORDER_ERR_MISSED_PARAM_%s' % e) shipaddr = req.get_param('shipaddr') billaddr = req.get_param('billaddr') telephone = req.get_param('telephone') id_order = req.get_param('id_order') ## Can't modify order which status > AWAITING_PAYMENT order_status = get_order_status(conn, id_order) if int(order_status) > ORDER_STATUS.AWAITING_PAYMENT: logging.warn( 'The order %s can not be modified because its status ' 'is %s', id_order, order_satus) return {'res': RESP_RESULT.F, 'err': '', 'id': 0} ## return id_order directly if no changes. order = get_order(conn, id_order) if int(order['id_shipaddr']) == int(shipaddr) and \ int(order['id_billaddr']) == int(billaddr) and \ int(order['id_phone']) == int(telephone): return {'res': RESP_RESULT.S, 'err': '', 'id': int(id_order)} order_items = get_order_items(conn, id_order) items = [] for item in order_items: items.append({ 'id_order_item': item['item_id'], 'id_shop': item['shop_id'], 'id_sale': item['sale_id'], 'id_variant': item['id_variant'], 'quantity': item['quantity'], 'id_weight_type': item['id_weight_type'], 'id_price_type': item['id_price_type'] }) id_order = modify_order(conn, self.users_id, id_order, telephone, items, shipaddr, billaddr) return {'res': RESP_RESULT.S, 'err': '', 'id': int(id_order)}
def _on_post(self, req, resp, conn, **kwargs): try: params = req.stream.read() params = ujson.loads(params) req._params.update(params) id_order = params.get('order') id_shipments = params.get('shipment') or '[]' id_brand = params.get('brand') id_shops = params.get('shops') if not id_brand or not id_shops or not id_order: raise ValidationError("iv_send_req_err: miss params") id_brand = int(id_brand) id_shops = [int(id_shop) for id_shop in ujson.loads(id_shops)] id_shipments = [ int(id_spm) for id_spm in ujson.loads(id_shipments) ] orig_id_spms = id_shipments shipments = get_shipments_by_order(conn, id_order) shipments = dict([(sp['id'], sp) for sp in shipments]) valid_shipments = shipments.keys() order = get_order(conn, id_order) if id_shipments: invalid_shipments = set(id_shipments) - set(valid_shipments) if len(invalid_shipments) > 0: raise ValidationError("shipments %s not belongs to " "order %s" % (invalid_shipments, id_order)) for id_spm in id_shipments: spm_brand = shipments[id_spm]['id_brand'] spm_shop = shipments[id_spm]['id_shop'] if spm_brand != id_brand: raise ValidationError( "iv_send_req_err: spm brand %s is not " "same with given %s" % (spm_brand, id_brand)) if spm_shop not in id_shops: raise ValidationError( "iv_send_req_err: spm shop %s is not " "in given shops %s" % (spm_shop, id_shops)) else: for id_spm, spm in shipments.iteritems(): if (spm['id_brand'] == id_brand and spm['id_shop'] in id_shops): id_shipments.append(id_spm) self.get_and_save_invoices(conn, req, order['id_user'], id_shipments=id_shipments) order_invoices = get_invoice_by_order(conn, id_order) invoices = dict([(oi['id_shipment'], oi) for oi in order_invoices]) users_mail = get_user_email(conn, order['id_user']) if orig_id_spms: for id_spm in id_shipments: if invoices.get(id_spm) is None: continue content = invoices[id_spm]['invoice_xml'] content_html = self.invoice_xslt(content) send_html_email(users_mail, INVOICE_MAIL_SUB, content_html) else: content_list = [] for id_spm in id_shipments: if invoices.get(id_spm) is None: continue content = invoices[id_spm]['invoice_xml'] content = self.parse_invoices_content(content) content_list.append(content) content = self.unitary_content(content_list) content_html = self.invoice_xslt(content) send_html_email(users_mail, INVOICE_MAIL_SUB, content_html) order_iv_status = order_iv_sent_status(conn, id_order, id_brand, id_shops) return {'res': SUCCESS, 'order_iv_status': order_iv_status} except ValidationError, e: conn.rollback() logging.error("send_invoice_invalidate: %s", str(e), exc_info=True) return {'res': FAILURE, 'err': str(e)}
def _get_order_create_date(self, conn, id_order): order = get_order(conn, id_order) return order['confirmation_time'].date()