-
Notifications
You must be signed in to change notification settings - Fork 218
/
Copy pathmedia.py
562 lines (454 loc) · 16.9 KB
/
media.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
import mimetypes
from textwrap import dedent
from django.conf import settings
from django.core.exceptions import ValidationError
from django.db import models
from django.urls import reverse
from django.utils.html import format_html
import structlog
from elasticsearch import Elasticsearch, NotFoundError
from openverse_attribution.license import License
from api.constants.moderation import DecisionAction
from api.models.base import OpenLedgerModel
from api.models.mixins import ForeignIdentifierMixin, IdentifierMixin, MediaMixin
PENDING = "pending_review"
MATURE_FILTERED = "mature_filtered"
DEINDEXED = "deindexed"
NO_ACTION = "no_action"
MATURE = "mature"
DMCA = "dmca"
OTHER = "other"
logger = structlog.get_logger(__name__)
class AbstractMedia(
IdentifierMixin, ForeignIdentifierMixin, MediaMixin, OpenLedgerModel
):
"""
Generic model from which to inherit all media classes.
This class stores information common to all media types indexed by Openverse.
Properties
==========
id: >-
This is Django's automatic primary key, used for models that do not
define one explicitly.
"""
watermarked = models.BooleanField(
blank=True,
null=True,
help_text="Whether the media contains a watermark. Not currently leveraged.",
)
license = models.CharField(
max_length=50,
help_text="The name of license for the media.",
)
license_version = models.CharField(
max_length=25,
blank=True,
null=True,
help_text="The version of the media license.",
)
source = models.CharField(
max_length=80,
blank=True,
null=True,
db_index=True,
help_text="The source of the data, meaning a particular dataset. "
"Source and provider can be different. Eg: the Google Open "
"Images dataset is source=openimages, but provider=flickr.",
)
last_synced_with_source = models.DateTimeField(
blank=True,
null=True,
db_index=True,
help_text="The date the media was last updated from the upstream source.",
)
removed_from_source = models.BooleanField(
default=False,
help_text="Whether the media has been removed from the upstream source.",
)
view_count = models.IntegerField(
blank=True, null=True, default=0, help_text="Vestigial field, purpose unknown."
)
tags = models.JSONField(
blank=True,
null=True,
help_text=dedent("""
JSON array of objects containing tags for the media. Each tag object
is expected to have:
- `name`: The tag itself (e.g. "dog")
- `provider`: The source of the tag
- `accuracy`: If the tag was added using a machine-labeler, the confidence
for that label expressed as a value between 0 and 1.
Note that only `name` and `accuracy` are presently surfaced in API results.
"""),
)
category = models.CharField(
max_length=80,
blank=True,
null=True,
db_index=True,
help_text="The top-level classification of this media file.",
)
meta_data = models.JSONField(
blank=True,
null=True,
help_text=dedent("""
JSON object containing extra data about the media item. No fields are expected,
but if the `license_url` field is available, it will be used for determining
the license URL for the media item. The `description` field, if available, is
also indexed into Elasticsearch and as a search field on queries.
"""),
)
@property
def license_url(self) -> str | None:
"""A direct link to the license deed or legal terms."""
if self.meta_data and (url := self.meta_data.get("license_url")):
return url
logger.warning(
"Media item missing `license_url` in `meta_data`",
media_class=self.__class__.__name__,
identifier=self.identifier,
)
try:
return License(self.license.lower(), self.license_version).url
except ValueError:
return None
@property
def attribution(self) -> str | None:
"""Legally valid attribution for the media item in plain-text English."""
try:
return License(
self.license.lower(),
self.license_version,
).get_attribution_text(
self.title,
self.creator,
self.license_url,
)
except ValueError:
return None
class Meta:
"""
Meta class for all media types indexed by Openverse.
All concrete media classes should inherit their Meta class from this.
"""
ordering = ["-created_on"]
abstract = True
constraints = [
models.UniqueConstraint(
fields=["foreign_identifier", "provider"],
name="unique_provider_%(class)s", # populated by concrete model
),
]
def __str__(self):
"""
Return the string representation of the model, used in the Django admin site.
:return: the string representation of the model
"""
return f"{self.__class__.__name__}: {self.identifier}"
class AbstractMediaReport(models.Model):
"""
Generic model from which to inherit all reported media classes.
'Reported' here refers to content reports such as sensitive, copyright-violating or
deleted content. Subclasses must populate the field ``media_class``.
"""
media_class: type[models.Model] = None
"""the model class associated with this media type e.g. ``Image`` or ``Audio``"""
REPORT_CHOICES = [(MATURE, MATURE), (DMCA, DMCA), (OTHER, OTHER)]
STATUS_CHOICES = [
(PENDING, PENDING),
(MATURE_FILTERED, MATURE_FILTERED),
(DEINDEXED, DEINDEXED),
(NO_ACTION, NO_ACTION),
]
created_at = models.DateTimeField(auto_now_add=True)
media_obj = models.ForeignKey(
to="AbstractMedia",
to_field="identifier",
on_delete=models.DO_NOTHING,
db_constraint=False,
db_column="identifier",
related_name="abstract_media_report",
help_text="The reference to the media being reported.",
)
"""
There can be many reports associated with a single media item, hence foreign key.
Sub-classes must override this field to point to a concrete sub-class of
``AbstractMedia``.
"""
reason = models.CharField(
max_length=20,
choices=REPORT_CHOICES,
help_text="The reason to report media to Openverse.",
)
description = models.TextField(
max_length=500,
blank=True,
null=True,
help_text="The explanation on why media is being reported.",
)
status = models.CharField(max_length=20, choices=STATUS_CHOICES, default=PENDING)
"""
All statuses except ``PENDING`` are deprecated. Instead refer to the
property ``is_pending``.
"""
decision = models.ForeignKey(
to="AbstractMediaDecision",
on_delete=models.SET_NULL,
blank=True,
null=True,
help_text="The moderation decision for this report.",
)
class Meta:
abstract = True
def clean(self):
"""Clean fields and raise errors that can be handled by Django Admin."""
if not self.media_class.objects.filter(identifier=self.media_obj_id).exists():
raise ValidationError(
f"No '{self.media_class.__name__}' instance "
f"with identifier '{self.media_obj_id}'."
)
def media_url(self, request=None) -> str:
"""
Build the URL of the media item. This uses ``reverse`` and
``request.build_absolute_uri`` to build the URL without having to worry
about canonical URL or trailing slashes.
:param request: the current request object, to get absolute URLs
:return: the URL of the media item
"""
url = reverse(
f"{self.media_class.__name__.lower()}-detail",
args=[self.media_obj_id],
)
if request is not None:
url = request.build_absolute_uri(url)
return url
def url(self, request=None) -> str:
url = self.media_url(request)
return format_html(f"<a href={url}>{url}</a>")
@property
def is_pending(self) -> bool:
"""
Determine if the report has not been moderated and does not have an
associated decision. Use the inverse of this function to determine
if a report has been reviewed and moderated.
:return: whether the report is in the "pending" state
"""
return self.decision_id is None
def save(self, *args, **kwargs):
"""Perform a clean, and then save changes to the DB."""
self.clean()
super().save(*args, **kwargs)
class AbstractMediaDecision(OpenLedgerModel):
"""Generic model from which to inherit all moderation decision classes."""
media_class: type[models.Model] = None
"""the model class associated with this media type e.g. ``Image`` or ``Audio``"""
moderator = models.ForeignKey(
to="auth.User",
on_delete=models.DO_NOTHING,
help_text="The moderator who undertook this decision.",
)
"""
The ``User`` referenced by this field must be a part of the moderators'
group.
"""
media_objs = models.ManyToManyField(
to="AbstractMedia",
through="AbstractMediaDecisionThrough",
help_text="The media items being moderated.",
)
"""
This is a many-to-many relation, using a bridge table, to enable bulk
moderation which applies a single action to more than one media items.
"""
notes = models.TextField(
max_length=500,
blank=True,
null=True,
help_text="The moderator's explanation for the decision or additional notes.",
)
action = models.CharField(
max_length=32,
choices=DecisionAction.choices,
help_text="Action taken by the moderator.",
)
class Meta:
abstract = True
# TODO: Implement ``clean`` and ``save``, if needed.
class AbstractMediaDecisionThrough(models.Model):
"""
Generic model for the many-to-many reference table between media and decisions.
This is made explicit (rather than using Django's default) so that the media can
be referenced by `identifier` rather than an arbitrary `id`.
"""
media_class: type[models.Model] = None
"""the model class associated with this media type e.g. ``Image`` or ``Audio``"""
media_obj = models.ForeignKey(
AbstractMedia,
to_field="identifier",
on_delete=models.CASCADE,
db_column="identifier",
)
decision = models.ForeignKey(AbstractMediaDecision, on_delete=models.CASCADE)
class Meta:
abstract = True
class PerformIndexUpdateMixin:
@property
def indexes(self):
return [self.es_index, f"{self.es_index}-filtered"]
def _perform_index_update(self, method: str, raise_errors: bool, **es_method_args):
"""
Call ``method`` on the Elasticsearch client.
Automatically handles ``DoesNotExist`` warnings, forces a refresh,
and calls the method for origin and filtered indexes.
"""
es: Elasticsearch = settings.ES
try:
document_id = self.media_obj.id
except self.media_class.DoesNotExist:
if raise_errors:
raise ValidationError(
f"No '{self.media_class.__name__}' instance "
f"with identifier {self.media_obj.identifier}."
)
for index in self.indexes:
try:
getattr(es, method)(
index=index,
id=document_id,
refresh=True,
**es_method_args,
)
except NotFoundError:
# This is expected for the filtered index, but we should still
# log, just in case.
logger.warning(
f"Document with _id {document_id} not found "
f"in {index} index. No update performed."
)
continue
class AbstractDeletedMedia(PerformIndexUpdateMixin, OpenLedgerModel):
"""
Generic model from which to inherit all deleted media classes.
'Deleted' here refers to media which has been deleted at the source or intentionally
de-indexed by us. Unlike sensitive reports, this action is irreversible. Subclasses
must populate ``media_class`` and ``es_index`` fields.
"""
media_class: type[models.Model] = None
"""the model class associated with this media type e.g. ``Image`` or ``Audio``"""
es_index: str = None
"""the name of the ES index from ``settings.MEDIA_INDEX_MAPPING``"""
media_obj = models.OneToOneField(
to="AbstractMedia",
to_field="identifier",
on_delete=models.DO_NOTHING,
primary_key=True,
db_constraint=False,
db_column="identifier",
related_name="deleted_abstract_media",
help_text="The reference to the deleted media.",
)
"""
Sub-classes must override this field to point to a concrete sub-class of
``AbstractMedia``.
"""
class Meta:
abstract = True
def _update_es(self, raise_errors: bool) -> models.Model:
self._perform_index_update(
"delete",
raise_errors,
)
def save(self, *args, **kwargs):
self._update_es(True)
super().save(*args, **kwargs)
self.media_obj.delete() # remove the actual model instance
class AbstractSensitiveMedia(PerformIndexUpdateMixin, models.Model):
"""
Generic model from which to inherit all sensitive media classes.
Subclasses must populate ``media_class`` and ``es_index`` fields.
"""
media_class: type[models.Model] = None
"""the model class associated with this media type e.g. ``Image`` or ``Audio``"""
es_index: str = None
"""the name of the ES index from ``settings.MEDIA_INDEX_MAPPING``"""
created_on = models.DateTimeField(auto_now_add=True)
media_obj = models.OneToOneField(
to="AbstractMedia",
to_field="identifier",
on_delete=models.DO_NOTHING,
primary_key=True,
db_constraint=False,
db_column="identifier",
related_name="sensitive_abstract_media",
help_text="The reference to the sensitive media.",
)
"""
Sub-classes must override this field to point to a concrete sub-class of
``AbstractMedia``.
"""
class Meta:
abstract = True
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 save(self, *args, **kwargs):
self._update_es(True, True)
super().save(*args, **kwargs)
def delete(self, *args, **kwargs):
self._update_es(False, False)
super().delete(*args, **kwargs)
class AbstractMediaList(OpenLedgerModel):
"""
Generic model from which to inherit media lists.
Each subclass should define its own `ManyToManyField` to point to a subclass of
`AbstractMedia`.
"""
title = models.CharField(max_length=2000, help_text="Display name")
slug = models.CharField(
max_length=200,
help_text="A unique identifier used to make a friendly URL for "
"downstream API consumers.",
unique=True,
db_index=True,
)
auth = models.CharField(
max_length=64,
help_text="A randomly generated string assigned upon list creation. "
"Used to authenticate updates and deletions.",
)
class Meta:
abstract = True
class AbstractAltFile:
"""
This is not a Django model.
This Python class serves as the schema for an alternative file. An alt file
provides alternative qualities, formats and resolutions that are available
from the provider that are not canonical.
The schema of the class must correspond to that of the
:py:class:`api.models.mixins.FileMixin` class.
"""
def __init__(self, attrs):
self.url = attrs.get("url")
self.filesize = attrs.get("filesize")
self.filetype = attrs.get("filetype")
@property
def size_in_mib(self): # ~ MiB or mibibytes
return self.filesize / 2**20
@property
def size_in_mb(self): # ~ MB or megabytes
return self.filesize / 1e6
@property
def mime_type(self):
"""
Get the MIME type of the file inferred from the extension of the file.
:return: the inferred MIME type of the file
"""
return mimetypes.types_map[f".{self.filetype}"]