def logistics(self): """ 物流 :return: """ items_range = request.headers.get('Range', '0-9') start, end = items_range.split('-') per_page = int(end) - int(start) + 1 query = restruct_query(request.args) tracking_no = query.pop('tracking_no', '') include_closed = query.get('include_closed') and query.pop( 'include_closed') try: if include_closed: logistic = Logistic.objects(**query) else: logistic = Logistic.objects(is_closed=False, **query) if tracking_no: logistic = logistic.filter( Q(detail__us_tracking_no=tracking_no) | Q(detail__cn_tracking_no=tracking_no)) if request.args.get('status'): logistic = logistic.order_by('detail__%s' % LogisticDetail.attr_by_log_status[ request.args.get('status')]) except: pass if query.get('receiver'): address = Address.objects( receiver=query.get('receiver')).distinct('id') orders = Order.commodities(address__in=address) logistic = list( chain.from_iterable(order.logistics for order in orders)) if query.get('order_id'): orders = Order.commodities(short_id=int(query.get('order_id'))) logistic = list( chain.from_iterable(order.logistics for order in orders)) try: logistic_size = logistic.count() except: logistic_size = len(logistic) data = logistic[int(start):int(end)] data = [to_json(l) for l in data] resp = make_response(json_util.dumps(data), 200) resp.headers['Accept_Range'] = 'items' resp.headers['Content_Range'] = '%s-%s/%s' % (start, end, logistic_size) resp.headers['Content_Type'] = 'application/json' return resp
def download(self): """ :return: """ FIELDS = [ '包裹ID', 'IMG NO', 'CTN', '下单日期', '订单ID', '订单短号', '收件人', '手机号', '合作物流商', 'remark', '下单备注', '估重', '渠道' ] now = datetime.datetime.now() status = request.args.get('status') query = restruct_query(request.args) delay_export = query.get('delay_export') and query.pop('delay_export') delay_type = query.get('delay_type') and query.pop('delay_type') try: logistics = Logistic.objects(is_closed=False, **query) if status: logistics = logistics.order_by( 'detail__%s' % LogisticDetail.attr_by_log_status[status]) except: pass if delay_export: date_field = LogisticDetail.attr_by_log_status[status] delay_days = datetime.timedelta(days=delay_status_by_date[status]) query = { 'detail__%s__lt' % date_field: datetime.datetime.utcnow() - delay_days, 'detail__status': status, } logistics = logistics.filter(**query).order_by('detail__%s' % date_field) if delay_type: logistics = logistics.filter( detail__delay_details__reason__contains=delay_type) if query.get('receiver'): addresses = Address.objects( receiver=query.get('receiver')).distince('id') orders = Order.commodities(address__in=addresses) logistics = list( chain.from_iterable(order.logistics for order in orders)) if query.get('order_id'): orders = Order.commodities(short_id=int(query.get('order_id'))) logistics = list( chain.from_iterable(order.logistics for order in orders)) def generate(): yield ','.join(st for st in FIELDS) + '\n' for logistic in logistics: yield ','.join([ str(logistic.id), logistic.detail.partner_tracking_no, logistic.detail.carrier_tracking_no, logistic.detail.cn_tracking_no, logistic.detail.cn_logistic_name, format_date(logistic.detail_payment_received_date), str(logistic.order.id), str(logistic.order.short_id), logistic.order.address.receiver, logistic.order.address.mobile_number, format_date(logistic.detail.processing_date), format_date(logistic.detail.shipping_date), format_date(logistic.detail.port_arrived_date), format_date(logistic.detail.received_date), format_date(logistic.detail.modified), logistic.detail.partner.name if logistic.detail.partner else '', ';'.join([r.content for r in logistic.detail.remarks]), logistic.detail.extra or '', str(logistic.estimated_weight), logistic.detail.channel, ]) + '\n' return Response(generate(), minetype='text/csv', headers={ 'Content_Disposition': 'attachment;filename=%s %s.csv' % (format_date(now, '%Y-%m-%d'), 'dumps_file') })