Spaces:
Sleeping
Sleeping
Ajit Panday
commited on
Commit
·
ddecf13
1
Parent(s):
fdff54c
Fix database session dependency in auth.py
Browse files- app/auth.py +18 -27
app/auth.py
CHANGED
|
@@ -93,38 +93,26 @@ async def login(form_data: OAuth2PasswordRequestForm = Depends()):
|
|
| 93 |
@router.post("/customers/", response_model=dict)
|
| 94 |
async def create_customer(
|
| 95 |
customer_data: dict,
|
| 96 |
-
db: Session = Depends(
|
| 97 |
current_admin: str = Depends(get_current_admin)
|
| 98 |
):
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
# Create new customer
|
| 103 |
customer = Customer(
|
| 104 |
name=customer_data["name"],
|
| 105 |
company_name=customer_data["company_name"],
|
| 106 |
email=customer_data["email"],
|
| 107 |
-
api_key=
|
| 108 |
-
# Database credentials
|
| 109 |
-
db_host=customer_data.get("db_host"),
|
| 110 |
-
db_port=customer_data.get("db_port"),
|
| 111 |
-
db_name=customer_data.get("db_name"),
|
| 112 |
-
db_user=customer_data.get("db_user"),
|
| 113 |
-
db_password=customer_data.get("db_password")
|
| 114 |
)
|
| 115 |
|
| 116 |
-
# Validate database connection if credentials are provided
|
| 117 |
-
if all([customer.db_host, customer.db_port, customer.db_name, customer.db_user, customer.db_password]):
|
| 118 |
-
try:
|
| 119 |
-
engine = customer.get_db_engine()
|
| 120 |
-
with engine.connect() as conn:
|
| 121 |
-
conn.execute("SELECT 1")
|
| 122 |
-
except Exception as e:
|
| 123 |
-
raise HTTPException(
|
| 124 |
-
status_code=400,
|
| 125 |
-
detail=f"Invalid database credentials: {str(e)}"
|
| 126 |
-
)
|
| 127 |
-
|
| 128 |
db.add(customer)
|
| 129 |
db.commit()
|
| 130 |
db.refresh(customer)
|
|
@@ -134,24 +122,25 @@ async def create_customer(
|
|
| 134 |
"name": customer.name,
|
| 135 |
"company_name": customer.company_name,
|
| 136 |
"email": customer.email,
|
| 137 |
-
"api_key": customer.api_key
|
| 138 |
-
"db_configured": bool(customer.get_db_url())
|
| 139 |
}
|
| 140 |
|
| 141 |
@router.get("/customers/", response_model=list)
|
| 142 |
async def list_customers(
|
| 143 |
-
db: Session = Depends(
|
| 144 |
current_admin: str = Depends(get_current_admin)
|
| 145 |
):
|
|
|
|
| 146 |
customers = db.query(Customer).all()
|
| 147 |
return customers
|
| 148 |
|
| 149 |
@router.get("/customers/{customer_id}", response_model=dict)
|
| 150 |
async def get_customer(
|
| 151 |
customer_id: int,
|
| 152 |
-
db: Session = Depends(
|
| 153 |
current_admin: str = Depends(get_current_admin)
|
| 154 |
):
|
|
|
|
| 155 |
customer = db.query(Customer).filter(Customer.id == customer_id).first()
|
| 156 |
if not customer:
|
| 157 |
raise HTTPException(status_code=404, detail="Customer not found")
|
|
@@ -160,12 +149,14 @@ async def get_customer(
|
|
| 160 |
@router.delete("/customers/{customer_id}")
|
| 161 |
async def delete_customer(
|
| 162 |
customer_id: int,
|
| 163 |
-
db: Session = Depends(
|
| 164 |
current_admin: str = Depends(get_current_admin)
|
| 165 |
):
|
|
|
|
| 166 |
customer = db.query(Customer).filter(Customer.id == customer_id).first()
|
| 167 |
if not customer:
|
| 168 |
raise HTTPException(status_code=404, detail="Customer not found")
|
|
|
|
| 169 |
db.delete(customer)
|
| 170 |
db.commit()
|
| 171 |
return {"message": "Customer deleted successfully"}
|
|
|
|
| 93 |
@router.post("/customers/", response_model=dict)
|
| 94 |
async def create_customer(
|
| 95 |
customer_data: dict,
|
| 96 |
+
db: Session = Depends(get_db),
|
| 97 |
current_admin: str = Depends(get_current_admin)
|
| 98 |
):
|
| 99 |
+
"""Create a new customer"""
|
| 100 |
+
# Check if email already exists
|
| 101 |
+
existing_customer = db.query(Customer).filter(Customer.email == customer_data["email"]).first()
|
| 102 |
+
if existing_customer:
|
| 103 |
+
raise HTTPException(
|
| 104 |
+
status_code=status.HTTP_400_BAD_REQUEST,
|
| 105 |
+
detail="Email already registered"
|
| 106 |
+
)
|
| 107 |
|
| 108 |
# Create new customer
|
| 109 |
customer = Customer(
|
| 110 |
name=customer_data["name"],
|
| 111 |
company_name=customer_data["company_name"],
|
| 112 |
email=customer_data["email"],
|
| 113 |
+
api_key=str(secrets.token_urlsafe(32))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
)
|
| 115 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
db.add(customer)
|
| 117 |
db.commit()
|
| 118 |
db.refresh(customer)
|
|
|
|
| 122 |
"name": customer.name,
|
| 123 |
"company_name": customer.company_name,
|
| 124 |
"email": customer.email,
|
| 125 |
+
"api_key": customer.api_key
|
|
|
|
| 126 |
}
|
| 127 |
|
| 128 |
@router.get("/customers/", response_model=list)
|
| 129 |
async def list_customers(
|
| 130 |
+
db: Session = Depends(get_db),
|
| 131 |
current_admin: str = Depends(get_current_admin)
|
| 132 |
):
|
| 133 |
+
"""List all customers"""
|
| 134 |
customers = db.query(Customer).all()
|
| 135 |
return customers
|
| 136 |
|
| 137 |
@router.get("/customers/{customer_id}", response_model=dict)
|
| 138 |
async def get_customer(
|
| 139 |
customer_id: int,
|
| 140 |
+
db: Session = Depends(get_db),
|
| 141 |
current_admin: str = Depends(get_current_admin)
|
| 142 |
):
|
| 143 |
+
"""Get customer details"""
|
| 144 |
customer = db.query(Customer).filter(Customer.id == customer_id).first()
|
| 145 |
if not customer:
|
| 146 |
raise HTTPException(status_code=404, detail="Customer not found")
|
|
|
|
| 149 |
@router.delete("/customers/{customer_id}")
|
| 150 |
async def delete_customer(
|
| 151 |
customer_id: int,
|
| 152 |
+
db: Session = Depends(get_db),
|
| 153 |
current_admin: str = Depends(get_current_admin)
|
| 154 |
):
|
| 155 |
+
"""Delete a customer"""
|
| 156 |
customer = db.query(Customer).filter(Customer.id == customer_id).first()
|
| 157 |
if not customer:
|
| 158 |
raise HTTPException(status_code=404, detail="Customer not found")
|
| 159 |
+
|
| 160 |
db.delete(customer)
|
| 161 |
db.commit()
|
| 162 |
return {"message": "Customer deleted successfully"}
|