forked from IcyPalm/todoist-location-labels
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
364 lines (327 loc) · 12.1 KB
/
app.py
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import sys
import os
import logging
import urllib.parse
import base64
from datetime import datetime
from tenacity import retry, stop_after_attempt, wait_fixed, retry_if_exception_type, before_log, after_log
from opentelemetry import trace
sys.path.append("todoist-python")
import todoist
import requests
from flask import Flask
from flask import request
from flask import json
from flask import render_template
from flask import redirect
from flask import session
from flask import abort
from flask import url_for
from flask_sqlalchemy import SQLAlchemy
from flask_session import Session # Import Session
app = Flask(__name__)
# Configure logging
logging.basicConfig(level=logging.INFO)
app.logger.setLevel(logging.INFO)
# Configure your app for Flask-Session
app.config['SESSION_TYPE'] = 'filesystem'
app.config['SESSION_PERMANENT'] = True
app.config['PERMANENT_SESSION_LIFETIME'] = 86400 # e.g., one day
# pool_pre_ping should help handle DB connection drops
app.config['SQLALCHEMY_ENGINE_OPTIONS'] = {"pool_pre_ping": True}
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
app.config["SQLALCHEMY_DATABASE_URI"] = os.environ.get(
"DATABASE_URL", "sqlite:///test.db"
)
app.config['SQLALCHEMY_POOL_SIZE'] = 10
app.config['SQLALCHEMY_POOL_TIMEOUT'] = 30
app.config['SQLALCHEMY_POOL_RECYCLE'] = 299
app.secret_key = os.environ["TODOIST_FLASK_SECRET_KEY"]
db = SQLAlchemy(app)
client_id = os.environ["TODOIST_CLIENT_ID"]
client_secret = os.environ["TODOIST_CLIENT_SECRET"]
google_map_api_key = os.environ["GOOGLE_MAP_API_KEY"]
google_analytics_id = os.environ.get("GOOGLE_ANALYTICS_ID")
tracer = trace.get_tracer("todoist-flask")
Session(app) # Initialize Flask-Session
class User(db.Model):
id = db.Column(db.BigInteger, primary_key=True)
oauth_token = db.Column(db.String(64), nullable=True)
class LocationLabel(db.Model):
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.BigInteger, db.ForeignKey("user.id"), nullable=False)
user = db.relationship(
"User", backref=db.backref("location_labels", lazy="dynamic")
)
label_id = db.Column(db.BigInteger, nullable=False, index=True)
name = db.Column(db.String, nullable=False)
long = db.Column(db.Float, nullable=False)
lat = db.Column(db.Float, nullable=False)
loc_trigger = db.Column(db.String, nullable=False)
radius = db.Column(db.Float, nullable=False)
# with app.app_context():
# db.create_all()
@tracer.start_as_current_span("get_current_user")
def get_current_user():
user_id = session.get("user_id")
if user_id is None:
abort(401)
user = User.query.get(user_id)
if user is None:
abort(401)
return user
def log_request(route):
app.logger.info(f"Request made to {route}: IP {request.headers.get('Fly-Client-IP')} at {datetime.now()}")
def log_retry_attempt(retry_state):
app.logger.warning(f"Retrying API Call: Attempt {retry_state.attempt_number}")
def log_retry_error(retry_state):
app.logger.error(f"Retry failed: {retry_state.outcome.exception()}")
@retry(
retry=retry_if_exception_type(requests.exceptions.HTTPError),
stop=stop_after_attempt(3),
wait=wait_fixed(2),
before=log_retry_attempt,
after=log_retry_error
)
def resilient_api_call(url, headers):
response = requests.get(url, headers=headers, timeout=10)
response.raise_for_status() # Raises HTTPError for 4xx/5xx responses
return response.json()
@tracer.start_as_current_span("get_todoist_labels")
def get_todoist_labels(headers):
try:
return resilient_api_call("https://api.todoist.com/rest/v2/labels", headers)
except requests.exceptions.HTTPError as e:
# Handle HTTP errors
# Log the error, notify the user, use fallback data, etc.
# Example: log and return an empty list or a cached response
app.logger.error(f"API request failed: {e}")
return []
except requests.exceptions.RequestException as e:
# Handle non-HTTP errors (like network issues)
app.logger.error(f"API request failed: {e}")
return []
@app.route("/")
@tracer.start_as_current_span("index")
def index():
log_request('/')
user_id = session.get("user_id")
kwargs = {
"google_map_api_key": google_map_api_key,
"google_analytics_id": google_analytics_id,
}
if user_id is not None:
user = User.query.get(user_id)
app.logger.info(f"user_id: {user_id} and token: {user.oauth_token}")
headers = {"Authorization": "Bearer " + user.oauth_token}
labels = get_todoist_labels(headers)
kwargs["labels"] = labels
api = todoist.TodoistAPI(user.oauth_token)
api.sync()
kwargs["user_full_name"] = api.user.get("full_name")
# map from label id to location labels
location_labels = {}
for item in user.location_labels.all():
location_labels[str(item.label_id)] = item
kwargs["location_labels"] = location_labels
return render_template("index.html", **kwargs)
@app.route("/authorize")
@tracer.start_as_current_span("authorize")
def authorize():
log_request('/authorize')
state = base64.b64encode(os.urandom(32)).decode("utf8")
session["oauth_secret_state"] = state
return redirect(
"https://todoist.com/oauth/authorize?"
+ urllib.parse.urlencode(
dict(
client_id=client_id,
scope="data:read_write,data:delete",
state=state,
)
)
)
@app.route("/oauth/redirect")
@tracer.start_as_current_span("oauth_redirect")
def oauth_redirect():
log_request('/oauth/redirect')
state = session["oauth_secret_state"]
if request.args.get("state") != state:
return abort(401)
code = request.args.get("code")
if not code:
return abort(400)
try:
resp = requests.post(
"https://todoist.com/oauth/access_token",
data=dict(
client_id=client_id,
client_secret=client_secret,
code=code,
redirect_uri=url_for("authorize", _external=True),
),
)
resp.raise_for_status()
except requests.exceptions.HTTPError as err:
# Log the error details
app.logger.error(f"HTTP Error occurred: {err}")
app.logger.error(f"Response status: {resp.status_code}")
app.logger.error(f"Response headers: {resp.headers}")
app.logger.error(f"Response body: {resp.text}") # or resp.json() if it's JSON
# Return an error response or handle it as you see fit
return abort(500)
access_token = resp.json()["access_token"]
api = todoist.TodoistAPI(access_token)
api.sync()
user_id = api.user.get_id()
user = User.query.get(user_id)
if user is None:
user = User(id=user_id, oauth_token=access_token)
db.session.add(user)
else:
user.oauth_token = access_token
db.session.commit()
session["user_id"] = user.id
return redirect(url_for("index"))
@app.route("/logout")
@tracer.start_as_current_span("logout")
def logout():
log_request('/logout')
del session["user_id"]
return redirect(url_for("index"))
@app.route("/delete_label_location/<int:label_location_id>")
@tracer.start_as_current_span("delete_label_location")
def delete_label_location(label_location_id):
log_request(f'/delete_label_location/{label_location_id}')
user = get_current_user()
label_location = LocationLabel.query.filter_by(label_id=label_location_id).all()[0]
if label_location is None:
return abort(404)
if label_location.user.id != user.id:
return abort(401)
db.session.delete(label_location)
db.session.commit()
return redirect(url_for("index"))
@app.route("/create_label_location", methods=["POST"])
@tracer.start_as_current_span("create_label_location")
def create_label_location():
log_request('/create_label_location')
user = get_current_user()
label_id = int(request.form["label_id"])
trigger = request.form["trigger"]
address = request.form["address"]
lat = float(request.form["lat"])
long = float(request.form["long"])
radius = float(request.form.get("radius", 300))
location_label = LocationLabel(
user=user,
label_id=label_id,
loc_trigger=trigger,
long=long,
lat=lat,
name=address,
radius=radius,
)
db.session.add(location_label)
db.session.commit()
return redirect(url_for("index"))
@app.route("/webhook", methods=["POST"])
@tracer.start_as_current_span("webhook")
def webhook():
log_request('/webhook')
event = request.json
# app.logger.info('Full event: %s', event)
if event["event_name"] not in ["item:added", "item:updated"]:
return ""
initiator = event["initiator"]
event_data = event["event_data"]
app.logger.info(
"Received webhook event %s for %s with info: %s",
event["event_name"],
event_data["id"],
event_data,
)
user = User.query.get(int(initiator["id"]))
api = todoist.TodoistAPI(user.oauth_token)
api.sync()
item_reminders = list(
filter(
lambda x: x["type"] == "location" and x["item_id"] == event_data["id"],
api.reminders.all(),
)
)
event_data_label_ids = []
for x in api.labels.all():
for l in event_data["labels"]:
if x["name"] == l:
event_data_label_ids.append(int(x["id"]))
user_location_labels = LocationLabel.query.filter_by(user_id=initiator["id"]).all()
not_used_location_labels = []
for x in user_location_labels:
if x.label_id not in event_data_label_ids:
not_used_location_labels.append(x)
to_be_deleted_reminders = list(
filter(
lambda x: x["name"] in map(lambda y: y.name, not_used_location_labels)
and x["loc_trigger"]
in map(lambda y: y.loc_trigger, not_used_location_labels)
and x["radius"] in map(lambda y: y.radius, not_used_location_labels),
item_reminders,
)
)
for reminder in to_be_deleted_reminders:
app.logger.info(
"Reminder found that should be deleted: %s, deleting", reminder["id"]
)
api.reminders.delete(reminder["id"])
for label_id in event_data_label_ids:
loc_labels = user.location_labels.filter_by(label_id=label_id).all()
if not loc_labels:
app.logger.info("No location labels found for label %s, skip", label_id)
continue
for loc_label in loc_labels:
app.logger.info(
"Adding location reminder for item %s from location label %s",
event_data["id"],
loc_label.id,
)
api_reminders = filter(
lambda x: x["item_id"] == event_data["id"] and x["type"] == "location",
api.state["reminders"],
)
existing_reminder = list(
filter(
lambda x: x["name"] == loc_label.name
and x["loc_trigger"] == loc_label.loc_trigger
and x["radius"] == loc_label.radius,
api_reminders,
)
)
if existing_reminder:
app.logger.info(
"Not adding location reminder for item %s from location label %s, does already exist!",
event_data["id"],
loc_label.id,
)
continue
api.reminders.add(
event_data["id"],
type="location",
name=loc_label.name,
loc_lat=str(loc_label.lat),
loc_long=str(loc_label.long),
loc_trigger=loc_label.loc_trigger,
radius=loc_label.radius,
)
app.logger.info(
"Location reminder added for item %s from location label %s",
event_data["id"],
loc_label.id,
)
api.commit()
return "ok"
if __name__ == "__main__":
if len(sys.argv) >= 2 and sys.argv[1] == "initdb":
db.create_all()
else:
app.run(debug=True, use_reloader=True)