def save_logistics_through_dict(cls, user_id, logistic_dict): logistic, state = cls.objects.get_or_create(tid=logistic_dict['tid']) logistic.user = User.objects.get(visitor_id=user_id) logistic.seller_id = user_id for k, v in logistic_dict.iteritems(): hasattr(logistic, k) and setattr(logistic, k, v) location = logistic_dict.get('location', None) logistic.location = json.dumps(location) logistic.delivery_start = parse_datetime(logistic_dict['delivery_start']) \ if logistic_dict.get('delivery_start', None) else None logistic.delivery_end = parse_datetime(logistic_dict['delivery_end']) \ if logistic_dict.get('delivery_end', None) else None logistic.created = parse_datetime(logistic_dict['created']) \ if logistic_dict.get('created', None) else None logistic.modified = parse_datetime(logistic_dict['modified']) \ if logistic_dict.get('modified', None) else None logistic.save() if logistic_dict['status'] in LOGISTICS_FINISH_STATUS: trade_extra_info, state = TradeExtraInfo.objects.get_or_create( tid=logistic_dict['tid']) trade_extra_info.is_update_logistic = True trade_extra_info.save() return logistic
def save_trade_amount_through_dict(cls, user_id, trade_amount_dict): trade_amount, state = cls.objects.get_or_create(pk=trade_amount_dict['tid']) trade_amount.user = User.objects.get(visitor_id=user_id) order_amounts = trade_amount_dict.pop('order_amounts') for k, v in trade_amount_dict.iteritems(): hasattr(trade_amount, k) and setattr(trade_amount, k, v) dt = parse_datetime(trade_amount_dict['created']) trade_amount.year = dt.year trade_amount.hour = dt.hour trade_amount.month = dt.month trade_amount.day = dt.day trade_amount.week = time.gmtime(time.mktime(dt.timetuple()))[7] / 7 + 1 trade_amount.created = parse_datetime(trade_amount_dict['created']) trade_amount.pay_time = parse_datetime(trade_amount_dict['pay_time']) \ if trade_amount_dict.get('pay_time', None) else None trade_amount.end_time = parse_datetime(trade_amount_dict['end_time']) \ if trade_amount_dict.get('end_time', None) else None trade_amount.promotion_details = json.dumps(trade_amount_dict.get('promotion_details', None)) trade_amount.save() order_amount = OrderAmount() order_amount.trade_amount = trade_amount for o in order_amounts['order_amount']: for k, v in o.iteritems(): hasattr(order_amount, k) and setattr(order_amount, k, v) order_amount.save() return trade_amount
def updateUserItemsTask(user_id): """ 更新淘宝线上商品信息入库 """ has_next = True cur_page = 1 onsale_item_ids = [] # 更新出售中的商品 try: while has_next: response_list = apis.taobao_items_onsale_get(page_no=cur_page, tb_user_id=user_id , page_size=settings.TAOBAO_PAGE_SIZE, fields='num_iid,modified') item_list = response_list['items_onsale_get_response'] if item_list['total_results'] > 0: items = item_list['items']['item'] for item in items: modified = parse_datetime(item['modified']) if item.get('modified', None) else None item_obj, state = Item.objects.get_or_create(num_iid=item['num_iid']) if modified != item_obj.modified: response = apis.taobao_item_get(num_iid=item['num_iid'], tb_user_id=user_id) item_dict = response['item_get_response']['item'] Item.save_item_through_dict(user_id, item_dict) onsale_item_ids.append(item['num_iid']) total_nums = item_list['total_results'] cur_nums = cur_page * settings.TAOBAO_PAGE_SIZE has_next = cur_nums < total_nums cur_page += 1 # 更新库存中的商品 has_next = True cur_page = 1 while has_next: response_list = apis.taobao_items_inventory_get(page_no=cur_page, tb_user_id=user_id , page_size=settings.TAOBAO_PAGE_SIZE, fields='num_iid,modified') item_list = response_list['items_inventory_get_response'] if item_list['total_results'] > 0: items = item_list['items']['item'] for item in item_list['items']['item']: modified = parse_datetime(item['modified']) if item.get('modified', None) else None item_obj, state = Item.objects.get_or_create(num_iid=item['num_iid']) if modified != item_obj.modified: response = apis.taobao_item_get(num_iid=item['num_iid'], tb_user_id=user_id) item_dict = response['item_get_response']['item'] Item.save_item_through_dict(user_id, item_dict) onsale_item_ids.append(item['num_iid']) total_nums = item_list['total_results'] cur_nums = cur_page * settings.TAOBAO_PAGE_SIZE has_next = cur_nums < total_nums cur_page += 1 except: logger.error('update user inventory items task error', exc_info=True) else: Item.objects.filter(user__visitor_id=user_id).exclude(num_iid__in=onsale_item_ids) \ .update(approve_status=pcfg.INSTOCK_STATUS, status=False) return len(onsale_item_ids)
def saveUserIncrementOrdersTask(user_id, update_from=None, update_to=None): s_dt_f = format_datetime(update_from) s_dt_t = format_datetime(update_to) has_next = True cur_page = 1 from shopback.trades.service import TradeService, OrderService while has_next: response_list = apis.taobao_trades_sold_increment_get(tb_user_id=user_id, page_no=cur_page, fields='tid,modified', page_size=settings.TAOBAO_PAGE_SIZE, use_has_next='true', start_modified=s_dt_f, end_modified=s_dt_t) trade_list = response_list['trades_sold_increment_get_response'] if trade_list.has_key('trades'): for trade in trade_list['trades']['trade']: modified = parse_datetime(trade['modified']) if trade.get('modified', None) else None if TradeService.isValidPubTime(user_id, trade['tid'], modified): try: trade_dict = OrderService.getTradeFullInfo(user_id, trade['tid']) order_trade = OrderService.saveTradeByDict(user_id, trade_dict) OrderService.createMergeTrade(order_trade) except Exception, exc: logger.error(u'淘宝订单下载失败:%s' % exc.message, exc_info=True) has_next = trade_list['has_next'] cur_page += 1
def updateTradeSellerFlagTask(): system_config = SystemConfig.getconfig() if system_config and system_config.is_flag_auto: dt = datetime.datetime.now() start_date = datetime.datetime(dt.year, dt.month, dt.day, 0, 0, 0) trades = MergeTrade.objects.filter(sys_status__in= (pcfg.WAIT_PREPARE_SEND_STATUS, pcfg.WAIT_SCAN_WEIGHT_STATUS, pcfg.WAIT_CONFIRM_SEND_STATUS, pcfg.WAIT_AUDIT_STATUS)) \ .include(modified__gt=modified, sys_status=pcfg.INVALID_STATUS) for trade in trades: rule_memo, state = RuleMemo.objects.get_or_create(tid=trade.tid) seller_flag = SYS_STATUS_MATCH_FLAGS.get(trade.sys_status, None) if seller_flag and seller_flag != rule_memo.seller_flag: try: response = taobao_trade_memo_update( tid=trade.tid, flag=seller_flag, tb_user_id=trade.user.visitor_id) trade_rep = response['trade_memo_update_response']['trade'] if trade_rep: RuleMemo.objects.filter(tid=trade.tid).update( seller_flag=seller_flag) MergeTrade.objects.filter(tid=trade_rep['tid']).update( modified=parse_datetime(trade_rep['modified'])) except: logger.error('update taobao trade flag error', exc_info=True)
def post(self, request, *args, **kwargs): creator = request.user content = request.POST start = content.get('start') end = content.get('end') start = parse_datetime(start) end = end and parse_datetime(end) or datetime.datetime( start.year, start.month, start.day) interval_day = content.get('interval_day', '0') title = content.get('title') type = content.get('type', 'temp') executor_string = content.get('executor', '') exectors = get_users_by_string(executor_string) or [creator] staff_events = [] for executor in exectors: staff_event = StaffEvent.objects.create(executor=executor, creator=creator, start=start, end=end, interval_day=interval_day, title=title, type=type) staff_events.append(staff_event) staff_list = [] for event in staff_events: staff_dict = { 'id': event.id, 'creator': event.creator.username, 'executor': event.executor.username, 'start': event.start, 'end': event.end, 'interval_day': event.interval_day, 'title': event.title, 'type': event.type, 'created': event.created, 'modified': event.modified, 'is_finished': event.is_finished, } staff_list.append(staff_dict) return Response(staff_list)
def save_trade_through_dict(cls, user_id, trade_dict): trade, state = cls.objects.get_or_create(pk=trade_dict['tid']) trade.user = ShopUser.objects.get(visitor_id=user_id) trade.seller_id = user_id for k, v in trade_dict.iteritems(): hasattr(trade, k) and setattr(trade, k, v) if trade_dict.get('pay_time', ''): dt = parse_datetime(trade_dict['pay_time']) trade.year = dt.year trade.hour = dt.hour trade.month = dt.month trade.day = dt.day trade.week = time.gmtime(time.mktime(dt.timetuple()))[7] / 7 + 1 trade.created = parse_datetime(trade_dict['created']) trade.pay_time = parse_datetime(trade_dict['pay_time']) \ if trade_dict.get('pay_time', None) else None trade.end_time = parse_datetime(trade_dict['end_time']) \ if trade_dict.get('end_time', None) else None trade.modified = parse_datetime(trade_dict['modified']) \ if trade_dict.get('modified', None) else None trade.consign_time = parse_datetime(trade_dict['consign_time']) \ if trade_dict.get('consign_time', None) else None trade.send_time = parse_datetime(trade_dict['send_time']) \ if trade_dict.get('send_time', None) else None trade.lg_aging = parse_datetime(trade_dict['lg_aging']) \ if trade_dict.get('lg_aging', None) else None trade.save() for o in trade_dict['orders']['order']: order, state = Order.objects.get_or_create(pk=o['oid']) order.seller_nick = trade_dict['seller_nick'] order.buyer_nick = trade_dict['buyer_nick'] order.trade = trade for k, v in o.iteritems(): hasattr(order, k) and setattr(order, k, v) order.outer_id = o.get('outer_iid', '') order.year = trade.year order.month = trade.month order.day = trade.day order.week = trade.week order.hour = trade.hour order.created = trade.created order.pay_time = trade.pay_time order.consign_time = trade.consign_time order.save() merge_trade_signal.send(sender=Trade, trade=trade) return trade
def save_refund_through_dict(self, seller_id, refund): self.user = User.objects.get(visitor_id=seller_id) from shopback.trades.models import MergeTrade try: merge_trade = MergeTrade.objects.get(tid=refund['tid']) except: merge_trade = None self.mobile = merge_trade and merge_trade.receiver_mobile or '' self.phone = merge_trade and merge_trade.receiver_phone or '' for k, v in refund.iteritems(): hasattr(self, k) and setattr(self, k, v) self.created = parse_datetime(refund['created']) \ if refund.get('created', None) else None self.modified = parse_datetime(refund['modified']) \ if refund.get('modified', None) else None self.save()
def save_order_through_dict(cls, seller_id, purchase_order_dict): purchase_order, state = cls.objects.get_or_create( fenxiao_id=purchase_order_dict['fenxiao_id']) purchase_order.user = User.objects.get(visitor_id=seller_id) purchase_order.seller_id = seller_id sub_purchase_orders = purchase_order_dict.pop('sub_purchase_orders') for k, v in purchase_order_dict.iteritems(): hasattr(purchase_order, k) and setattr(purchase_order, k, v) purchase_order.created = parse_datetime(purchase_order_dict['created']) \ if purchase_order_dict.get('created', None) else None purchase_order.pay_time = parse_datetime(purchase_order_dict['pay_time']) \ if purchase_order_dict.get('pay_time', None) else None purchase_order.modified = parse_datetime(purchase_order_dict['modified']) \ if purchase_order_dict.get('modified', None) else None purchase_order.consign_time = parse_datetime(purchase_order_dict['consign_time']) \ if purchase_order_dict.get('consign_time', None) else None purchase_order.save() for sub_order in sub_purchase_orders['sub_purchase_order']: sub_purchase_order, state = SubPurchaseOrder.objects.get_or_create( fenxiao_id=sub_order['fenxiao_id']) for k, v in sub_order.iteritems(): hasattr(sub_purchase_order, k) and setattr( sub_purchase_order, k, v) sub_purchase_order.purchase_order = purchase_order sub_purchase_order.fenxiao_product = FenxiaoProduct.get_or_create( seller_id, sub_order['item_id']) sub_purchase_order.created = parse_datetime(sub_order['created']) \ if sub_order.get('created', None) else None sub_purchase_order.save() return purchase_order
def saveUserDuringOrdersTaskBak(user_id, update_from=None, update_to=None, status=None): """ 下载用户商城订单 """ update_tids = [] try: update_from = format_datetime(update_from) if update_from else None update_to = format_datetime(update_to) if update_to else None has_next = True cur_page = 1 from shopback.trades.service import TradeService, OrderService while has_next: response_list = apis.taobao_trades_sold_get(tb_user_id=user_id, page_no=cur_page, use_has_next='true', fields='tid,modified', page_size=settings.TAOBAO_PAGE_SIZE, start_created=update_from, end_created=update_to, status=status) order_list = response_list['trades_sold_get_response'] if order_list.has_key('trades'): for trade in order_list['trades']['trade']: modified = parse_datetime(trade['modified']) if trade.get('modified', None) else None if TradeService.isValidPubTime(user_id, trade['tid'], modified): try: trade_dict = OrderService.getTradeFullInfo(user_id, trade['tid']) order_trade = OrderService.saveTradeByDict(user_id, trade_dict) OrderService.createMergeTrade(order_trade) except Exception, exc: logger.error(u'淘宝订单下载失败:%s' % exc.message, exc_info=True) update_tids.append(trade['tid']) has_next = order_list['has_next'] cur_page += 1 except Exception, exc: logger.error(u'淘宝订单批量下载错误:%s' % exc.message, exc_info=True) raise saveUserDuringOrdersTask.retry(exc=exc, countdown=60)
def get(self, request, *args, **kwargs): # print "get99" content = request.GET exector = content.get('exector') date_type = content.get('date_type', 'task') finished = content.get('is_finished', '') order_desc = content.get('order_desc') df = content.get('df') dt = content.get('dt') try: django_user = DjangoUser.objects.get(username=exector) except: django_user = request.user start = parse_datetime(df) end = dt and parse_datetime(dt) or None staff_events = StaffEvent.objects.filter(Q(executor=django_user) | Q(creator=django_user), status='normal') if finished: staff_events = staff_events.filter( is_finished=finished.upper() == 'Y') order_by = '' if date_type == 'task': if end: staff_events = staff_events.filter( Q(start__gte=start) | Q(end__lte=end)) else: staff_events = staff_events.filter(start__gte=start) order_by = order_desc == '1' and '-end' or 'start' elif date_type == 'modify': staff_events = staff_events.filter(modified__gte=start) if end: staff_events = staff_events.filter(modified__lte=end) order_by = order_desc == '1' and '-modified' or 'modified' elif date_type == 'create': staff_events = staff_events.filter(created__gte=start) if end: staff_events = staff_events.filter(created__lte=end) order_by = order_desc == '1' and '-created' or 'created' if order_by: staff_events = staff_events.order_by(order_by) staff_list = [] for event in staff_events: staff_dict = { 'id': event.id, 'creator': event.creator.username, 'executor': event.executor.username, 'start': event.start, 'end': event.end, 'interval_day': event.interval_day, 'title': event.title, 'type': event.type, 'created': event.created, 'modified': event.modified, 'is_finished': event.is_finished, } staff_list.append(staff_dict) return Response(staff_list)
def getMessageTime(self, message): return parse_datetime(message['pub_time'])