Skip to content

Commit

Permalink
Merge pull request #247 from SELab-2/publish_date
Browse files Browse the repository at this point in the history
fix issue  #234 (add publish date checks)
  • Loading branch information
pieterjanin authored May 23, 2024
2 parents 1efaf42 + e03f50d commit 171cd65
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 3 deletions.
2 changes: 2 additions & 0 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 All @@ -14,6 +15,7 @@ async def create_project(db: AsyncSession, project_in: ProjectCreate) -> Project
new_project = Project(
name=project_in.name,
deadline=project_in.deadline,
publish_date=project_in.publish_date,
subject_id=project_in.subject_id,
description=project_in.description,
is_visible=project_in.is_visible,
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]

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)
3 changes: 2 additions & 1 deletion backend/tests/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
# skeletons for basic json objects
subject = {"name": "test subject"}
future_date = datetime.now(timezone.utc) + timedelta(weeks=1)
past_date = datetime.now(timezone.utc) - timedelta(weeks=1)
project = {
"name": "test project",
"subject_id": 0, # temp needs to be filled in by actual subject id
Expand All @@ -18,7 +19,7 @@
"enroll_deadline": future_date.strftime("%Y-%m-%dT%H:%M:%SZ"),
"capacity": 1,
"requirements": [{"mandatory": "false", "value": "*.pdf"}],
"publish_date": future_date.strftime("%Y-%m-%dT%H:%M:%SZ"),
"publish_date": past_date.strftime("%Y-%m-%dT%H:%M:%SZ"),
}


Expand Down
3 changes: 3 additions & 0 deletions frontend/src/i18n/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ export default {
testfiles: "Testfiles",
requirements_disclaimer: "For info on the usage of requirements please visit our",
to_project: "To Project",
titlereq: "Project title is required.",
date_check: "Deadline or publish date cannot be in the past. Please correct the dates.",
publish_check: "Publish date must be before or on the deadline. Please correct the dates.",
},
navigation: {
home: "Home",
Expand Down
3 changes: 3 additions & 0 deletions frontend/src/i18n/locales/nl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ export default {
testfiles: "Testbestanden",
requirements_disclaimer: "Voor info over het gebruik van bestandsvereisten, bezoek onze ",
to_project: "Naar project",
titlereq: "Project titel is verplicht.",
date_check: "Deadline of publiceerdatum kan niet in het verleden liggen.",
publish_check: "Publiceerdatum moet voor de deadline liggen.",
},
navigation: {
home: "Hoofdscherm",
Expand Down
23 changes: 23 additions & 0 deletions frontend/src/views/CreateProjectView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
:label="$t('project.assignment')"
required
:placeholder="$t('submit.create_title_tip')"
:rules="titleRules"
/>
<v-select
v-if="!isEditMode"
Expand Down Expand Up @@ -297,6 +298,17 @@ function setErrorAlert(message) {
}
async function submitForm() {
if (
isDateInPast(deadline.value) ||
(enrollDeadline.value && isDateInPast(enrollDeadline.value))
) {
setErrorAlert(t("project.date_check"));
return;
}
if (!isPublishDateValid(publishDate.value, deadline.value)) {
setErrorAlert(t("project.publish_check"));
return;
}
const projectData = formatProjectData();
try {
if (isEditMode.value) {
Expand Down Expand Up @@ -431,6 +443,17 @@ function divideStudentsIntoGroups(students: User[], capacity: number) {
return groups;
}
function isDateInPast(date) {
const now = new Date();
return new Date(date) < now;
}
function isPublishDateValid(publishDate, deadline) {
return new Date(publishDate) <= new Date(deadline);
}
const titleRules = computed(() => [(v) => !!v.trim() || t("project.titlereq")]);
</script>

<style>
Expand Down

0 comments on commit 171cd65

Please sign in to comment.