Skip to content

Commit

Permalink
refactor(console): format code
Browse files Browse the repository at this point in the history
  • Loading branch information
bouassaba committed Dec 14, 2024
1 parent 5d2de49 commit 427f102
Show file tree
Hide file tree
Showing 27 changed files with 18 additions and 141 deletions.
12 changes: 1 addition & 11 deletions console/api/database/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@
# AGPL-3.0-only in the root of this repository.

from typing import Tuple, Iterable, Dict

from psycopg import DatabaseError

from ..dependencies import conn, parse_sql_update_query
from ..dependencies import conn
from ..errors import EmptyDataException, NotFoundException
from .generic import exists

Expand All @@ -22,7 +20,6 @@ def fetch_group(_id: str) -> Dict:
with conn.cursor() as curs:
if not exists(curs=curs, tablename="group", _id=_id):
raise NotFoundException(message=f"Group with id={_id} does not exist!")

data = curs.execute(
f"""
SELECT g.id as "group_id", g."name" as "group_name", g.create_time, g.update_time, o.id as "org_id",
Expand All @@ -32,7 +29,6 @@ def fetch_group(_id: str) -> Dict:
WHERE g.id='{_id}'
"""
).fetchone()

return (
{
"createTime": data.get("create_time"),
Expand All @@ -48,7 +44,6 @@ def fetch_group(_id: str) -> Dict:
if data is not None
else None
)

except DatabaseError as error:
raise error

Expand All @@ -57,7 +52,6 @@ def fetch_group_count() -> Dict:
try:
with conn.cursor() as curs:
return curs.execute('SELECT count(id) FROM "group"').fetchone()

except DatabaseError as error:
raise error

Expand All @@ -77,12 +71,9 @@ def fetch_groups(page=1, size=10) -> Tuple[Iterable[Dict], int]:
LIMIT {size}
"""
).fetchall()

if data is None or data == {}:
raise EmptyDataException

count = curs.execute('SELECT count(1) FROM "group"').fetchone()

return (
{
"id": d.get("group_id"),
Expand All @@ -98,6 +89,5 @@ def fetch_groups(page=1, size=10) -> Tuple[Iterable[Dict], int]:
}
for d in data
), count["count"]

except DatabaseError as error:
raise error
21 changes: 1 addition & 20 deletions console/api/database/organization.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@
# AGPL-3.0-only in the root of this repository.

from typing import Dict, Tuple, Iterable

from psycopg import DatabaseError

from . import exists
from ..dependencies import conn, parse_sql_update_query
from ..dependencies import conn
from ..errors import EmptyDataException, NotFoundException


Expand All @@ -24,15 +22,13 @@ def fetch_organization(organization_id: str) -> Dict:
raise NotFoundException(
message=f"Organization with id={organization_id} does not exist!"
)

return curs.execute(
f"""
SELECT id, name, create_time as "createTime", update_time as "updateTime"
FROM organization
WHERE id='{organization_id}'
"""
).fetchone()

except DatabaseError as error:
raise error

Expand All @@ -49,14 +45,10 @@ def fetch_organizations(page=1, size=10) -> Tuple[Iterable[Dict], int]:
LIMIT {size}
"""
).fetchall()

if data is None or data == {}:
raise EmptyDataException

count = curs.execute('SELECT count(1) FROM "organization"').fetchone()

return data, count["count"]

except DatabaseError as error:
raise error

Expand All @@ -65,7 +57,6 @@ def fetch_organization_count() -> Dict:
try:
with conn.cursor() as curs:
return curs.execute('SELECT count(id) FROM "organization"').fetchone()

except DatabaseError as error:
raise error

Expand All @@ -87,10 +78,8 @@ def fetch_organization_users(
LIMIT {size}
"""
).fetchall()

if data is None or data == {}:
raise EmptyDataException

count = curs.execute(
f"""
SELECT count(u.id)
Expand All @@ -100,9 +89,7 @@ def fetch_organization_users(
WHERE o.id = '{organization_id}'
"""
).fetchone()

return data, count["count"]

except DatabaseError as error:
raise error

Expand All @@ -122,10 +109,8 @@ def fetch_organization_workspaces(
LIMIT {size}
"""
).fetchall()

if data is None or data == {}:
raise EmptyDataException

count = curs.execute(
f"""
SELECT count(u.id)
Expand All @@ -134,7 +119,6 @@ def fetch_organization_workspaces(
WHERE w.organization_id = '{organization_id}'
"""
).fetchone()

return data, count["count"]
except DatabaseError as error:
raise error
Expand All @@ -155,18 +139,15 @@ def fetch_organization_groups(
LIMIT {size}
"""
).fetchall()

if data is None or data == {}:
raise EmptyDataException

count = curs.execute(
f"""
SELECT count(id)
FROM "group"
WHERE organization_id = '{organization_id}'
"""
).fetchone()

return data, count["count"]
except DatabaseError as error:
raise error
1 change: 0 additions & 1 deletion console/api/database/overview.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
# AGPL-3.0-only in the root of this repository.

from psycopg import DatabaseError

from ..dependencies import conn


Expand Down
14 changes: 0 additions & 14 deletions console/api/database/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
# AGPL-3.0-only in the root of this repository.

from typing import Tuple, Iterable, Dict

from psycopg import DatabaseError

from . import exists
from ..dependencies import conn
from ..errors import EmptyDataException, NotFoundException
Expand All @@ -26,7 +24,6 @@ def fetch_user_organizations(
raise NotFoundException(
message=f"User with id={user_id} does not exist!"
)

data = curs.execute(
f"""
SELECT u.id, u."permission", u.create_time as "createTime", o.id as "organizationId",
Expand All @@ -39,10 +36,8 @@ def fetch_user_organizations(
LIMIT {size}
"""
).fetchall()

if data is None or data == {}:
raise EmptyDataException

count = curs.execute(
f"""
SELECT count(1)
Expand All @@ -51,7 +46,6 @@ def fetch_user_organizations(
WHERE u.user_id = '{user_id}'
"""
).fetchone()

return data, count["count"]
except DatabaseError as error:
raise error
Expand All @@ -61,7 +55,6 @@ def fetch_user_count() -> Dict:
try:
with conn.cursor() as curs:
return curs.execute('SELECT count(id) FROM "user"').fetchone()

except DatabaseError as error:
raise error

Expand All @@ -73,7 +66,6 @@ def fetch_user_workspaces(user_id: str, page=1, size=10) -> Tuple[Iterable[Dict]
raise NotFoundException(
message=f"User with id={user_id} does not exist!"
)

data = curs.execute(
f"""
SELECT u.id, u.permission, u.create_time as "createTime", w.id as "workspaceId",
Expand All @@ -86,10 +78,8 @@ def fetch_user_workspaces(user_id: str, page=1, size=10) -> Tuple[Iterable[Dict]
LIMIT {size}
"""
).fetchall()

if data is None or data == {}:
raise EmptyDataException

count = curs.execute(
f"""
SELECT count(1)
Expand All @@ -111,7 +101,6 @@ def fetch_user_groups(user_id: str, page=1, size=10) -> Tuple[Iterable[Dict], in
raise NotFoundException(
message=f"User with id={user_id} does not exist!"
)

data = curs.execute(
f"""
SELECT u.id, u.permission, u.create_time as "createTime", g.id as "groupId", g."name" as "groupName"
Expand All @@ -122,10 +111,8 @@ def fetch_user_groups(user_id: str, page=1, size=10) -> Tuple[Iterable[Dict], in
LIMIT {size}
"""
).fetchall()

if data is None or data == {}:
raise EmptyDataException

count = curs.execute(
f"""
SELECT count(1)
Expand All @@ -134,7 +121,6 @@ def fetch_user_groups(user_id: str, page=1, size=10) -> Tuple[Iterable[Dict], in
WHERE u.user_id = '{user_id}'
"""
).fetchone()

return data, count["count"]
except DatabaseError as error:
raise error
1 change: 0 additions & 1 deletion console/api/database/userpermission.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
# AGPL-3.0-only in the root of this repository.

from psycopg import DatabaseError

from api.dependencies import conn, new_id, new_timestamp


Expand Down
11 changes: 1 addition & 10 deletions console/api/database/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@
# AGPL-3.0-only in the root of this repository.

from typing import Dict, Tuple, Iterable

from psycopg import DatabaseError

from . import exists
from ..dependencies import conn, parse_sql_update_query
from ..dependencies import conn
from ..errors import EmptyDataException, NotFoundException


Expand All @@ -24,7 +22,6 @@ def fetch_workspace(_id: str) -> Dict:
raise NotFoundException(
message=f"Workspace with id={_id} does not exist!"
)

data = curs.execute(
f"""
SELECT w.id, w.name, w.organization_id, o.name as "organizationName",
Expand All @@ -35,7 +32,6 @@ def fetch_workspace(_id: str) -> Dict:
WHERE w.id = '{_id}'
"""
).fetchone()

return {
"id": data.get("id"),
"createTime": data.get("create_time"),
Expand All @@ -59,7 +55,6 @@ def fetch_workspace_count() -> Dict:
try:
with conn.cursor() as curs:
return curs.execute('SELECT count(id) FROM "workspace"').fetchone()

except DatabaseError as error:
raise error

Expand All @@ -79,12 +74,9 @@ def fetch_workspaces(page=1, size=10) -> Tuple[Iterable[Dict], int]:
LIMIT {size}
"""
).fetchall()

if data is None or data == {}:
raise EmptyDataException

count = curs.execute("SELECT count(1) FROM workspace").fetchone()

return (
{
"id": d.get("id"),
Expand All @@ -103,6 +95,5 @@ def fetch_workspaces(page=1, size=10) -> Tuple[Iterable[Dict], int]:
}
for d in data
), count["count"]

except DatabaseError as error:
raise error
1 change: 0 additions & 1 deletion console/api/dependencies/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

from psycopg import connect
from psycopg.rows import dict_row

from . import settings

conn = connect(
Expand Down
5 changes: 0 additions & 5 deletions console/api/dependencies/jwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import jwt
from fastapi import Request
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials

from . import settings
from ..errors import GenericForbiddenException

Expand All @@ -28,7 +27,6 @@ async def __call__(self, request: Request):
if credentials:
if not credentials.scheme == "Bearer":
raise GenericForbiddenException(detail="Invalid authentication scheme.")

try:
decoded_token = jwt.decode(
jwt=credentials.credentials,
Expand All @@ -38,13 +36,10 @@ async def __call__(self, request: Request):
issuer=settings.URL,
verify=True,
)

except Exception as e:
raise GenericForbiddenException(detail=str(e)) from e

if not decoded_token["is_admin"]:
raise GenericForbiddenException(detail="User is not admin.")

return credentials.credentials
else:
raise GenericForbiddenException(detail="Invalid token.")
1 change: 0 additions & 1 deletion console/api/dependencies/meilisearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
# AGPL-3.0-only in the root of this repository.

from meilisearch import Client

from . import settings

meilisearch_client = Client(settings.SEARCH_URL)
Loading

0 comments on commit 427f102

Please sign in to comment.