Skip to content

Commit

Permalink
Auto accept edits after update
Browse files Browse the repository at this point in the history
  • Loading branch information
volbil committed Jul 29, 2024
1 parent 167937e commit 296f98b
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 17 deletions.
8 changes: 5 additions & 3 deletions app/aggregator/info/manga.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ async def process_genres(session, manga, data):


def process_translated_ua(data):
return (
len(data["honey"]) > 0 or len(data["zenko"]) > 0 or len(data["miu"]) > 0
)
honey_count = len(data["honey"]) if "honey" in data else 0
zenko_count = len(data["zenko"]) if "zenko" in data else 0
miu_count = len(data["miu"]) if "miu" in data else 0

return honey_count > 0 or zenko_count > 0 or miu_count > 0


def process_external(data):
Expand Down
1 change: 1 addition & 0 deletions app/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@
LOG_EDIT_CLOSE = "edit_close"
LOG_EDIT_ACCEPT = "edit_accept"
LOG_EDIT_ACCEPT_AUTO = "edit_accept_auto"
LOG_EDIT_UPDATE_ACCEPT_AUTO = "edit_update_accept_auto"
LOG_EDIT_DENY = "edit_deny"
LOG_WATCH_CREATE = "watch_create"
LOG_WATCH_UPDATE = "watch_update"
Expand Down
6 changes: 6 additions & 0 deletions app/edit/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ async def validate_edit_create_args(
async def validate_edit_update_args(
args: EditArgs,
edit: Edit = Depends(validate_edit_update),
author: User = Depends(auth_required()),
) -> EditArgs:
"""Validate update edit args"""

Expand All @@ -158,6 +159,11 @@ async def validate_edit_update_args(
if len(args.after) == 0:
raise Abort("edit", "empty-edit")

if args.auto and not check_user_permissions(
author, [constants.PERMISSION_EDIT_AUTO]
):
raise Abort("permission", "denied")

return args


Expand Down
14 changes: 11 additions & 3 deletions app/edit/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,12 @@ async def update_pending_edit(
},
)

# If user marked edit as auto accept we should do that
if args.auto:
await accept_pending_edit(
session, edit, user, constants.LOG_EDIT_UPDATE_ACCEPT_AUTO
)

return edit


Expand Down Expand Up @@ -283,7 +289,7 @@ async def accept_pending_edit(
session: AsyncSession,
edit: Edit,
moderator: User,
auto: bool = False,
log_type: str = constants.LOG_EDIT_ACCEPT,
) -> Edit:
"""Accept pending edit"""

Expand Down Expand Up @@ -321,7 +327,7 @@ async def accept_pending_edit(

await create_log(
session,
constants.LOG_EDIT_ACCEPT_AUTO if auto else constants.LOG_EDIT_ACCEPT,
log_type,
moderator,
edit.id,
)
Expand Down Expand Up @@ -366,7 +372,9 @@ async def create_pending_edit(
# If user marked edit as auto accept we should do that
if args.auto:
await session.refresh(edit)
await accept_pending_edit(session, edit, author, True)
await accept_pending_edit(
session, edit, author, constants.LOG_EDIT_ACCEPT_AUTO
)

else:
await create_log(
Expand Down
2 changes: 2 additions & 0 deletions tests/aggregator/test_import_manga_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ async def test_import_manga_info(
"external": [],
"synonyms": [],
"synopsis_en": None,
"translated_ua": False,
}

assert edit.after == {
Expand Down Expand Up @@ -103,4 +104,5 @@ async def test_import_manga_info(
"bodies with its power. However, their quest for the fated stone "
"also leads them to unravel far darker secrets than they could ever "
"imagine.\n\n[Written by MAL Rewrite]",
"translated_ua": True,
}
11 changes: 1 addition & 10 deletions tests/edit/test_edit_create_auto.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
from client_requests import request_create_edit
from sqlalchemy import select, desc, func
from app.models import Log
from fastapi import status
from app import constants

from app.models import (
CharacterEdit,
PersonEdit,
AnimeEdit,
Edit,
)


async def test_edit_create_auto(
client,
Expand Down Expand Up @@ -40,7 +31,7 @@ async def test_edit_create_auto(

# Check status and data
assert response.status_code == status.HTTP_200_OK
# assert response.json()["status"] == constants.EDIT_ACCEPTED
assert response.json()["status"] == constants.EDIT_ACCEPTED


async def test_edit_create_auto_bad_permission(
Expand Down
48 changes: 48 additions & 0 deletions tests/edit/test_edit_update_auto.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from client_requests import request_create_edit
from client_requests import request_update_edit
from fastapi import status
from app import constants
import asyncio


async def test_edit_update_auto(
client,
aggregator_anime,
aggregator_anime_info,
create_test_user_moderator,
get_test_token,
test_session,
):
# Create edit for anime
response = await request_create_edit(
client,
get_test_token,
"anime",
"bocchi-the-rock-9e172d",
{
"description": "Brief description",
"after": {"title_en": "Bocchi The Rock!"},
},
)

# Check status
assert response.status_code == status.HTTP_200_OK
assert response.json()["created"] == response.json()["updated"]

# Simulate delay between create/update
await asyncio.sleep(1)

# Update created edit
response = await request_update_edit(
client,
get_test_token,
18,
{
"description": "Brief description 2",
"after": {"title_en": "Bocchi The Rock!"},
"auto": True,
},
)

assert response.status_code == status.HTTP_200_OK
assert response.json()["status"] == constants.EDIT_ACCEPTED
2 changes: 1 addition & 1 deletion tests/manga/test_manga_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async def test_manga_info(
assert len(response.json()["external"]) == 4
assert len(response.json()["genres"]) == 6

assert response.json()["translated_ua"] is False
assert response.json()["translated_ua"] is True
assert response.json()["stats"] == {
"dropped": 6393,
"on_hold": 16941,
Expand Down

0 comments on commit 296f98b

Please sign in to comment.