Skip to content

Commit

Permalink
black format
Browse files Browse the repository at this point in the history
  • Loading branch information
jiisanda committed Dec 22, 2024
1 parent 9a82abd commit 7426d07
Show file tree
Hide file tree
Showing 28 changed files with 643 additions and 582 deletions.
39 changes: 27 additions & 12 deletions app/api/dependencies/auth_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# Password Hashing
password_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
# oauth2 scheme
oauth2_scheme = OAuth2PasswordBearer(tokenUrl='api/u/login', scheme_name="JWT")
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="api/u/login", scheme_name="JWT")


def get_hashed_password(password: str) -> str:
Expand All @@ -25,31 +25,49 @@ def verify_password(password: str, hashed_password: str) -> bool:
return password_context.verify(password, hashed_password)


def create_access_token(subject: Dict[str, Any], expires_delta: timedelta = None) -> str:
def create_access_token(
subject: Dict[str, Any], expires_delta: timedelta = None
) -> str:
if expires_delta is not None:
expires_delta = datetime.utcnow() + expires_delta
else:
expires_delta = datetime.utcnow() + timedelta(minutes=settings.access_token_expire_min)
expires_delta = datetime.utcnow() + timedelta(
minutes=settings.access_token_expire_min
)

to_encode = {"exp": expires_delta, "id": subject.get("id"), "username": subject.get("username")}
to_encode = {
"exp": expires_delta,
"id": subject.get("id"),
"username": subject.get("username"),
}

return jwt.encode(to_encode, settings.jwt_secret_key, settings.algorithm)


def create_refresh_token(subject: Dict[str, Any], expires_delta: timedelta = None) -> str:
def create_refresh_token(
subject: Dict[str, Any], expires_delta: timedelta = None
) -> str:
if expires_delta is not None:
expires_delta = datetime.utcnow() + expires_delta
else:
expires_delta = datetime.utcnow() + timedelta(minutes=settings.refresh_token_expire_min)
expires_delta = datetime.utcnow() + timedelta(
minutes=settings.refresh_token_expire_min
)

to_encode = {"exp": expires_delta, "id": subject.get("id"), "username": subject.get("username")}
to_encode = {
"exp": expires_delta,
"id": subject.get("id"),
"username": subject.get("username"),
}

return jwt.encode(to_encode, settings.jwt_secret_key, settings.algorithm)


def verify_access_token(token: str, credentials_exception):
try:
payload = jwt.decode(token, settings.jwt_secret_key, algorithms=[settings.algorithm])
payload = jwt.decode(
token, settings.jwt_secret_key, algorithms=[settings.algorithm]
)
uid = payload.get("id")
username = payload.get("username")
if username is None:
Expand All @@ -63,10 +81,7 @@ def verify_access_token(token: str, credentials_exception):

def get_current_user(token: str = Depends(oauth2_scheme)):
credentials_exception = http_401(
msg="Could not validate credentials",
headers={
"WWW-Authenticate": "Bearer"
}
msg="Could not validate credentials", headers={"WWW-Authenticate": "Bearer"}
)

return verify_access_token(token=token, credentials_exception=credentials_exception)
52 changes: 26 additions & 26 deletions app/api/dependencies/constants.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
SUPPORTED_FILE_TYPES = {
'image/jpeg': 'jpg',
'image/png': 'png',
'image/gif': 'gif',
'image/bmp': 'bmp',
'image/tiff': 'tiff',
'application/pdf': 'pdf',
'text/plain': 'txt',
'application/msword': 'doc',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document': 'docx',
'application/vnd.ms-excel': 'xls',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': 'xlsx',
'application/vnd.ms-powerpoint': 'ppt',
'application/vnd.openxmlformats-officedocument.presentationml.presentation': 'pptx',
'application/zip': 'zip',
'application/x-gzip': 'gzip',
'application/x-tar': 'tar',
'application/x-bzip2': 'bz2',
'application/x-7z-compressed': '7z',
'application/xml': 'xml',
'application/json': 'json',
'video/mp4': 'mp4',
'video/mpeg': 'mpeg',
'video/quicktime': 'mov',
'audio/mpeg': 'mp3',
'audio/wav': 'wav',
'audio/x-ms-wma': 'wma',
"image/jpeg": "jpg",
"image/png": "png",
"image/gif": "gif",
"image/bmp": "bmp",
"image/tiff": "tiff",
"application/pdf": "pdf",
"text/plain": "txt",
"application/msword": "doc",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document": "docx",
"application/vnd.ms-excel": "xls",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": "xlsx",
"application/vnd.ms-powerpoint": "ppt",
"application/vnd.openxmlformats-officedocument.presentationml.presentation": "pptx",
"application/zip": "zip",
"application/x-gzip": "gzip",
"application/x-tar": "tar",
"application/x-bzip2": "bz2",
"application/x-7z-compressed": "7z",
"application/xml": "xml",
"application/json": "json",
"video/mp4": "mp4",
"video/mpeg": "mpeg",
"video/quicktime": "mov",
"audio/mpeg": "mp3",
"audio/wav": "wav",
"audio/x-ms-wma": "wma",
}
14 changes: 7 additions & 7 deletions app/api/dependencies/mail_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
from app.core.exceptions import http_500


def mail_service(mail_to: str, subject: str, content: str, file_path: str = None) -> None:
def mail_service(
mail_to: str, subject: str, content: str, file_path: str = None
) -> None:
port = settings.smtp_port # For starttls
smtp_server = settings.smtp_server
sender_email = settings.email
Expand All @@ -20,8 +22,8 @@ def mail_service(mail_to: str, subject: str, content: str, file_path: str = None

# Creating Multipart message and headers
message = MIMEMultipart()
message['Subject'] = subject
message.attach(MIMEText(content, _subtype='plain'))
message["Subject"] = subject
message.attach(MIMEText(content, _subtype="plain"))

# Open file in binary mode
if file_path is not None:
Expand All @@ -36,7 +38,7 @@ def mail_service(mail_to: str, subject: str, content: str, file_path: str = None
# header as attachment
part.add_header(
"Content-Disposition",
f"attachment; filename= {os.path.basename(file_path)}"
f"attachment; filename= {os.path.basename(file_path)}",
)

message.attach(part)
Expand All @@ -50,6 +52,4 @@ def mail_service(mail_to: str, subject: str, content: str, file_path: str = None
server.login(sender_email, password)
server.sendmail(sender_email, receiver_email, message.as_string())
except Exception as e:
raise http_500(
msg="There was some error sending email..."
) from e
raise http_500(msg="There was some error sending email...") from e
8 changes: 6 additions & 2 deletions app/api/router.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
from fastapi import APIRouter

from app.api.routes.auth.auth import router as auth_router
from app.api.routes.documents.documents_metadata import router as documents_metadata_router
from app.api.routes.documents.documents_metadata import (
router as documents_metadata_router,
)
from app.api.routes.documents.document import router as documents_router
from app.api.routes.documents.document_organization import router as document_organization_router
from app.api.routes.documents.document_organization import (
router as document_organization_router,
)
from app.api.routes.documents.document_sharing import router as document_sharing_router
from app.api.routes.documents.notify import router as notify_router

Expand Down
11 changes: 5 additions & 6 deletions app/api/routes/auth/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@
response_model=UserOut,
status_code=status.HTTP_201_CREATED,
name="signup",
summary="Create new user"
summary="Create new user",
)
async def signup(
data: UserAuth,
repository: AuthRepository = Depends(get_repository(AuthRepository))
data: UserAuth, repository: AuthRepository = Depends(get_repository(AuthRepository))
):

return await repository.signup(userdata=data)
Expand All @@ -28,11 +27,11 @@ async def signup(
"/login",
status_code=status.HTTP_200_OK,
name="login",
summary="Create access and refresh tokens for user"
summary="Create access and refresh tokens for user",
)
async def login(
form_data: OAuth2PasswordRequestForm = Depends(),
repository: AuthRepository = Depends(get_repository(AuthRepository))
repository: AuthRepository = Depends(get_repository(AuthRepository)),
):

return await repository.login(ipdata=form_data)
Expand All @@ -43,7 +42,7 @@ async def login(
status_code=status.HTTP_200_OK,
response_model=TokenData,
name="get_user_data",
summary="Get details of currently logged in user"
summary="Get details of currently logged in user",
)
async def get_me(user: TokenData = Depends(get_current_user)):

Expand Down
Loading

0 comments on commit 7426d07

Please sign in to comment.