diff --git a/.gitignore b/.gitignore index a00fae1..7dba06a 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,9 @@ __pycache__/ .coverage coverage.xml htmlcov/ + +# VSCode +.vscode/ + +# Environment variables +.env diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..fd4b310 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,20 @@ +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v5.0.0 + hooks: + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-yaml + - id: check-added-large-files + + - repo: local + hooks: + - id: pylint + name: pylint + entry: pylint + language: system + types: [python] + args: + - "--rcfile=pyproject.toml" + - "--disable=E0401" + - "--fail-under=8.0" diff --git a/webapi/auth/auth_service.py b/webapi/auth/auth_service.py index 4b9c6fa..8e64763 100644 --- a/webapi/auth/auth_service.py +++ b/webapi/auth/auth_service.py @@ -1,15 +1,17 @@ -from fastapi import HTTPException, Depends -from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials -from typing import Optional -from sqlmodel import Session, select -from models.user import User -from db.db_connection import get_session +import re from datetime import datetime, timedelta +from typing import Optional + import jwt +from fastapi import Depends, HTTPException +from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer from passlib.hash import sha256_crypt -import re -from dotenv import load_dotenv +from sqlmodel import Session, select + from core import config +from db.db_connection import get_session +from dotenv import load_dotenv +from models.user import User load_dotenv() @@ -43,6 +45,7 @@ def crear_jwt(data: dict): token = jwt.encode(payload, SECRET_KEY, algorithm="HS256") return token + # Validar un token con prefijo "Bearer" (compatibilidad legado) def validar_jwt(token: str): try: @@ -60,7 +63,7 @@ def validar_jwt(token: str): return None except jwt.InvalidTokenError: return None - except Exception: + except Exception: # pylint: disable=broad-exception-caught return None @@ -75,7 +78,7 @@ def validar_jwt_raw(token: str): return None except jwt.InvalidTokenError: return None - except Exception: + except Exception: # pylint: disable=broad-exception-caught return None @@ -90,5 +93,4 @@ def get_current_user( data = validar_jwt_raw(credentials.credentials) if not data: raise HTTPException(status_code=401, detail="Unauthorized token") - return data - \ No newline at end of file + return data \ No newline at end of file diff --git a/webapi/db/redis_connection.py b/webapi/db/redis_connection.py index f2d8863..37fd297 100644 --- a/webapi/db/redis_connection.py +++ b/webapi/db/redis_connection.py @@ -1,12 +1,12 @@ from redis import Redis + from core import config -""" -docker run -d \ - --name redis-server \ - -p 6379:6379 \ - redis/redis-stack:latest -""" +# docker run -d \ +# --name redis-server \ +# -p 6379:6379 \ +# redis/redis-stack:latest + def get_redis(): redis = Redis( diff --git a/webapi/models/prompts.py b/webapi/models/prompts.py index dcc58ed..c96827d 100644 --- a/webapi/models/prompts.py +++ b/webapi/models/prompts.py @@ -1,6 +1,7 @@ from typing import Optional -from sqlmodel import SQLModel, Field, Relationship -import models + +from sqlmodel import Field, Relationship, SQLModel + class Prompts(SQLModel, table=True): diff --git a/webapi/models/user.py b/webapi/models/user.py index 29ada1a..55dc56d 100644 --- a/webapi/models/user.py +++ b/webapi/models/user.py @@ -1,8 +1,8 @@ from typing import Optional, List + from pydantic import EmailStr -from sqlmodel import SQLModel, Field, Relationship from sqlalchemy import BIGINT -import models +from sqlmodel import Field, Relationship, SQLModel class User(SQLModel, table=True): @@ -10,12 +10,11 @@ class User(SQLModel, table=True): username: str = Field(max_length=50, index=True, nullable=False) name: str = Field(max_length=100, index=True, nullable=False) last_name: str = Field(max_length=100, index=True, nullable=False) - #phone: Optional[int] = Field(default=12, sa_type=BIGINT, index=True, unique=True) # Check why numbers lengh like 10 digits is not working phone: Optional[int] = Field(default=None, sa_type=BIGINT, index=True, unique=True) email: EmailStr = Field(max_length=100, nullable=False) hashed_password: str = Field(max_length=255) # Longer for bcrypt hashes prompts: List["Prompts"] = Relationship( - sa_relationship_kwargs={"lazy": "selectin"}, + sa_relationship_kwargs={"lazy": "selectin"}, back_populates="user" )