-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
90 lines (74 loc) · 3.27 KB
/
app.py
File metadata and controls
90 lines (74 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
"""
This is a file which create the flask application and register the blueprints, apscheduler, mail service and database.
The entrance of the flask application. We can start the application by app.run(port=8000, debug=True)
"""
from flask import Flask, redirect, session, url_for
from flask_apscheduler import APScheduler
from flask_migrate import Migrate
import config
from config import remindConfig
from models import User, Class, Task, Captcha
from exts import db, mail, aps
from flask_mail import Message
import datetime
from blueprints import all_bp, completed_bp, schedule_bp, statistics_bp, today_bp, user_bp, module_bp
# send to users who have tasks that are due in the future 4 hours.
# the function is called every one minute
def taskReminder():
with app.app_context():
uncompleted_tasks = Task.query.filter_by(task_status=False).all()
for task in uncompleted_tasks:
if task.task_date == datetime.date.today():
# convert task_date and task_time to timestamp
task_timestamp = datetime.datetime.combine(task.task_date, task.task_time)
# the time difference between now and the task
time_difference = task_timestamp - datetime.datetime.now()
# if the time difference is less than 10 minutes, send an email to remind the user
if time_difference < datetime.timedelta(minutes=240):
if not task.informed:
task.informed = True # set the informed flag to True
try:
db.session.commit()
except Exception as e:
db.session.rollback()
print(e)
uid = Class.query.filter_by(cid=task.cid).first().uid # get the uid of the user
email = User.query.filter_by(uid=uid).first().usermail # get the email of the user
message = Message('A reminder of about task', recipients=[email],
body="Hello, this is a reminder about your task: " + task.task_name)
mail.send(message)
print('Sent a reminder to ' + email)
else:
pass
else:
pass
# initialize the app
app = Flask(__name__)
# set the configuration
app.config.from_object(config)
app.config.from_object(remindConfig)
# register the blueprints
app.register_blueprint(all_bp)
app.register_blueprint(completed_bp)
app.register_blueprint(schedule_bp)
app.register_blueprint(statistics_bp)
app.register_blueprint(today_bp)
app.register_blueprint(user_bp)
app.register_blueprint(module_bp)
# register the apscheduler and start it
aps.init_app(app)
aps.start()
# register the database
db.init_app(app)
# register the mail
mail.init_app(app)
# register the migrate
migrate = Migrate(app, db, compare_type=True, compare_server_default=True)
@app.route('/')
def index():
if session.get('uid'):
return redirect(url_for('today.index')) # if the user has logged in, redirect to the today page
else:
return redirect('/user/signup') # if the user has not logged in, redirect to the signup page
if __name__ == '__main__':
app.run(port=8000, debug=True)