Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alter Django Admin media views to surface content reports #4254

Merged
merged 13 commits into from
May 7, 2024

Conversation

AetherUnbound
Copy link
Contributor

@AetherUnbound AetherUnbound commented May 2, 2024

Fixes

Fixes #3635 by @sarayourfriend

Description

This PR modifies the base media list views to accomplish the following:

  • Exclude media items that have no reports
  • Add columns for total report count, pending report count, oldest report date, and pending report links
  • Automatically filter by records with pending reports, but allow viewing all reported records

This work was admittedly pretty hairy 😅 I'm hoping that the changes I've made here make sense, I'm certainly pleased that I was able to get all the functionality set up correctly! I'm happy to make anything more idiomatically "Django" where it makes sense to.

I also exposed the decision views so we could more easily add decisions for media, at least for testing. I'm sure that will change, and I'm honestly a little unclear on all of the moving parts here and how decisions will actually be applied. This view currently links to each individual report, but since decisions can be applied to multiple reports and multiple media records, I'm just not sure what all the actions will look like. That to say, this view might change a little once that's built out, which is okay!

Screenshots

Pending only filtered (default)
Screenshot 2024-05-01_17-01-53

All reported records
Screenshot 2024-05-01_17-02-02

Testing Instructions

The easiest way to do this is the following:

  1. Run just down -v && just api/init
  2. Run just api/dbshell and execute the following:
COPY public.api_imagedecision (id, created_on, updated_on, notes, action, moderator_id) FROM stdin;
1	2024-05-01 23:27:09.884471+00	2024-05-01 23:27:09.884487+00		rejected_reports	3
2	2024-05-01 23:32:51.386486+00	2024-05-01 23:32:51.386501+00		marked_sensitive	3
\.


COPY public.api_imagedecision_media_objs (id, imagedecision_id, image_id) FROM stdin;
1	1	893
2	2	111
\.


COPY public.nsfw_reports (id, identifier, reason, description, status, created_at, decision_id) FROM stdin;
1	2e22c7e7-58b7-486c-9df8-16e84c27478f	mature		pending_review	2024-04-26 20:44:31.271278+00	\N
2	2e22c7e7-58b7-486c-9df8-16e84c27478f	dmca		pending_review	2024-04-26 20:44:36.178746+00	\N
4	2e22c7e7-58b7-486c-9df8-16e84c27478f	mature		pending_review	2024-04-26 20:44:46.869982+00	\N
5	c296b69b-c8d7-4fc3-b820-a83de6138e0e	dmca		pending_review	2024-04-26 20:44:52.034395+00	\N
6	de37812d-2daa-49f6-91ab-e87e780ca653	dmca	asdfsadfsadfsadfsadfsadf	pending_review	2024-04-26 20:45:31.272964+00	\N
7	de37812d-2daa-49f6-91ab-e87e780ca653	dmca		pending_review	2024-05-01 21:12:53.177529+00	\N
8	c296b69b-c8d7-4fc3-b820-a83de6138e0e	dmca		pending_review	2024-05-01 21:21:10.442789+00	\N
9	ba711b67-2cbe-487a-9feb-19d70accf912	dmca		pending_review	2024-05-01 21:28:45.869087+00	\N
3	2e22c7e7-58b7-486c-9df8-16e84c27478f	other		pending_review	2024-04-26 20:44:42.274724+00	1
10	052d2cfc-fdcb-40de-994c-40eadef753fc	dmca		pending_review	2024-05-01 21:31:04.020163+00	2
\.

Then you should be able to go to http://localhost:50280/admin/api/image/ and see the views in the screenshots above!

The actual image_ids in the api_imagedecision_media_objs table might be non-deterministic, I'm not sure. It makes me wonder too if we should alter that table to use identifier in its table reference rather than {media}_id. It's what we do for the sensitive/deleted objects tables as well: https://github.com/WordPress/openverse/blob/0db784ebc4d4937c925dcb14c1465bafb3e20c69/api/api/models/image.py#L97-L96. CC @dhruvkb as you might be the best informed on that.

Checklist

  • My pull request has a descriptive title (not a vague title likeUpdate index.md).
  • My pull request targets the default branch of the repository (main) or a parent feature branch.
  • My commit messages follow best practices.
  • My code follows the established code style of the repository.
  • I added or updated tests for the changes I made (if applicable).
  • I added or updated documentation (if applicable).
  • I tried running the project locally and verified that there are no visible errors.
  • I ran the DAG documentation generator (just catalog/generate-docs for catalog
    PRs) or the media properties generator (just catalog/generate-docs media-props
    for the catalog or just api/generate-docs for the API) where applicable.

Developer Certificate of Origin

Developer Certificate of Origin
Developer Certificate of Origin
Version 1.1

Copyright (C) 2004, 2006 The Linux Foundation and its contributors.
1 Letterman Drive
Suite D4700
San Francisco, CA, 94129

Everyone is permitted to copy and distribute verbatim copies of this
license document, but changing it is not allowed.


Developer's Certificate of Origin 1.1

By making a contribution to this project, I certify that:

(a) The contribution was created in whole or in part by me and I
    have the right to submit it under the open source license
    indicated in the file; or

(b) The contribution is based upon previous work that, to the best
    of my knowledge, is covered under an appropriate open source
    license and I have the right under that license to submit that
    work with modifications, whether created in whole or in part
    by me, under the same open source license (unless I am
    permitted to submit under a different license), as indicated
    in the file; or

(c) The contribution was provided directly to me by some other
    person who certified (a), (b) or (c) and I have not modified
    it.

(d) I understand and agree that this project and the contribution
    are public and that a record of the contribution (including all
    personal information I submit with it, including my sign-off) is
    maintained indefinitely and may be redistributed consistent with
    this project or the open source license(s) involved.

@AetherUnbound AetherUnbound requested a review from a team as a code owner May 2, 2024 00:21
@github-actions github-actions bot added the 🧱 stack: api Related to the Django API label May 2, 2024
@openverse-bot openverse-bot added 🟨 priority: medium Not blocking but should be addressed soon ✨ goal: improvement Improvement to an existing user-facing feature 💻 aspect: code Concerns the software code in the repository labels May 2, 2024
@sarayourfriend
Copy link
Contributor

The actual image_ids in the api_imagedecision_media_objs table might be non-deterministic, I'm not sure. It makes me wonder too if we should alter that table to use identifier in its table reference rather than {media}_id. It's what we do for the sensitive/deleted objects tables as well: https://github.com/WordPress/openverse/blob/0db784ebc4d4937c925dcb14c1465bafb3e20c69/api/api/models/image.py#L97-L96. CC @dhruvkb as you might be the best informed on that.

Yes, this needs to be to the identifier, not the id, otherwise you're right it will drift. This was missed as a requirement in the IP, though we should treat it as an assumed requirement. It's really frustrating to have these differences, I wish we could find a way to not have two identifiers, especially one that is ephemeral (kind of defeats the purpose of an identifier, really).

The fix is a two line change to add to_field on each media type's Decision model set to identifier:

diff --git a/api/api/models/audio.py b/api/api/models/audio.py
index 917be115f..163bb4d58 100644
--- a/api/api/models/audio.py
+++ b/api/api/models/audio.py
@@ -325,6 +325,7 @@ class AudioDecision(AbstractMediaDecision):
 
     media_objs = models.ManyToManyField(
         to="Audio",
+        to_field="identifier",
         db_constraint=False,
         help_text="The audio items being moderated.",
     )
diff --git a/api/api/models/image.py b/api/api/models/image.py
index d0ad5323a..2f349ab29 100644
--- a/api/api/models/image.py
+++ b/api/api/models/image.py
@@ -138,6 +138,7 @@ class ImageDecision(AbstractMediaDecision):
 
     media_objs = models.ManyToManyField(
         to="Image",
+        to_field="identifier",
         db_constraint=False,
         help_text="The image items being moderated.",
     )

Best to either add that in this PR or open an issue as a fast follow.

Similarly, (and this is definintely for a separate issue), it'd be nice to set related_name on all of these report, decision, etc, so that they use the same name for each model, excluding the media type. That would make things like this unncessary:

            - Count(f"{self.media_type}_report__decision__pk")

That would just need to be Count("report__decision__pk") if related_name="reports", related_query_name="report" was set on each media type's implementation. Neither would cause a SQL migration, as far as I am aware (these are virtual fields) but would make writing isomorphic functions for all of this stuff a lot more straightforward (no need to pass in media_type everywhere in this code).

Copy link
Contributor

@sarayourfriend sarayourfriend left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This LGTM as is. I've left a comment describing the fix for the identifier issue you found. Either add that to this PR or create an issue as a fast-follow. Fixing that will block rolling the feature out, and it should be fixed as soon as possible to avoid introducing more code that will reference the incorrect identifier in queries.

api/api/admin/media_report.py Outdated Show resolved Hide resolved
api/api/admin/media_report.py Outdated Show resolved Hide resolved
api/api/admin/media_report.py Show resolved Hide resolved
api/api/admin/media_report.py Show resolved Hide resolved
Copy link
Member

@dhruvkb dhruvkb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, seconding @sarayourfriend's comments about using identifier field instead of id.

@AetherUnbound AetherUnbound force-pushed the feature/content-report-admin-tables branch from 74c2fed to 3337b46 Compare May 7, 2024 21:22
@AetherUnbound
Copy link
Contributor Author

@sarayourfriend It looks like to_field is not available for Fields:

web-1  |   File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
web-1  |   File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
web-1  |   File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
web-1  |   File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
web-1  |   File "<frozen importlib._bootstrap_external>", line 940, in exec_module
web-1  |   File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
web-1  |   File "/api/api/models/__init__.py", line 2, in <module>
web-1  |     from api.models.audio import (
web-1  |   File "/api/api/models/audio.py", line 321, in <module>
web-1  |     class AudioDecision(AbstractMediaDecision):
web-1  |   File "/api/api/models/audio.py", line 326, in AudioDecision
web-1  |     media_objs = models.ManyToManyField(
web-1  |                  ^^^^^^^^^^^^^^^^^^^^^^^
web-1  |   File "/.venv/lib/python3.11/site-packages/django/db/models/fields/related.py", line 1370, in __init__
web-1  |     super().__init__(
web-1  |   File "/.venv/lib/python3.11/site-packages/django/db/models/fields/related.py", line 108, in __init__
web-1  |     super().__init__(**kwargs)
web-1  | TypeError: Field.__init__() got an unexpected keyword argument 'to_field'

Since this is approved and the way to get reference fields working correctly isn't immediately obvious to me, I'm going to go ahead and merge this and create an issue for following up on the field name.

@AetherUnbound AetherUnbound merged commit 0a7d31b into main May 7, 2024
45 checks passed
@AetherUnbound AetherUnbound deleted the feature/content-report-admin-tables branch May 7, 2024 21:33
obulat pushed a commit that referenced this pull request May 8, 2024
* Use a more ambiguous related name for reports

* Change the media views to order records by total report count

* Use plural name for media_reports reference

* Let media ID be used for autocomplete, and return unaltered queryset

* Add more fields to sort on

* Add links to the pending reports in a column

* Rename to "total report count"

* Expose the decisions tables (for now)

* Add filtering based on pending reports

* More tweaks to table

* Make pending the default filter rather than all

* Revert back to media-specific tables

* Wording and whitespace corrections

Co-authored-by: sarayourfriend <git@sarayourfriend.pictures>

---------

Co-authored-by: sarayourfriend <git@sarayourfriend.pictures>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💻 aspect: code Concerns the software code in the repository ✨ goal: improvement Improvement to an existing user-facing feature 🟨 priority: medium Not blocking but should be addressed soon 🧱 stack: api Related to the Django API
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Create new content report Django admin table view
4 participants