def get(self, request, hub_id): lang_info = parse_language_v2(request.META) date_info = get_date_information_v3(hub_id) target_db = lang_info.target_db cn_header = lang_info.cn_header os_type = lang_info.os_type time_type = date_info.time_type try: # Get product list from specific hub product_manager = ProductManagerV3(self.logger_info, self.logger_error) product_list = product_manager.get_product_list( hub_id, date_info.current_date) menu_manager = MenuManagerV2(self.logger_info, self.logger_error) for product in product_list: menu_manager.get_menu_data_for_list(product, target_db, cn_header, product['sales_time']) # Check the current available time bomb time_bomb_manager = TimeBombManager(self.logger_info, self.logger_error) time_bomb_id = time_bomb_manager.get_time_bomb_now(hub_id, os_type, has_after=False) if time_bomb_id is not None: # Discount information add discount_map = product_manager.get_all_discount_info( time_bomb_id, cn_header) for product in product_list: if product['id'] in discount_map: discount_info = discount_map[product['id']] add_discount_information(product, discount_info) except DataValidationError as instance: message, err_code = instance.args result = ResultResponse(code.ARIES_400_BAD_REQUEST, message, err_code) except BusinessLogicError as instance: message, err_code, data_set = instance.args result = ResultResponse(code.ARIES_500_INTERNAL_SERVER_ERROR, message, err_code) result.set_map(data_set) except Exception as e: result = ResultResponse(code.ARIES_500_INTERNAL_SERVER_ERROR, str(e)) else: result = ResultResponse(code.ARIES_200_SUCCESS, 'success') result.set('hub_id', hub_id) result.set('current_time_type', time_type) result.set('phase_next_day', date_info.phase_next_day) result.set('phase_date', date_info.current_date.isoformat()) result.set('products', product_list) return Response(result.get_response(), status=result.get_code())
def get(self, request, product_id): result = ResultResponse(code.ARIES_200_SUCCESS, self.SUCCESS_MSG) lang_info = parse_language_v2(request.META) target_db = lang_info.target_db os_type = lang_info.os_type cn_header = lang_info.cn_header product_manager = ProductManagerV3(logger_info, logger_error) menu_manager = MenuManagerV2(logger_info, logger_error) # Get product data product = product_manager.get_product_data(product_id) menu_manager.get_menu_data(product, target_db, cn_header) hub_id = product['hub'] # Check the current available time bomb time_bomb_manager = TimeBombManager(logger_info, logger_error) time_bomb_id = time_bomb_manager.get_time_bomb_now(hub_id, os_type) if time_bomb_id is not None: discount_map = product_manager.get_all_discount_info(time_bomb_id, cn_header) # Time bomb information parsing if product['id'] in discount_map: discount_info = discount_map[product['id']] add_discount_information(product, discount_info) result.set('product', product) return Response(result.get_response(), result.get_code())
def get(self, request, product_id): result = ResultResponse(code.ARIES_200_SUCCESS, 'success') lang_info = parse_language_v2(request.META) target_db = lang_info.target_db os_type = lang_info.os_type cn_header = lang_info.cn_header product_queryset = Product.objects.filter(id=product_id, type__lte=10) product_count = product_queryset.count() if product_count < 1: result = ResultResponse(code.ARIES_400_BAD_REQUEST, 'Product not found') return Response(result.get_response(), status=result.get_code()) # Create manager product_manager = ProductManagerV3(logger_info, logger_error) menu_manager = MenuManagerV2(logger_info, logger_error) review_manager = ReviewManager(logger_info, logger_error) # Get product information product = product_manager.get_product_data(product_id) hub_id = product['hub'] # Parsing menu and restaurant data menu_manager.get_menu_data(product, target_db, cn_header) result.set('product', product) # Recommend product information product_list = product_manager.get_recommend_product(hub_id) for product_obj in product_list: menu_manager.get_menu_data(product_obj, target_db, cn_header) result.set('products', product_list) # Time bomb information time_bomb_manager = TimeBombManager(self.logger_info, self.logger_error) time_bomb_id = time_bomb_manager.get_time_bomb_now(hub_id, os_type, has_after=False) if time_bomb_id is not None: discount_map = product_manager.get_all_discount_info(time_bomb_id, cn_header) if product['id'] in discount_map: discount_info = discount_map[product['id']] add_discount_information(product, discount_info) # Expert review expert_review = review_manager.get_expert_review(target_db, product['menu']['id']) result.set('expert_review', expert_review) # Review articles # reviews = review_manager.get_product_review(language_info[2], product_id) result.set('total_count', 0) result.set('page_size', 0) result.set('customer_reviews', []) return Response(result.get_response(), status=result.get_code())
def get_stock_list(self, hub_id, product_list, target_db, os_type): """ :return: tuple(stock_list) """ cn_header = False if target_db == 'default' else True stock_list = [] # Product service product_service = ProductService(self.logger_info, self.logger_error) menu_service = MenuService(self.logger_info, self.logger_error, target_db) # Time bomb service time_bomb_service = TimeBombService(self.logger_info, self.logger_error) time_bomb_id = time_bomb_service.get_current_time_bomb_id(hub_id, os_type) discount_info = {} if time_bomb_id is not None: discount_list = product_service.read_product_discount_info_with_id(time_bomb_id) discount_info.update(get_discount_info_map(discount_list, cn_header)) for product in product_list: if product['has_product']: product_instance = product_service.read_product_with_id(product['product_id']) hub_stock = menu_service.read_hub_stock_instance(product_instance.hub, product_instance.menu) current_stock = hub_stock.stock product['stock'] = current_stock product['price'] = product_instance.price # Stock product['has_changed'] = True # To delete next update product['sales_time_limit'] = False # Time bomb check and add information if product['product_id'] in discount_info: add_discount_information(product, discount_info[product['product_id']]) del product['has_product'] stock_list.append(product) stock_result = stock_list return stock_result
def get(self, request, hub_id): result = ResultResponse(code.ARIES_200_SUCCESS, 'success') lang_info = header_parser.parse_language_v2(request.META) target_db = lang_info.target_db cn_header = lang_info.cn_header os_type = lang_info.os_type product_manager = ProductManagerV3(self.logger_info, self.logger_error) product_list = product_manager.get_recommend_product(hub_id) menu_manager = MenuManagerV2(self.logger_info, self.logger_error) for product in product_list: menu_manager.get_menu_data(product, target_db, cn_header) # Check the current available time bomb time_bomb_manager = TimeBombManager(self.logger_info, self.logger_error) time_bomb_id = time_bomb_manager.get_time_bomb_now(hub_id, os_type, has_after=False) if time_bomb_id is not None: # Discount information add discount_map = product_manager.get_all_discount_info( time_bomb_id, cn_header) for product in product_list: # Time bomb information parsing if product['id'] in discount_map: discount_info = discount_map[product['id']] add_discount_information(product, discount_info) result.set('hub_id', hub_id) result.set('products', product_list) return Response(result.get_response(), status=result.get_code())
def post(self, request): result = ResultResponse(code.ARIES_200_SUCCESS, self.SUCCESS_MSG) language_info = header_parser.parse_language(request.META) cn_header = language_info[0] target_db = language_info[1] result_list = list() result_product_list = list() try: request_data = request.data os_type = request_data.get('os_type', 0) hub_id = request_data.get('hub_id', 1) product_list = request.data['product_list'] delivery_schedule = request.data['delivery_schedule'] except Exception as e: logger_info.info(str(e)) result = ResultResponse(code.ARIES_400_BAD_REQUEST, 'Request data invalid') return Response(result.get_response(), result.get_code()) first_product = True product_order_name = '' sales_time = 0 # Time bomb service product_service = ProductService(logger_info, logger_error) time_bomb_service = TimeBombService(logger_info, logger_error) time_bomb_id = time_bomb_service.get_current_time_bomb_id(hub_id, os_type) discount_info = {} if time_bomb_id is not None: discount_list = product_service.read_product_discount_info_with_id(time_bomb_id) discount_info.update(get_discount_info_map(discount_list, cn_header)) for product in product_list: try: product_instance = Product.objects.get(id=product['product_id']) menu_instance = Menu.objects.using(target_db).get(id=product_instance.menu.id) hub_stock = HubStock.objects.get(hub=product_instance.hub, menu=menu_instance) # Check sales_time sales_time = product_instance.sales_time # Check real stock unit order_quantity = product['quantity'] stock = hub_stock.stock if stock < order_quantity: result = ResultResponse(code.ARIES_500_INTERNAL_SERVER_ERROR, 'Product already sold') error_code = code.ERROR_3001_PRODUCT_ALREADY_SOLD result.set_error(error_code) error_message = get_msg(error_code, cn_header) error_message += ' [' + menu_instance.name + ']' result.set_error_message(error_message) return Response(result.get_response(), result.get_code()) # Check schedule time if not check_delivery_schedule(delivery_schedule): result = ResultResponse(code.ARIES_500_INTERNAL_SERVER_ERROR, 'Schedule invalid') error_code = code.ERROR_3015_PRODUCT_DELIVERY_TIME_INVALID error_message = get_msg(error_code, cn_header) result.set_error(error_code) result.set_error_message(error_message) return Response(result.get_response(), result.get_code()) # Product information product_serializer = ProductListInfoSerializer(product_instance) product_data = product_serializer.data menu_serializer = MenuValidationSerializer(menu_instance) menu_data = menu_serializer.data product_data['menu'] = menu_data product_data['quantity'] = product['quantity'] # Add time bomb information if product_data['id'] in discount_info: add_discount_information(product_data, discount_info[product_data['id']]) result_product_list.append(product_data) if first_product: product_order_name = product_instance.menu.name # product_order_name = menu_instance.name first_product = False logger_info.info(str(stock) + ' ' + str(product['quantity'])) if stock >= product['quantity']: result_list.append(product['product_id']) except Exception as e: print(e) result.set('product_title', product_order_name) # Check if result list is zero, don't display more if int(len(result_list)-1) != 0: result.set('product_sub', '(and ' + str(len(result_list)-1) + ' more)') else: result.set('product_sub', '') result.set('sales_time', sales_time) result.set('product_count', len(result_list)) result.set('product_index_list', result_list) result.set('product_list', result_product_list) return Response(result.get_response(), result.get_code())
def post(self, request, hub_id=1): try: request_data = request.data request_validation(request_data) menu_id_list = request_data['menu_id_list'] target_db = request_data['target_db'] cn_header = False if target_db == 'default' else True sales_time_str = get_sales_time_str() date_info = get_date_information_v2(hub_id, sales_time_str) lang_info = parse_language_v2(request.META) os_type = lang_info.os_type product_manager = ProductManagerV3(self.logger_info, self.logger_error) product_data = product_manager.get_product_info( hub_id, menu_id_list) menu_manager = MenuManagerV2(self.logger_info, self.logger_error) product_list = [] for product in product_data: product_list.append( menu_manager.get_menu_data(product, target_db, cn_header)) # Check the current available time bomb time_bomb_manager = TimeBombManager(self.logger_info, self.logger_error) time_bomb_id = time_bomb_manager.get_time_bomb_now(hub_id, os_type) # Get time bomb information if time_bomb_id is not None: discount_map = product_manager.get_all_discount_info( time_bomb_id, cn_header) for product in product_list: if product['id'] in discount_map: discount_info = discount_map[product['id']] add_discount_information(product, discount_info) except DataValidationError as instance: message, err_code = instance.args result = ResultResponse(code.ARIES_400_BAD_REQUEST, message, err_code) except Exception as e: self.logger_info.info(str(e)) result = ResultResponse(code.ARIES_500_INTERNAL_SERVER_ERROR, str(e)) else: result = ResultResponse(code.ARIES_200_SUCCESS, self.SUCCESS_MSG) result.set('products', product_list) result.set('hub_id', int(hub_id)) result.set('time_type', date_info.time_type) result.set('phase_next_day', date_info.phase_next_day) result.set('phase_date', date_info.current_date.isoformat()) result.set('order_available', product_util.get_available_time(date_info.time_type)) return Response(result.get_response(), result.get_code())
def get(self, request, time_bomb_id): lang_info = parse_language_v2(request.META) lang_type = 1 if lang_info.cn_header else 0 cn_header = lang_info.cn_header target_db = lang_info.target_db os_type = lang_info.os_type self.logger_info.info('os_type : ' + self.OS_TYPE[os_type]) try: # Get time bomb data time_bomb_manager = TimeBombManager(self.logger_info, self.logger_error) time_bomb_exists = time_bomb_manager.check_time_bomb_exists( time_bomb_id) # Default product list setting product_list = [] if time_bomb_exists: time_bomb = time_bomb_manager.get_time_bomb( time_bomb_id, lang_type, os_type) hub_id = time_bomb['hub'] # Get product data and check sold out if time_bomb['status'] == self.STATUS_AVAILABLE: product_manager = ProductManagerV3(self.logger_info, self.logger_error) product_id_list = time_bomb['products'] product_list = product_manager.get_product_list_from_ids( product_id_list) # Get menu id and check stock stock_manager = StockManager(self.logger_info, self.logger_error) menu_id_list = [ product['menu'] for product in product_list ] # Check if status is sold out or available if stock_manager.check_sold_out(hub_id, menu_id_list): time_bomb['status'] = self.STATUS_SOLD_OUT # For menu data parsing menu_manager = MenuManagerV2(self.logger_info, self.logger_error) # Discount information discount_map = product_manager.get_time_bomb_discount_info( time_bomb_id, product_id_list, cn_header) # Stock information stock_map = stock_manager.get_stock_map( hub_id, menu_id_list) for product in product_list: menu_manager.get_menu_data(product, target_db, cn_header) if product['id'] in discount_map: discount_info = discount_map[product['id']] # Stock check if product['menu']['id'] in stock_map: discount_info['has_stock'] = True discount_info['stock'] = stock_map[ product['menu']['id']] add_discount_information(product, discount_info) else: # Time bomb expired template if cn_header: time_bomb = time_bomb_expired_template_cn() else: time_bomb = time_bomb_expired_template_en() # Add action template # time_bomb_url = 'https://m.viastelle.com/#/promotions/timeBombInt' extra = { 'share_image': '', 'share_title': '', 'share_description': '', 'share_enable': False, 'link': 'https://stg-cli.viastelle.com/dist/#/promotions/timeBombInt' } if cn_header: extra['title'] = '推广' else: extra['title'] = 'PROMOTION' action_object = { 'action_type': 13, 'action_target': 'https://stg-cli.viastelle.com/dist/#/promotions/timeBombInt', 'action_extra': json.dumps(extra) } # Stock api call interval api_call_interval = 60 except DataValidationError as instance: message, err_code = instance.args result = ResultResponse(code.ARIES_400_BAD_REQUEST, message, err_code) except Exception as e: self.logger_info.info(str(e)) result = ResultResponse(code.ARIES_500_INTERNAL_SERVER_ERROR, str(e)) else: result = ResultResponse(code.ARIES_200_SUCCESS, 'success') result.set_map(time_bomb) result.set('action', action_object) result.set('stock_api_interval', api_call_interval) result.set('products', product_list) return Response(result.get_response(), result.get_code())
def get(self, request, time_bomb_id): lang_info = parse_language_v2(request.META) lang_type = 1 if lang_info.cn_header else 0 cn_header = lang_info.cn_header target_db = lang_info.target_db os_type = lang_info.os_type self.logger_info.info('os_type : ' + self.OS_TYPE[os_type]) try: # Get time bomb data time_bomb_manager = TimeBombManager(self.logger_info, self.logger_error) time_bomb = time_bomb_manager.get_time_bomb( time_bomb_id, lang_type, os_type) hub_id = time_bomb['hub'] # Default product list setting product_list = [] # Get product data and check sold out if time_bomb['status'] == self.STATUS_AVAILABLE: product_manager = ProductManagerV3(self.logger_info, self.logger_error) product_id_list = time_bomb['products'] product_list = product_manager.get_product_list_from_ids( product_id_list) # Get menu id and check stock stock_manager = StockManager(self.logger_info, self.logger_error) menu_id_list = [product['menu'] for product in product_list] # Check if status is sold out or available if stock_manager.check_sold_out(hub_id, menu_id_list): time_bomb['status'] = self.STATUS_SOLD_OUT # For menu data parsing menu_manager = MenuManagerV2(self.logger_info, self.logger_error) # Discount information discount_map = product_manager.get_time_bomb_discount_info( time_bomb_id, product_id_list, cn_header) # Stock information stock_map = stock_manager.get_stock_map(hub_id, menu_id_list) for product in product_list: menu_manager.get_menu_data(product, target_db, cn_header) if product['id'] in discount_map: discount_info = discount_map[product['id']] # Stock check if product['menu']['id'] in stock_map: discount_info['has_stock'] = True discount_info['stock'] = stock_map[product['menu'] ['id']] add_discount_information(product, discount_info) except DataValidationError as instance: message, err_code = instance.args result = ResultResponse(code.ARIES_400_BAD_REQUEST, message, err_code) except Exception as e: self.logger_info.info(str(e)) result = ResultResponse(code.ARIES_500_INTERNAL_SERVER_ERROR, str(e)) else: result = ResultResponse(code.ARIES_200_SUCCESS, 'success') result.set('status', time_bomb['status']) result.set('products', product_list) return Response(result.get_response(), result.get_code())