Spaces:
Sleeping
Sleeping
Abineshkumar
commited on
Commit
Β·
c31e237
1
Parent(s):
c83de6e
Add application file
Browse files- Dockerfile +16 -0
- README.md +4 -5
- __pycache__/config.cpython-310.pyc +0 -0
- __pycache__/main.cpython-310.pyc +0 -0
- config.py +6 -0
- models/__pycache__/user_model.cpython-310.pyc +0 -0
- models/user_model.py +39 -0
- requirements.txt +14 -0
- routes/__init__.py +0 -0
- routes/__pycache__/__init__.cpython-310.pyc +0 -0
- routes/__pycache__/users.cpython-310.pyc +0 -0
- routes/users.py +31 -0
- serviceAccountKey.json +13 -0
- services/__pycache__/users_service.cpython-310.pyc +0 -0
- services/users_service.py +74 -0
Dockerfile
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
|
2 |
+
# you will also find guides on how best to write your Dockerfile
|
3 |
+
|
4 |
+
FROM python:3.9
|
5 |
+
|
6 |
+
RUN useradd -m -u 1000 user
|
7 |
+
USER user
|
8 |
+
ENV PATH="/home/user/.local/bin:$PATH"
|
9 |
+
|
10 |
+
WORKDIR /app
|
11 |
+
|
12 |
+
COPY --chown=user ./requirements.txt requirements.txt
|
13 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
14 |
+
|
15 |
+
COPY --chown=user . /app
|
16 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
README.md
CHANGED
@@ -1,11 +1,10 @@
|
|
1 |
---
|
2 |
-
title: Age Better
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: docker
|
7 |
pinned: false
|
8 |
-
license: apache-2.0
|
9 |
---
|
10 |
|
11 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
+
title: Age Better Endpoints
|
3 |
+
emoji: π
|
4 |
+
colorFrom: red
|
5 |
+
colorTo: blue
|
6 |
sdk: docker
|
7 |
pinned: false
|
|
|
8 |
---
|
9 |
|
10 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
__pycache__/config.cpython-310.pyc
ADDED
Binary file (377 Bytes). View file
|
|
__pycache__/main.cpython-310.pyc
ADDED
Binary file (614 Bytes). View file
|
|
config.py
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import firebase_admin
|
2 |
+
from firebase_admin import credentials, auth, firestore
|
3 |
+
|
4 |
+
cred = credentials.Certificate("serviceAccountKey.json")
|
5 |
+
default_app = firebase_admin.initialize_app(credential=cred)
|
6 |
+
db = firestore.client()
|
models/__pycache__/user_model.cpython-310.pyc
ADDED
Binary file (1.62 kB). View file
|
|
models/user_model.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from pydantic import BaseModel, EmailStr
|
2 |
+
from uuid import UUID
|
3 |
+
|
4 |
+
class User(BaseModel):
|
5 |
+
id: UUID
|
6 |
+
name: str
|
7 |
+
surname: str
|
8 |
+
phone: str
|
9 |
+
email: EmailStr
|
10 |
+
role: str
|
11 |
+
status: str
|
12 |
+
|
13 |
+
class CreateUser(BaseModel):
|
14 |
+
name: str
|
15 |
+
surname: str
|
16 |
+
phone: str
|
17 |
+
email: EmailStr
|
18 |
+
role: str
|
19 |
+
password: str
|
20 |
+
|
21 |
+
class UpdateUser(BaseModel):
|
22 |
+
name: str = None
|
23 |
+
surname: str = None
|
24 |
+
phone: str = None
|
25 |
+
email: EmailStr = None
|
26 |
+
role: str = None
|
27 |
+
status: str = None
|
28 |
+
|
29 |
+
class OTPVerification(BaseModel):
|
30 |
+
email: EmailStr
|
31 |
+
otp: str
|
32 |
+
|
33 |
+
class ResetPassword(BaseModel):
|
34 |
+
email: EmailStr
|
35 |
+
newPassword: str
|
36 |
+
|
37 |
+
class ChangePassword(BaseModel):
|
38 |
+
currentPassword: str
|
39 |
+
newPassword: str
|
requirements.txt
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
firebase-admin
|
2 |
+
fastapi
|
3 |
+
uvicorn
|
4 |
+
sqlmodel
|
5 |
+
typer
|
6 |
+
dynaconf
|
7 |
+
jinja2
|
8 |
+
python-jose[cryptography]
|
9 |
+
passlib[bcrypt]
|
10 |
+
python-multipart
|
11 |
+
psycopg2-binary
|
12 |
+
uuid
|
13 |
+
pydantic
|
14 |
+
email-validator
|
routes/__init__.py
ADDED
File without changes
|
routes/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (142 Bytes). View file
|
|
routes/__pycache__/users.cpython-310.pyc
ADDED
Binary file (1.15 kB). View file
|
|
routes/users.py
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import APIRouter
|
2 |
+
from uuid import UUID
|
3 |
+
from services.users_service import (
|
4 |
+
create_user_in_firestore,
|
5 |
+
get_user_from_firestore,
|
6 |
+
update_user_in_firestore,
|
7 |
+
delete_user_from_firestore
|
8 |
+
)
|
9 |
+
from models.user_model import CreateUser, UpdateUser
|
10 |
+
|
11 |
+
router = APIRouter(
|
12 |
+
prefix="/api/v1/users",
|
13 |
+
tags=["Users"]
|
14 |
+
)
|
15 |
+
|
16 |
+
@router.post("/")
|
17 |
+
def create_user(user_data: CreateUser):
|
18 |
+
return create_user_in_firestore(user_data.dict())
|
19 |
+
|
20 |
+
@router.get("/{id}")
|
21 |
+
def get_user(id: str): # Accept uid as a string, not UUID
|
22 |
+
return get_user_from_firestore(id)
|
23 |
+
|
24 |
+
@router.put("/{id}")
|
25 |
+
def update_user(id: str, user_data: UpdateUser): # Accept uid as a string
|
26 |
+
return update_user_in_firestore(id, user_data.dict())
|
27 |
+
|
28 |
+
@router.delete("/{id}")
|
29 |
+
def delete_user(id: str): # Accept uid as a string
|
30 |
+
return delete_user_from_firestore(id)
|
31 |
+
|
serviceAccountKey.json
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"type": "service_account",
|
3 |
+
"project_id": "fastapi-cc369",
|
4 |
+
"private_key_id": "ac45a289f6bf93c2afa763be7d7aeb3533f10332",
|
5 |
+
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEugIBADANBgkqhkiG9w0BAQEFAASCBKQwggSgAgEAAoIBAQDt6ODbsL6/kaxh\nOmW6PVpHy+f1t43q3W8K4EtvKQzdtaoMTJbZV18z6fijy4ZE7x9QMFMXu54xTsRd\nB/Y//ttx3JlluZoD2J1xh0zdSagZF85iQFZSpN1lfKoMWsiNEf5Gm2MYhRuKGO60\no74W+v7Z18Fnq8A6rSkud0udbzmlM1y4zW7m28IZFucoohVZKc6lLdQpmA//JTdl\nhEljYUfry+4roQCE229pMexNXrqromI8obO4wVOCzobm8kptLCg4VkXUdVAU9i/c\n9l7MrdCKx159LpTYfw6Hs0MEPAkM+2wBNxjN7xOmqmEFCq2JgeZTQOKIfMpwx9eb\nip6ohmBNAgMBAAECggEAF4Nw992V2K6v3BMuuzO1oMtzI0qvNNOKVkeZRQUIX8BE\n7H7EWPpE70dUWvHyVQ1SKbWA/OM+7Q4lI7G2AZixfSUppw9rPK6TzwTpkKRnQAWl\nkPndXkErnYxu7RYq5KBu4nE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p3NnN8KOm0We0qOC68RAyQvlctKwuC4X/Cdmyia6Idd8Mr8ghnP\n8GbgKZQpgSI4mWah+xI=\n-----END PRIVATE KEY-----\n",
|
6 |
+
"client_email": "[email protected]",
|
7 |
+
"client_id": "113038661830473581595",
|
8 |
+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
|
9 |
+
"token_uri": "https://oauth2.googleapis.com/token",
|
10 |
+
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
|
11 |
+
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-leg2l%40fastapi-cc369.iam.gserviceaccount.com",
|
12 |
+
"universe_domain": "googleapis.com"
|
13 |
+
}
|
services/__pycache__/users_service.cpython-310.pyc
ADDED
Binary file (2.04 kB). View file
|
|
services/users_service.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import HTTPException
|
2 |
+
from config import db, auth
|
3 |
+
|
4 |
+
|
5 |
+
def create_user_in_firestore(user_data):
|
6 |
+
try:
|
7 |
+
# Create user in Firebase Authentication
|
8 |
+
user = auth.create_user(
|
9 |
+
email=user_data["email"],
|
10 |
+
password=user_data["password"],
|
11 |
+
display_name=f"{user_data['name']} {user_data['surname']}"
|
12 |
+
)
|
13 |
+
|
14 |
+
# Store user data in Firestore with Firebase UID
|
15 |
+
user_ref = db.collection("users").document(user.uid)
|
16 |
+
user_ref.set({
|
17 |
+
"uid": user.uid, # Store Firebase UID in Firestore
|
18 |
+
"name": user_data["name"],
|
19 |
+
"surname": user_data["surname"],
|
20 |
+
"email": user_data["email"],
|
21 |
+
"phone": user_data["phone"],
|
22 |
+
"role": user_data["role"],
|
23 |
+
"status": "Active"
|
24 |
+
})
|
25 |
+
|
26 |
+
return {"isSuccess": True, "message": "User created successfully", "uid": user.uid}
|
27 |
+
|
28 |
+
except Exception as e:
|
29 |
+
raise HTTPException(status_code=400, detail=str(e))
|
30 |
+
|
31 |
+
|
32 |
+
def get_user_from_firestore(user_id: str):
|
33 |
+
try:
|
34 |
+
# Use Firebase UID instead of UUID
|
35 |
+
user_ref = db.collection("users").document(user_id)
|
36 |
+
user_snapshot = user_ref.get()
|
37 |
+
|
38 |
+
if user_snapshot.exists:
|
39 |
+
return {"isSuccess": True, "data": user_snapshot.to_dict()}
|
40 |
+
else:
|
41 |
+
raise HTTPException(status_code=404, detail="User not found")
|
42 |
+
except Exception as e:
|
43 |
+
raise HTTPException(status_code=500, detail=str(e))
|
44 |
+
|
45 |
+
|
46 |
+
def update_user_in_firestore(user_id: str, update_data):
|
47 |
+
try:
|
48 |
+
# Use Firebase UID instead of UUID
|
49 |
+
user_ref = db.collection("users").document(user_id)
|
50 |
+
user_ref.update(update_data)
|
51 |
+
|
52 |
+
# Optionally update user data in Firebase Auth (e.g., email, password)
|
53 |
+
if 'email' in update_data:
|
54 |
+
auth.update_user(user_id, email=update_data['email'])
|
55 |
+
if 'password' in update_data:
|
56 |
+
auth.update_user(user_id, password=update_data['password'])
|
57 |
+
|
58 |
+
return {"isSuccess": True, "message": "User updated successfully"}
|
59 |
+
except Exception as e:
|
60 |
+
raise HTTPException(status_code=400, detail=str(e))
|
61 |
+
|
62 |
+
|
63 |
+
def delete_user_from_firestore(user_id: str):
|
64 |
+
try:
|
65 |
+
# Delete user from Firestore using Firebase UID
|
66 |
+
user_ref = db.collection("users").document(user_id)
|
67 |
+
user_ref.delete()
|
68 |
+
|
69 |
+
# Delete user from Firebase Auth
|
70 |
+
auth.delete_user(user_id)
|
71 |
+
|
72 |
+
return {"isSuccess": True, "message": "User deleted successfully"}
|
73 |
+
except Exception as e:
|
74 |
+
raise HTTPException(status_code=400, detail=str(e))
|