-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
267 lines (216 loc) · 11.3 KB
/
main.py
File metadata and controls
267 lines (216 loc) · 11.3 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
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
import os
import typing
if not hasattr(typing, "_ClassVar") and hasattr(typing, "ClassVar"):
typing._ClassVar = typing.ClassVar
import json
from time import timezone
from send_mail import send_welcome_email
from email_template import render_email_template
from fastapi import FastAPI, HTTPException, Depends, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.templating import Jinja2Templates
from fastapi.security import OAuth2PasswordBearer
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse
from datetime import datetime, timedelta, timezone
from models import ParticipantRegistration, ProgressInfo, TaskSubmission, TaskInfo, Feedback
from db import get_one_where, insert_into_table, get_some_thing, get_some_where, get_record_by_email, get_record_by_email
from dotenv import load_dotenv
from routes.cron import router as cron_router
load_dotenv()
challenge_start_date = datetime(2025, 7, 23, 0, 0, 0)
app = FastAPI(title="30-Day Python Challenge API", version="1.0.0")
app.include_router(cron_router, prefix="/api/cron", tags=["Cron Jobs"])
API_ROOT = os.environ.get("API_ROOT")
token_url = f"{API_ROOT}/token"
oauth2_scheme = OAuth2PasswordBearer(tokenUrl=token_url)
# CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
static_folder = os.path.join(os.path.dirname(__file__), "static")
templates_folder = os.path.join(os.path.dirname(__file__), "templates")
app.mount("/static", StaticFiles(directory=static_folder), name="static")
templates = Jinja2Templates(directory=templates_folder)
def populate_tasks():
"""Populate the tasks table with 30 days of challenges"""
# The deadline for each task is 23:59:59 of the day before the next task
# Example: task 1 = 2025-07-20 23:59:59, task 2 = 2025-07-21 23:59:59, etc.
with open("templates/tasks.json", "r") as file:
tasks = json.load(file)
for task in tasks:
day = task["day"]
deadline = challenge_start_date + timedelta(days=day)
task_start_at = challenge_start_date + timedelta(days=day - 1)
formate_task_start_at = task_start_at.strftime("%Y-%m-%d %H:%M:%S")
task["start_at"] = formate_task_start_at
formatted_deadline = deadline.strftime("%Y-%m-%d %H:%M:%S")
task["deadline"] = formatted_deadline
inserted = insert_into_table("tasks", task)
task_info = TaskInfo(
day=day,
title_en=task["title_en"],
title_fr=task["title_fr"],
link_fr=task.get("link_fr"),
link_en=task.get("link_en"),
difficulty=(
"Beginner" if day <= 10 else
"Intermediate" if day <= 20 else
"Advanced"
),
deadline=formatted_deadline
)
try:
if inserted:
print(f"Task for day {day} inserted successfully.")
else:
print(f"Failed to insert task for day {day}.")
except Exception as e:
print(f"Error inserting task for day {day}: {e}")
@app.get("/")
def read_root(request: Request):
"""Root endpoint to serve the home page"""
return templates.TemplateResponse("home.html", {"request": request, "display_submission_card": True, "display_progress_card": True, "display_registration_card": True})
@app.get('/submit')
def submit_page(request: Request):
"""Serve the task submission page"""
return templates.TemplateResponse("home.html", {"request": request, "display_submission_card": True, "display_progress_card": True, "display_registration_card": False})
@app.get('/progress')
def progress_page(request: Request):
"""Serve the progress page"""
return templates.TemplateResponse("home.html", {"request": request, "display_submission_card": False, "display_progress_card": True, "display_registration_card": False})
@app.get('/register')
def register_page(request: Request):
"""Serve the registration page"""
return templates.TemplateResponse("home.html", {"request": request, "display_submission_card": False, "display_progress_card": False, "display_registration_card": True})
@app.post("/api/register")
async def register_participant(participant: ParticipantRegistration):
"""Register a new participant for the challenge"""
closing_date = datetime(2025, 7, 23) + timedelta(days=3, hours=23, minutes=59, seconds=59)
closing_date = closing_date.replace(tzinfo=timezone.utc)
if datetime.now(timezone.utc) > closing_date:
raise HTTPException(status_code=400, detail="Registration is closed for this challenge")
is_registered = get_record_by_email("participants", participant.email)
if is_registered:
raise HTTPException(status_code=400, detail="Participant already registered")
try:
data = participant.dict()
print(f"Participant {participant.full_name} registered successfully.")
try:
send_welcome_email(first_name=participant.full_name, participant_email=participant.email)
inserted_record = insert_into_table("participants", data)
print(f"Welcome email sent to {participant.email}")
except Exception as e:
print(f"Error sending welcome email: {e}")
raise HTTPException(status_code=500, detail="Failed to send welcome email, please try again with different email.")
if inserted_record:
return {"message": "Participant registered successfully"}
except Exception as e:
print(f"Error inserting participant: {e}")
raise HTTPException(status_code=500, detail="Participant already exists")
@app.post("/api/submit")
async def submit_task(submission: TaskSubmission):
"""Submit a solution for a specific task"""
is_participant = get_record_by_email("participants", submission.participant_email)
task = get_one_where("tasks", "day", int(submission.task_day))
if not task:
raise HTTPException(status_code=404, detail="Task not found")
start_date = datetime.fromisoformat(task["start_at"]).astimezone(timezone.utc)
if datetime.now(timezone.utc) < start_date:
raise HTTPException(status_code=400, detail="Task has not started yet")
task_deadline = datetime.fromisoformat(task["deadline"]).astimezone(timezone.utc)
now = datetime.now(timezone.utc)
if now > task_deadline:
raise HTTPException(status_code=400, detail="Task deadline has passed")
if not is_participant:
raise HTTPException(status_code=404, detail="Participant not found. Please register first.")
existing_submission = get_some_where("submissions", "participant_email", submission.participant_email, "task_day", submission.task_day)
if existing_submission:
raise HTTPException(status_code=400, detail="Task already submitted")
try:
# type coverting the task_day to int
submission.task_day = int(submission.task_day)
data = submission.dict()
inserted_record = insert_into_table("submissions", data)
return {"message": "Task submitted successfully", "submission": inserted_record}
except Exception as e:
raise HTTPException(status_code=500, detail="Database error occurred")
@app.post("/api/progress")
async def get_progress(progress: ProgressInfo):
"""Get progress for a specific participant"""
now = datetime.now(timezone.utc)
participant = get_record_by_email("participants", progress.participant_email)
if not participant:
raise HTTPException(status_code=404, detail="Participant not found")
tasks = get_some_thing("tasks")
task_progress = []
for task in tasks:
task_day = task.get("day")
task_start_at = datetime.fromisoformat(task["start_at"]).astimezone(timezone.utc)
now = datetime.now(timezone.utc)
if task_day is None or task_start_at is None:
continue
if now >=task_start_at:
submission = get_some_where("submissions", "participant_email", progress.participant_email, "task_day", task_day)
if len(submission) > 0:
submission = submission[0]
else:
submission = None
if submission:
task_progress.append({
"day": task_day,
"title_en": task.get("title_en"),
"status": "submitted" if submission.get("solution") else "not submitted",
"solution": submission.get("solution"),
"has_started": True if datetime.fromisoformat(task["start_at"]).isoformat() <= now.isoformat() else False,
"not_started": "not started" if datetime.fromisoformat(task["start_at"]).isoformat() > now.isoformat() else "not submitted",
"explanation": submission.get("explanation"),
"deadline": datetime.fromisoformat(task["deadline"]).isoformat(),
"has_started": True if datetime.fromisoformat(task["start_at"]).isoformat() <= now.isoformat() else False
})
else:
# cast deadline to datetime
task_progress.append({
"day": task_day,
"title_en": task.get("title_en"),
"status": "in progress" if datetime.fromisoformat(task["start_at"]).isoformat() <= now.isoformat() else "not submitted",
"not_started": "not started" if datetime.fromisoformat(task["start_at"]).isoformat() > now.isoformat() else "not submitted",
"deadline": datetime.fromisoformat(task["deadline"]).isoformat(),
"has_started": True if datetime.fromisoformat(task["start_at"]).isoformat() <= now.isoformat() else False
})
stat_count = {
"total_tasks": len(tasks),
"tasks": [task for task in task_progress if task["has_started"]],
"total_submissions": sum(1 for task in task_progress if task["status"] == "submitted"),
"not_started": sum(1 for task in task_progress if task["status"] == "not submitted" and not task["has_started"]),
"completed_tasks": sum(1 for task in task_progress if task["status"] == "submitted"),
"in_progress_tasks": sum(1 for task in task_progress if task["status"] == "in progress"),
"late_submissions": sum(1 for task in task_progress if task["status"] == "not submitted" and task.get("deadline") < now.isoformat())
}
if not task_progress:
raise HTTPException(status_code=404, detail="No tasks found for this participant")
return {"progress": task_progress, "stats": stat_count}
@app.get('/feedback')
def feedback_page(request: Request):
"""Serve the feedback page"""
return templates.TemplateResponse("feedback.html", {"request": request})
@app.post('/api/feedback')
async def receive_feedback(feedback: Feedback):
"""Receive feedback from participants and store it in the database"""
try:
data = feedback.dict()
inserted = insert_into_table('feedbacks', data)
if inserted:
return {"message": "Feedback received successfully"}
else:
raise HTTPException(status_code=500, detail="Failed to store feedback")
except Exception as e:
print(f"Error storing feedback: {e}")
raise HTTPException(status_code=500, detail="Server error")
if __name__ == "__main__":
# Populate tasks in the database
populate_tasks()