def is_owner_or_404(request, user_id): """ 校验登录用户是否是资源所有者 :param request: :param user_id: :return: """ if request.user.id != safe_int(user_id): raise exceptions.NotFound
def aggregator_case_record_get(request): logs = AggregatorCaseRecord.objects.all() offset = safe_int(request.query_params.get('offset', 0)) limit = safe_int(request.query_params.get('limit', 15)) limit = get_limit(limit) module = request.query_params.get('module') action_flag = request.query_params.get('action_flag') key = request.query_params.get('key') platform = request.query_params.get('platform') start_time = request.query_params.get('start_time') end_time = request.query_params.get('end_time') if module: logs = logs.filter(module=module) if key: logs = logs.filter(key=key) if platform: logs = logs.filter(platform=platform) if action_flag: logs = logs.filter(action_flag=action_flag) if start_time and end_time: start_time += ' 00:00:00' end_time += ' 23:59:59' logs = logs.filter(create_time__gte=start_time).filter( create_time__lte=end_time) total = logs.count() logs = logs.order_by('-create_time')[offset:offset + limit] serializer = AggregatorCaseRecordSerializer(logs, many=True) # extras转为dict for data in serializer.data: if check_json_format(data['extras']): data['extras'] = json.loads(data['extras']) data['create_time'] = time.mktime( time.strptime(data['create_time'], '%Y-%m-%dT%H:%M:%S.%f')) response = APIResponse(success=True, data=serializer.data) # 分页数据 response[CONTENT_RANGE] = '{0}-{1}'.format(offset, offset + len(logs)) response[CONTENT_TOTAL] = total return response
def get(self, request): logs = ExportRecord.objects.all() offset = safe_int(request.query_params.get('offset', 0)) limit = safe_int(request.query_params.get('limit', 15)) limit = get_limit(limit) file_name = request.query_params.get('file_name') status = request.query_params.get('status') operator_name = request.query_params.get('operator_name') start_time = request.query_params.get('start_time') end_time = request.query_params.get('end_time') if file_name: logs = logs.filter(file_name=file_name) if status: logs = logs.filter(status=status) if operator_name: logs = logs.filter(operator_name=operator_name) if start_time and end_time: start_time += ' 00:00:00' end_time += ' 23:59:59' logs = logs.filter(create_time__gte=start_time).filter( create_time__lte=end_time) total = logs.count() logs = logs.order_by('-create_time')[offset:offset + limit] serializer = ExportRecordSerializer(logs, many=True) # extras转为dict for data in serializer.data: if check_json_format(data['extras']): data['extras'] = json.loads(data['extras']) data['create_time'] = time.mktime( time.strptime(data['create_time'], '%Y-%m-%dT%H:%M:%S.%f')) response = APIResponse(success=True, data=serializer.data) # 分页数据 response[CONTENT_RANGE] = '{0}-{1}'.format(offset, offset + len(logs)) response[CONTENT_TOTAL] = total return response
from utils.string_extension import safe_int
from utils.string_extension import safe_int
def online_generation_order(data, request): data = re.sub('\'', '\"', data) now = datetime.now() date = now.date() delta = timedelta(days=15) due_time = date + delta try: data = json.loads(data) # 商品信息 goods_info = data['goods_info'] goods_id_list = [] number_list = [] for info in goods_info: goods_id_list.append(int(info['goods_id'])) number_list.append(int(info['number'])) # 收货人 receiver = data['receiver'] # 电话 mobile = data['mobile'] # 省 province = data['province'] # 市 city = data['city'] # 区 district = data['district'] # 详细地址 address = data['address'] # 备注 remarks = data['remarks'] # 买家ID guest_id = request.user.id # guest_id = 2 # 这里后续需要加上获取佣金比例的逻辑 暂时先用 0.0 替代 ratio = 0.0 order_status = 1 headers = {'content-type': 'application/json', 'user-agent': "User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36"} parameters = json.dumps({}) try: response = requests.post(ORDER_API_HOST + '/api/order', data=parameters, headers=headers) response_dict = json.loads(response.text) except Exception as e: logger.info('ID生成器连接失败!!!') response = APIResponse(success=False, data={}, msg='ID生成器连接失败!!!') return response if response_dict['rescode'] != '10000': return response # 获取商品信息 try: parameters = {'goods_ids': goods_id_list, 'quantities': number_list} goods_response = requests.get(GOODS_API_HOST + '/api/service/checkout/goods', parameters, headers=headers) goods_dict = json.loads(goods_response.text) except Exception as e: logger.info('ID生成器连接失败!!!') response = APIResponse(success=False, data={}, msg='ID生成器连接失败!!!') return response if goods_dict['rescode'] != '10000': return goods_response # 母订单号 mother_order_sn = response_dict['data']['order_sn'] goods_data = [] for goods in goods_dict['data'].values(): _ = {} try: parameters = json.dumps({'order_id': mother_order_sn}) response = requests.post(ORDER_API_HOST + '/api/order', data=parameters, headers=headers) except Exception as e: logger.info('生成子订单出错') # response = APIResponse(success=False, data={}, msg='请求ID生成器出错') return response son_order_sn = json.loads(response.text)['data']['order_sn'] supplier_id = goods['supplier_id'] try: response = requests.get(SUPPLIER_API_HOST + '/api/service/suppliers/%s' % safe_int(supplier_id)) supplier_info = json.loads(response.text) except Exception as e: logger.info('查询供应商信息出错') response = APIResponse(success=False, data={}, msg='查询供应商信息出错') return response if supplier_info['rescode'] != '10000': return supplier_info _['univalent'] = goods['price'] _['supplier_name'] = supplier_info['data']['company'] _['price_discount'] = 0.0 _['goods_id'] = goods['goods_id'] _['supplier_id'] = supplier_id _['goods_name'] = goods['goods_name'] _['model'] = goods['partnumber'] _['goods_unit'] = goods['sales_unit'] res = re.match(r'\d+', goods['lead_time']) _['max_delivery_time'] = int(res.group()) if res else 7 _['brand'] = goods['brand_name'] _['goods_sn'] = goods['gno'] _['number'] = goods['quantity'] _['product_place'] = goods['prodution_place'] _['son_order_sn'] = son_order_sn goods_data.append(_) # 发票信息 if data['receipt_type'] == 3: receipt = Receipt.objects.create(receipt_type=data['receipt_type']) else: receipt = Receipt.objects.create( title=data['title'], account=data['account'], tax_number=data['tax_number'], telephone=data['telephone'], bank=data['bank'], company_address=data['company_address'], receipt_type=data['receipt_type'], ) # 创建母订单信息, 母订单类型订单不存在母订单 mother_order = Order.objects.create(receipt=receipt.id, remarks=remarks, receiver=receiver, mobile=mobile, guest_id=guest_id, order_sn=mother_order_sn, address=address, province=province, city=city, district=district) # 增加母订单状态信息 OrderOperationRecord.objects.create(order_sn=mother_order.order_sn, status=1, operator=guest_id, execution_detail='提交订单', progress='未支付') # 增加初始订单支付信息 OrderPayment.objects.create(order_sn=mother_order_sn, pay_status=1) total_money = 0.0 for _data in goods_data: # parameters = json.dumps({'order_id': mother_order_sn}) # response = requests.post(ORDER_API_HOST + '/api/order', data=parameters, headers=headers) # order_sn = json.loads(response.text)['data']['order_sn'] price_discount = float(_data.get('price_discount', 0.0)) subtotal_money = float(_data['univalent']) * float(_data['number']) * (1.0 - price_discount) total_money += subtotal_money _order = OrderDetail.objects.create( order=mother_order.id, son_order_sn=_data['son_order_sn'], supplier_id=_data['supplier_id'], goods_id=_data['goods_id'], goods_sn=_data['goods_sn'], goods_name=_data['goods_name'], goods_unit=_data['goods_unit'], product_place=_data['product_place'], model=_data['model'], brand=_data['brand'], number=int(_data['number']), univalent=float(_data['univalent']), subtotal_money=subtotal_money, commission=subtotal_money * ratio, price_discount=price_discount, status=order_status, max_delivery_time=_data['max_delivery_time'], due_time=due_time ) # 增加子订单状态信息 OrderOperationRecord.objects.create(order_sn=_order.son_order_sn, status=1, operator=guest_id, execution_detail='用户[%s]提交订单' % guest_id, progress='未支付') OrderPayment.objects.create(order_sn=_order.son_order_sn, pay_status=1) mother_order.total_money = total_money mother_order.save() except json.JSONDecodeError as e: logger.info("{msg: 请求参数异常}") response = APIResponse(success=False, data={}, msg='请求参数异常') return response result = { 'id': mother_order.id, 'mother_order_sn': mother_order_sn, 'add_time': mother_order.add_time, 'address': mother_order.address, 'receipt_title': receipt.title, 'receipt_type': RECEIPT_TYPE[receipt.receipt_type], 'status': order_status } response = APIResponse(success=True, data=result, msg='创建订单信息成功') return response