Spaces:
Runtime error
Runtime error
Commit
·
e941a76
1
Parent(s):
9af3b3c
update-delete
Browse files- KTP.jpg +0 -0
- app/__pycache__/dependencies.cpython-310.pyc +0 -0
- app/__pycache__/main.cpython-310.pyc +0 -0
- app/api/v1/endpoints/__pycache__/auth.cpython-310.pyc +0 -0
- app/api/v1/endpoints/__pycache__/ocrtemplate.cpython-310.pyc +0 -0
- app/api/v1/endpoints/__pycache__/user.cpython-310.pyc +0 -0
- app/api/v1/endpoints/auth.py +2 -2
- app/api/v1/endpoints/ocrtemplate.py +12 -0
- app/api/v1/endpoints/user.py +24 -7
- app/core/__pycache__/middleware.cpython-310.pyc +0 -0
- app/crud/__pycache__/ocrtemplate.cpython-310.pyc +0 -0
- app/crud/__pycache__/users.cpython-310.pyc +0 -0
- app/crud/ocrtemplate.py +18 -3
- app/crud/users.py +1 -1
- app/dependencies.py +5 -5
- app/main.py +1 -1
- app/models/__pycache__/token.cpython-310.pyc +0 -0
- app/models/__pycache__/users.cpython-310.pyc +0 -0
- app/models/token.py +1 -0
- app/models/users.py +8 -2
KTP.jpg
DELETED
|
Binary file (140 kB)
|
|
|
app/__pycache__/dependencies.cpython-310.pyc
CHANGED
|
Binary files a/app/__pycache__/dependencies.cpython-310.pyc and b/app/__pycache__/dependencies.cpython-310.pyc differ
|
|
|
app/__pycache__/main.cpython-310.pyc
CHANGED
|
Binary files a/app/__pycache__/main.cpython-310.pyc and b/app/__pycache__/main.cpython-310.pyc differ
|
|
|
app/api/v1/endpoints/__pycache__/auth.cpython-310.pyc
CHANGED
|
Binary files a/app/api/v1/endpoints/__pycache__/auth.cpython-310.pyc and b/app/api/v1/endpoints/__pycache__/auth.cpython-310.pyc differ
|
|
|
app/api/v1/endpoints/__pycache__/ocrtemplate.cpython-310.pyc
CHANGED
|
Binary files a/app/api/v1/endpoints/__pycache__/ocrtemplate.cpython-310.pyc and b/app/api/v1/endpoints/__pycache__/ocrtemplate.cpython-310.pyc differ
|
|
|
app/api/v1/endpoints/__pycache__/user.cpython-310.pyc
CHANGED
|
Binary files a/app/api/v1/endpoints/__pycache__/user.cpython-310.pyc and b/app/api/v1/endpoints/__pycache__/user.cpython-310.pyc differ
|
|
|
app/api/v1/endpoints/auth.py
CHANGED
|
@@ -15,11 +15,11 @@ async def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends(
|
|
| 15 |
if not user:
|
| 16 |
raise HTTPException(
|
| 17 |
status_code=status.HTTP_401_UNAUTHORIZED,
|
| 18 |
-
detail="Incorrect
|
| 19 |
headers={"WWW-Authenticate": "Bearer"},
|
| 20 |
)
|
| 21 |
access_token_expires = timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES)
|
| 22 |
access_token = create_access_token(
|
| 23 |
-
data={"sub": user["
|
| 24 |
)
|
| 25 |
return {"access_token": access_token, "token_type": "bearer"}
|
|
|
|
| 15 |
if not user:
|
| 16 |
raise HTTPException(
|
| 17 |
status_code=status.HTTP_401_UNAUTHORIZED,
|
| 18 |
+
detail="Incorrect email or password",
|
| 19 |
headers={"WWW-Authenticate": "Bearer"},
|
| 20 |
)
|
| 21 |
access_token_expires = timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES)
|
| 22 |
access_token = create_access_token(
|
| 23 |
+
data={"sub": user["email"]}, expires_delta=access_token_expires
|
| 24 |
)
|
| 25 |
return {"access_token": access_token, "token_type": "bearer"}
|
app/api/v1/endpoints/ocrtemplate.py
CHANGED
|
@@ -9,6 +9,10 @@ router = APIRouter()
|
|
| 9 |
|
| 10 |
# In-memory storage for simplicity; replace with your database logic
|
| 11 |
templates = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
@router.post("/", response_model=OCRTemplate)
|
| 14 |
async def create_OCR_template(template: OCRTemplate, current_user: User = Depends(get_current_user)):
|
|
@@ -16,6 +20,12 @@ async def create_OCR_template(template: OCRTemplate, current_user: User = Depend
|
|
| 16 |
template = await create_template(template, current_user['user_id'])
|
| 17 |
return template
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
@router.get("/", response_model=List[Union[OCRTemplateInDB, dict]])
|
| 20 |
async def get_OCR_templates_for_user(template_name:Optional[bool] = False, current_user: User = Depends(get_current_user)):
|
| 21 |
|
|
@@ -30,6 +40,8 @@ async def get_template(template_name: str, current_user: dict = Depends(get_curr
|
|
| 30 |
raise HTTPException(status_code=404, detail="Template not found")
|
| 31 |
return template
|
| 32 |
|
|
|
|
|
|
|
| 33 |
@router.put("/templates", response_model=OCRTemplate)
|
| 34 |
async def update_template_endpoint(template: OCRTemplate, user: str = Depends(get_current_user)):
|
| 35 |
print("Updating template:", template)
|
|
|
|
| 9 |
|
| 10 |
# In-memory storage for simplicity; replace with your database logic
|
| 11 |
templates = {}
|
| 12 |
+
@router.get("/all", response_model=List[OCRTemplateInDB])
|
| 13 |
+
async def get_all_templates_endpoint():
|
| 14 |
+
templates = await get_all_templates()
|
| 15 |
+
return templates
|
| 16 |
|
| 17 |
@router.post("/", response_model=OCRTemplate)
|
| 18 |
async def create_OCR_template(template: OCRTemplate, current_user: User = Depends(get_current_user)):
|
|
|
|
| 20 |
template = await create_template(template, current_user['user_id'])
|
| 21 |
return template
|
| 22 |
|
| 23 |
+
@router.post("/templateForAll", response_model=OCRTemplate)
|
| 24 |
+
async def create_OCR_template(template: OCRTemplate, current_user: User = Depends(get_current_user)):
|
| 25 |
+
|
| 26 |
+
template = await create_template(template, current_user['user_id'], templateForAll=True)
|
| 27 |
+
return template
|
| 28 |
+
|
| 29 |
@router.get("/", response_model=List[Union[OCRTemplateInDB, dict]])
|
| 30 |
async def get_OCR_templates_for_user(template_name:Optional[bool] = False, current_user: User = Depends(get_current_user)):
|
| 31 |
|
|
|
|
| 40 |
raise HTTPException(status_code=404, detail="Template not found")
|
| 41 |
return template
|
| 42 |
|
| 43 |
+
|
| 44 |
+
|
| 45 |
@router.put("/templates", response_model=OCRTemplate)
|
| 46 |
async def update_template_endpoint(template: OCRTemplate, user: str = Depends(get_current_user)):
|
| 47 |
print("Updating template:", template)
|
app/api/v1/endpoints/user.py
CHANGED
|
@@ -1,24 +1,41 @@
|
|
| 1 |
# app/api/v1/endpoints/user.py
|
| 2 |
-
from fastapi import APIRouter, Depends, HTTPException
|
| 3 |
from app.models import users
|
| 4 |
from app.crud.users import *
|
| 5 |
from app.core.security import get_password_hash
|
| 6 |
from app.dependencies import get_current_user
|
|
|
|
| 7 |
|
| 8 |
router = APIRouter()
|
| 9 |
|
| 10 |
@router.post("/")
|
| 11 |
-
async def register_user(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
db_user = await get_user_by_username(user.username)
|
| 14 |
-
if db_user:
|
| 15 |
-
|
| 16 |
|
| 17 |
-
db_user = await get_user_by_email(
|
| 18 |
if db_user:
|
| 19 |
-
raise HTTPException(status_code=400, detail="Email already exists")
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
user_in_db = UserInDB(**user.dict(), hashed_password=get_password_hash(user.password))
|
|
|
|
| 22 |
|
| 23 |
user = await create_user(user_in_db)
|
| 24 |
|
|
|
|
| 1 |
# app/api/v1/endpoints/user.py
|
| 2 |
+
from fastapi import APIRouter, Depends, HTTPException, Form
|
| 3 |
from app.models import users
|
| 4 |
from app.crud.users import *
|
| 5 |
from app.core.security import get_password_hash
|
| 6 |
from app.dependencies import get_current_user
|
| 7 |
+
from pydantic import EmailStr
|
| 8 |
|
| 9 |
router = APIRouter()
|
| 10 |
|
| 11 |
@router.post("/")
|
| 12 |
+
async def register_user( first_name: str = Form(...),
|
| 13 |
+
surname: str = Form(...),
|
| 14 |
+
email: EmailStr = Form(...),
|
| 15 |
+
phone: str = Form(...),
|
| 16 |
+
country: str = Form(...),
|
| 17 |
+
address: str = Form(...),
|
| 18 |
+
password: str = Form(...)):
|
| 19 |
|
| 20 |
+
# db_user = await get_user_by_username(user.username)
|
| 21 |
+
# if db_user:
|
| 22 |
+
# raise HTTPException(status_code=400, detail="Username already exists")
|
| 23 |
|
| 24 |
+
db_user = await get_user_by_email(email)
|
| 25 |
if db_user:
|
| 26 |
+
raise HTTPException(status_code=400, detail="Email already exists! Please try another one.")
|
| 27 |
|
| 28 |
+
user = User(
|
| 29 |
+
first_name=first_name,
|
| 30 |
+
surname=surname,
|
| 31 |
+
phone=phone,
|
| 32 |
+
country=country,
|
| 33 |
+
address=address,
|
| 34 |
+
email=email,
|
| 35 |
+
password=password
|
| 36 |
+
)
|
| 37 |
user_in_db = UserInDB(**user.dict(), hashed_password=get_password_hash(user.password))
|
| 38 |
+
print("User in db:", user_in_db)
|
| 39 |
|
| 40 |
user = await create_user(user_in_db)
|
| 41 |
|
app/core/__pycache__/middleware.cpython-310.pyc
ADDED
|
Binary file (1.33 kB). View file
|
|
|
app/crud/__pycache__/ocrtemplate.cpython-310.pyc
CHANGED
|
Binary files a/app/crud/__pycache__/ocrtemplate.cpython-310.pyc and b/app/crud/__pycache__/ocrtemplate.cpython-310.pyc differ
|
|
|
app/crud/__pycache__/users.cpython-310.pyc
CHANGED
|
Binary files a/app/crud/__pycache__/users.cpython-310.pyc and b/app/crud/__pycache__/users.cpython-310.pyc differ
|
|
|
app/crud/ocrtemplate.py
CHANGED
|
@@ -9,23 +9,38 @@ from pymongo import ReturnDocument
|
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
-
async def create_template(template: OCRTemplate, user_id: str):
|
| 13 |
existing_template = await get_template_by_name_and_user(template.template_name, user_id)
|
| 14 |
if existing_template:
|
| 15 |
raise HTTPException(status_code=401, detail="Template already exists")
|
| 16 |
|
| 17 |
template_dict = template.dict()
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
| 19 |
db = get_database(settings.MongoDB_NAME)
|
| 20 |
template = await db["templates"].insert_one(template_dict)
|
| 21 |
if template:
|
| 22 |
return OCRTemplate(**template_dict)
|
| 23 |
return None
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
async def get_all_templates_by_user_id(user_id: str, template_name: Optional[bool] = False)-> Union[List[OCRTemplateInDB], List[dict]]:
|
| 27 |
db = get_database(settings.MongoDB_NAME)
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
if template_name:
|
| 31 |
# Return only the template_name field
|
|
|
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
+
async def create_template(template: OCRTemplate, user_id: str, templateForAll: Optional[bool] = False):
|
| 13 |
existing_template = await get_template_by_name_and_user(template.template_name, user_id)
|
| 14 |
if existing_template:
|
| 15 |
raise HTTPException(status_code=401, detail="Template already exists")
|
| 16 |
|
| 17 |
template_dict = template.dict()
|
| 18 |
+
if templateForAll:
|
| 19 |
+
template_dict["user_id"] = "all"
|
| 20 |
+
else:
|
| 21 |
+
template_dict["user_id"] = user_id
|
| 22 |
db = get_database(settings.MongoDB_NAME)
|
| 23 |
template = await db["templates"].insert_one(template_dict)
|
| 24 |
if template:
|
| 25 |
return OCRTemplate(**template_dict)
|
| 26 |
return None
|
| 27 |
|
| 28 |
+
async def get_all_templates():
|
| 29 |
+
db = get_database(settings.MongoDB_NAME)
|
| 30 |
+
templates = await db["templates"].find().to_list(1000)
|
| 31 |
+
return [OCRTemplateInDB(**template) for template in templates]
|
| 32 |
|
| 33 |
async def get_all_templates_by_user_id(user_id: str, template_name: Optional[bool] = False)-> Union[List[OCRTemplateInDB], List[dict]]:
|
| 34 |
db = get_database(settings.MongoDB_NAME)
|
| 35 |
+
|
| 36 |
+
# Query to find templates with user_id equal to the given user_id or "all"
|
| 37 |
+
templates = await db["templates"].find({
|
| 38 |
+
"$or": [
|
| 39 |
+
{"user_id": user_id},
|
| 40 |
+
{"user_id": "all"}
|
| 41 |
+
]
|
| 42 |
+
}).to_list(1000)
|
| 43 |
+
# templates = await db["templates"].find({"user_id": user_id}).to_list(1000)
|
| 44 |
|
| 45 |
if template_name:
|
| 46 |
# Return only the template_name field
|
app/crud/users.py
CHANGED
|
@@ -29,7 +29,7 @@ async def get_user_by_email(email: str):
|
|
| 29 |
return user
|
| 30 |
|
| 31 |
async def authenticate_user(username: str, password: str):
|
| 32 |
-
user = await
|
| 33 |
print("User:", user)
|
| 34 |
if user and verify_password(password, user["password"]):
|
| 35 |
return user
|
|
|
|
| 29 |
return user
|
| 30 |
|
| 31 |
async def authenticate_user(username: str, password: str):
|
| 32 |
+
user = await get_user_by_email(username)
|
| 33 |
print("User:", user)
|
| 34 |
if user and verify_password(password, user["password"]):
|
| 35 |
return user
|
app/dependencies.py
CHANGED
|
@@ -3,7 +3,7 @@ from fastapi import Depends, HTTPException, status
|
|
| 3 |
from fastapi.security import OAuth2PasswordBearer
|
| 4 |
from jose import JWTError, jwt
|
| 5 |
from app.models.token import TokenData
|
| 6 |
-
from app.crud.users import get_user_by_username
|
| 7 |
from app.core.config import settings
|
| 8 |
|
| 9 |
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
|
|
@@ -16,13 +16,13 @@ async def get_current_user(token: str = Depends(oauth2_scheme)):
|
|
| 16 |
)
|
| 17 |
try:
|
| 18 |
payload = jwt.decode(token, settings.SECRET_KEY, algorithms=[settings.ALGORITHM])
|
| 19 |
-
|
| 20 |
-
if
|
| 21 |
raise credentials_exception
|
| 22 |
-
token_data = TokenData(
|
| 23 |
except JWTError:
|
| 24 |
raise credentials_exception
|
| 25 |
-
user = await
|
| 26 |
if user is None:
|
| 27 |
raise credentials_exception
|
| 28 |
return user
|
|
|
|
| 3 |
from fastapi.security import OAuth2PasswordBearer
|
| 4 |
from jose import JWTError, jwt
|
| 5 |
from app.models.token import TokenData
|
| 6 |
+
from app.crud.users import get_user_by_username, get_user_by_email
|
| 7 |
from app.core.config import settings
|
| 8 |
|
| 9 |
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
|
|
|
|
| 16 |
)
|
| 17 |
try:
|
| 18 |
payload = jwt.decode(token, settings.SECRET_KEY, algorithms=[settings.ALGORITHM])
|
| 19 |
+
email: str = payload.get("sub")
|
| 20 |
+
if email is None:
|
| 21 |
raise credentials_exception
|
| 22 |
+
token_data = TokenData(email=email)
|
| 23 |
except JWTError:
|
| 24 |
raise credentials_exception
|
| 25 |
+
user = await get_user_by_email(email)
|
| 26 |
if user is None:
|
| 27 |
raise credentials_exception
|
| 28 |
return user
|
app/main.py
CHANGED
|
@@ -55,7 +55,7 @@ app = FastAPI(lifespan=lifespan)
|
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
-
app.add_middleware(BasicAuthMiddleware, username=os.getenv("fastapiusername"), password=os.getenv("fastapipassword"))
|
| 59 |
# Allow CORS for specific origin with credentials
|
| 60 |
origins = [
|
| 61 |
os.getenv("client")
|
|
|
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
+
# app.add_middleware(BasicAuthMiddleware, username=os.getenv("fastapiusername"), password=os.getenv("fastapipassword"))
|
| 59 |
# Allow CORS for specific origin with credentials
|
| 60 |
origins = [
|
| 61 |
os.getenv("client")
|
app/models/__pycache__/token.cpython-310.pyc
CHANGED
|
Binary files a/app/models/__pycache__/token.cpython-310.pyc and b/app/models/__pycache__/token.cpython-310.pyc differ
|
|
|
app/models/__pycache__/users.cpython-310.pyc
CHANGED
|
Binary files a/app/models/__pycache__/users.cpython-310.pyc and b/app/models/__pycache__/users.cpython-310.pyc differ
|
|
|
app/models/token.py
CHANGED
|
@@ -8,3 +8,4 @@ class Token(BaseModel):
|
|
| 8 |
|
| 9 |
class TokenData(BaseModel):
|
| 10 |
username: Optional[str] = None
|
|
|
|
|
|
| 8 |
|
| 9 |
class TokenData(BaseModel):
|
| 10 |
username: Optional[str] = None
|
| 11 |
+
email: Optional[str] = None
|
app/models/users.py
CHANGED
|
@@ -5,8 +5,13 @@ from datetime import timedelta, datetime
|
|
| 5 |
import uuid
|
| 6 |
|
| 7 |
class User(BaseModel):
|
| 8 |
-
user_id: str = Field(default_factory=uuid.uuid4())
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
email: EmailStr
|
| 11 |
password: str
|
| 12 |
|
|
@@ -14,3 +19,4 @@ class UserInDB(User):
|
|
| 14 |
is_validated: bool = Field(default=False)
|
| 15 |
created_at: datetime = Field(default_factory=datetime.utcnow)
|
| 16 |
updated_at: Optional[str] = None
|
|
|
|
|
|
| 5 |
import uuid
|
| 6 |
|
| 7 |
class User(BaseModel):
|
| 8 |
+
user_id: str = Field(default_factory=lambda: str(uuid.uuid4()))
|
| 9 |
+
first_name: str
|
| 10 |
+
surname: str
|
| 11 |
+
phone: str
|
| 12 |
+
country: str
|
| 13 |
+
address: str
|
| 14 |
+
# username: str
|
| 15 |
email: EmailStr
|
| 16 |
password: str
|
| 17 |
|
|
|
|
| 19 |
is_validated: bool = Field(default=False)
|
| 20 |
created_at: datetime = Field(default_factory=datetime.utcnow)
|
| 21 |
updated_at: Optional[str] = None
|
| 22 |
+
|