Skip to content

Commit

Permalink
fix: improve delete
Browse files Browse the repository at this point in the history
  • Loading branch information
trim21 committed Aug 17, 2024
1 parent 0334b70 commit 036ae02
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 26 deletions.
26 changes: 1 addition & 25 deletions server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from litestar.contrib.jinja import JinjaTemplateEngine
from litestar.exceptions import (
InternalServerException,
NotAuthorizedException,
NotFoundException,
)
from litestar.response import Template
Expand All @@ -23,7 +22,7 @@
from server import tmpl
from server.auth import callback, login, session_auth_config
from server.base import Request, pg, pg_pool_startup
from server.contrib import suggest_api, suggest_ui
from server.contrib import delete_patch, suggest_api, suggest_ui
from server.model import Patch
from server.review import review_patch

Expand Down Expand Up @@ -104,29 +103,6 @@ async def get_patch(patch_id: str, request: Request) -> Template:
)


@litestar.post("/api/delete-patch/{patch_id:str}")
async def delete_patch(patch_id: str, request: Request) -> Template:
if not request.auth:
raise NotAuthorizedException

p = await pg.fetchrow("""select * from patch where id = $1 and deleted_at is NULL""", patch_id)
if not p:
raise NotFoundException()

patch = Patch(**p)

if patch.from_user_id != request.auth.user_id:
raise NotAuthorizedException

await pg.execute(
"update patch set deleted_at = $1 where id = $2 ",
datetime.now(tz=UTC),
patch_id,
)

return Template("patch.html.jinja2", context={"patch": p, "auth": request.auth})


def before_req(req: litestar.Request):
req.state["now"] = datetime.now(tz=UTC)

Expand Down
32 changes: 31 additions & 1 deletion server/contrib.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
from dataclasses import dataclass
from datetime import datetime
from typing import Annotated

import litestar
from litestar.enums import RequestEncodingType
from litestar.exceptions import (
HTTPException,
NotAuthorizedException,
NotFoundException,
PermissionDeniedException,
ValidationException,
)
from litestar.params import Body
from litestar.response import Redirect, Template

from config import UTC
from server.base import Request, http_client, pg
from server.model import Wiki
from server.model import Patch, Wiki


@litestar.get("/suggest")
Expand Down Expand Up @@ -110,3 +113,30 @@ async def suggest_api(
)

return Redirect(f"/patch/{pk}")


@litestar.post("/api/delete-patch/{patch_id:str}")
async def delete_patch(patch_id: str, request: Request) -> Redirect:
if not request.auth:
raise NotAuthorizedException

async with pg.acquire() as conn:
async with conn.transaction():
p = await conn.fetchrow(
"""select * from patch where id = $1 and deleted_at is NULL""", patch_id
)
if not p:
raise NotFoundException()

patch = Patch(**p)

if patch.from_user_id != request.auth.user_id:
raise NotAuthorizedException

await conn.execute(
"update patch set deleted_at = $1 where id = $2 ",
datetime.now(tz=UTC),
patch_id,
)

return Redirect("/")

0 comments on commit 036ae02

Please sign in to comment.