Further cleanup

This commit is contained in:
Mawoka
2025-11-01 15:57:10 +01:00
parent abd2155469
commit 02c917a28f
16 changed files with 215 additions and 336 deletions
+48 -68
View File
@@ -4,7 +4,6 @@
import uuid
from typing import Optional
import asyncpg
import authlib.integrations.base_client
@@ -35,43 +34,43 @@ class Plan(BaseModel):
class GitHubOauthResponse(BaseModel):
login: str
id: int
email: Optional[str] = None
node_id: Optional[str] = None
avatar_url: Optional[str] = None
gravatar_id: Optional[str] = None
url: Optional[str] = None
html_url: Optional[str] = None
followers_url: Optional[str] = None
following_url: Optional[str] = None
gists_url: Optional[str] = None
starred_url: Optional[str] = None
subscriptions_url: Optional[str] = None
organizations_url: Optional[str] = None
repos_url: Optional[str] = None
events_url: Optional[str] = None
received_events_url: Optional[str] = None
type: Optional[str] = None
site_admin: Optional[bool] = None
name: Optional[str] = None
company: Optional[str] = None
blog: Optional[str] = None
location: Optional[str] = None
hireable: Optional[bool] = None
bio: Optional[str] = None
twitter_username: Optional[str] = None
public_repos: Optional[int] = None
public_gists: Optional[int] = None
followers: Optional[int] = None
following: Optional[int] = None
email: str | None = None
node_id: str | None = None
avatar_url: str | None = None
gravatar_id: str | None = None
url: str | None = None
html_url: str | None = None
followers_url: str | None = None
following_url: str | None = None
gists_url: str | None = None
starred_url: str | None = None
subscriptions_url: str | None = None
organizations_url: str | None = None
repos_url: str | None = None
events_url: str | None = None
received_events_url: str | None = None
type: str | None = None
site_admin: bool | None = None
name: str | None = None
company: str | None = None
blog: str | None = None
location: str | None = None
hireable: bool | None = None
bio: str | None = None
twitter_username: str | None = None
public_repos: int | None = None
public_gists: int | None = None
followers: int | None = None
following: int | None = None
created_at: datetime
updated_at: datetime
private_gists: Optional[int] = None
total_private_repos: Optional[int] = None
owned_private_repos: Optional[int] = None
disk_usage: Optional[int] = None
collaborators: Optional[int] = None
two_factor_authentication: Optional[bool] = None
plan: Optional[Plan] = None
private_gists: int | None = None
total_private_repos: int | None = None
owned_private_repos: int | None = None
disk_usage: int | None = None
collaborators: int | None = None
two_factor_authentication: bool | None = None
plan: Plan | None = None
@router.get("/login")
@@ -107,39 +106,20 @@ async def auth(request: Request, response: Response):
user_data = GitHubOauthResponse(**data)
if user_data.email is None:
return RedirectResponse("/account/oauth-error?error=email")
user_in_db = await User.objects.get_or_none(email=user_data.email)
if user_in_db is None:
# REGISTER USER
try:
await User.objects.create(
id=uuid.uuid4(),
email=user_data.email,
username=user_data.login,
verified=True,
auth_type=UserAuthTypes.GITHUB,
avatar=gzipped_user_avatar(),
)
# skipcq: PYL-W0703
except Exception as e:
if type(e) is asyncpg.exceptions.UniqueViolationError:
error = True
counter = 1
while error:
try:
await User.objects.create(
id=uuid.uuid4(),
email=user_data.email,
username=user_data.login,
verified=True,
auth_type=UserAuthTypes.GITHUB,
avatar=gzipped_user_avatar(),
)
error = False
except asyncpg.exceptions.UniqueViolationError:
counter += 1
error = True
else:
raise HTTPException(status_code=500, detail=str(e))
try:
await User.objects.create(
id=uuid.uuid4(),
email=user_data.email,
username=user_data.login,
verified=True,
auth_type=UserAuthTypes.GITHUB,
avatar=gzipped_user_avatar(),
)
except asyncpg.exceptions.UniqueViolationError:
raise HTTPException(status_code=400, detail="User already exists.")
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
user = await User.objects.get_or_none(
email=user_data.email,
username=user_data.login,