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):
        auth_info = header_parser.parse_auth_info(request)
        lang_info = parse_language_v2(request.META)

        if auth_info.open_id is not None:
            logger_info.info('[HubGeoInformation][get][' +
                             str(auth_info.open_id) + ']')

        try:
            hub_manager = HubManagerV2(logger_info, logger_error)
            hub_list = hub_manager.get_hub_list(self.DEFAULT_HUB,
                                                lang_info.cn_header,
                                                lang_info.target_db)
        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, self.SUCCESS_MSG)
            result.set('hub_list', hub_list)

        return Response(result.get_response(), result.get_code())
Ejemplo n.º 3
0
    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 put(self, request, hub_id):
        self.logger_info.info('[UserAddressHubSelector][put][' + str(hub_id) + ']')
        auth_info = header_parser.parse_auth_info(request)
        lang_info = parse_language_v2(request.META)

        try:
            check_auth_info_v2(auth_info.open_id, auth_info.access_token)

            address_manager = AddressManagerV2(self.logger_info, self.logger_error)
            address_list = address_manager.select_hub(auth_info.open_id, lang_info.cn_header, int(hub_id))
        except DataValidationError as instance:
            message, err_code = instance.args
            result = ResultResponse(code.ARIES_400_BAD_REQUEST, message, err_code)
        except AuthInfoError:
            result = ResultResponse(code.ARIES_401_UNAUTHORIZED, 'Authentication error')
        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('user_addresses', address_list)

        return Response(result.get_response(), result.get_code())
    def get(self, request):
        auth_info = header_parser.parse_auth_info(request)
        lang_info = header_parser.parse_language_v2(request.META)

        logger_info.info('[views_coupon][CouponDetail][get][' +
                         str(auth_info.open_id) + ']')

        try:
            coupon_manager = CouponManagerV3(logger_info, logger_error)
            result_map = coupon_manager.get_coupon_detail(
                auth_info.open_id, lang_info.target_db, auth_info.access_token)
        except DataValidationError as instance:
            message, err_code = instance.args
            result = ResultResponse(code.ARIES_400_BAD_REQUEST, message,
                                    err_code)
        except AuthInfoError:
            result = ResultResponse(code.ARIES_401_UNAUTHORIZED,
                                    'Authentication error')
        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, self.SUCCESS_MSG)
            result.set_map(result_map)

        return Response(result.get_response(), result.get_code())
Ejemplo n.º 6
0
    def post(self, request):
        request_data = request.data
        self.logger_info.info('[UserReceiptList][post][' + str(request_data) +
                              ']')
        auth_info = header_parser.parse_auth_info(request)
        lang_info = parse_language_v2(request.META)

        try:
            check_auth_info_v2(auth_info.open_id, auth_info.access_token)

            receipt_util.request_validation(auth_info.open_id, request_data)
            receipt_manager = ReceiptManager(self.logger_info,
                                             self.logger_error)
            receipt_list = receipt_manager.create_user_receipt(
                auth_info.open_id, request_data, lang_info.cn_header)
        except DataValidationError as instance:
            message, err_code = instance.args
            result = ResultResponse(code.ARIES_400_BAD_REQUEST, message,
                                    err_code)
        except AuthInfoError:
            result = ResultResponse(code.ARIES_401_UNAUTHORIZED,
                                    'Authentication error')
        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('user_receipts', receipt_list)

        return Response(result.get_response(), result.get_code())
    def post(self, request):
        language_info = header_parser.parse_language_v2(request.META)
        request_data = request.data

        logger_info.info('[views_coupon][CouponInformation][post][' +
                         str(request.data) + ']')

        try:
            coupon_manager = CouponManagerV3(logger_info, logger_error)
            coupon_info_list = coupon_manager.read_coupon_information(
                request_data, language_info.target_db)
        except DataValidationError as instance:
            message, err_code = instance.args
            result = ResultResponse(code.ARIES_400_BAD_REQUEST, message,
                                    err_code)
        except AuthInfoError:
            result = ResultResponse(code.ARIES_401_UNAUTHORIZED,
                                    'Authentication error')
        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('coupon_info', coupon_info_list)

        return Response(result.get_response(), result.get_code())
    def get(self, request):
        language_info = header_parser.parse_language_v2(request.META)

        referral_manager = ReferralManager(self.logger_info, self.logger_error)

        try:
            referral_info = referral_manager.get_referral_information(
                language_info.accept_lang)
        except AuthInfoError:
            result = ResultResponse(code.ARIES_401_UNAUTHORIZED,
                                    'Authentication error')
        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('friend_coupon_status',
                       referral_info['friend_coupon_status'])
            result.set('first_coupon_status',
                       referral_info['first_coupon_status'])

        return Response(result.get_response(), result.get_code())
    def post(self, request):
        request_data = request.data
        logger_info.info(request_data)

        lang_info = parse_language_v2(request.META)

        try:
            login_type = request_data['login_type']
            sign_in_manager = SignInManager(logger_info, logger_error)
            sign_in_manager.set_login_info(login_type, request_data,
                                           lang_info.accept_lang)
        except Exception as e:
            logger_info.info(str(e))
            result = ResultResponse(code.ARIES_400_BAD_REQUEST,
                                    'Request data is invalid')
            return Response(result.get_response(), result.get_code())

        if sign_in_manager.login_validation():
            result = sign_in_manager.do_sign_in()
            sign_in_manager.update_user_information(request_data)
        else:
            logger_info.info('ID or password is incorrect')
            result = ResultResponse(code.ARIES_400_BAD_REQUEST,
                                    'ID or password is incorrect.')

        return Response(result.get_response(), result.get_code())
Ejemplo n.º 10
0
    def get(self, request, hub_id):
        result = ResultResponse(code.ARIES_200_SUCCESS, 'success')

        auth_info = header_parser.parse_authentication(request)
        lang_info = header_parser.parse_language_v2(request.META)

        promotion_manager = PromotionManager(logger_info, logger_error,
                                             lang_info.target_db)
        promotion_manager.update_notification_count(auth_info)

        page = int(request.GET.get('page', 1))
        limit = int(request.GET.get('limit', 10))
        os_type = int(request.GET.get('os_type', 2))

        try:
            # Get promotion list
            promotion_list_result = promotion_manager.get_promotion_list(
                page, limit, hub_id, os_type)
            promotion_count = promotion_list_result[0]
            promotion_list = promotion_list_result[1]

            result.set('total_count', promotion_count)
            result.set('promotions', promotion_list)
        except Exception as e:
            logger_info.info(str(e))
            result = ResultResponse(code.ARIES_400_BAD_REQUEST,
                                    'Request data error')
            return Response(result.get_response(), result.get_code())

        return Response(result.get_response(), result.get_code())
    def get(self, request, hub_id):
        lang_info = parse_language_v2(request.META)
        lang_type = 1 if lang_info.cn_header else 0
        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_id = time_bomb_manager.get_time_bomb_now(hub_id, os_type)

            if time_bomb_id is None:
                time_bomb = {'status': 0}
            else:
                time_bomb = time_bomb_manager.get_time_bomb(
                    time_bomb_id, lang_type, os_type)
        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)

        return Response(result.get_response(), result.get_code())
    def post(self, request, hub_id, time):
        result = ResultResponse(code.ARIES_200_SUCCESS, self.SUCCESS)
        logger_info.info(str(time))

        lang_info = header_parser.parse_language_v2(request.META)

        cn_header = lang_info.cn_header
        target_db = lang_info.target_db
        os_type = lang_info.os_type

        try:
            product_list = request.data['product_list']
        except Exception as e:
            print(e)
            result = ResultResponse(code.ARIES_400_BAD_REQUEST,
                                    'Request data invalid')
            return Response(result.get_response(), result.get_code())

        product_manager = ProductManagerV3(logger_info, logger_error)
        menu_manager = MenuManager(logger_info, logger_error)
        restaurant_manager = RestaurantManager(logger_info, logger_error)
        hub_manager = HubManagerV2(logger_info, logger_error)

        product_list = hub_manager.get_product_list(product_list, hub_id)
        stock_list = hub_manager.get_stock_list(hub_id, product_list,
                                                target_db, os_type)

        for product in stock_list:
            # Get product data
            product_data = product_manager.get_product_for_valid(
                product['product_id'], cn_header)

            # Set time bomb information
            if 'time_bomb_info' in product:
                product['time_bomb_info']['stock'] = product['stock']
                product['time_bomb_info']['has_stock'] = True
                product_data['price_discount'] = product['price_discount']
                product_data['event_product'] = product['event_product']
                product_data['price_discount_event'] = True

            product['product'] = product_data

            # Get menu data
            menu_id = product_data['menu']
            menu_data = menu_manager.get_menu_data(target_db, menu_id,
                                                   product_data['sales_time'],
                                                   cn_header)

            menu_data['review_statics'] = menu_manager.get_menu_statics(
                menu_id)
            menu_data['restaurant'] = restaurant_manager.get_restaurant_data(
                menu_data['restaurant'], target_db)

            product['product']['menu'] = menu_data

        result.set('product_list', stock_list)

        return Response(result.get_response(), result.get_code())
Ejemplo n.º 13
0
    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())
Ejemplo n.º 14
0
    def post(self, request):
        lang_info = header_parser.parse_language_v2(request.META)

        request_data = request.data
        logger_info.info('[views_referral_coupon][PromotionCoupon][post][' +
                         str(request_data) + ']')

        try:
            member_coupon_issue_validation(request_data)

            open_id = request_data['open_id']
            promotion_type = request_data['promotion_type']
            promotion_manager = PromotionManager(logger_info, logger_error,
                                                 lang_info)

            lang_type = 1 if lang_info.cn_header else 0
            member_promotion = promotion_manager.get_promotion_list_with_latest(
                promotion_type, lang_type)

            if member_promotion is None:
                has_member_promotion = False
                member_promotion = {}
            else:
                has_member_promotion = True

                coupon_id = member_promotion['coupon_id']
                coupon_days = member_promotion['coupon_days']
                coupon_code = member_promotion['coupon_code']
                sender_id = member_promotion['sender_id']

                coupon_manager = CouponManagerV3(logger_info, logger_error)
                coupon_manager.create_coupon_with_promotion(
                    open_id, coupon_id, coupon_days, coupon_code, sender_id)

                member_promotion = member_promotion_filtering(member_promotion)
        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, self.SUCCESS_MSG)
            result.set('has_member_promotion', has_member_promotion)
            result.set('member_promotion', member_promotion)

        return Response(result.get_response(), result.get_code())
    def post(self, request):
        auth_info = header_parser.parse_auth_info(request)
        lang_info = header_parser.parse_language_v2(request.META)

        request_data = request.data

        self.logger_info.info('[ReferralCoupon][post][' +
                              str(auth_info.open_id) +
                              str(auth_info.access_token) + ']')
        self.logger_info.info('[ReferralCoupon][post][' + str(request_data) +
                              ']')

        try:
            referral_authentication_validation(auth_info)
            coupon_issue_validation(request_data)

            open_id = auth_info.open_id
            coupon_list = request_data['coupon_list']
            coupon_type = request_data['coupon_type']

            referral_manager = ReferralManager(self.logger_info,
                                               self.logger_error)
            res_map = referral_manager.issue_referral_coupon(
                open_id, lang_info.cn_header, coupon_type, coupon_list)
            update_coupon_count_no_log(auth_info.open_id,
                                       auth_info.access_token, 0,
                                       len(coupon_list))
        except DataValidationError as instance:
            message, err_code = instance.args
            result = ResultResponse(code.ARIES_400_BAD_REQUEST, message,
                                    err_code)
        except AuthInfoError:
            result = ResultResponse(code.ARIES_401_UNAUTHORIZED,
                                    'Authentication error')
        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_map(res_map)

        return Response(result.get_response(), result.get_code())
Ejemplo n.º 16
0
    def get(self, request, promotion_id):
        result = ResultResponse(code.ARIES_200_SUCCESS, 'success')

        lang_info = header_parser.parse_language_v2(request.META)
        promotion_manager = PromotionManager(logger_info, logger_error,
                                             lang_info.target_db)

        try:
            promotion_data = promotion_manager.get_promotion(promotion_id)
            result.set('promotion', promotion_data)
        except Exception as e:
            logger_info.info(str(e))
            result = ResultResponse(code.ARIES_400_BAD_REQUEST,
                                    'Request data error')
            return Response(result.get_response(), result.get_code())

        return Response(result.get_response(), result.get_code())
    def get(self, request, hub_id):
        self.logger_info.info('[AdminProductV2][get][' + hub_id + ']')

        lang_info = parse_language_v2(request.META)
        target_db = lang_info.target_db
        cn_header = lang_info.cn_header

        today_str = str(datetime.datetime.today())[:10]
        start_date = str(request.GET.get('start_date', today_str))
        end_date = str(request.GET.get('end_date', today_str))

        try:
            product_manager = ProductManagerV3(self.logger_info, self.logger_error)
            product_list = product_manager.get_product_list_with_query(hub_id, start_date, end_date)

            menu_manager = MenuManagerV2(self.logger_info, self.logger_error)

            for product in product_list:
                menu_manager.get_menu_data(product, target_db, cn_header)
                menu_data = product['menu']
                product['menu'] = {
                    'image_main': menu_data['image_main'],
                    'name': menu_data['name'],
                    'restaurant_name': menu_data['restaurant']['name']
                }

                product_data = product_manager.get_product_data(product['id'])
                product['badge_en'] = json.loads(product_data['badge_en'])
                product['badge_cn'] = json.loads(product_data['badge_cn'])
        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('product_list', product_list)
            result.set('hub_id', int(hub_id))

        return Response(result.get_response(), result.get_code())
    def get(self, request):
        result = ResultResponse(code.ARIES_200_SUCCESS, 'success')
        lang_info = header_parser.parse_language_v2(request.META)

        accept_lang = lang_info.accept_lang
        os_type = lang_info.os_type

        try:
            open_id = request.META['HTTP_OPEN_ID']
            access_token = str(request.META['HTTP_AUTHORIZATION']).split(' ')[1]
        except Exception as e:
            logger_info.info(str(e))
            result = ResultResponse(code.ARIES_401_UNAUTHORIZED, 'Authorization error')
            return Response(result.get_response(), result.get_code())

        try:
            # Get upcoming order list
            order_manager = OrderManager(logger_info, logger_error)
            order_list = order_manager.get_orders(order_manager.UPCOMING_ORDER, {
                'open_id': open_id,
                'order_status__lt': 3
            })
            result.set('upcoming_orders', order_list)

            # Get user default hub id
            user_information = api_request_util.get_user_information(open_id, access_token)
            hub_id = user_information[1]['user']['default_hub_id'] if user_information[0] else 1

            # Recommend product list
            product_list = api_request_util.get_recommend_products(accept_lang, hub_id, os_type)
            result.set('products', product_list)

            # Check upcoming order notification
            api_request_util.read_upcoming_order(open_id, access_token)
        except Exception as e:
            logger_info.info(str(e))
            result = ResultResponse(code.ARIES_500_INTERNAL_SERVER_ERROR, str(e))
            return Response(result.get_response(), result.get_code())

        return Response(result.get_response(), result.get_code())
    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())
Ejemplo n.º 20
0
    def get(self, request):
        self.logger_info.info('[HubDelivery][get]')
        default_hub = 1
        lang_info = parse_language_v2(request.META)

        try:
            hub_manager = HubManagerV2(logger_info, logger_error)
            delivery_hub_list = hub_manager.get_hub_delivery(
                lang_info.cn_header, lang_info.target_db)
        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('delivery_hub_list', delivery_hub_list)

        return Response(result.get_response(), result.get_code())
    def get(self, request):
        auth_info = header_parser.parse_auth_info(request)
        language_info = header_parser.parse_language_v2(request.META)

        logger_info.info('[views_coupon][CouponPage][get][' +
                         auth_info.open_id + ',' + auth_info.access_token +
                         ']')

        try:
            coupon_manager = CouponManagerV3(logger_info, logger_error)
            check_auth_info_v2(auth_info.open_id, auth_info.access_token)

            page = int(request.GET.get('page', 1))
            limit = int(request.GET.get('limit', 20))

            coupon_data = coupon_manager.get_coupon_list(
                auth_info.open_id, auth_info.access_token,
                language_info.target_db, page, limit, self.EXPIRED_DAY)
        except DataValidationError as instance:
            message, err_code = instance.args
            result = ResultResponse(code.ARIES_400_BAD_REQUEST, message,
                                    err_code)
        except AuthInfoError:
            result = ResultResponse(code.ARIES_401_UNAUTHORIZED,
                                    'Authentication error')
        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_map(coupon_data)

        return Response(result.get_response(), result.get_code())
    def get(self, request, restaurant_id):
        result = ResultResponse(code.ARIES_200_SUCCESS, 'success')

        language_info = header_parser.parse_language_v2(request.META)
        date_info = product_util.get_date_information(1, self.LUNCH_TIME)
        target_db = language_info[1]

        try:
            restaurant_manager = RestaurantManager(self.logger_info,
                                                   self.logger_error)
            brand_data = restaurant_manager.get_brand(target_db, restaurant_id)

            brand_id = brand_data['id']
            result.set('brand', brand_data)

            try:
                brand_time = product_util.get_sales_time()
                product_manager = ProductManager(self.logger_info,
                                                 self.logger_error,
                                                 language_info, date_info)
                product_list = product_manager.get_brand_product_list(
                    self.PRODUCT_BRAND, brand_id, brand_time)
            except Exception as e:
                print(str(e))
                result.set('products', [])
            else:
                result.set('products', product_list)

        except ObjectDoesNotExist:
            result = ResultResponse(code.ARIES_400_BAD_REQUEST,
                                    'Brand info not found.')
        except Exception as e:
            self.logger_info.info(str(e))
            result = ResultResponse(code.ARIES_400_BAD_REQUEST,
                                    'Error : ' + str(e))

        return Response(result.get_response(), result.get_code())
    def get(self, request):
        result = ResultResponse(code.ARIES_200_SUCCESS, 'success')
        lang_info = header_parser.parse_language_v2(request.META)

        accept_lang = lang_info.accept_lang
        os_type = lang_info.os_type

        try:
            open_id = request.META['HTTP_OPEN_ID']
            access_token = str(request.META['HTTP_AUTHORIZATION']).split(' ')[1]
        except Exception as e:
            logger_info.info(str(e))
            result = ResultResponse(code.ARIES_401_UNAUTHORIZED, 'Authorization error')
            return Response(result.get_response(), result.get_code())

        try:
            # Get past order list
            order_manager = OrderManager(logger_info, logger_error)
            order_list = order_manager.get_orders(order_manager.PAST_ORDER, {
                'open_id': open_id,
                'order_status__gt': 9
            })
            result.set('past_orders', order_list)

            # Get user default hub id
            user_information = api_request_util.get_user_information(open_id, access_token)
            hub_id = user_information[1]['user']['default_hub_id'] if user_information[0] else 1

            # Recommend product list
            product_list = api_request_util.get_recommend_products(accept_lang, hub_id, os_type)
            result.set('products', product_list)
        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())

        return Response(result.get_response(), result.get_code())
    def get(self, request):
        auth_info = header_parser.parse_auth_info(request)
        language_info = header_parser.parse_language_v2(request.META)

        self.logger_info.info('[views_referral][ReferralBoard][get][' +
                              str(auth_info.open_id) +
                              str(auth_info.access_token) + ']')

        referral_manager = ReferralManager(self.logger_info, self.logger_error)

        try:
            referral_authentication_validation(auth_info)

            open_id = auth_info.open_id
            access_token = auth_info.access_token
            accept_lang = language_info.accept_lang

            referral_event = referral_manager.get_referral_status(
                open_id, access_token, accept_lang)
            request_notify_info(open_id, access_token, False)
        except AuthInfoError:
            result = ResultResponse(code.ARIES_401_UNAUTHORIZED,
                                    'Authentication error')
        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('referral_event', referral_event)

        return Response(result.get_response(), result.get_code())
    def post(self, request):
        result = ResultResponse(code.ARIES_200_SUCCESS, 'success')
        request_data = request.data
        lang_info = parse_language_v2(request.META)

        logger_info.info(request_data)

        try:
            login_type = request_data['login_type']
            login_key = request_data['login_key']
            login_value = request_data['login_value']
            login_sns_open_id = ''
            login_sns_access_token = ''
            login_sns_refresh_token = ''

            login_type = int(login_type)
        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())

        # Mobile app/web: 0, QQ: 1, WeChat: 2, Password registration: 3
        # Check sms verification & user count
        if login_type == 0:
            user_count = User.objects.filter(mdn=login_key).count()

            # MDN is only one in member pool
            if user_count >= 1:
                result = ResultResponse(
                    code.ARIES_500_INTERNAL_SERVER_ERROR,
                    get_msg(code.ERROR_1005_USER_ALREADY_REGISTRATION))
                result.set_error(code.ERROR_1005_USER_ALREADY_REGISTRATION)
                logger_error.error(
                    get_msg(code.ERROR_1005_USER_ALREADY_REGISTRATION))
                return Response(result.get_response(), result.get_code())

            today = datetime.datetime.today()
            sms_auth = SmsAuthHistory.objects.filter(
                date=today, target_mdn=login_key).latest('id')
            sms_auth_count = sms_auth.verification_count

            if not sms_auth.has_verified or sms_auth_count > 3:
                result = ResultResponse(
                    code.ARIES_500_INTERNAL_SERVER_ERROR,
                    get_msg(code.ERROR_1008_SMS_VALIDATION_INFO_NOT_FOUND))
                result.set_error(code.ERROR_1008_SMS_VALIDATION_INFO_NOT_FOUND)
                logger_error.error(
                    get_msg(code.ERROR_1008_SMS_VALIDATION_INFO_NOT_FOUND))
                return Response(result.get_response(), result.get_code())

            response = requests.get(
                urlmapper.get_url('USER_SMS_VERIFICATION') + login_key)

            if response.status_code != code.ARIES_200_SUCCESS:
                result = ResultResponse(
                    code.ARIES_500_INTERNAL_SERVER_ERROR,
                    get_msg(code.ERROR_1008_SMS_VALIDATION_INFO_NOT_FOUND))
                result.set_error(code.ERROR_1008_SMS_VALIDATION_INFO_NOT_FOUND)
                logger_error.error(
                    get_msg(code.ERROR_1008_SMS_VALIDATION_INFO_NOT_FOUND))
                return Response(result.get_response(), result.get_code())

            # create open id
            random_token = str(uuid.uuid4()).split('-')
            open_id = str(random_token[0] + random_token[2] +
                          random_token[4]).upper()

            # get token from server
            if login_type is 0:
                payload = {'user_open_id': open_id, 'user_account': login_key}
            else:
                payload = {
                    'user_open_id': open_id,
                    'user_account': login_sns_open_id
                }
            response = requests.post(urlmapper.get_url('PLATFORM_SERVER'),
                                     json=payload)

            if response.status_code == code.ARIES_200_SUCCESS:
                response_json = response.json()
                token = response_json['token']
                token = str(token).upper()
            else:
                result = ResultResponse(code.ARIES_500_INTERNAL_SERVER_ERROR,
                                        'API connection fail')
                result.set_error(code.ERROR_9000_INTERNAL_API_CALL_FAILED)
                logger_error.error(response.text)
                return Response(result.get_response(), result.get_code())

            # Sign-up user
            if login_type is 0:
                user_instance = User.objects.create(open_id=open_id,
                                                    mdn=login_key,
                                                    name=login_key,
                                                    mdn_verification=True,
                                                    access_token=token,
                                                    parent_type=0)
            else:
                user_instance = User.objects.create(open_id=open_id,
                                                    mdn='',
                                                    name=open_id[:6],
                                                    mdn_verification=False,
                                                    access_token=token)

            UserLoginInfo.objects.create(
                user=user_instance,
                login_type=login_type,
                login_key=login_key,
                login_value=login_value,
                login_sns_open_id=login_sns_open_id,
                login_sns_access_token=login_sns_access_token,
                login_sns_refresh_token=login_sns_refresh_token)

            UserInfo.objects.create(user=user_instance, number_of_logon=0)

            user_notify_info = UserNotifyInfo.objects.create(
                user=user_instance)

            UserGrade.objects.create(user=user_instance,
                                     type=0,
                                     extra_meal_point=0,
                                     upgrade_date=datetime.datetime.now())

            ShoppingBag.objects.create(user=user_instance, )

            user_serializer = UserAccountSerializer(user_instance)
            user_data = user_serializer.data

            # User notify data
            notify_info = UserNotifyInfo.objects.get(user=user_instance)
            notify_info_serializer = UserNotifyInfoSerializer(notify_info)
            notify_info_data = notify_info_serializer.data
            del notify_info_data['id']
            del notify_info_data['user']
            user_data['notification_info'] = notify_info_data

            # User grade data
            user_grade = UserGrade.objects.get(user=user_instance)
            grade_serializer = UserGradeSerializer(user_grade)
            user_data['grade'] = grade_serializer.data
            user_data['connection_account'] = json.loads(
                user_data['connection_account'])

            result.set('auto_registration', False)
            result.set('user', user_data)

            # If there is Member promotion, call member coupon interface
            member_promo_result = request_member_promotion(
                open_id, 0, lang_info.accept_lang)
            result.set_map(member_promo_result)
            logger_info.info('[views_sign][SignUp][post][' +
                             str(member_promo_result) + ']')

            if member_promo_result['has_member_promotion']:
                user_notify_info.has_new_coupon = True
                user_notify_info.coupon_count = 1
                user_notify_info.save()

            # If there is a registration with referrer, update referrer count
            if 'referrer_id' in request_data:
                logger_info.info('[views_sign][SignUp][post][' +
                                 'REFERRER SIGNUP' + ']')
                user_mdn = user_instance.mdn
                user_referral_service = UserReferralService(
                    logger_info, logger_error)

                # Check if its mdn was used for registration before
                registration_check = user_referral_service.read_user_referral_info_check(
                    user_mdn)

                if registration_check:
                    share_id = request_data['referrer_id']
                    referrer_open_id = request_share_id_validation(share_id)

                    # Save User referral history
                    logger_info.info('[views_sign][SignUp][post][' +
                                     str(open_id) + ']')
                    logger_info.info('[views_sign][SignUp][post][' +
                                     str(user_mdn) + ']')
                    logger_info.info('[views_sign][SignUp][post][' +
                                     str(share_id) + ']')
                    logger_info.info('[views_sign][SignUp][post][' +
                                     str(referrer_open_id) + ']')

                    ref = user_referral_service.create_user_referral_info(
                        open_id, user_mdn, share_id, referrer_open_id)
                    logger_info.info('[views_sign][SignUp][post][' + str(ref) +
                                     ']')

                    user_service = UserService(logger_info, logger_error)
                    referrer_user = user_service.get_user_instance(
                        referrer_open_id)
                    referrer_notify_info = user_service.get_user_notify_info(
                        referrer_user)
                    referrer_notify_info.has_referral_event = True
                    referrer_notify_info.save()
                    logger_info.info(
                        '[views_sign][SignUp][post][Notification information saved]'
                    )

                    # Send notification
                    user_manager_v2 = UserManagerV2(logger_info, logger_error)
                    user_manager_v2.send_sign_up_push(referrer_open_id, True)
                    logger_info.info(
                        '[views_sign][SignUp][post][Send push completed]')
                else:
                    logger_info.info(
                        '[views_sign][SignUp][post][Referral already registration.]'
                    )

            # Change user info
            user_info = UserInfo.objects.get(user=user_instance)
            user_info.number_of_logon = F('number_of_logon') + 1
            user_info.date_account_last_modified = datetime.datetime.now()
            if 'os_type' in request_data:
                if request_data['os_type'] == 0 or request_data['os_type'] == 1:
                    user_info.os_type = request_data['os_type']
            user_info.save()

            return Response(result.get_response(), status=result.get_code())

        else:
            result = ResultResponse(code.ARIES_400_BAD_REQUEST,
                                    'Sign up method not supported')
            logger_info.info('Sign up method not supported')
            return Response(result.get_response(), result.get_code())
    def post(self, request):
        result = ResultResponse(code.ARIES_200_SUCCESS, 'success')

        request_data = request.data
        self.logger_info.info(request_data)

        auth_info = header_parser.parse_auth_info(request)
        lang_info = parse_language_v2(request.META)

        open_id = auth_info.open_id
        access_token = auth_info.access_token
        cn_header = lang_info.cn_header
        accept_lang = lang_info.accept_lang
        os_type = lang_info.os_type
        hub_id = request_data.get('hub_id', 1)

        try:
            # Order reserve manager
            reserve_manager = OrderReserveManager(self.logger_info, self.logger_error, request_data)

            # Request data validation
            request_validation(open_id, access_token, request_data)

            # Request product validation
            validation_result = purchase_validation(accept_lang, request_data, os_type)

            # Product check
            valid_result = reserve_manager.product_validation(validation_result, cn_header)

            # Sales time check
            delivery_time_result = reserve_manager.delivery_time_validation(valid_result, accept_lang,
                                                                            os_type, hub_id)

            # Product detail setting
            product_list = reserve_manager.set_product_detail(delivery_time_result)

            # Delivery detail check
            reserve_manager.set_delivery_detail(lang_info.cn_header)

            # Coupon business logic process, product list from sales_time_response
            coupon_list = request_data['coupon_list']

            coupon_manager = CouponManagerV2(self.logger_info, self.logger_error)
            coupon_manager.coupon_validate(auth_info.open_id, product_list, coupon_list)

            # Calculate total price
            coupon_detail = coupon_manager.get_coupon_detail()
            discount_amount = -coupon_manager.get_discount_price()
            reserve_manager.set_total_price(coupon_detail, discount_amount, open_id)

            # Set misc parameters
            reserve_manager.set_misc_params()

            # Get payment params from payment parameter factory
            param_result = reserve_manager.get_payment_param(cn_header)
            payment_param_factory = PayParamInstance.factory(param_result[0], self.logger_info, self.logger_error)
            payment_params = payment_param_factory.get_payment_params(param_result[1])

            for key in payment_params.keys():
                result.set(key, payment_params[key])

            # Save purchase order object to database
            order_id = reserve_manager.save_purchase_order()
        except DataValidationError as instance:
            message, err_code = instance.args
            result = ResultResponse(code.ARIES_400_BAD_REQUEST, message, err_code)
        except AuthInfoError:
            result = ResultResponse(code.ARIES_401_UNAUTHORIZED, 'Authentication error')
        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.set('order_id', order_id)

        return Response(result.get_response(), result.get_code())
    def get(self, request, hub_id):
        lang_info = parse_language_v2(request.META)
        today = datetime.datetime.today()

        try:
            # Latest schedule
            target_schedule = DeliverySchedule.objects.filter(
                hub_id=int(hub_id)).latest('-id')
            delivery_day = today

            # After 9:00 pm, next day setting
            if delivery_day.time() > target_schedule.delivery_end:
                next_day = today + datetime.timedelta(days=1)
            else:
                next_day = today

            time_slots = json.loads(target_schedule.delivery_time_table)

            available_time = 0
            matched_table = []

            for target_slot in time_slots:
                time = datetime.datetime.strptime(
                    next_day.strftime("%Y.%m.%d") + ' ' +
                    target_slot["starttime"], "%Y.%m.%d %I:%M %p")

                if today > time:
                    available_time += 1
                else:
                    table = {
                        'index': target_slot['index'],
                        'starttime': target_slot['starttime'],
                        'endtime': target_slot['endtime'],
                        'delivery_price': target_schedule.delivery_price
                    }
                    matched_table.append(table)

            minimum_order_price = target_schedule.minimum_order_price
            order_available = target_schedule.order_available

            if lang_info.cn_header:
                unavailable_msg = target_schedule.unavailable_msg_chn
            else:
                unavailable_msg = target_schedule.unavailable_msg_eng

            if not order_available:
                order_unavailable_msg = unavailable_msg
            else:
                order_unavailable_msg = ''

        except Exception as e:
            result = ResultResponse(code.ARIES_400_BAD_REQUEST, str(e))
        else:
            result = ResultResponse(code.ARIES_200_SUCCESS, self.SUCCESS_MSG)
            result.set('working_day', next_day.strftime('%Y.%m.%d'))
            result.set('timetable', matched_table)
            result.set('order_available', order_available)
            result.set('minimum_order_price', minimum_order_price)
            result.set('order_unavailable_message', order_unavailable_msg)

        return Response(result.get_response(), result.get_code())
    def get(self, request, order_id):
        result = ResultResponse(code.ARIES_200_SUCCESS, 'success')

        try:
            open_id = request.META['HTTP_OPEN_ID']
            access_token = str(request.META['HTTP_AUTHORIZATION']).split(' ')[1]
        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())

        lang_info = header_parser.parse_language_v2(request.META)
        accept_lang = lang_info.accept_lang
        os_type = lang_info.os_type

        # Check order_id length
        if len(order_id) < 17:
            logger_error.error(code.ERROR_9001_PARAMETER_VALIDATION_FAILED)
            result = ResultResponse(code.ARIES_500_INTERNAL_SERVER_ERROR,
                                    get_msg(code.ERROR_9001_PARAMETER_VALIDATION_FAILED))
            result.set_error(code.ERROR_9001_PARAMETER_VALIDATION_FAILED)
            return Response(result.get_response(), result.get_code())

        # Request data parsing
        try:
            order = Order.objects.get(open_id=open_id, order_id=order_id)
            purchase_order = PurchaseOrder.objects.get(order_id=order.order_id)

            purchase_order_serializer = UpcomingPurchaseOrderSerializer(purchase_order)
            purchase_order_data = purchase_order_serializer.data
            purchase_order_data['order_details'] = json.loads(purchase_order_data['order_details'])
            purchase_order_data['shipping_detail'] = json.loads(purchase_order_data['shipping_detail'])
            del purchase_order_data['product_list']
            del purchase_order_data['coupon_list']
            del purchase_order_data['order_hash']

            if purchase_order_data['extra_telephone'] is None:
                purchase_order_data['extra_telephone'] = ''
            if purchase_order_data['special_instruction'] is None:
                purchase_order_data['special_instruction'] = ''

            order_serializer = UpcomingOrderSerializer(order)
            order_data = order_serializer.data
            order_data['purchase_order'] = purchase_order_data
            start_date = order.order_start_date.strftime(resources.DATE_WITH_AM_PM)
            order_data['order_start_date'] = dateformatter.get_yymmdd_time(start_date)
            del order_data['order_status_history']

            if order.order_status == 11:
                order_cancel_date = order.order_cancel_date
                if order_cancel_date is not None:
                    order_cancel_date = order.order_cancel_date.strftime(resources.DATE_WITH_AM_PM)
                    order_data['order_cancel_date'] = dateformatter.get_yymmdd_time(order_cancel_date)
                else:
                    order_data['order_cancel_date'] = ''
            else:
                order_data['order_cancel_date'] = ''

            result.add(order_data)

            product_list = json.loads(purchase_order.product_list)

            headers = {'accept-language': accept_lang}
            json_data = {
                'product_list': product_list,
                'open_id': open_id,
                'order_id': order_id,
                'hub_id': purchase_order_data['hub_id']
            }

            response = requests.post(urlmapper.get_url('MENU_VALIDATE'), headers=headers, json=json_data)
            response_json = response.json()

            result.set('reviews', response_json['reviews'])
            result.set('review_items', response_json['review_items'])
        except Exception as e:
            logger_info.info(str(e))
            print(str(e))
            result = ResultResponse(code.ARIES_400_BAD_REQUEST, 'Request data invalid')
            return Response(result.get_response(), result.get_code())

        # Get user default hub id
        user_information = api_request_util.get_user_information(open_id, access_token)
        hub_id = user_information[1]['user']['default_hub_id'] if user_information[0] else 1

        # Recommend product list
        product_list = api_request_util.get_recommend_products(accept_lang, hub_id, os_type)
        result.set('products', product_list)

        return Response(result.get_response(), result.get_code())
Ejemplo n.º 29
0
    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())
Ejemplo n.º 30
0
    def get(self, request):

        auth_info = header_parser.parse_auth_info(request)
        lang_info = parse_language_v2(request.META)

        open_id = auth_info.open_id
        access_token = auth_info.access_token
        cn_header = lang_info.cn_header
        accept_lang = lang_info.accept_lang

        try:
            check_auth_info_v2(auth_info.open_id, auth_info.access_token)

            user_manager = UserManagerV2(self.logger_info, self.logger_error)

            # User information
            user_data = user_manager.get_user_data(open_id)
            user_info = user_manager.get_user_info(open_id)

            # Default hub id
            default_hub_id = user_data['default_hub_id']

            # Latest payment method and shipping method
            latest_payment_method = user_info['latest_payment_method']
            latest_shipping_method = user_info['latest_shipping_method']

            # Latest cart information and special instruction template
            cart_info = user_manager.get_user_cart_info_data(open_id)
            include_cutlery = cart_info['include_cutlery']
            user_inst_template = cart_util.get_user_inst_list(cn_header, cart_info['instruction_history'])
            special_instruction_template = cart_util.get_sp_inst_template(cn_header)

            # Delivery time table information (map)
            delivery_map = cart_util.get_delivery_time(default_hub_id, lang_info.accept_lang)

            # Add order unavailable message
            order_available = delivery_map['order_available']
            order_un_msg = payment_util.get_order_unavailable_msg(cn_header) if not order_available else ''

            # Purchase information (map)
            purchase_map = cart_util.get_purchases_data(open_id, access_token, accept_lang)

            # Next day information
            phase_next_day = product_util.get_phase_next_day()

            # Recommend product information
            products = cart_util.get_recommend_product(default_hub_id, accept_lang)
        except DataValidationError as instance:
            message, err_code = instance.args
            result = ResultResponse(code.ARIES_400_BAD_REQUEST, message, err_code)
        except AuthInfoError:
            result = ResultResponse(code.ARIES_401_UNAUTHORIZED, 'Authentication error')
        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('latest_payment_method', latest_payment_method)
            result.set('latest_shipping_method', latest_shipping_method)
            result.set('special_instruction_template', special_instruction_template + user_inst_template)
            result.set('order_unavailable_message', order_un_msg)
            result.set('phase_next_day', phase_next_day)
            result.set('products', products)
            result.set('include_cutlery', include_cutlery)
            result.set_map(delivery_map)
            result.set_map(purchase_map)

        return Response(result.get_response(), result.get_code())