Skip to content
This repository was archived by the owner on Jun 7, 2025. It is now read-only.

Commit c294c9a

Browse files
authored
Merge pull request #36 from CodeHex16/fix-yihao-sprint15
Update MongoDB connection logic and comment out error handling
2 parents 510891b + 163a9e4 commit c294c9a

13 files changed

Lines changed: 186 additions & 31 deletions

File tree

app/database.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
11
import os
2+
import regex
3+
4+
5+
MONGODB_URL = os.getenv("MONGODB_URL")
6+
MONGO_USERNAME = os.getenv("MONGO_USERNAME")
7+
MONGO_PASSWORD = os.getenv("MONGO_PASSWORD")
8+
9+
# Se l'MONGODB_URL se contiene username e password, allora usa MONGODB_URL
10+
if MONGODB_URL and regex.match(r"^mongodb://.*:.*@.*:.*", MONGODB_URL):
11+
MONGODB_URL = MONGODB_URL
12+
# Se l'MONGODB_URL non contiene username e password, allora usa MONGO_USERNAME e MONGO_PASSWORD
13+
elif MONGO_USERNAME and MONGO_PASSWORD and regex.match(r"^.*:.*", MONGODB_URL):
14+
MONGODB_URL = f"mongodb://{MONGO_USERNAME}:{MONGO_PASSWORD}@{MONGODB_URL}"
15+
else:
16+
# Se non sono presenti username e password, usa l'URL di default
17+
MONGODB_URL = "mongodb://root:example@localhost:27017"
18+
219

3-
MONGODB_URL = os.getenv("MONGODB_URL") or "mongodb://root:example@mongo-db:27017"
420

521
# Variabile globale per memorizzare la connessione al database
622
_db = None

app/main.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ async def lifespan(app: FastAPI):
1717
# Startup
1818
app.mongodb_client = AsyncIOMotorClient(MONGODB_URL + "/supplai?authSource=admin")
1919
app.database = app.mongodb_client.get_default_database()
20+
2021
info("Connected to the MongoDB database!")
2122
init_db(app.database)
2223

app/routes/auth.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from app.repositories.user_repository import UserRepository, get_user_repository
1717
import app.schemas as schemas
1818
from app.utils import verify_password
19-
from app.service.auth_service import AccessRoles
19+
from app.auth_roles import AccessRoles
2020

2121
load_dotenv()
2222

app/routes/chat.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@
44
from fastapi import Query
55
from typing import Optional
66

7-
from app.database import get_db
8-
from app.repositories.chat_repository import ChatRepository
7+
from app.repositories.chat_repository import ChatRepository, get_chat_repository
98
import app.schemas as schemas
109
from app.routes.auth import (
1110
verify_user,
1211
verify_admin,
1312
)
14-
from app.repositories.chat_repository import get_chat_repository
1513

1614
router = APIRouter(
1715
prefix="/chats",

app/routes/document.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
from bson import ObjectId
44
from pymongo.errors import DuplicateKeyError
55

6-
from app.database import get_db
7-
from app.repositories.document_repository import DocumentRepository
86
from app.repositories.user_repository import UserRepository
97
import app.schemas as schemas
108
from app.routes.auth import (
@@ -76,11 +74,11 @@ async def get_documents(
7674

7775
documents = await document_repository.get_documents()
7876

79-
if not documents:
80-
raise HTTPException(
81-
status_code=status.HTTP_404_NOT_FOUND,
82-
detail="Nessun documento trovato",
83-
)
77+
# if not documents:
78+
# raise HTTPException(
79+
# status_code=status.HTTP_404_NOT_FOUND,
80+
# detail="Nessun documento trovato",
81+
# )
8482

8583
return documents
8684

@@ -124,9 +122,6 @@ async def delete_document(
124122
)
125123

126124
try:
127-
print("Deleting document:", file)
128-
print("AAAAAAAAAAAAAAAAAAAAA")
129-
print("ObjectId:", ObjectId(file.id))
130125
result = await document_repository.delete_document(file_id=ObjectId(file.id))
131126
if result.deleted_count == 0:
132127
raise HTTPException(

app/routes/user.py

Lines changed: 155 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,85 @@
2323
tags=["user"],
2424
)
2525

26+
style = """<style>
27+
body {
28+
background-color: #f8f9fa;
29+
font-family: 'Roboto', sans-serif;
30+
margin: 0;
31+
padding: 0;
32+
color: #212121;
33+
}
34+
.container {
35+
max-width: 600px;
36+
margin: 2rem auto;
37+
background: #fff;
38+
border-radius: 8px;
39+
box-shadow: 0 4px 12px rgba(0,0,0,0.08);
40+
padding: 2rem;
41+
}
42+
.header {
43+
text-align: center;
44+
margin-bottom: 1rem;
45+
}
46+
.title {
47+
font-size: 1.5rem;
48+
font-weight: 500;
49+
}
50+
.content {
51+
margin: 1rem 0;
52+
line-height: 1.6;
53+
}
54+
.password-box {
55+
background-color: #e3f2fd;
56+
padding: 1rem;
57+
font-size: 1.25rem;
58+
font-weight: bold;
59+
border-radius: 6px;
60+
text-align: center;
61+
color: #0d47a1;
62+
letter-spacing: 0.5px;
63+
}
64+
.btn {
65+
display: inline-block;
66+
background-color: #1976d2;
67+
color: #fff !important;
68+
padding: 0.75rem 1.5rem;
69+
text-decoration: none;
70+
border-radius: 24px;
71+
font-weight: 500;
72+
transition: background 0.3s ease;
73+
}
74+
.btn:hover {
75+
background-color: #1565c0;
76+
}
77+
.footer {
78+
text-align: center;
79+
font-size: 0.875rem;
80+
margin-top: 2rem;
81+
color: #666;
82+
}
83+
.icon-links {
84+
margin-top: 1rem;
85+
text-align: center;
86+
}
87+
.icon-links a {
88+
margin: 0 0.5rem;
89+
display: inline-block;
90+
text-decoration: none;
91+
}
92+
.icon-links img {
93+
width: 24px;
94+
height: 24px;
95+
vertical-align: middle;
96+
filter: grayscale(100%);
97+
transition: filter 0.3s;
98+
}
99+
.icon-links img:hover {
100+
filter: grayscale(0%);
101+
}
102+
</style>
103+
"""
104+
26105
SECRET_KEY_JWT = os.getenv("SECRET_KEY_JWT") or "$2b$12$zqt9Rgv1PzORjG5ghJSb6OSdYrt7f7cLc38a21DgX/DMyqt80AUCi"
27106
ALGORITHM = "HS256"
28107

@@ -84,7 +163,44 @@ async def register_user(
84163
await EmailService().send_email(
85164
to=[user_data.email],
86165
subject=f"[Suppl-AI] Registrazione utente",
87-
body=f"Benvenuto in Suppl-AI!\nEcco la tua password temporanea\n\n{password}\n\n Accedi e cambiala subito!",
166+
body=f"""
167+
<!DOCTYPE html>
168+
<html lang="it">
169+
<head>
170+
<meta charset="UTF-8" />
171+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
172+
<title>Registrazione utente</title>
173+
{style}
174+
</head>
175+
<body>
176+
<div class="container">
177+
<div class="header">
178+
<div class="title" style="font-size: 2rem; font-weight: 700; color: #1976d2;">SUPPL-AI</div>
179+
</div>
180+
<div class="content">
181+
<p><strong>Benvenuto in Suppl-AI!</strong></p>
182+
<p>Ecco la tua password temporanea:</p>
183+
<div class="password-box">{password}</div>
184+
<p>Accedi e cambiala subito!</p>
185+
</div>
186+
187+
<div class="icon-links">
188+
<a href="https://codehex16.github.io/" title="Sito Web">
189+
<img src="https://img.icons8.com/ios-filled/50/000000/domain.png" alt="Sito">
190+
</a>
191+
<a href="https://github.com/codehex16" title="GitHub">
192+
<img src="https://img.icons8.com/ios-filled/50/000000/github.png" alt="GitHub">
193+
</a>
194+
</div>
195+
196+
<div class="footer">
197+
Questo progetto &egrave; realizzato da <strong>CodeHex16</strong>, gruppo 16 del Progetto di SWE dell'Universit&agrave; degli Studi di Padova.
198+
</div>
199+
</div>
200+
</body>
201+
</html>
202+
"""
203+
# body=f"Benvenuto in Suppl-AI!\nEcco la tua password temporanea\n\n{password}\n\n Accedi e cambiala subito!",
88204
)
89205
except Exception as e:
90206
raise HTTPException(
@@ -362,7 +478,44 @@ async def reset_password(
362478
await EmailService().send_email(
363479
to=[user.get("_id")],
364480
subject="[Suppl-AI] Password Reset",
365-
body=f"Ciao {user.get('name')},\n\nEcco la tua nuova password temporanea:\n\n{password}\n\nAccedi e cambiala subito!",
481+
body=f"""
482+
<!DOCTYPE html>
483+
<html lang="it">
484+
<head>
485+
<meta charset="UTF-8" />
486+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
487+
<title>Password Reset</title>
488+
{style}
489+
</head>
490+
<body>
491+
<div class="container">
492+
<div class="header">
493+
<div class="title" style="font-size: 2rem; font-weight: 700; color: #1976d2;">SUPPL-AI</div>
494+
<div class="title">Ciao {html.escape(user.get('name'))},</div>
495+
</div>
496+
<div class="content">
497+
Ecco la tua nuova password temporanea:
498+
<div class="password-box">{password}</div>
499+
<p>Accedi e cambiala subito per mantenere il tuo account sicuro.</p>
500+
</div>
501+
502+
<div class="icon-links">
503+
<a href="https://codehex16.github.io/" title="Sito Web">
504+
<img src="https://img.icons8.com/ios-filled/50/000000/domain.png" alt="Sito">
505+
</a>
506+
<a href="https://github.com/codehex16" title="GitHub">
507+
<img src="https://img.icons8.com/ios-filled/50/000000/github.png" alt="GitHub">
508+
</a>
509+
</div>
510+
511+
<div class="footer">
512+
Questo progetto &egrave; realizzato da <strong>CodeHex16</strong>, gruppo 16 del Progetto di SWE dell'Universit&agrave; degli Studi di Padova.
513+
</div>
514+
</div>
515+
</body>
516+
</html>
517+
"""
518+
# body=f"Ciao {user.get('name')},\n\nEcco la tua nuova password temporanea:\n\n{password}\n\nAccedi e cambiala subito!",
366519
)
367520
except Exception as e:
368521
# raise HTTPException(

app/schemas.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,8 @@ class UserForgotPassword(BaseModel):
5151
email: EmailStr
5252

5353

54-
class UserUpdatePassword(BaseModel):
54+
class UserUpdatePassword(UserAuth):
5555
password: str
56-
current_password: str
5756

5857
@field_validator("password")
5958
def check_password_complexity(cls, value: str):

app/service/email_service.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ async def send_email(self, to: List[EmailStr], subject: str, body: str):
3131
raise ValueError("Email configuration is not valid. Please check your environment variables.")
3232
else:
3333
message = MessageSchema(
34-
subject=subject, recipients=to, body=body, subtype=MessageType.plain #or MessageType.html
34+
subject=subject, recipients=to, body=body, subtype=MessageType.html #or MessageType.plain
3535
)
3636
await self.mail.send_message(message)
3737

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ pytest-asyncio==0.26.0
1818
pytest-cov==6.1.1
1919
httpx==0.28.1
2020
coverage==7.8.0
21-
coveralls==4.0.1
21+
coveralls==4.0.1
22+
regex==2024.11.6

0 commit comments

Comments
 (0)