Example #1
0
    def notify_10mins_ahead(self):
        try:
            userdao = UserDAO()

            current_time = dt.datetime.now()
            print '-' * 40
            print 'push before 10 minutes'
            print 'current_time :', current_time

            hour = current_time.hour
            if current_time.minute >= 20 and current_time.minute <= 29:
                minute = 30
            elif current_time.minute >= 50 and current_time.minute <= 59:
                hour += 1
                minute = 0
            else:
                minute = 0

            cron_time = current_time.replace(hour=hour,
                                             minute=minute,
                                             second=0,
                                             microsecond=0)

            print 'cron_time :', cron_time
            print '-' * 40

            session = Session()
            result = session.query(Booking, Master, User, UserPushKey) \
                            .join(Master, Booking.master_id == Master.id) \
                            .join(User, Booking.user_id == User.id) \
                            .outerjoin(UserPushKey, User.id == UserPushKey.user_id) \
                            .filter(or_(Booking.cleaning_status == BC.BOOKING_UPCOMMING, Booking.cleaning_status == BC.BOOKING_STARTED)) \
                            .filter(func.date(Booking.start_time) == cron_time.date()) \
                            .filter(func.HOUR(Booking.start_time) == cron_time.time().hour) \
                            .filter(func.MINUTE(Booking.start_time) == cron_time.time().minute) \
                            .all()

            for row in result:
                key = userdao.get_user_salt_by_id(row.User.id)[:16]
                crypto = aes.MyCrypto(key)

                booking_id = row.Booking.id
                pushkey = row.UserPushKey.pushkey if row.UserPushKey != None else ''
                user_name = crypto.decodeAES(row.User.name)
                phone = crypto.decodeAES(row.User.phone)
                master_name = row.Master.name
                devicetype = row.User.devicetype

                print 'push to', user_name, master_name, booking_id
                print pushkey

                if devicetype != 'android':
                    pushkey = crypto.decodeAES(row.User.phone)

                send_10mins_ahead_notification(devicetype, [pushkey],
                                               booking_id)
                send_alimtalk(phone, 'noti_10', user_name, master_name)

        except Exception, e:
            print_err_detail(e)
    def notify(self):
        try:
            userdao = UserDAO()

            current_time = dt.datetime.now()

            print '-' * 40
            print 'push before 2 hours'
            print 'current_time :', current_time

            hour = current_time.hour
            minute = 30 if current_time.minute >= 30 else 0

            #current_time = current_time + dt.timedelta(hours=2)
            cron_time = current_time.replace(hour=hour+2, minute=minute, second = 0, microsecond=0)

            print 'cron_time :', cron_time
            print '-' * 40

            session = Session()
            result = session.query(Booking, Master, MasterPushKey, User) \
                            .join(Master, Booking.master_id == Master.id) \
                            .join(MasterPushKey, Master.id == MasterPushKey.master_id) \
                            .join(User, Booking.user_id == User.id) \
                            .filter(Booking.cleaning_status == BC.BOOKING_UPCOMMING) \
                            .filter(func.date(Booking.start_time) == cron_time.date()) \
                            .filter(func.HOUR(Booking.start_time) == cron_time.time().hour) \
                            .filter(func.MINUTE(Booking.start_time) == cron_time.time().minute) \
                            .all()


            for row in result:

                key = userdao.get_user_salt_by_id(row.User.id)[:16]
                crypto = aes.MyCrypto(key)

                if row.Master.id == 'da2a1f50-fd36-40bf-8460-55b3e1b2c459':
                    continue

                booking_id = row.Booking.id
                pushkey = row.MasterPushKey.pushkey if row.MasterPushKey != None else ''
                master_name = row.Master.name

                start_time = row.Booking.start_time
                time_str = convert_datetime_format4(start_time)

                print 'push to', master_name, booking_id
                print pushkey

                send_master_ahead_notification('android', [pushkey], booking_id, master_name, time_str)

        except Exception, e:
            print_err_detail(e)
    def notify_24hours_ahead(self):
        try:
            userdao = UserDAO()

            current_time = dt.datetime.now()

            print '-' * 40
            print 'push before 24 hours'
            print 'current_time :', current_time

            hour = current_time.hour
            minute = 30 if current_time.minute >= 30 else 0

            current_time = current_time + dt.timedelta(days=1)
            cron_time = current_time.replace(hour=hour,
                                             minute=minute,
                                             second=0,
                                             microsecond=0)

            print 'cron_time :', cron_time
            print '-' * 40

            session = Session()
            result = session.query(Booking, Master, User, UserPushKey) \
                            .join(Master, Booking.master_id == Master.id) \
                            .join(User, Booking.user_id == User.id) \
                            .outerjoin(UserPushKey, User.id == UserPushKey.user_id) \
                            .filter(or_(Booking.cleaning_status == BC.BOOKING_UPCOMMING, Booking.cleaning_status == BC.BOOKING_STARTED)) \
                            .filter(func.date(Booking.start_time) == cron_time.date()) \
                            .filter(func.HOUR(Booking.start_time) == cron_time.time().hour) \
                            .filter(func.MINUTE(Booking.start_time) == cron_time.time().minute) \
                            .all()

            for row in result:

                key = userdao.get_user_salt_by_id(row.User.id)[:16]
                crypto = aes.MyCrypto(key)

                booking_id = row.Booking.id
                pushkey = row.UserPushKey.pushkey if row.UserPushKey != None else ''
                user_name = crypto.decodeAES(row.User.name)
                phone = crypto.decodeAES(row.User.phone)
                master_name = row.Master.name
                devicetype = row.User.devicetype
                price = row.Booking.price_with_task

                start_time = row.Booking.start_time

                print 'push to', user_name, master_name, booking_id
                print pushkey

                if devicetype != 'android':
                    pushkey = crypto.decodeAES(row.User.phone)

                ampm = '오전'
                hour = start_time.hour
                if hour >= 12:
                    if hour > 12:
                        hour -= 12
                    ampm = '오후'

                minute = start_time.minute

                time_str = '%s %d시' % (ampm, hour)
                if minute == 30:
                    time_str += ' 반'

                send_day_ahead_notification(devicetype, [pushkey], booking_id,
                                            user_name, time_str)
                send_alimtalk(phone, 'noti_24', user_name, master_name)

        except Exception, e:
            print_err_detail(e)
Example #4
0
    def notify_charge_failure(self):
        try:
            mongo_logger = get_mongo_logger()

            userdao = UserDAO()

            current_time = dt.datetime.now()

            print '-' * 40
            print 'charge failure notification via alimtalk'
            print 'current_time :', current_time

            hour = current_time.hour
            minute = 30 if current_time.minute >= 30 else 0

            cron_time = current_time.replace(hour=hour,
                                             minute=minute,
                                             second=0,
                                             microsecond=0)

            print 'cron_time :', cron_time
            print '-' * 40

            session = Session()
            result = session.query(Booking, User) \
                            .join(User, Booking.user_id == User.id) \
                            .filter(Booking.cleaning_status == BC.BOOKING_COMPLETED) \
                            .filter(Booking.payment_status != BC.BOOKING_PAID) \
                            .filter(func.date(Booking.estimated_end_time) == cron_time.date()) \
                            .filter(func.HOUR(Booking.estimated_end_time) == cron_time.time().hour) \
                            .filter(func.MINUTE(Booking.estimated_end_time) == cron_time.time().minute) \
                            .filter(User.is_b2b == 0) \
                            .filter(Booking.source == 'hm') \
                            .all()

            for row in result:
                if 'b2b.com' in row.User.email:  # b2b 무시
                    continue

                if row.User.is_b2b == 1:  # b2b 무시
                    continue

                if row.Booking.source != 'hm':  # 11번가 무시
                    continue

                key = userdao.get_user_salt_by_id(row.User.id)[:16]
                crypto = aes.MyCrypto(key)

                user_id = row.User.id
                phone = crypto.decodeAES(row.User.phone)

                cleaning_time = convert_datetime_format2(
                    row.Booking.start_time)
                price = '{:,}원'.format(row.Booking.price_with_task)

                try:
                    send_alimtalk(phone, 'notify_charge_failure',
                                  cleaning_time, price)

                    mongo_logger.debug('user charge failure',
                                       extra={
                                           'dt': current_time,
                                           'user_id': user_id,
                                           'price': price,
                                           'cleaning_time': cleaning_time
                                       })
                except Exception, e:
                    mongo_logger.error('err user charge failure',
                                       extra={
                                           'dt': current_time,
                                           'user_id': user_id,
                                           'price': price,
                                           'cleaning_time': cleaning_time
                                       })

        except Exception, e:
            print_err_detail(e)
            mongo_logger.error('failed to user charge failure',
                               extra={'err': str(e)})
    def add_upcoming(self):
        try:
            print '-' * 40
            print 'charge and add upcoming appointment started'
            current_time = dt.datetime.now()

            print current_time

            hour = current_time.hour
            minute = 30 if current_time.minute >= 30 else 0

            cron_time = current_time.replace(hour=hour,
                                             minute=minute,
                                             second=0,
                                             microsecond=0)
            print cron_time
            print '-' * 40

            userdao = UserDAO()
            session = Session()
            result = session.query(Booking) \
                                .filter(or_(Booking.appointment_type == BC.ONE_TIME_A_MONTH, Booking.appointment_type == BC.TWO_TIME_A_MONTH, Booking.appointment_type == BC.FOUR_TIME_A_MONTH)) \
                                .filter(func.date(Booking.start_time) == cron_time.date()) \
                                .filter(func.HOUR(Booking.start_time) == cron_time.time().hour) \
                                .filter(func.MINUTE(Booking.start_time) == cron_time.time().minute) \
                                .all()

            for row in result:
                try:
                    # add next appointment
                    booking_id = row.id
                    request_id = row.request_id

                    # 11번가의 경우 정해진 새로 추가 하지 않는다.
                    if row.source in [
                            '11st', 'auction', 'gmarket'
                    ] or row.user_type in ['11st', 'auction', 'gmarket']:
                        continue

                    # 전체 취소인 경우가 아닐 때만 새로 추가한다.
                    if self.is_cancelled_all(booking_id):
                        continue

                    org_master_id = self.get_org_master(request_id)

                    appointment_type = row.appointment_type
                    user_id = row.user_id
                    master_id = org_master_id if org_master_id != None else row.master_id
                    additional_task = row.additional_task
                    cleaning_duration = row.cleaning_duration
                    booking_time = dt.datetime.now()
                    price = row.price
                    price_with_task = row.price_with_task
                    card_idx = userdao.get_user_default_card_index(user_id)
                    addr_idx = row.addr_idx
                    message = row.message
                    trash_location = row.trash_location
                    laundry_apply_all = self.get_last_laundry_all(request_id)
                    enterhome = row.enterhome
                    enterbuilding = row.enterbuilding
                    havetools = row.havetools
                    havepet = row.havepet
                    is_dirty = row.is_dirty
                    master_gender = row.master_gender
                    wage_per_hour = row.wage_per_hour
                    status = row.status
                    cleaning_status = row.cleaning_status
                    routing_method = row.routing_method
                    source = row.source
                    user_type = row.user_type

                    actual_price = price
                    if havetools == 1:
                        additional_task = 0
                    else:
                        additional_task = 64
                        #actual_price += BC.VACCUM_CHARGE

                    if laundry_apply_all == 1:
                        additional_task += 4  # 빨래

                    print 'booking_id', booking_id
                    print 'user_id', user_id, 'master_id', master_id, 'price', price, 'request_id', request_id

                    # get next appointment info
                    start_time, estimated_end_time = self.get_next_appointment_date(
                        request_id, appointment_type)
                    appointment_index = self.get_next_appointment_index(
                        request_id, cleaning_status, appointment_type)

                    # add appointment
                    self.add_new_appointment(
                        request_id, master_id, user_id, appointment_type,
                        appointment_index, booking_time, start_time,
                        estimated_end_time, cleaning_duration, additional_task,
                        price, actual_price, card_idx, addr_idx, message,
                        trash_location, enterhome, enterbuilding,
                        routing_method, havetools, havepet, laundry_apply_all,
                        is_dirty, master_gender, wage_per_hour, source,
                        user_type)
                except Exception, e:
                    session.rollback()
                    print_err_detail(e)
                    continue

        except Exception, e:
            session.rollback()
            print_err_detail(e)
    def charge(self):
        try:
            print '-' * 40
            print 'charge started'
            current_time = dt.datetime.now()

            print current_time

            hour = current_time.hour
            minute = 30 if current_time.minute >= 30 else 0

            cron_time = current_time.replace(hour=hour,
                                             minute=minute,
                                             second=0,
                                             microsecond=0)
            print cron_time
            print '-' * 40

            userdao = UserDAO()

            session = Session()
            result = session.query(Booking) \
                                .filter(or_(Booking.cleaning_status == BC.BOOKING_UPCOMMING, Booking.cleaning_status == BC.BOOKING_STARTED)) \
                                .filter(Booking.payment_status == BC.BOOKING_UNPAID_YET) \
                                .filter(func.date(Booking.start_time) == cron_time.date()) \
                                .filter(func.HOUR(Booking.start_time) == cron_time.time().hour) \
                                .filter(func.MINUTE(Booking.start_time) == cron_time.time().minute) \
                                .all()

            for row in result:
                try:
                    # payment
                    booking_id = row.id
                    user_id = row.user_id
                    price = int(row.price_with_task)
                    user_name = userdao.get_user_name(user_id)

                    devicetype = userdao.get_user_device_type(user_id)
                    if devicetype == 'None':  # 안드로이드 사용자만 자동 과금한다
                        continue

                    print booking_id, user_id, price, user_name, 'tried to pay'

                    ret_code, msg = request_payment(
                        user_id, user_name, booking_id, price,
                        row.appointment_type)  # 자동 결제 성공
                    if ret_code:
                        # paid 로 상태 변경이 필요함
                        card_idx = userdao.get_user_default_card_index(user_id)

                        tid = msg
                        row.card_idx = card_idx
                        row.tid = tid
                        row.payment_status = BC.BOOKING_PAID
                        #row.cleaning_status = BC.BOOKING_UPCOMMING
                        row.status = BC.BOOKING_PAID
                        print tid, 'success'
                    else:  # 자동 결제 실패
                        row.payment_status = BC.BOOKING_PAYMENT_FAILED
                        #row.cleaning_status = BC.BOOKING_UPCOMMING
                        row.status == BC.BOOKING_PAYMENT_FAILED
                        print msg, 'failed'

                    session.commit()

                except Exception, e:
                    session.rollback()
                    print_err_detail(e)
                    continue

        except Exception, e:
            session.rollback()
            print_err_detail(e)