def valid_date(self, str_start_date, str_end_date): time_format = "%Y-%m-%d" try: start_date = datetime.strptime(str_start_date, time_format) end_date = datetime.strptime(str_end_date, time_format) except: raise JsonException(errcode=2001, errmsg="invalid date: date parse fail") if end_date < start_date: raise JsonException( errcode=2001, errmsg="invalid date: end date before start date") min_date = date.today() max_date = min_date + timedelta(days=365) if start_date.date() < min_date or start_date.date() > max_date: raise JsonException(errcode=2001, errmsg="invalid date: start date out of range") if end_date.date() < min_date or end_date.date() > max_date: raise JsonException(errcode=2001, errmsg="invalid date: end date out of range") return start_date, end_date
def valid_date(self, str_start_date, str_end_date, weekdays): time_format = "%Y-%m-%d" try: start_date = datetime.strptime(str_start_date, time_format) end_date = datetime.strptime(str_end_date, time_format) except: raise JsonException(errcode=2001, errmsg="invalid date: date parse fail") if end_date < start_date: raise JsonException( errcode=2001, errmsg="invalid date: end date before start date") min_date = date.today() max_date = min_date + timedelta(days=365) if start_date.date() < min_date or start_date.date() >= max_date: raise JsonException(errcode=2001, errmsg="invalid date: start date out of range") if end_date.date() < min_date or end_date.date() >= max_date: raise JsonException(errcode=2001, errmsg="invalid date: end date out of range") if weekdays: if not set([1, 2, 3, 4, 5, 6, 7]).difference(weekdays): raise JsonException( errcode=2002, errmsg= "invalid weekdays: end date out of range [1,2,3,4,5,6,7]") return start_date, end_date
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
def valid_ota_ids(self, ota_ids): all_ota_ids = yield self.get_otas() if 0 in ota_ids: raise JsonException(1001, 'illeage ota ids') for id in ota_ids: if id not in all_ota_ids: raise JsonException(1001, 'illeage ota ids')
def modify_cooped_roomtype(self, merchant_id, hotel_id, roomtype_id, prefix_name, remark_name): coop = CooperateRoomTypeModel.get_by_id(self.db, roomtype_id) if not coop: return JsonException(404, 'coop not found') if coop.merchant_id != merchant_id: return JsonException(1000, 'merchant not valid') coop.prefix_name = prefix_name coop.remark_name = remark_name self.db.commit() return coop
def process_celery_task(self, task, is_list=False): result = task.result if task.status == 'SUCCESS': if is_list: return result if result else [] else: return result else: if isinstance(result, CeleryException): raise JsonException(errcode=result.errcode, errmsg=result.errmsg) else: raise JsonException(errcode=500, errmsg="server error")
def valid_args(self, pay_type, ota_ids, year, month, value, remark): if pay_type not in [0, 1]: raise JsonException(2001, 'invalid pay_type') #if ota_id not in OTA: #raise JsonException(2002, 'invalid ota_id') if year <= 2000: raise JsonException(2003, 'invalid year') if month not in range(1, 13): raise JsonException(2004, 'invalid month')
def delete(self, hotel_id, roomtype_id): room = CooperateRoomTypeModel.get_by_merchant_hotel_and_id(self.db, self.merchant.id, hotel_id, roomtype_id) if not room: raise JsonException(1001, 'roomtype not found') r = yield self.delete_roomtype(room) if r: self.db.commit() self.finish_json(result=dict( roomtype=room.todict())) else: self.db.rollback() raise JsonException(2000, 'delete fail')
def delete(self, hotel_id, roomtype_id, rateplan_id): rateplan = RatePlanModel.get_by_id_with_merchant(self.db, rateplan_id, self.merchant.id) if not rateplan: raise JsonException(1001, 'rateplan not found') r = yield self.delete_rateplan(rateplan) if r: self.db.commit() self.finish_json(result=dict( rateplan=rateplan.todict(), )) else: self.db.rollback() raise JsonException(2000, 'delete fail')
def delete(self, hotel_id): hotel = CooperateHotelModel.get_by_merchant_id_and_hotel_id(self.db, self.merchant.id, hotel_id) if not hotel: raise JsonException(1000, u'hotel not found') r = yield self.delete_hotel(hotel) if r: self.db.commit() self.finish_json(result=dict( hotel=hotel.todict() ) ) else: self.db.rollback() raise JsonException(2000, 'delete fail')
def get(self, merchant_id, hotel_id): hotel = CooperateHotelModel.get_by_id(self.db, hotel_id) if not hotel: raise JsonException(4001, 'hotel not found') contract_hotel = ContractHotelModel.get_by_hotel(self.db, hotel_id) if not contract_hotel: contract_hotel = ContractHotelModel.new( self.db, merchant_id=merchant_id, hotel_id=hotel_id, base_hotel_id=hotel.base_hotel_id, weekend="5,6") roomtypes = CooperateRoomTypeModel.get_by_hotel_id(self.db, hotel_id) contract_roomtypes = ContractRoomTypeModel.get_by_hotel( self.db, hotel_id) hotel_dict = hotel.todict() roomtype_dicts = [room.todict() for room in roomtypes] yield self.merge_base_info(hotel_dict, roomtype_dicts) self.finish_json(result=dict( hotel=hotel_dict, roomtypes=roomtype_dicts, contract_hotel=contract_hotel.todict() if contract_hotel else {}, contract_roomtypes=[c.todict() for c in contract_roomtypes], ))
def pre_check_order(self, order_entity): order = self.create_order(order_entity) if not order: raise JsonException(1, 'create order fail') if order.status != 0: return order # valid is roomtype online roomtype = CooperateRoomTypeModel.get_by_id(self.db, order.roomtype_id) if roomtype.is_online != 1: Log.info('roomtype is not online') order.status = 200 order.exception_info = 'roomtype is not online' self.db.commit() OrderHistoryModel.set_order_status_by_server( self.db, order, 0, 200) return order # valid is inventory enough if not self.valid_inventory(order_entity): Log.info('more room please') order.status = 200 order.exception_info = 'inventory not enough' self.db.commit() OrderHistoryModel.set_order_status_by_server( self.db, order, 0, 200) return order return order
def valid_date(self, year, month): date = datetime(year, month, 1) today = datetime.today() min_date = datetime(today.year, today.month, 1) max_date = today + timedelta(days=365) if date < min_date or date > max_date: raise JsonException(errcode=1003, errmsg="query date out of range")
def logout(self): Log.info("valid weixin fail") clear_domain_cookie(self, "open_id", domain=".betterwood.com") clear_domain_cookie(self, "username", domain=".betterwood.com") clear_domain_cookie(self, "merchant_id", domain=".betterwood.com") clear_domain_cookie(self, "open_id") clear_domain_cookie(self, "username") clear_domain_cookie(self, "merchant_id") raise JsonException(500, "weixin login valid fai")
def put(self): args = self.get_json_arguments() is_online, = get_and_valid_arguments(args, 'is_online') if is_online not in [0, 1]: raise JsonException(errmsg='wrong arg is_online', errcode=2001) CooperateRoomTypeModel.set_online_by_merchant(self.db, self.merchant.id, is_online, commit=False) self.db.flush() r = yield InventoryAsyncPusher(self.db).push_inventory_by_merchant(self.merchant.id) if r: self.db.commit() self.finish_json() else: self.db.rollback() yield InventoryAsyncPusher(self.db).push_inventory_by_merchant(self.merchant.id) raise JsonException(1000, "push stock error")
def put(self, ota_id, hotel_id, is_online): ota_id = int(ota_id) is_online = int(is_online) ota_ids = yield self.get_otas() if ota_id not in ota_ids: raise JsonException(1001, 'illeage ota ids') new_ota_ids = [] if not ota_ids: raise JsonException(1000, 'get ota fail') ota_channel = OtaChannelModel.get_by_hotel_id(self.db, hotel_id) if not ota_channel: if is_online == 0: new_ota_ids = [] else: new_ota_ids = [ota_id] else: if is_online == 0: if ota_id in ota_channel.get_ota_ids(): new_ota_ids = [ id for id in ota_channel.get_ota_ids() if id != ota_id ] else: new_ota_ids = ota_channel.get_ota_ids() else: if ota_id not in ota_channel.get_ota_ids(): new_ota_ids = ota_channel.get_ota_ids() new_ota_ids.append(ota_id) else: new_ota_ids = ota_channel.get_ota_ids() ota_channel = OtaChannelModel.set_ota_ids(self.db, hotel_id, new_ota_ids, commit=False) r = yield change_ota(hotel_id, new_ota_ids) if not r: self.db.rollback() raise JsonException(1000, 'fail') else: self.db.commit() self.finish_json(result=dict(ota_channel=ota_channel.todict(), ))
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
def get(self, hotel_id): resp = yield AsyncHTTPClient().fetch(API['POI'] + "/api/hotel/" + hotel_id) r = json_decode(resp.body) if r and r['errcode'] == 0: self.finish_json(result=dict( hotel=r['result']['hotel'] )) else: raise JsonException(errcode=404, errmsg="hotel not found")
def put(self, merchant_id, hotel_id): contract_hotel_args = self.get_json_arguments() contract_hotel = ContractHotelModel.get_by_hotel(self.db, hotel_id) if not contract_hotel: raise JsonException(1000, 'contract not exist') ContractHotelModel.update(self.db, **contract_hotel_args) self.finish_json(result=dict(contract_hotel=contract_hotel.todict(), ))
def change_hotel_online_status(self, merchant_id, hotel_id, is_online): hotel = CooperateHotelModel.get_by_merchant_id_and_hotel_id(self.db, merchant_id, hotel_id) if not hotel: raise JsonException(404, 'hotel not found') hotel.is_online = is_online self.db.commit() PushHotelTask().push_hotel.delay(hotel.id) return hotel
def post(self, order_id): order = OrderModel.get_by_id(self.db, order_id) pre_status = order.status if order.merchant_id != self.merchant.id: raise JsonException(100, 'merchant not valid') if order.status != 100: raise JsonException(200, 'illegal status') if (yield self.callback_order_server(order)): order.confirm_by_user(self.db) if order.status != pre_status: OrderHistoryModel.set_order_status_by_user( self.db, self.current_user, order, pre_status, order.status) else: raise JsonException(1000, 'callback order server fail') self.finish_json(result=dict(order=order.todict(), ))
def put(self, merchant_id, hotel_id, roomtype_id, contract_id): params = self.get_json_arguments() contract = ContractSpecPriceModel.get_by_id(self.db, contract_id) if not contract: raise JsonException(1000, 'contract not found') params.update({'hotel_id': hotel_id, 'roomtype_id': roomtype_id}) ContractSpecPriceModel.update(self.db, contract_id, **params) self.finish_json(result=dict(contract_spec_price=contract.todict(), ))
def put(self, hotel_id, is_online): merchant_id = self.current_user.merchant_id is_online = int(is_online) if is_online not in [0, 1]: raise JsonException(errcode=500, errmsg="wrong arg: is_online") hotel = self.change_hotel_online_status(merchant_id, hotel_id, is_online) self.finish_json(result=dict( cooperate_hotel=hotel.todict() ))
def cancel_order_by_server(self, order_id): order = OrderModel.get_by_id(self.db, order_id) if not order: raise JsonException(2000, 'no order') pre_status = order.status if order.status == 0 or order.status == 100: _order = yield self.cancel_before_user_confirm(order.id) elif order.status == 300: _order = yield self.cancel_after_user_confirm(order.id) elif order.status in [400, 500, 600]: raise gen.Return(order) else: raise JsonException(1000, 'illegal status') if _order.status != pre_status: OrderHistoryModel.set_order_status_by_server( self.db, _order, pre_status, _order.status) raise gen.Return(_order)
def get(self): url = "{}/webchart/user/ebk/get_2dcode?merchantId={}&merchantName={}&username={}".format( API['WEIXIN'], self.merchant.id, self.merchant.name, self.current_user.username) print url resp = yield AsyncHTTPClient().fetch(url) r = json_decode(resp.body) if r and r['errcode'] == 0: self.finish_json(result=dict(url=r['result']['url'])) else: raise JsonException(1000, 'error')
def reset_order_status(self, order_entity): order = OrderModel.get_by_main_order_id(self.db, order_entity.id) if not order: raise JsonException(1, 'order not exist') pre_status = order.status OrderModel.change_order_status_by_main_order_id( self.db, order_entity.id, 0) OrderHistoryModel.set_order_status_by_server(self.db, order, pre_status, 0)
def put(self, merchant_id, hotel_id, roomtype_id, pay_type): contract_roomtype_args = self.get_json_arguments() contract = ContractRoomTypeModel.get_by_hotel_roomtype_pay_type( self.db, hotel_id, roomtype_id, pay_type) if not contract: raise JsonException(1000, 'contract not exist') ContractRoomTypeModel.update(self.db, hotel_id, roomtype_id, pay_type, **contract_roomtype_args) self.finish_json(result=dict(contract_roomtype=contract.todict(), ))
def cancel_after_user_confirm(self, order_id): task = yield gen.Task( Cancel.cancel_order_after_user_confirm.apply_async, args=[order_id]) if task.status == 'SUCCESS': raise gen.Return(task.result) else: if isinstance(task.result, CeleryException): raise JsonException(task.result.errcode, task.result.errmsg) else: raise task.result
def format_order(self, order_json): Log.info(order_json) try: order_entity = SubmitOrder(order_json) except ValueError as e: raise JsonException(errcode=1, errmsg=e.message) Log.info(order_entity) rate_plan = RatePlanModel.get_by_id(self.db, order_entity.rateplan_id) if not rate_plan: raise JsonException(1, 'no rate plan') order_entity.pay_type = rate_plan.pay_type order_entity.merchant_id = rate_plan.merchant_id order_entity.cancel_type = rate_plan.cancel_type order_entity.punish_type = rate_plan.punish_type order_entity.punish_value = rate_plan.punish_value order_entity.guarantee_type = rate_plan.guarantee_type order_entity.guarantee_start_time = rate_plan.guarantee_start_time return order_entity
def post(self, hotel_id): merchant_id = self.current_user.merchant_id args = self.get_json_arguments() roomtype_ids, = get_and_valid_arguments(args, 'roomtype_ids') if not roomtype_ids: raise JsonException(errcode=2001, errmsg="need id") coops = yield self.new_roomtype_coops(merchant_id, hotel_id, roomtype_ids) self.finish_json(result=dict( cooped_roomtypes=[coop.todict() for coop in coops], ))