-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathusers.py
More file actions
39 lines (34 loc) · 1.16 KB
/
Copy pathusers.py
File metadata and controls
39 lines (34 loc) · 1.16 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
from sqlalchemy.orm import Session
from src.db.models.public.profiles import Profiles
from src.db.utils.db_transaction import db_transaction
import uuid
from loguru import logger
def ensure_profile_exists(
db: Session,
user_uuid: uuid.UUID,
email: str | None = None,
username: str | None = None,
avatar_url: str | None = None,
is_approved: bool = False,
) -> Profiles:
"""
Ensure a profile exists for the given user UUID.
If not, create one.
"""
profile = db.query(Profiles).filter(Profiles.user_id == user_uuid).first()
if not profile:
logger.info(f"Creating new profile for user {user_uuid}")
with db_transaction(db):
profile = Profiles(
user_id=user_uuid,
email=email,
username=username,
avatar_url=avatar_url,
is_approved=is_approved,
)
db.add(profile)
# No need for explicit commit/refresh as db_transaction handles commit,
# but we might need refresh if we access attributes immediately after.
# db_transaction usually commits.
db.refresh(profile)
return profile