Skip to content

Commit

Permalink
Add project soft delete
Browse files Browse the repository at this point in the history
  • Loading branch information
berrydenhartog committed Nov 6, 2024
1 parent eaedf30 commit a089612
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 3 deletions.
10 changes: 10 additions & 0 deletions amt/api/routes/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,16 @@ def find_requirement_tasks_by_measure_urn(system_card: SystemCard, measure_urn:
return requirement_tasks


@router.delete("/{project_id}")
async def delete_project(
request: Request,
project_id: int,
projects_service: Annotated[ProjectsService, Depends(ProjectsService)],
) -> HTMLResponse:
await projects_service.delete(project_id)
return templates.Redirect(request, "/algorithm-system")


@router.get("/{project_id}/measure/{measure_urn}")
async def get_measure(
request: Request,
Expand Down
28 changes: 28 additions & 0 deletions amt/migrations/versions/6581a03aabec_add_deleted_at_to_project.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""add deleted_at to project
Revision ID: 6581a03aabec
Revises: 7f20f8562007
Create Date: 2024-11-01 10:29:58.930558
"""

from collections.abc import Sequence

import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import sqlite

# revision identifiers, used by Alembic.
revision: str = "6581a03aabec"
down_revision: str | None = "7f20f8562007"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
op.add_column("project", sa.Column("deleted_at", sa.DateTime(), nullable=True))


def downgrade() -> None:
op.drop_column("project", "deleted_at")

1 change: 1 addition & 0 deletions amt/models/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class Project(Base):
lifecycle: Mapped[Lifecycles | None] = mapped_column(ENUM(Lifecycles, name="lifecycle"), nullable=True)
system_card_json: Mapped[dict[str, Any]] = mapped_column(JSON, default=dict)
last_edited: Mapped[datetime] = mapped_column(server_default=func.now(), onupdate=func.now(), nullable=False)
deleted_at: Mapped[datetime | None] = mapped_column(server_default=None, nullable=True)

def __init__(self, *args: Any, **kwargs: Any) -> None: # noqa: ANN401
system_card: SystemCard | None = kwargs.pop("system_card", None)
Expand Down
5 changes: 3 additions & 2 deletions amt/repositories/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def __init__(self, session: Annotated[AsyncSession, Depends(get_session)]) -> No
self.session = session

async def find_all(self) -> Sequence[Project]:
result = await self.session.execute(select(Project))
result = await self.session.execute(select(Project).where(Project.deleted_at.is_(None)))
return result.scalars().all()

async def delete(self, project: Project) -> None:
Expand Down Expand Up @@ -66,7 +66,7 @@ async def save(self, project: Project) -> Project:

async def find_by_id(self, project_id: int) -> Project:
try:
statement = select(Project).where(Project.id == project_id)
statement = select(Project).where(Project.id == project_id).where(Project.deleted_at.is_(None))
result = await self.session.execute(statement)
return result.scalars().one()
except NoResultFound as e:
Expand Down Expand Up @@ -103,6 +103,7 @@ async def paginate( # noqa
statement = statement.order_by(Project.last_edited.desc())
else:
statement = statement.order_by(func.lower(Project.name))
statement = statement.filter(Project.deleted_at.is_(None))
statement = statement.offset(skip).limit(limit)
db_result = await self.session.execute(statement)
result = list(db_result.scalars())
Expand Down
9 changes: 9 additions & 0 deletions amt/services/projects.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import logging
from datetime import UTC, datetime
from functools import lru_cache
from os import listdir
from os.path import isfile, join
Expand Down Expand Up @@ -36,6 +37,14 @@ def __init__(

async def get(self, project_id: int) -> Project:
project = await self.repository.find_by_id(project_id)
if project.deleted_at:
raise AMTNotFound()
return project

async def delete(self, project_id: int) -> Project:
project = await self.repository.find_by_id(project_id)
project.deleted_at = datetime.now(tz=UTC)
project = await self.repository.save(project)
return project

async def create(self, project_new: ProjectNew) -> Project:
Expand Down
5 changes: 5 additions & 0 deletions amt/site/static/scss/layout.scss
Original file line number Diff line number Diff line change
Expand Up @@ -303,3 +303,8 @@ main {
color: var(--rvo-form-feedback-error-color);
font-weight: var(--rvo-form-feedback-error-font-weight);
}

.amt-flex-container {
display: flex;
justify-content: space-between;
}
18 changes: 17 additions & 1 deletion amt/site/templates/projects/details_base.html.j2
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,25 @@
<div id="dynamic-modal-content"></div>
</div>
</div>
<div id="delete-modal" class="minbzk-modal display-none">
<div class="modal-underlay" onclick="amt.closeModal('delete-modal')"></div>
<div class="modal-content rvo-layout-column rvo-layout-gap--md"
style="height: auto">
<div id="dynamic-modal-content"></div>
</div>
</div>
<div class="rvo-max-width-layout rvo-max-width-layout--md rvo-max-width-layout-inline-padding--none">
<div class="rvo-content">
<h1 class="utrecht-heading-1">{{ editable(project, "name") }}</h1>
<div class="amt-flex-container">
<h1 class="utrecht-heading-1 project-title">{{ project.name }}</h1>
<a class="rvo-link rvo-link--normal rvo-link--with-icon"
onclick="amt.openModal('delete-modal')">
<span class="utrecht-icon rvo-icon rvo-icon-verwijderen rvo-icon--md rvo-icon--hemelblauw rvo-link__icon--before"
role="img"
aria-label="Home"></span>
{% trans %}Delete algorithm system{% endtrans %}
</a>
</div>
</div>
<!-- Requirements Widget -->
<div class="rvo-layout-grid-container">
Expand Down

0 comments on commit a089612

Please sign in to comment.