Skip to content

Commit

Permalink
fix: add image to user profile (#1053)
Browse files Browse the repository at this point in the history
* feat: added new field to user model to store user profile_img

* feat: updated schema to add updated task history

* updated response of update_task_status

* revert migration

* Revert migration inside revert folder

---------

Co-authored-by: sujanadh <sujanadh07@gmail.com>
  • Loading branch information
Sujanadh and sujanadh authored Dec 23, 2023
1 parent b98c745 commit 6b2847c
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 24 deletions.
16 changes: 10 additions & 6 deletions src/backend/app/auth/auth_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
from loguru import logger as log
from sqlalchemy.orm import Session

from ..db import database
from ..db.db_models import DbUser
from ..users import user_crud
from .osm import AuthUser, init_osm_auth, login_required
from app.db import database
from app.db.db_models import DbUser
from app.users import user_crud
from app.auth.osm import AuthUser, init_osm_auth, login_required

router = APIRouter(
prefix="/auth",
Expand Down Expand Up @@ -108,10 +108,14 @@ async def my_data(
"Please contact the administrator."
),
)

# Add user to database
db_user = DbUser(id=user_data["id"], username=user_data["username"])
db_user = DbUser(id=user_data["id"], username=user_data["username"], profile_img = user_data["img_url"])
db.add(db_user)
db.commit()
else:
if user_data.get("img_url"):
user.profile_img = user_data["img_url"]
db.commit()

return JSONResponse(content={"user_data": user_data}, status_code=200)
1 change: 1 addition & 0 deletions src/backend/app/db/db_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class DbUser(Base):

id = Column(BigInteger, primary_key=True, index=True)
username = Column(String, unique=True)
profile_img = Column(String)
role = Column(Enum(UserRole), default=UserRole.MAPPER)

name = Column(String)
Expand Down
34 changes: 28 additions & 6 deletions src/backend/app/tasks/tasks_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
# along with FMTM. If not, see <https:#www.gnu.org/licenses/>.
#
import base64
from typing import List

from fastapi import HTTPException
from fastapi import HTTPException, Depends
from geoalchemy2.shape import from_shape
from geojson import dump
from loguru import logger as log
Expand All @@ -26,15 +27,16 @@
from sqlalchemy.orm import Session
from sqlalchemy.sql import text

from ..central import central_crud
from ..db import db_models
from ..models.enums import (
from app.tasks import tasks_schemas
from app.central import central_crud
from app.db import db_models, database
from app.models.enums import (
TaskStatus,
get_action_for_status_change,
verify_valid_status_update,
)
from ..projects import project_crud
from ..users import user_crud
from app.projects import project_crud
from app.users import user_crud


async def get_task_count_in_project(db: Session, project_id: int):
Expand Down Expand Up @@ -317,3 +319,23 @@ async def edit_task_boundary(db: Session, task_id: int, boundary: str):
)

return True


async def update_task_history(tasks: List[tasks_schemas.TaskBase], db: Session = Depends(database.get_db)):
def process_history_entry(history_entry):
status = history_entry.action_text.split()
history_entry.status = status[5]

if history_entry.user_id:
user = db.query(db_models.DbUser).filter_by(id=history_entry.user_id).first()
if user:
history_entry.username = user.username
history_entry.profile_img = user.profile_img

for task in tasks if isinstance(tasks, list) else [tasks]:
task_history = task.task_history
if isinstance(task_history, list):
for history_entry in task_history:
process_history_entry(history_entry)

return tasks
21 changes: 11 additions & 10 deletions src/backend/app/tasks/tasks_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
from sqlalchemy.orm import Session
from sqlalchemy.sql import text

from ..central import central_crud
from ..db import database
from ..models.enums import TaskStatus
from ..projects import project_crud, project_schemas
from ..users import user_schemas
from app.central import central_crud
from app.db import database
from app.models.enums import TaskStatus
from app.projects import project_crud, project_schemas
from app.users import user_schemas
from . import tasks_crud, tasks_schemas

router = APIRouter(
Expand All @@ -37,17 +37,17 @@
responses={404: {"description": "Not found"}},
)


@router.get("/task-list", response_model=List[tasks_schemas.Task])
@router.get("/task-list", response_model=List[tasks_schemas.ReadTask])
async def read_task_list(
project_id: int,
limit: int = 1000,
db: Session = Depends(database.get_db),
):
tasks = await tasks_crud.get_tasks(db, project_id, limit)
updated_tasks = await tasks_crud.update_task_history(tasks, db)
if not tasks:
raise HTTPException(status_code=404, detail="Tasks not found")
return tasks
return updated_tasks


@router.get("/", response_model=List[tasks_schemas.Task])
Expand Down Expand Up @@ -109,7 +109,7 @@ async def read_tasks(task_id: int, db: Session = Depends(database.get_db)):
return task


@router.post("/{task_id}/new_status/{new_status}", response_model=tasks_schemas.Task)
@router.post("/{task_id}/new_status/{new_status}", response_model=tasks_schemas.ReadTask)
async def update_task_status(
user: user_schemas.User,
task_id: int,
Expand All @@ -120,9 +120,10 @@ async def update_task_status(
user_id = user.id

task = await tasks_crud.update_task_status(db, user_id, task_id, new_status)
updated_task = await tasks_crud.update_task_history(task, db)
if not task:
raise HTTPException(status_code=404, detail="Task status could not be updated.")
return task
return updated_task


@router.post("/task-qr-code/{task_id}")
Expand Down
10 changes: 8 additions & 2 deletions src/backend/app/tasks/tasks_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ class TaskHistoryBase(BaseModel):

class TaskHistoryOut(TaskHistoryBase):
"""Task mapping history display."""

pass
status: str
username: str
profile_img: Optional[str]


class TaskBase(BaseModel):
Expand Down Expand Up @@ -144,3 +145,8 @@ def get_qrcode_base64(cls, v: Any, info: ValidationInfo) -> str:
else:
log.warning(f"No QR code found for task ID {info.data.get('id')}")
return ""

class ReadTask(Task):
"""Task details plus updated task history."""

task_history: Optional[List[TaskHistoryOut]] = None
10 changes: 10 additions & 0 deletions src/backend/migrations/002-add-profile-img.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-- ## Migration to:
-- * Add field user.profile_img (str).

-- Start a transaction
BEGIN;

ALTER TABLE IF EXISTS public.users
ADD COLUMN IF NOT EXISTS profile_img VARCHAR;
-- Commit the transaction
COMMIT;
9 changes: 9 additions & 0 deletions src/backend/migrations/revert/002-add-profile-img.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- Start a transaction
BEGIN;

-- Remove the profile_img column from the public.users table
ALTER TABLE IF EXISTS public.users
DROP COLUMN IF EXISTS profile_img;

-- Commit the transaction
COMMIT;

0 comments on commit 6b2847c

Please sign in to comment.