Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
from fastapi import FastAPI, Request, Form
|
| 2 |
-
from fastapi.responses import HTMLResponse
|
| 3 |
from fastapi.templating import Jinja2Templates
|
| 4 |
-
import
|
| 5 |
import datetime
|
|
|
|
| 6 |
import re
|
| 7 |
|
| 8 |
app = FastAPI()
|
|
@@ -13,69 +13,69 @@ async def read_index(request: Request):
|
|
| 13 |
return templates.TemplateResponse("index.html", {"request": request})
|
| 14 |
|
| 15 |
@app.post("/fortune", response_class=HTMLResponse)
|
| 16 |
-
async def
|
| 17 |
request: Request,
|
| 18 |
birthday: str = Form(...),
|
| 19 |
standard: str = Form(...)
|
| 20 |
):
|
| 21 |
-
#
|
| 22 |
try:
|
| 23 |
birth_date = datetime.datetime.strptime(birthday, "%Y-%m-%d").date()
|
| 24 |
except Exception:
|
| 25 |
return templates.TemplateResponse(
|
| 26 |
"index.html",
|
| 27 |
-
{"request": request, "error": "Invalid date format
|
| 28 |
)
|
| 29 |
|
| 30 |
-
# Define fortunes
|
| 31 |
fortunes_by_grade = {
|
| 32 |
"1": [
|
| 33 |
-
"
|
| 34 |
-
"
|
| 35 |
],
|
| 36 |
"2": [
|
| 37 |
-
"
|
| 38 |
-
"
|
| 39 |
],
|
| 40 |
"3": [
|
| 41 |
-
"
|
| 42 |
-
"
|
| 43 |
],
|
| 44 |
"4": [
|
| 45 |
-
"
|
| 46 |
-
"Your ideas are
|
| 47 |
],
|
| 48 |
"5": [
|
| 49 |
-
"
|
| 50 |
-
"
|
| 51 |
],
|
| 52 |
"6": [
|
| 53 |
-
"
|
| 54 |
-
"
|
| 55 |
],
|
| 56 |
"7": [
|
| 57 |
-
"
|
| 58 |
-
"
|
| 59 |
],
|
| 60 |
"8": [
|
| 61 |
-
"
|
| 62 |
-
"
|
| 63 |
],
|
| 64 |
"9": [
|
| 65 |
-
"
|
| 66 |
-
"
|
| 67 |
],
|
| 68 |
"10": [
|
| 69 |
-
"
|
| 70 |
-
"
|
| 71 |
],
|
| 72 |
}
|
| 73 |
|
| 74 |
-
# Extract
|
| 75 |
grade_match = re.search(r'\d+', standard)
|
| 76 |
grade = grade_match.group(0) if grade_match else "1"
|
| 77 |
-
|
| 78 |
-
fortune_message = random.choice(
|
| 79 |
|
| 80 |
return templates.TemplateResponse(
|
| 81 |
"index.html",
|
|
|
|
| 1 |
from fastapi import FastAPI, Request, Form
|
|
|
|
| 2 |
from fastapi.templating import Jinja2Templates
|
| 3 |
+
from fastapi.responses import HTMLResponse
|
| 4 |
import datetime
|
| 5 |
+
import random
|
| 6 |
import re
|
| 7 |
|
| 8 |
app = FastAPI()
|
|
|
|
| 13 |
return templates.TemplateResponse("index.html", {"request": request})
|
| 14 |
|
| 15 |
@app.post("/fortune", response_class=HTMLResponse)
|
| 16 |
+
async def get_fortune(
|
| 17 |
request: Request,
|
| 18 |
birthday: str = Form(...),
|
| 19 |
standard: str = Form(...)
|
| 20 |
):
|
| 21 |
+
# Validate birthday (format: YYYY-MM-DD)
|
| 22 |
try:
|
| 23 |
birth_date = datetime.datetime.strptime(birthday, "%Y-%m-%d").date()
|
| 24 |
except Exception:
|
| 25 |
return templates.TemplateResponse(
|
| 26 |
"index.html",
|
| 27 |
+
{"request": request, "error": "Invalid date format! Please use YYYY-MM-DD."}
|
| 28 |
)
|
| 29 |
|
| 30 |
+
# Define positive fortunes for each grade (1st to 10th)
|
| 31 |
fortunes_by_grade = {
|
| 32 |
"1": [
|
| 33 |
+
"Your smile is the key to unlock a world of coding adventures!",
|
| 34 |
+
"You're just starting out, and the world of code is waiting for you!"
|
| 35 |
],
|
| 36 |
"2": [
|
| 37 |
+
"Every day is a new opportunity to learn something amazing!",
|
| 38 |
+
"Your curiosity will lead you to great coding adventures!"
|
| 39 |
],
|
| 40 |
"3": [
|
| 41 |
+
"Imagine, create, and code β your journey has just begun!",
|
| 42 |
+
"Keep exploring and you'll build wonders with code!"
|
| 43 |
],
|
| 44 |
"4": [
|
| 45 |
+
"Each line of code is a step towards greatness!",
|
| 46 |
+
"Your ideas are as bright as your future in coding!"
|
| 47 |
],
|
| 48 |
"5": [
|
| 49 |
+
"Coding magic happens when you dare to dream big!",
|
| 50 |
+
"You have the power to create amazing things with code!"
|
| 51 |
],
|
| 52 |
"6": [
|
| 53 |
+
"Innovation begins with your brilliant ideas!",
|
| 54 |
+
"Every keystroke brings you closer to a bright future!"
|
| 55 |
],
|
| 56 |
"7": [
|
| 57 |
+
"Your creativity will change the world β keep coding!",
|
| 58 |
+
"Never stop dreaming, coding, and inspiring others!"
|
| 59 |
],
|
| 60 |
"8": [
|
| 61 |
+
"Your passion for learning is the secret to coding success!",
|
| 62 |
+
"Keep coding and let your creativity shine!"
|
| 63 |
],
|
| 64 |
"9": [
|
| 65 |
+
"The digital world awaits your innovative ideas!",
|
| 66 |
+
"Believe in yourself and code on β the future is yours!"
|
| 67 |
],
|
| 68 |
"10": [
|
| 69 |
+
"Your coding skills are your superpower β use them to inspire!",
|
| 70 |
+
"The future is bright when you combine passion with code!"
|
| 71 |
],
|
| 72 |
}
|
| 73 |
|
| 74 |
+
# Extract the numeric grade from the standard string (e.g., "1st" -> "1")
|
| 75 |
grade_match = re.search(r'\d+', standard)
|
| 76 |
grade = grade_match.group(0) if grade_match else "1"
|
| 77 |
+
messages = fortunes_by_grade.get(grade, ["You're amazing! Keep coding and shining bright!"])
|
| 78 |
+
fortune_message = random.choice(messages)
|
| 79 |
|
| 80 |
return templates.TemplateResponse(
|
| 81 |
"index.html",
|