refactor: clean up code by adding missing commas and improving import order
This commit is contained in:
@@ -2,17 +2,17 @@
|
||||
|
||||
import secrets
|
||||
from datetime import datetime
|
||||
from typing import Optional, TYPE_CHECKING
|
||||
from typing import TYPE_CHECKING, Optional
|
||||
|
||||
from werkzeug.security import check_password_hash, generate_password_hash
|
||||
from sqlalchemy import String, DateTime, Integer, ForeignKey
|
||||
from sqlalchemy import DateTime, ForeignKey, Integer, String
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
from werkzeug.security import check_password_hash, generate_password_hash
|
||||
|
||||
from app.database import db
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from app.models.user_oauth import UserOAuth
|
||||
from app.models.plan import Plan
|
||||
from app.models.user_oauth import UserOAuth
|
||||
|
||||
|
||||
class User(db.Model):
|
||||
@@ -25,16 +25,16 @@ class User(db.Model):
|
||||
# Primary user information (can be updated from any connected provider)
|
||||
email: Mapped[str] = mapped_column(String(255), nullable=False, unique=True)
|
||||
name: Mapped[str] = mapped_column(String(255), nullable=False)
|
||||
picture: Mapped[Optional[str]] = mapped_column(String(500), nullable=True)
|
||||
picture: Mapped[str | None] = mapped_column(String(500), nullable=True)
|
||||
|
||||
# Password authentication (optional - users can use OAuth instead)
|
||||
password_hash: Mapped[Optional[str]] = mapped_column(
|
||||
String(255), nullable=True
|
||||
password_hash: Mapped[str | None] = mapped_column(
|
||||
String(255), nullable=True,
|
||||
)
|
||||
|
||||
# Role-based access control
|
||||
role: Mapped[str] = mapped_column(
|
||||
String(50), nullable=False, default="user"
|
||||
String(50), nullable=False, default="user",
|
||||
)
|
||||
|
||||
# User status
|
||||
@@ -42,21 +42,21 @@ class User(db.Model):
|
||||
|
||||
# Plan relationship
|
||||
plan_id: Mapped[int] = mapped_column(
|
||||
Integer, ForeignKey("plans.id"), nullable=False
|
||||
Integer, ForeignKey("plans.id"), nullable=False,
|
||||
)
|
||||
|
||||
# User credits (populated from plan credits on creation)
|
||||
credits: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
|
||||
|
||||
# API token for programmatic access
|
||||
api_token: Mapped[Optional[str]] = mapped_column(String(255), nullable=True)
|
||||
api_token_expires_at: Mapped[Optional[datetime]] = mapped_column(
|
||||
DateTime, nullable=True
|
||||
api_token: Mapped[str | None] = mapped_column(String(255), nullable=True)
|
||||
api_token_expires_at: Mapped[datetime | None] = mapped_column(
|
||||
DateTime, nullable=True,
|
||||
)
|
||||
|
||||
# Timestamps
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime, default=datetime.utcnow, nullable=False
|
||||
DateTime, default=datetime.utcnow, nullable=False,
|
||||
)
|
||||
updated_at: Mapped[datetime] = mapped_column(
|
||||
DateTime,
|
||||
@@ -67,7 +67,7 @@ class User(db.Model):
|
||||
|
||||
# Relationships
|
||||
oauth_providers: Mapped[list["UserOAuth"]] = relationship(
|
||||
"UserOAuth", back_populates="user", cascade="all, delete-orphan"
|
||||
"UserOAuth", back_populates="user", cascade="all, delete-orphan",
|
||||
)
|
||||
plan: Mapped["Plan"] = relationship("Plan", back_populates="users")
|
||||
|
||||
@@ -190,15 +190,15 @@ class User(db.Model):
|
||||
provider_id: str,
|
||||
email: str,
|
||||
name: str,
|
||||
picture: Optional[str] = None,
|
||||
picture: str | None = None,
|
||||
) -> tuple["User", "UserOAuth"]:
|
||||
"""Find existing user or create new one from OAuth data."""
|
||||
from app.models.user_oauth import UserOAuth
|
||||
from app.models.plan import Plan
|
||||
from app.models.user_oauth import UserOAuth
|
||||
|
||||
# First, try to find existing OAuth provider
|
||||
oauth_provider = UserOAuth.find_by_provider_and_id(
|
||||
provider, provider_id
|
||||
provider, provider_id,
|
||||
)
|
||||
|
||||
if oauth_provider:
|
||||
@@ -211,7 +211,7 @@ class User(db.Model):
|
||||
|
||||
# Update user info with latest data
|
||||
user.update_from_provider(
|
||||
{"email": email, "name": name, "picture": picture}
|
||||
{"email": email, "name": name, "picture": picture},
|
||||
)
|
||||
else:
|
||||
# Try to find user by email to link the new provider
|
||||
@@ -256,7 +256,7 @@ class User(db.Model):
|
||||
|
||||
@classmethod
|
||||
def create_with_password(
|
||||
cls, email: str, password: str, name: str
|
||||
cls, email: str, password: str, name: str,
|
||||
) -> "User":
|
||||
"""Create new user with email and password."""
|
||||
from app.models.plan import Plan
|
||||
@@ -293,7 +293,7 @@ class User(db.Model):
|
||||
|
||||
@classmethod
|
||||
def authenticate_with_password(
|
||||
cls, email: str, password: str
|
||||
cls, email: str, password: str,
|
||||
) -> Optional["User"]:
|
||||
"""Authenticate user with email and password."""
|
||||
user = cls.find_by_email(email)
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
"""User OAuth model for storing user's connected providers."""
|
||||
|
||||
from datetime import datetime
|
||||
from typing import Optional, TYPE_CHECKING
|
||||
from typing import TYPE_CHECKING, Optional
|
||||
|
||||
from sqlalchemy import String, DateTime, Text, ForeignKey
|
||||
from sqlalchemy import DateTime, ForeignKey, String, Text
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from app.database import db
|
||||
@@ -29,11 +29,11 @@ class UserOAuth(db.Model):
|
||||
# Provider-specific user information
|
||||
email: Mapped[str] = mapped_column(String(255), nullable=False)
|
||||
name: Mapped[str] = mapped_column(String(255), nullable=False)
|
||||
picture: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
|
||||
picture: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
|
||||
# Timestamps
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime, default=datetime.utcnow, nullable=False
|
||||
DateTime, default=datetime.utcnow, nullable=False,
|
||||
)
|
||||
updated_at: Mapped[datetime] = mapped_column(
|
||||
DateTime,
|
||||
@@ -45,13 +45,13 @@ class UserOAuth(db.Model):
|
||||
# Unique constraint on provider + provider_id combination
|
||||
__table_args__ = (
|
||||
db.UniqueConstraint(
|
||||
"provider", "provider_id", name="unique_provider_user"
|
||||
"provider", "provider_id", name="unique_provider_user",
|
||||
),
|
||||
)
|
||||
|
||||
# Relationships
|
||||
user: Mapped["User"] = relationship(
|
||||
"User", back_populates="oauth_providers"
|
||||
"User", back_populates="oauth_providers",
|
||||
)
|
||||
|
||||
def __repr__(self) -> str:
|
||||
@@ -73,11 +73,11 @@ class UserOAuth(db.Model):
|
||||
|
||||
@classmethod
|
||||
def find_by_provider_and_id(
|
||||
cls, provider: str, provider_id: str
|
||||
cls, provider: str, provider_id: str,
|
||||
) -> Optional["UserOAuth"]:
|
||||
"""Find OAuth provider by provider name and provider ID."""
|
||||
return cls.query.filter_by(
|
||||
provider=provider, provider_id=provider_id
|
||||
provider=provider, provider_id=provider_id,
|
||||
).first()
|
||||
|
||||
@classmethod
|
||||
@@ -88,7 +88,7 @@ class UserOAuth(db.Model):
|
||||
provider_id: str,
|
||||
email: str,
|
||||
name: str,
|
||||
picture: Optional[str] = None,
|
||||
picture: str | None = None,
|
||||
) -> "UserOAuth":
|
||||
"""Create new OAuth provider or update existing one."""
|
||||
oauth_provider = cls.find_by_provider_and_id(provider, provider_id)
|
||||
|
||||
Reference in New Issue
Block a user