Add last login tracking, batch date auto-fill, and bug fixes
- Track last_login_at on User model, updated on every successful login - Show last login date in admin panel user table - Fix admin/garden date display (datetime strings already contain T separator) - Fix My Garden Internal Server Error (MySQL does not support NULLS LAST syntax) - Fix Log Batch infinite loop when user has zero varieties - Auto-fill batch dates from today when creating a new batch, calculated from selected variety's week offsets (germination, greenhouse, garden) - Update README with new features and batch date auto-fill formula table Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -44,6 +44,7 @@ class User(Base):
|
||||
is_admin = Column(Boolean, default=False, nullable=False)
|
||||
is_disabled = Column(Boolean, default=False, nullable=False)
|
||||
created_at = Column(DateTime, server_default=func.now())
|
||||
last_login_at = Column(DateTime, nullable=True)
|
||||
|
||||
varieties = relationship("Variety", back_populates="user", cascade="all, delete-orphan")
|
||||
batches = relationship("Batch", back_populates="user", cascade="all, delete-orphan")
|
||||
|
||||
@@ -23,7 +23,7 @@ def list_users(db: Session = Depends(get_db), _: User = Depends(get_admin_user))
|
||||
return [
|
||||
AdminUserOut(
|
||||
id=u.id, email=u.email, is_admin=u.is_admin, is_disabled=u.is_disabled,
|
||||
created_at=u.created_at, **_user_stats(db, u.id)
|
||||
created_at=u.created_at, last_login_at=u.last_login_at, **_user_stats(db, u.id)
|
||||
)
|
||||
for u in users
|
||||
]
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
@@ -48,6 +50,8 @@ def login(data: UserLogin, db: Session = Depends(get_db)):
|
||||
raise HTTPException(status_code=401, detail="Invalid email or password")
|
||||
if user.is_disabled:
|
||||
raise HTTPException(status_code=403, detail="Account has been disabled")
|
||||
user.last_login_at = datetime.now(timezone.utc)
|
||||
db.commit()
|
||||
return {"access_token": create_access_token(user.id), "token_type": "bearer"}
|
||||
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlalchemy import case
|
||||
from sqlalchemy.orm import Session, joinedload
|
||||
from typing import List
|
||||
|
||||
@@ -16,7 +17,7 @@ def list_batches(db: Session = Depends(get_db), current_user: User = Depends(get
|
||||
db.query(Batch)
|
||||
.options(joinedload(Batch.variety))
|
||||
.filter(Batch.user_id == current_user.id)
|
||||
.order_by(Batch.sow_date.desc().nullslast(), Batch.created_at.desc())
|
||||
.order_by(case((Batch.sow_date.is_(None), 1), else_=0), Batch.sow_date.desc(), Batch.created_at.desc())
|
||||
.all()
|
||||
)
|
||||
|
||||
|
||||
@@ -33,6 +33,7 @@ class AdminUserOut(BaseModel):
|
||||
is_admin: bool
|
||||
is_disabled: bool
|
||||
created_at: Optional[datetime]
|
||||
last_login_at: Optional[datetime]
|
||||
variety_count: int
|
||||
batch_count: int
|
||||
|
||||
|
||||
Reference in New Issue
Block a user