Beispiel #1
0
def index():

    form = ReminderForm(request.form)
    if request.method == 'POST' and form.validate():
        with open('db.bin', 'wb') as f:

            # getting timezone with phone number
            raw_phone_number = request.form['phone']
            phone_number = phonenumbers.parse(raw_phone_number)
            time_zone = ptz.time_zones_for_number(phone_number)[0]

            pickle.dump(
                {
                    'phone': request.form['phone'],
                    'message': request.form['message'],
                    'timestamp': datetime.now(),
                    'time_zone': time_zone
                }, f)

        _time_zone = timezone(time_zone)
        time_zone_time = datetime.now(_time_zone)

        # timezone helps determining the night hours of user's country
        scheduler = BackgroundScheduler()
        scheduler.add_job(send_message,
                          'cron',
                          hour='7-23',
                          second=time_zone_time.second,
                          minute=time_zone_time.minute,
                          id="sms_job_id",
                          timezone=time_zone)
        # the above cronjob will run forever every hour from the time it called
        scheduler.start()
        # Shut down the scheduler when exiting the app
        atexit.register(lambda: scheduler.shutdown(wait=False))
        return redirect('/info')
    return render_template('home.html', form=form)