Spaces:
Running
Running
Prathamesh Sable
commited on
Commit
·
e3c16ea
1
Parent(s):
00e8345
lower email and null name handle
Browse files
db/models.py
CHANGED
|
@@ -56,8 +56,8 @@ class User(Base):
|
|
| 56 |
__tablename__ = "users"
|
| 57 |
|
| 58 |
id = Column(Integer, primary_key=True, index=True)
|
| 59 |
-
name = Column(String, unique=False, index=False)
|
| 60 |
-
email = Column(String, unique=True, index=True)
|
| 61 |
hashed_password = Column(String, nullable=False)
|
| 62 |
is_active = Column(Boolean, default=True)
|
| 63 |
|
|
|
|
| 56 |
__tablename__ = "users"
|
| 57 |
|
| 58 |
id = Column(Integer, primary_key=True, index=True)
|
| 59 |
+
name = Column(String, unique=False, index=False, nullable=False)
|
| 60 |
+
email = Column(String, unique=True, index=True, nullable=False)
|
| 61 |
hashed_password = Column(String, nullable=False)
|
| 62 |
is_active = Column(Boolean, default=True)
|
| 63 |
|
migrations/versions/b640d22a64f5_added_name_made_not_null.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""added name made not null
|
| 2 |
+
|
| 3 |
+
Revision ID: b640d22a64f5
|
| 4 |
+
Revises: f48ac1ac9bd0
|
| 5 |
+
Create Date: 2025-04-17 22:59:15.180563
|
| 6 |
+
|
| 7 |
+
"""
|
| 8 |
+
from typing import Sequence, Union
|
| 9 |
+
|
| 10 |
+
from alembic import op
|
| 11 |
+
import sqlalchemy as sa
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
# revision identifiers, used by Alembic.
|
| 15 |
+
revision: str = 'b640d22a64f5'
|
| 16 |
+
down_revision: Union[str, None] = 'f48ac1ac9bd0'
|
| 17 |
+
branch_labels: Union[str, Sequence[str], None] = None
|
| 18 |
+
depends_on: Union[str, Sequence[str], None] = None
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def upgrade() -> None:
|
| 22 |
+
"""Upgrade schema."""
|
| 23 |
+
# ### commands auto generated by Alembic - please adjust! ###
|
| 24 |
+
op.alter_column('users', 'name',
|
| 25 |
+
existing_type=sa.VARCHAR(),
|
| 26 |
+
nullable=False)
|
| 27 |
+
op.alter_column('users', 'email',
|
| 28 |
+
existing_type=sa.VARCHAR(),
|
| 29 |
+
nullable=False)
|
| 30 |
+
# ### end Alembic commands ###
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def downgrade() -> None:
|
| 34 |
+
"""Downgrade schema."""
|
| 35 |
+
# ### commands auto generated by Alembic - please adjust! ###
|
| 36 |
+
op.alter_column('users', 'email',
|
| 37 |
+
existing_type=sa.VARCHAR(),
|
| 38 |
+
nullable=True)
|
| 39 |
+
op.alter_column('users', 'name',
|
| 40 |
+
existing_type=sa.VARCHAR(),
|
| 41 |
+
nullable=True)
|
| 42 |
+
# ### end Alembic commands ###
|
services/auth_service.py
CHANGED
|
@@ -3,6 +3,7 @@ from jose import JWTError, jwt
|
|
| 3 |
from datetime import datetime, timedelta
|
| 4 |
from fastapi import Depends, HTTPException, status
|
| 5 |
from fastapi.security import OAuth2PasswordBearer
|
|
|
|
| 6 |
from sqlalchemy.orm import Session,Mapped
|
| 7 |
from db.database import get_db
|
| 8 |
from db.models import User
|
|
@@ -39,13 +40,13 @@ def get_password_hash(password):
|
|
| 39 |
def get_user(db, email: str):
|
| 40 |
log_info(f"Getting user: {email}")
|
| 41 |
try:
|
| 42 |
-
return db.query(User).filter(User.email == email).first()
|
| 43 |
except Exception as e:
|
| 44 |
log_error(f"Error getting user: {str(e)}")
|
| 45 |
raise HTTPException(status_code=500, detail=str(e))
|
| 46 |
|
| 47 |
def authenticate_user(db: Session, username: str, password: str):
|
| 48 |
-
user = db.query(User).filter(User.email == username).first()
|
| 49 |
if not user:
|
| 50 |
return None
|
| 51 |
if not verify_password(password, user.hashed_password):
|
|
@@ -100,7 +101,7 @@ def create_user(db: Session, name: str, email: str, password: str):
|
|
| 100 |
log_info(f"Creating user: {name}")
|
| 101 |
try:
|
| 102 |
hashed_password = get_password_hash(password)
|
| 103 |
-
db_user = User(name=name, email=email, hashed_password=hashed_password)
|
| 104 |
db.add(db_user)
|
| 105 |
db.commit()
|
| 106 |
db.refresh(db_user)
|
|
|
|
| 3 |
from datetime import datetime, timedelta
|
| 4 |
from fastapi import Depends, HTTPException, status
|
| 5 |
from fastapi.security import OAuth2PasswordBearer
|
| 6 |
+
from sqlalchemy import func
|
| 7 |
from sqlalchemy.orm import Session,Mapped
|
| 8 |
from db.database import get_db
|
| 9 |
from db.models import User
|
|
|
|
| 40 |
def get_user(db, email: str):
|
| 41 |
log_info(f"Getting user: {email}")
|
| 42 |
try:
|
| 43 |
+
return db.query(User).filter(func.lower(User.email) == email.lower()).first()
|
| 44 |
except Exception as e:
|
| 45 |
log_error(f"Error getting user: {str(e)}")
|
| 46 |
raise HTTPException(status_code=500, detail=str(e))
|
| 47 |
|
| 48 |
def authenticate_user(db: Session, username: str, password: str):
|
| 49 |
+
user = db.query(User).filter(func.lower(User.email) == username.lower()).first()
|
| 50 |
if not user:
|
| 51 |
return None
|
| 52 |
if not verify_password(password, user.hashed_password):
|
|
|
|
| 101 |
log_info(f"Creating user: {name}")
|
| 102 |
try:
|
| 103 |
hashed_password = get_password_hash(password)
|
| 104 |
+
db_user = User(name=name, email=email.lower(), hashed_password=hashed_password)
|
| 105 |
db.add(db_user)
|
| 106 |
db.commit()
|
| 107 |
db.refresh(db_user)
|