def export_compose_rule(self, request, queryset): is_windows = request.META['HTTP_USER_AGENT'].lower().find( 'windows') > -1 pcsv = [(u'SN', u'商品编码', u'规格编码', u'品名', u'数量')] rindex = 0 for rule in queryset: pcsv.append(('CR%s' % rindex, rule.outer_id, rule.outer_sku_id, rule.extra_info, str(rule.gif_count))) for item in rule.compose_items.all(): pcsv.append( ('', item.outer_id, item.outer_sku_id, item.extra_info, str(item.num))) rindex += 1 tmpfile = StringIO.StringIO() writer = CSVUnicodeWriter(tmpfile, encoding=is_windows and "gbk" or 'utf8') writer.writerows(pcsv) response = HttpResponse(tmpfile.getvalue(), content_type='application/octet-stream') tmpfile.close() response[ 'Content-Disposition'] = 'attachment; filename="compose-rule-%s.csv"' % str( int(time.time())) return response
def get_old(request, order_list_id): headers = [ u'商品编码', u'供应商编码', u'商品名称', u'规格', u'购买数量', u'买入价格', u'单项价格', u'已入库数', u'次品数' ] order_list = OrderList.objects.get(id=order_list_id) order_details = OrderDetail.objects.filter(orderlist_id=order_list_id, buy_quantity__gt=0) items = [] for o in order_details: sku = ProductSku.objects.get(id=o.chichu_id) product = sku.product item = model_to_dict(o) item['pic_path'] = product.pic_path item['supplier_outer_id'] = sku.get_supplier_outerid() if product.sale_product: try: saleproduct = SaleProduct.objects.get( id=product.sale_product) item[ 'memo'] = saleproduct.memo if saleproduct.orderlist_show_memo else '' except: pass items.append(item) items = [ map(unicode, [ i['outer_id'], i['supplier_outer_id'], i['product_name'], i['product_chicun'], i['buy_quantity'], i['buy_unitprice'], i['total_price'], i['arrival_quantity'], i['inferior_quantity'] ]) for i in items ] sum_of_total_price = round(sum(map(lambda x: float(x[-3]), items)), 2) items.append([''] * 5 + [u'总计', unicode(sum_of_total_price)] + [''] * 2) items.insert(0, headers) buff = StringIO() is_windows = request.META['HTTP_USER_AGENT'].lower().find( 'windows') > -1 writer = CSVUnicodeWriter(buff, encoding=is_windows and 'gbk' or 'utf-8') writer.writerows(items) response = HttpResponse(buff.getvalue(), content_type='application/octet-stream') response[ 'Content-Disposition'] = 'attachment;filename="dinghuodetail-%s.csv"' % order_list_id return response
def export_branch_zone(self, request, queryset): is_windows = request.META['HTTP_USER_AGENT'].lower().find('windows') > -1 pcsv = [] bz_tuple = gen_cvs_tuple(queryset, fields=['barcode', 'name', 'code'], title=[u'网点条码', u'网点名称', u'网点编号']) tmpfile = StringIO.StringIO() writer = CSVUnicodeWriter(tmpfile, encoding=is_windows and "gbk" or 'utf8') writer.writerows(bz_tuple) response = HttpResponse(tmpfile.getvalue(), content_type='application/octet-stream') tmpfile.close() response['Content-Disposition'] = 'attachment; filename="branch-zone-%s.csv"' % str(int(time.time())) return response
def task_make_Manager_Summary_Cvs(file_dir=None): print 'summary task is running .....' from flashsale.xiaolumm.views.views import manage_Summar yestoday = datetime.datetime.today() - datetime.timedelta(days=1) date_time = datetime.datetime(yestoday.year, yestoday.month, yestoday.day, 23, 59, 59) data = manage_Summar(date_time) # 字典列表[{"a":1},{"b":2}] if not file_dir: file_dir = os.path.join(settings.DOWNLOAD_ROOT, REPORT_DIR) if not os.path.exists(file_dir): os.makedirs(file_dir) field_name_list = [(u'管理员', 'username'), (u'订单数量', 'sum_ordernumcount'), (u'购买人数', 'sum_buyercount'), (u'UV', 'uv_summary'), (u'PV', 'pv_summary'), (u'转化率', 'conversion_rate'), (u'代理人数', 'xlmm_num'), (u'活跃度', 'activity'), (u'有效点击', 'sum_click_valid')] file_name = u'manager_summary_{0}_{1}_{2}.csv'.format( date_time.year, date_time.month, date_time.day) file_path_name = os.path.join(file_dir, file_name) print file_path_name with open(file_path_name, 'w+') as myfile: writer = CSVUnicodeWriter(myfile, encoding='gbk') thead = [k[0] for k in field_name_list] writer.writerow(thead) for i in data: ivalues = [str(i[k[1]]) for k in field_name_list] writer.writerow(ivalues)
def export_distinct_mobile_action(self, request, queryset): """ 导出唯一号码 """ dt = datetime.datetime.now() queryset = queryset.filter(is_valid=True) mobile_tuple = gen_cvs_tuple(queryset, fields=['mobile', ], title=[u'手机']) is_windows = request.META['HTTP_USER_AGENT'].lower().find('windows') > -1 file_name = u'mobile-%s-%s.csv' % (dt.month, dt.day) myfile = StringIO.StringIO() writer = CSVUnicodeWriter(myfile, encoding=is_windows and "gbk" or 'utf8') writer.writerows(mobile_tuple) response = HttpResponse(myfile.getvalue(), content_type='application/octet-stream') myfile.close() response['Content-Disposition'] = 'attachment; filename=%s' % file_name return response
def export_Refund_Product_Action(self, request, queryset): is_windows = request.META['HTTP_USER_AGENT'].lower().find( 'windows') > -1 pcsv = [] pcsv.append((u'ID', u'买家昵称', u'手机', u"原单id", u'出售价格', u'物流单号', u'物流名称', u'商品编码', u'规格编码', u'数量', u'商品名称', u'二次销售', u'处理完成', u'退货原因', u'创建时间')) for prod in queryset: outer_id = prod.outer_id outer_sku_id = prod.outer_sku_id skus = ProductSku.objects.filter(product__outer_id=outer_id, outer_id=outer_sku_id) price = 0 if skus.exists(): price = skus[0].agent_price pcsv.append( (str(prod.id), prod.buyer_nick, prod.buyer_mobile, prod.trade_id, str(price), prod.out_sid, prod.company, prod.outer_id, prod.outer_sku_id, str(prod.num), prod.title, str(prod.can_reuse), str(prod.is_finish), str(prod.get_reason_display()), str(prod.created))) pcsv.append(['', '', '', '', '', '', '', '', '', '', '', '', '']) tmpfile = StringIO.StringIO() writer = CSVUnicodeWriter(tmpfile, encoding=is_windows and "gbk" or 'utf8') writer.writerows(pcsv) response = HttpResponse(tmpfile.getvalue(), content_type='application/octet-stream') tmpfile.close() response[ 'Content-Disposition'] = 'attachment; filename="refund-pro-info-%s.csv"' % str( int(time.time())) return response
def export_Refund_Product_Action(self, request, queryset): is_windows = request.META['HTTP_USER_AGENT'].lower().find( 'windows') > -1 pcsv = [] pcsv.append((u'退款编号', u'交易编号', u'出售标题', u'退款费用', u'是否退货')) for rf in queryset: strade = SaleTrade.objects.get(id=rf.trade_id) pcsv.append( (rf.refund_no, strade.tid, rf.title, str(rf.refund_fee), str(rf.has_good_return))) pcsv.append(['', '', '', '', '']) tmpfile = StringIO.StringIO() writer = CSVUnicodeWriter(tmpfile, encoding=is_windows and "gbk" or 'utf8') writer.writerows(pcsv) response = HttpResponse(tmpfile.getvalue(), content_type='application/octet-stream') tmpfile.close() response[ 'Content-Disposition'] = 'attachment; filename="sale_refund-info-%s.csv"' % str( int(time.time())) return response
def export_refund_action(self, request, queryset): """ 导出商品及规格信息 """ from django.core import exceptions if not request.user.is_superuser: raise exceptions.PermissionDenied is_windows = request.META['HTTP_USER_AGENT'].lower().find( 'windows') > -1 csv_fields = [ 'trade_id', 'user_openid', 'vip_code', 'pay_amount', 'refund_status', 'created', 'pay_time', 'refund_type', 'pay_type', 'pay_note' ] csv_title = (u'订单编号', u'用户OPENID', u'邀请码', u'退款金额', u'退款状态', u'创建日期', u'付款日期', u'返利类型', u'支付方式', u'备注', u'退款状态(0等待审核,1审核通过,2审核不通过,3完成)', u'返利类型(1VIP邀请,210积分换购,3满100元返10元,4100元免单,510积分返邮费)') pcsv = gen_cvs_tuple(queryset, fields=csv_fields, title=csv_title) tmpfile = StringIO.StringIO() writer = CSVUnicodeWriter(tmpfile, encoding=is_windows and "gbk" or 'utf8') writer.writerows(pcsv) response = HttpResponse(tmpfile.getvalue(), content_type='application/octet-stream') tmpfile.close() response[ 'Content-Disposition'] = 'attachment; filename="weixin-refund-%s.csv"' % str( int(time.time())) return response