Skip to content

Commit

Permalink
publish date checks
Browse files Browse the repository at this point in the history
  • Loading branch information
DRIESASTER committed May 23, 2024
1 parent a2f5dc1 commit 87dc0f5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
8 changes: 6 additions & 2 deletions backend/src/project/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.future import select
from src.subject.models import InstructorSubject, StudentSubject, Subject
from datetime import datetime, timezone

from .exceptions import ProjectNotFound
from .models import Project, Requirement
Expand Down Expand Up @@ -32,12 +33,15 @@ async def get_project(db: AsyncSession, project_id: int) -> Project:
return result.scalars().first()


async def get_projects_by_user(db: AsyncSession, user_id: str) -> tuple[Sequence[Project], Sequence[Project]]:
async def get_projects_by_user(db: AsyncSession, user_id: str) -> tuple:
now_utc = datetime.now(timezone.utc)
student_result = await db.execute(
select(Project)
.join(Subject, Project.subject_id == Subject.id)
.join(StudentSubject, StudentSubject.c.subject_id == Subject.id)
.where(StudentSubject.c.uid == user_id)
.where(
(StudentSubject.c.uid == user_id)
)
)
instructor_result = await db.execute(
select(Project)
Expand Down
6 changes: 4 additions & 2 deletions backend/src/subject/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from src.user.dependencies import get_authenticated_user, retrieve_user, teacher_or_admin_user_validation
from src.user.schemas import User
from src.subject.utils import has_subject_privileges
from datetime import datetime, timezone

from . import service
from .dependencies import (
Expand Down Expand Up @@ -177,8 +178,9 @@ async def list_projects(
user: User = Depends(get_authenticated_user)
) -> ProjectList:
projects = await get_projects_for_subject(db, subject_id)

if not await has_subject_privileges(subject_id, user, db):
projects.projects = list(filter(lambda x: x.is_visible, projects.projects))
now = datetime.now(timezone.utc)
projects.projects = [
project for project in projects.projects if project.publish_date <= now and project.is_visible]

return projects
7 changes: 7 additions & 0 deletions backend/src/user/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import src.project.service as project_service
import src.subject.service as subject_service
import src.user.service as user_service
from datetime import datetime, timezone
from fastapi import Depends
from sqlalchemy.ext.asyncio import AsyncSession
from src.auth.dependencies import jwt_token_validation
Expand Down Expand Up @@ -81,4 +82,10 @@ async def retrieve_projects(
student_projects, instructor_projects = await project_service.get_projects_by_user(
db, user.uid
)
now = datetime.now(timezone.utc)
student_projects = [
project for project in student_projects
if project.publish_date <= now and project.is_visible
]

return UserProjectList(as_student=student_projects, as_instructor=instructor_projects)

0 comments on commit 87dc0f5

Please sign in to comment.