Ejemplo n.º 1
0
    def update_rateplans_valid_status(self, rateplan_ids):
        Log.info("<< push rateplans {} update rateplan valid>>".format(
            rateplan_ids))
        if not IS_PUSH_TO_STOCK:
            raise gen.Return(True)

        rateplans = RatePlanModel.get_by_ids(self.db,
                                             rateplan_ids,
                                             with_delete=True)
        rateplan_datas = [{
            "chain_id": CHAIN_ID,
            "hotel_id": rateplan.hotel_id,
            "rate_plan_id": rateplan.id,
            "is_valid": self.cal_rateplan_isvalid(rateplan)
        } for rateplan in rateplans]

        track_id = self.generate_track_id(rateplan_ids)
        data = {'list': rateplan_datas, 'type': 3}
        params = {'track_id': track_id, 'data': json.dumps(data)}
        body = urllib.urlencode(params)
        url = API['STOCK'] + '/stock/update_state?is_async=false'

        Log.info(
            "<< push rateplan {} update rateplan valid request {}>>".format(
                rateplan_ids, params))
        try:
            r = yield AsyncHTTPClient().fetch(url, method='POST', body=body)
            Log.info("<< push rateplan {} update rateplan valid response {}>>".
                     format(rateplan_ids, r.body))
            resp = json.loads(r.body)
        except Exception, e:
            Log.exception(e)
            raise gen.Return(False)
Ejemplo n.º 2
0
def change_ota(hotel_id, ota_ids):
    Log.info("change ota>>hotel {}>>ota {}".format(hotel_id, ota_ids))
    url =  API['STOCK'] + '/stock/switch_ota'
    track_id = generate_track_id(hotel_id)
    if not ota_ids:
        ota_ids_str = '-1'
    else:
        ota_ids_str = '|'.join([str(id) for id in ota_ids])
    data = {
            'track_id': track_id,
            'chain_id': CHAIN_ID,
            'chain_hotel_id': hotel_id,
            'ota_ids': ota_ids_str,
            }
    body = urllib.urlencode(data)
    Log.info("change ota>>hotel {}>>ota {}:req {}".format(hotel_id, ota_ids, body))

    if not IS_PUSH_TO_STOCK:
        raise gen.Return(True)

    try:
        r = yield AsyncHTTPClient().fetch(url, method='POST', body=body)
        Log.info("change ota>>hotel {}>>ota {}:resp {}".format(hotel_id, ota_ids, r.body))
        resp = json.loads(r.body)
    except Exception, e:
        Log.exception(e)
        raise gen.Return(False)
Ejemplo n.º 3
0
 def fetch_base_hotel_and_roomtypes(self, hotel_id):
     url = API['POI'] + "/api/hotel/" + str(hotel_id) + "/roomtype/"
     try:
         r = yield AsyncHTTPClient().fetch(url)
         result = json.loads(r.body)
     except Exception, e:
         Log.exception(e)
         raise gen.Return((None, None))
Ejemplo n.º 4
0
 def fetch_base_hotel_and_roomtypes(self, hotel_id):
     url = API['POI'] + "/api/hotel/" + str(hotel_id) + "/roomtype/"
     try:
         r = yield AsyncHTTPClient().fetch(url)
         result = json.loads(r.body)
     except Exception, e:
         Log.exception(e)
         raise gen.Return((None, None))
Ejemplo n.º 5
0
 def call_weixin(self, order):
     if order.confirm_type == OrderModel.CONFIRM_TYPE_AUTO:
         raise gen.Return()
     url = API['WEIXIN'] + "/webchart/ebkOrderPush"
     body = urllib.urlencode(order.todict())
     try:
         yield AsyncHTTPClient().fetch(url, method='POST', body=body)
     except Exception, e:
         Log.exception(e)
Ejemplo n.º 6
0
 def post_hotel(self, hotel_data):
     Log.info(u"<<POI push hotel mapping>> push request {}".format(hotel_data))
     url = API['POI'] + '/api/push/ebooking/hotel/'
     body = json.dumps(hotel_data)
     try:
         r = yield AsyncHTTPClient().fetch(url, method='POST', headers={"Content-Type": "application/json"}, body=body)
         Log.info("<<POI push hotel mapping>> response {}".format(r.body))
         resp = json.loads(r.body)
     except Exception, e:
         Log.exception(e)
         raise gen.Return(False)
Ejemplo n.º 7
0
 def post_room(self, room_data):
     Log.info(u"<<POI push roomtype mapping>> push request {}".format(room_data))
     url = API['POI'] + '/api/push/ebooking/room/'
     body = json.dumps(room_data)
     if not IS_PUSH_TO_POI:
         raise gen.Return(True)
     try:
         r = yield AsyncHTTPClient().fetch(url, method='POST', headers={"Content-Type": "application/json"}, body=body)
         Log.info("<<POI push roomtype mapping>> response {}".format(r.body))
         resp = json.loads(r.body)
     except Exception, e:
         Log.exception(e)
         raise gen.Return(False)
Ejemplo n.º 8
0
 def post_hotel(self, hotel_data):
     Log.info(
         u"<<POI push hotel mapping>> push request {}".format(hotel_data))
     url = API['POI'] + '/api/push/ebooking/hotel/'
     body = json.dumps(hotel_data)
     try:
         r = yield AsyncHTTPClient().fetch(
             url,
             method='POST',
             headers={"Content-Type": "application/json"},
             body=body)
         Log.info("<<POI push hotel mapping>> response {}".format(r.body))
         resp = json.loads(r.body)
     except Exception, e:
         Log.exception(e)
         raise gen.Return(False)
Ejemplo n.º 9
0
 def call_weixin(self, order):
     if order.confirm_type == OrderModel.CONFIRM_TYPE_AUTO:
         raise gen.Return()
     map_hotels = UserHotelMappingModel.get_hotel_by_id(self.db, merchant_id=order.merchant_id,
                                                       hotel_id=order.hotel_id)
     order_dict = order.todict()
     if map_hotels:
         user_ids = [map_hotel.user_id for map_hotel in map_hotels]
         users = UserModel.get_users_by_id(self.db, user_ids)
         usernames = [user.username for user in users]
         order_dict['usernames'] = ",".join(usernames)
     url = API['WEIXIN'] + "/webchart/ebkOrderPush"
     body = urllib.urlencode(order_dict)
     try:
         yield AsyncHTTPClient().fetch(url, method='POST', body=body)
     except Exception, e:
         Log.exception(e)
Ejemplo n.º 10
0
    def post_hotels(self, hotel_datas):
        hotel_ids = [hotel_data['id'] for hotel_data in hotel_datas]
        track_id = self.generate_track_id(id(hotel_ids))
        data = {'list': hotel_datas}
        params = {'track_id': track_id, 'data': json.dumps(data)}
        Log.info(u"<<push hotels {}>> push data {}".format(hotel_ids, params))
        url = API['STOCK'] + '/stock/update_hotel?is_async=false'

        body = urllib.urlencode(params)

        if not IS_PUSH_TO_STOCK:
            raise gen.Return(True)

        try:
            r = yield AsyncHTTPClient().fetch(url, method='POST', body=body)
            resp = json.loads(r.body)
        except Exception, e:
            Log.exception(e)
            raise gen.Return(False)
Ejemplo n.º 11
0
    def post_hotels(self, hotel_datas):
        hotel_ids = [hotel_data['id'] for hotel_data in hotel_datas]
        track_id = self.generate_track_id(id(hotel_ids))
        data = {'list': hotel_datas}
        params = {'track_id': track_id, 'data': json.dumps(data)}
        Log.info(u"<<push hotels {}>> push data {}".format(hotel_ids, params))
        url = API['STOCK'] + '/stock/update_hotel?is_async=false'

        body = urllib.urlencode(params)

        if not IS_PUSH_TO_STOCK:
            raise gen.Return(True)

        try:
            r = yield AsyncHTTPClient().fetch(url, method='POST', body=body)
            resp = json.loads(r.body)
        except Exception, e:
            Log.exception(e)
            raise gen.Return(False)
Ejemplo n.º 12
0
 def post_room(self, room_data):
     Log.info(
         u"<<POI push roomtype mapping>> push request {}".format(room_data))
     url = API['POI'] + '/api/push/ebooking/room/'
     body = json.dumps(room_data)
     if not IS_PUSH_TO_POI:
         raise gen.Return(True)
     try:
         r = yield AsyncHTTPClient().fetch(
             url,
             method='POST',
             headers={"Content-Type": "application/json"},
             body=body)
         Log.info("<<POI push roomtype mapping>> response {}".format(
             r.body))
         resp = json.loads(r.body)
     except Exception, e:
         Log.exception(e)
         raise gen.Return(False)
Ejemplo n.º 13
0
    def update_rateplans_valid_status(self, rateplan_ids):
        Log.info("<< push rateplans {} update rateplan valid>>".format(rateplan_ids))
        if not IS_PUSH_TO_STOCK:
            raise gen.Return(True)

        rateplans = RatePlanModel.get_by_ids(self.db, rateplan_ids, with_delete=True)
        rateplan_datas = [{"chain_id": CHAIN_ID, "hotel_id": rateplan.hotel_id, "rate_plan_id": rateplan.id, "is_valid": self.cal_rateplan_isvalid(rateplan)} for rateplan in rateplans]

        track_id = self.generate_track_id(rateplan_ids)
        data = {'list': rateplan_datas, 'type': 3}
        params = {'track_id': track_id, 'data': json.dumps(data)}
        body = urllib.urlencode(params)
        url = API['STOCK'] + '/stock/update_state?is_async=false'

        Log.info("<< push rateplan {} update rateplan valid request {}>>".format(rateplan_ids, params))
        try:
            r = yield AsyncHTTPClient().fetch(url, method='POST', body=body)
            Log.info("<< push rateplan {} update rateplan valid response {}>>".format(rateplan_ids, r.body))
            resp = json.loads(r.body)
        except Exception, e:
            Log.exception(e)
            raise gen.Return(False)
Ejemplo n.º 14
0
class RatePlanAPIHandler(BtwBaseHandler, RatePlanValidMixin):

    @gen.coroutine
    @auth_login(json=True)
    @auth_permission(PERMISSIONS.admin | PERMISSIONS.pricing, json=True)
    def post(self, hotel_id, roomtype_id):
        args = self.get_json_arguments()
        merchant_id = self.current_user.merchant_id

        name, meal_num, punish_type = get_and_valid_arguments(
            args, 'name', 'meal_num', 'punish_type')
        pay_type = args.get('pay_type', RatePlanModel.PAY_TYPE_PRE)
        ahead_days = args.get('ahead_days', None)
        stay_days = args.get('stay_days', None)

        self.valid_pay_type(pay_type)
        self.valid_rateplan_arguments(name, meal_num, punish_type)

        if pay_type == RatePlanModel.PAY_TYPE_ARRIVE:
            guarantee_start_time, guarantee_type = get_and_valid_arguments(
                args, 'guarantee_start_time', 'guarantee_type')
            self.valid_arrive_pay_args(guarantee_type, guarantee_start_time)

            rateplan, roomrate = yield self.new_rate_plan(
                merchant_id, hotel_id, roomtype_id, name, meal_num, punish_type, ahead_days, stay_days, pay_type, guarantee_type, guarantee_start_time)
        else:
            rateplan, roomrate = yield self.new_rate_plan(
                merchant_id, hotel_id, roomtype_id, name, meal_num, punish_type, ahead_days, stay_days)

        self.finish_json(result=dict(
            rateplan=rateplan.todict(),
            roomrate=roomrate.todict(),
        ))

    @auth_login(json=True)
    @auth_permission(PERMISSIONS.admin | PERMISSIONS.pricing, json=True)
    def get(self, hotel_id, roomtype_id):
        merchant_id = self.current_user.merchant_id

        rateplans, roomrates = self.get_by_room(merchant_id, hotel_id, roomtype_id)

        self.finish_json(result=dict(
            rateplans=[rateplan.todict() for rateplan in rateplans],
            roomrates=[roomrate.todict() for roomrate in roomrates],
        ))

    def get_by_room(self, merchant_id, hotel_id, roomtype_id):
        rateplans = RatePlanModel.get_by_room(self.db, merchant_id, hotel_id, roomtype_id)
        rateplan_ids = [rateplan.id for rateplan in rateplans]
        roomrates= RoomRateModel.get_by_rateplans(self.db, rateplan_ids)
        Log.info(roomrates)
        return rateplans, roomrates


    @gen.coroutine
    def new_rate_plan(self, merchant_id, hotel_id, roomtype_id, name, meal_num, punish_type, ahead_days, stay_days, pay_type=None, guarantee_type=None, guarantee_start_time=None):
        room = CooperateRoomTypeModel.get_by_id(self.db, roomtype_id)
        if not room:
            raise JsonException(errcode=404, errmsg='room not exist')

        rate_plan = RatePlanModel.get_by_merchant_hotel_room_name(
            self.db, merchant_id, hotel_id, roomtype_id, name)
        if rate_plan:
            raise JsonException(errcode=405, errmsg="name exist")

        new_rateplan = RatePlanModel.new_rate_plan(self.db,
                                                   merchant_id, hotel_id, roomtype_id, room.base_hotel_id, room.base_roomtype_id,  name, meal_num, punish_type, ahead_days, stay_days, pay_type, guarantee_type, guarantee_start_time, commit=False)
        self.db.flush()
        new_roomrate = RoomRateModel.new_roomrate(
            self.db, hotel_id, roomtype_id, room.base_hotel_id, room.base_roomtype_id, new_rateplan.id, meal_num, commit=False)
        self.db.flush()

        rateplan_pusher = RatePlanPusher(self.db)
        roomrate_pusher = RoomRatePusher(self.db)
        try:
            if not (yield rateplan_pusher.post_rateplan(new_rateplan)):
                raise JsonException(2000, 'rateplan push fail')
            if not (yield rateplan_pusher.post_cancel_rule(new_rateplan)):
                raise JsonException(2001, 'cancel rule push fail')
            if not (yield roomrate_pusher.push_roomrate(new_rateplan.merchant_id, new_roomrate)):
                raise JsonException(2002, 'roomrate push fail')
            self.db.commit()

        except JsonException, e:
            self.db.rollback()
            raise e
        except Exception, e:
            self.db.rollback()
            Log.exception(e)
            raise JsonException(2003, 'push stock fail')
Ejemplo n.º 15
0
class RatePlanModifyAPIHandler(BtwBaseHandler, RatePlanValidMixin, CooperateMixin):

    @gen.coroutine
    @auth_login(json=True)
    @auth_permission(PERMISSIONS.admin | PERMISSIONS.pricing, json=True)
    def put(self, hotel_id, roomtype_id, rateplan_id):
        args = self.get_json_arguments()

        name = args.get("name", None)
        meal_num = args.get("meal_num", None)
        punish_type = args.get("punish_type", None)
        guarantee_type = args.get("guarantee_type", None)
        guarantee_start_time = args.get("guarantee_start_time", None)
        ahead_days = args.get('ahead_days', None)
        stay_days = args.get('stay_days', None)

        if name is not None:
            self.valid_name(name)
        if meal_num is not None:
            self.valid_meal_num(meal_num)
        if punish_type is not None:
            self.valid_punish_type(punish_type)
        if guarantee_type is not None:
            self.valid_guarantee_type(guarantee_type)
        if guarantee_start_time is not None:
            self.valid_gurantee_start_time(guarantee_start_time)


        rateplan, roomrate = yield self.modify_rateplan(
            rateplan_id, name, meal_num, punish_type, guarantee_type, guarantee_start_time, ahead_days, stay_days)

        self.finish_json(result=dict(
            rateplan=rateplan.todict(),
            roomrate=roomrate.todict(),
        ))

    @gen.coroutine
    def modify_rateplan(self, rateplan_id, name, meal_num, punish_type, guarantee_type, guarantee_start_time, ahead_days, stay_days):
        rateplan = RatePlanModel.get_by_id(self.db, rateplan_id)
        if not rateplan:
            raise JsonException(errcode=404, errmsg="rateplan not found")

        if name is not None:
            _rateplan = RatePlanModel.get_by_merchant_hotel_room_name(self.db,
                                                                      rateplan.merchant_id, rateplan.hotel_id, rateplan.roomtype_id, name)
            if _rateplan and _rateplan.id != rateplan.id:
                raise JsonException(errcode=405, errmsg="name exist")
            else:
                rateplan.name = name

        if meal_num is not None:
            roomrate = RoomRateModel.set_meal(
                self.db, rateplan.id, meal_num, commit=False)
        if punish_type is not None:
            rateplan.punish_type = punish_type
        if rateplan.pay_type == rateplan.PAY_TYPE_ARRIVE:
            if guarantee_type is not None:
                rateplan.guarantee_type = guarantee_type
            if guarantee_start_time is not None:
                rateplan.guarantee_start_time = guarantee_start_time
        if ahead_days is not None:
            rateplan.ahead_days = ahead_days
        if stay_days is not None:
            rateplan.stay_days = stay_days


        self.db.flush()
        rateplan_pusher = RatePlanPusher(self.db)
        roomrate_pusher = RoomRatePusher(self.db)
        try:
            if not (yield rateplan_pusher.post_rateplan(rateplan)):
                raise JsonException(2000, 'rateplan push fail')
            if not (yield rateplan_pusher.post_cancel_rule(rateplan)):
                raise JsonException(2001, 'cancel rule push fail')
            if not (yield roomrate_pusher.push_roomrate(rateplan.merchant_id, roomrate)):
                raise JsonException(2002, 'roomrate push fail')
            self.db.commit()

        except JsonException, e:
            self.db.rollback()
            raise e
        except Exception, e:
            self.db.rollback()
            Log.exception(e)
            raise JsonException(2003, 'push stock fail')