Skip to content

Commit

Permalink
moderation: fix percolators index naming and CLI usage
Browse files Browse the repository at this point in the history
  • Loading branch information
slint committed Nov 11, 2024
1 parent 660b28a commit dcc93f9
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 26 deletions.
13 changes: 8 additions & 5 deletions site/zenodo_rdm/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"""Zenodo RDM cli commands."""

import csv
import os

import click
from flask.cli import with_appcontext
Expand Down Expand Up @@ -261,11 +260,16 @@ def delete_record(recid):


@click.group()
def moderation():
def moderation_cli():
"""Moderation commands."""


@moderation.command("create-queries-index")
@moderation_cli.group()
def queries_cli():
"""Moderation queries commands."""


@queries_cli.command("create-index")
@click.option(
"-r",
"--record-cls",
Expand All @@ -286,7 +290,7 @@ def create_index(record_cls):
click.secho(f"Error creating percolator index: {e}")


@moderation.command("add-query")
@queries_cli.command("add")
@click.option(
"-r",
"--record-cls",
Expand All @@ -302,7 +306,6 @@ def create_index(record_cls):
@click.option(
"-n",
"--notes",
default="Example note",
help="Additional notes for the moderation query (optional if loading from CSV).",
)
@click.option(
Expand Down
25 changes: 11 additions & 14 deletions site/zenodo_rdm/moderation/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ def exempt_users(self):
def run(self, identity, draft=None, record=None, user=None, uow=None):
"""Calculate the moderation score for a given record or draft."""
try:
if user.id in self.exempt_users:
current_app.logger.debug(
"User is exempt from moderation", extra={"user_id": user.id}
)
return

score = 0
for rule in self.rules:
score += rule(identity, draft=draft, record=record)
Expand Down Expand Up @@ -122,6 +128,10 @@ def run(self, identity, draft=None, record=None, user=None, uow=None):
"Manual moderation action triggered",
extra=action_ctx,
)
# Re-raise UserBlockedException to prevent further processing of the record
except UserBlockedException:
raise
# Failsafe in the moderation check is faulty
except Exception:
current_app.logger.exception("Error calculating moderation score")

Expand Down Expand Up @@ -196,13 +206,6 @@ def publish(self, identity, draft=None, record=None, uow=None, **kwargs):
"No user found for moderation action", stack_info=True
)
return

if user_id in self.exempt_users:
current_app.logger.info(
"User is exempt from moderation", extra={"user_id": user_id}
)
return

user = UserAggregate.get_record(user_id)

# Perform the moderation checks asynchronously for verified users
Expand All @@ -228,13 +231,7 @@ def __init__(self):

def _run(self, identity, record, uow):
"""Run the moderation scoring."""
user_id = identity.id
if user_id in self.exempt_users:
current_app.logger.info(
"User is exempt from moderation", extra={"user_id": user_id}
)
return
user = UserAggregate.get_record(user_id)
user = UserAggregate.get_record(identity.id)

# Perform the moderation checks asynchronously for verified users
if user.verified:
Expand Down
5 changes: 2 additions & 3 deletions site/zenodo_rdm/moderation/percolator.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@

"""Percolator."""


from flask import current_app
from invenio_search import current_search_client
from invenio_search.utils import build_alias_name, build_index_name
from invenio_search.utils import build_alias_name


def get_percolator_index(record_cls):
Expand All @@ -30,7 +29,7 @@ def create_percolator_index(record_cls):
"""
# Build the name for the new percolator index, using a prefix and the record's index name
combined_index_name = f"{current_app.config.get('MODERATION_PERCOLATOR_INDEX_PREFIX')}-{record_cls.index._name}"
percolator_index = build_index_name(combined_index_name, app=current_app)
percolator_index = build_alias_name(combined_index_name)

# Get the current mapping for the record index to copy its structure
record_index = build_alias_name(record_cls.index._name)
Expand Down
20 changes: 16 additions & 4 deletions site/zenodo_rdm/moderation/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@

@shared_task(ignore_result=True)
def update_moderation_request(user_id, action_ctx):
"""(Create and) update a moderation request."""
"""Update a moderation request from an automated moderation action.
This task is called by moderation handlers to log the results of an
automated moderation action on the user moderation request. If no request
exists for the user, a new one is created.
"""
# Find the moderation request for the user (even if it's closed)
results = requests_service.search(
system_identity,
Expand Down Expand Up @@ -71,10 +76,17 @@ def update_moderation_request(user_id, action_ctx):
)
req: Request = requests_service.record_cls.get_record(request_id)

# If the request is closed, reopen it
if req.is_closed:
# If the request is closed or we want to moderate again, reopen it...
if req.is_closed or action_ctx["action"] == "moderate":
req.status = "submitted"
uow.register(RecordCommitOp(req, indexer=requests_service.indexer))
else:
# ...otherwise update the request based on the action
if action_ctx["action"] == "block":
req.status = "declined"
elif action_ctx["action"] == "approve":
req.status = "accepted"

uow.register(RecordCommitOp(req, indexer=requests_service.indexer))

# TODO: Generate content/links in a cleaner way
# Add a comment with the moderation context (score, etc.)
Expand Down

0 comments on commit dcc93f9

Please sign in to comment.