-
Notifications
You must be signed in to change notification settings - Fork 213
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
Add new SensitiveMedia and MediaReport models #3510
Conversation
Signed-off-by: Olga Bulat <obulat@gmail.com>
77dc679
to
fb07787
Compare
Signed-off-by: Olga Bulat <obulat@gmail.com>
This PR has migrations. Please rebase it before merging to ensure that conflicting migrations are not introduced. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@obulat Can you recreate the migrations to see if it can be in a single migration rather than two? It is hard to keep track of what is actually changing between the two migrations.
I also think it might be easier to reivew this PR if it was split into two separate PRs: first to just add the new models/update references; second to actually implement the new writing to the new models.
Seeing how complex it is to juggle the table rename, I wonder if it's worth renaming the table, instead of just using a view to give an alias with the new name. @AetherUnbound @obulat what do y'all think? It would massively simplify this PR and the later data migration work because we'd just need to add the new sensitive-based choices and transform them in place in the same table, rather than copy data between the new tables and juggle the reading/writing changes between the old and new model.
I think the goal here was just to get rid of nsfw
from the table name, but an alias would make it possible to use a consistent name when querying the API databases directly without all of this massive amount of complexity.
|
||
|
||
class MatureImageFactory(DjangoModelFactory): | ||
class SensitiveImageFactory(DjangoModelFactory): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The old factories should probably stay around until we've removed the model entirely. Tests can be updated to use the new factory, but the new factory should create the old model in the create
classmethod, so that tests reflect the actual behaviour of these models.
Alternatively, it'd be better to update the tests to create both as needed to reflect the actual business logic (even if it's temporary).
REPORT_CHOICES = [ | ||
(SENSITIVE, SENSITIVE), | ||
(DMCA, DMCA), | ||
(OTHER, OTHER), | ||
] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These can't be changed like this, it can cause downtime. After the canary applies the migration to change the model, the old code would try to write "mature" rather than "sensitive".
The temporary Nsfw
models that represent the existing tables need to override this (and STATUS_CHOICES
) to use mature
rather than sensitive
.
The new models should use sensitive
though. The management command later on will transform the existing table data into the new version.
""" | ||
|
||
self.clean() | ||
|
||
super().save(*args, **kwargs) | ||
|
||
if self.status == MATURE_FILTERED: | ||
if self.status == SENSITIVE_FILTERED: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if self.status == SENSITIVE_FILTERED: | |
if self.status in [SENSITIVE_FILTERED, MATURE_FILTERED]: |
This can safely handle both without the implementing class needing to override it.
def _update_es(self, is_mature: bool, raise_errors: bool): | ||
""" | ||
Update the Elasticsearch document associated with the given model. | ||
|
||
:param is_mature: whether to mark the media item as mature | ||
:param raise_errors: whether to raise an error if the no media item is found | ||
""" | ||
self._perform_index_update( | ||
"update", | ||
raise_errors, | ||
doc={"mature": is_mature}, | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose we can update these variable names as well for the new model, if we want:
def _update_es(self, is_mature: bool, raise_errors: bool): | |
""" | |
Update the Elasticsearch document associated with the given model. | |
:param is_mature: whether to mark the media item as mature | |
:param raise_errors: whether to raise an error if the no media item is found | |
""" | |
self._perform_index_update( | |
"update", | |
raise_errors, | |
doc={"mature": is_mature}, | |
) | |
def _update_es(self, is_sensitive: bool, raise_errors: bool): | |
""" | |
Update the Elasticsearch document associated with the given model. | |
Note: The Elasticsearch data model still uses `mature` rather than `sensitive`. | |
:param is_sensitive: whether to mark the media item as sensitive | |
:param raise_errors: whether to raise an error if the no media item is found | |
""" | |
self._perform_index_update( | |
"update", | |
raise_errors, | |
doc={"mature": is_sensitive}, | |
) |
primary_key=True, | ||
db_constraint=False, | ||
db_column="identifier", | ||
related_name="sensitive_abstract_media", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new related name is the reason for the additional query. It should get added to the get_queryset
method of the media views: https://github.com//WordPress/openverse/tree/main/api/api/views/image_views.py#L65-L66. Otherwise, whenever sensitive_media
is accessed on the model instance Django automatically does a query in the background to populate sensitive_media
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, @sarayourfriend!
return super().url("images") | ||
|
||
|
||
class NsfwReport(AbstractMediaReport): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know the IP says to use NsfwReport
for this but I think it would be clearer to use NsfwImageReport
instead for the temporary name, like we do for the other media-specific models.
Closing this PR because we decided that changing the table names is too much work for too little benefit. |
Was this decided between you @obulat and @sarayourfriend? If I recall correctly, the team decided to make the table name change with the mature -> sensitive text changes. Edit: Nevermind. I read the document with the stated reasons, and even though I really don't like the disparity in names, I understand the complexity 👍 |
Fixes
Fixes #2626 by @AetherUnbound
Description
This PR adds the models as described in the Steps 1 and 2 of the Implementation Plan/ API
@WordPress/openverse-api , I need help figuring out why these changes cause one sensitive table db query per result, both for search and for the single result requests:
Sample search query
``` E 1. SELECT "image"."id", "image"."created_on", "image"."updated_on", "image"."identifier", "image"."foreign_identifier", "image"."title", "image"."foreign_landing_url", "image"."creator", "image"."creator_url", "image"."thumbnail", "image"."provider", "image"."url", "image"."filesize", "image"."filetype", "image"."watermarked", "image"."license", "image"."license_version", "image"."source", "image"."last_synced_with_source", "image"."removed_from_source", "image"."view_count", "image"."tags", "image"."category", "image"."meta_data", "image"."width", "image"."height", "api_matureimage"."created_on", "api_matureimage"."identifier" FROM "image" LEFT OUTER JOIN "api_matureimage" ON ("image"."identifier" = "api_matureimage"."identifier") WHERE (NOT ("image"."provider" IN (SELECT U0."provider_identifier" FROM "content_provider" U0 WHERE U0."filter_content") AND "image"."provider" IS NOT NULL) AND "image"."identifier" IN ('6b7d6870ab734fe7b187d5509f408b48'::uuid, 'f890597b52c8432fbd86a8f4362182af'::uuid, '5aa22382cc77481eafb88693b304a44e'::uuid, '676777e3bb9c432eb8c80ce27f6a639b'::uuid, '4657177f42764b379e9cfc2e88b6b996'::uuid, '32a1ccf579d84eafb974045d93ea769e'::uuid, '69a7b7fda0c9497e8ec7f3a2177298fd'::uuid, 'caefbd828ae145e2b5f5d901778198f4'::uuid, '06f5b2e2f2c546e789e5d7c2fe2330f6'::uuid, '467d9209e90a4f469447d4511ef00bdf'::uuid, '475ead8a9fa4481c999c101b8407bd30'::uuid, 'cb64afe904864da8a5c46714bf72b02d'::uuid, '9c6571bf310740b6baab3a7e170ae301'::uuid, '6af6e4ca356c4c828047a3d0fbede4bb'::uuid, '8ca3fb5ed56747c9abc7e9c4b7374c92'::uuid, 'c2020b32f0c04b20a714a74a3750d8e2'::uuid, 'acabd6bf5a774289a587918dac0eb66b'::uuid, 'ea4edd319f974cdaa02a43d85bce6674'::uuid, '0f5bf191878b45588674f22a210a80b0'::uuid, '798eea993dd74aa5a9a92acfd281a573'::uuid)) ORDER BY "image"."created_on" DESC E 2. SELECT "api_sensitiveimage"."created_on", "api_sensitiveimage"."identifier" FROM "api_sensitiveimage" WHERE "api_sensitiveimage"."identifier" = '6b7d6870ab734fe7b187d5509f408b48'::uuid LIMIT 21 ```Testing Instructions
main
branch and mark several items asmature
in the admin (http://localhost:50280/admin, userdeploy
, passworddeploy
). I used a search for "Couple Food Photo" and marked a couple of results as sensitive.just api/dj migrate
. This should create the new db tables.Checklist
Update index.md
).main
) or a parent feature branch.Developer Certificate of Origin
Developer Certificate of Origin