-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
214 lines (157 loc) · 6.43 KB
/
main.py
File metadata and controls
214 lines (157 loc) · 6.43 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
from flask import Flask, render_template, request,jsonify,Blueprint,redirect,url_for
from db_model import db, Clients, User,Log
import os
from dotenv import load_dotenv, find_dotenv
import datetime
from apscheduler.schedulers.background import BackgroundScheduler
from msg import send_message
import time
from auth.login_actual import auth_bp
from flask_login import LoginManager,login_required, current_user
from app_factory import create_app
app = create_app()
app.register_blueprint(auth_bp,url_prefix="/auth")
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = "auth.login"
with app.app_context():
db.create_all()
@login_manager.user_loader
def load_user(id):
return User.query.get(int(id))
@app.route("/")
@login_required
def render_calendar():
return render_template("index.html",user = current_user)
@app.route("/api_python/submit", methods = ["POST"])
@login_required
def submit_data():
reservation = request.get_json()
if reservation is None:
return jsonify({"error":"data is missing"}),400
format_date = "%Y-%m-%d"
datetime_date = datetime.datetime.strptime(reservation["date"], format_date)
x = reservation["time"].split(":")
datetime_time = datetime.time(hour=int(x[0]), minute=int(x[1]))
new_client = Clients(name = reservation["name"], date = datetime_date, time = datetime_time, phone = int(reservation["phone"]),work_type = reservation["work"], msg_sent = False)
db.session.add(new_client)
db.session.commit()
return jsonify({"status" : "successfuly created new user"}),201
@app.route("/send_event", methods = ["GET","POST"])
@login_required
def event_send():
with app.app_context():
all_clients = Clients.query.all()
list_of_clients = []
for client in all_clients:
combine_dt = datetime.datetime.combine(client.date, client.time)
date_to_iso = combine_dt.isoformat()
clients = {
"id" : client.id,
"name" : client.name,
"start" : date_to_iso,
"phone" : client.phone,
"work" : client.work_type
}
list_of_clients.append(clients)
return jsonify(list_of_clients)
@app.route("/delete_id", methods = ["POST"])
@login_required
def delete_user():
data = request.get_json()
if data is None:
return jsonify({"status":"missing user data"}),400
id = data["userID"]
with app.app_context():
client_to_del = Clients.query.get(id)
if client_to_del is None:
return jsonify({"error":"user not found"}),404
db.session.delete(client_to_del)
db.session.commit()
return jsonify({"status":"user succesfuly deleted"}),204
@app.route("/update_db", methods = ["PATCH"])
@login_required
def patch_user():
data = request.get_json()
if data is None:
return jsonify({"error":"missing user data"}),400
id = data["id"]
format_date = "%Y-%m-%d"
datetime_date = datetime.datetime.strptime(data["date"], format_date)
t = data["time"].split(":")
datetime_time = datetime.time(hour=int(t[0]), minute=int(t[1]))
with app.app_context():
all_clients = Clients.query.get(id)
if all_clients.name != data["name"]:
all_clients.name = data["name"]
if all_clients.date != datetime_date:
all_clients.date = datetime_date
if all_clients.time != datetime_time:
all_clients.time = datetime_time
if all_clients.phone != data["phone"]:
all_clients.phone = data["phone"]
if all_clients.work_type != data["work"]:
all_clients.work_type = data["work"]
db.session.commit()
return jsonify({"status":"user succesfuly updated"}),200
def choose_tomorrow_reservation():
time_now = datetime.datetime.now().date()
day_after_today = time_now + datetime.timedelta(days=1)
clients_to_send_notification = []
with app.app_context():
all_clients = Clients.query.filter_by(date = day_after_today,msg_sent = False).all()
for client in all_clients:
if client.msg_sent == True:
continue
else:
str_time = client.time.strftime("%H:%M")
client_info = {
"id" : client.id,
"phone" : client.phone,
"reservation_time" : str_time
}
db.session.commit()
clients_to_send_notification.append(client_info)
return clients_to_send_notification
def delete_after_reservation():
today = datetime.datetime.now().date()
time_now = datetime.datetime.now().time()
with app.app_context():
clients_sent = Clients.query.filter_by(date = today, msg_sent = True).all()
clients_not_sent = Clients.query.filter_by(date = today, delete_error = True).all()
clients = []
clients = clients_sent + clients_not_sent
for client in clients:
reservation_cl = client.time
dt = datetime.datetime.combine(datetime.datetime.now(), reservation_cl)
dt_plus = dt + datetime.timedelta(minutes=30)
reservation = dt_plus.time()
time_now_toformat = datetime.datetime.now().time()
time_now = time_now_toformat.replace(microsecond=0)
if time_now > reservation:
db.session.delete(client)
db.session.commit()
time.sleep(1)
@app.route("/checklog")
@login_required
def check_log():
logs = Log.query.all()
return render_template("log.html", logs = logs)
@app.route("/checklog/<id>")
def delete_err(id):
log = Log.query.get(id)
db.session.delete(log)
db.session.commit()
return redirect(url_for("check_log"))
def automatic_sending_msg():
scheduler = BackgroundScheduler()
scheduler.add_job(automate_msg, "interval", minutes = 60, id="job1")
scheduler.add_job(delete_after_reservation, "interval", minutes = 60, id="job2")
scheduler.start()
def automate_msg():
clients = choose_tomorrow_reservation()
if clients:
send_message(clients=clients)
if __name__ == "__main__":
automatic_sending_msg()
app.run(host="0.0.0.0", port=5000)