Skip to content

Commit

Permalink
Delete ov-attribution code and delete redundant utils from the API
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvkb committed Apr 17, 2024
1 parent 5bad012 commit 05d32e0
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 238 deletions.
8 changes: 4 additions & 4 deletions api/api/models/media.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
from django.utils.html import format_html

from elasticsearch import Elasticsearch, NotFoundError
from ov_attribution.attribution import get_attribution_text
from ov_attribution.license import License

from api.constants.moderation import DecisionAction
from api.models.base import OpenLedgerModel
from api.models.mixins import ForeignIdentifierMixin, IdentifierMixin, MediaMixin
from api.utils.attribution import get_attribution_text
from api.utils.licenses import get_license_url


PENDING = "pending_review"
Expand Down Expand Up @@ -97,16 +97,16 @@ def license_url(self) -> str:
if self.meta_data and (url := self.meta_data.get("license_url")):
return url
else:
return get_license_url(self.license.lower(), self.license_version)
return License(self.license.lower()).url(self.license_version)

@property
def attribution(self) -> str:
"""Legally valid attribution for the media item in plain-text English."""

return get_attribution_text(
self.license.lower(),
self.title,
self.creator,
self.license.lower(),
self.license_version,
self.license_url,
)
Expand Down
6 changes: 3 additions & 3 deletions api/api/serializers/media_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from drf_spectacular.utils import extend_schema_serializer
from elasticsearch_dsl.response import Hit
from ov_attribution.license import License

from api.constants import sensitivity
from api.constants.licenses import LICENSE_GROUPS
Expand All @@ -32,7 +33,6 @@
)
from api.serializers.fields import SchemableHyperlinkedIdentityField
from api.utils.help_text import make_comma_separated_help_text
from api.utils.licenses import get_license_url
from api.utils.url import add_protocol


Expand Down Expand Up @@ -741,8 +741,8 @@ def to_representation(self, *args, **kwargs):
output["license"] = output["license"].lower()

if output.get("license_url") is None:
output["license_url"] = get_license_url(
output["license"], output["license_version"]
output["license_url"] = License(output["license"]).url(
output["license_version"]
)

# Ensure URLs have scheme
Expand Down
61 changes: 0 additions & 61 deletions api/api/utils/attribution.py

This file was deleted.

95 changes: 0 additions & 95 deletions api/api/utils/licenses.py

This file was deleted.

39 changes: 8 additions & 31 deletions api/api/utils/watermark.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from rest_framework.exceptions import APIException

import requests
from ov_attribution.attribution import get_attribution_text
from PIL import Image, ImageDraw, ImageFont, UnidentifiedImageError
from sentry_sdk import capture_exception

Expand Down Expand Up @@ -130,36 +131,6 @@ def _frame_image(image, frame, left_margin, top_margin):
# Attribution


def _full_license(image_info):
"""
Get the full license from the image info.
:param image_info: the information about a particular image
:return: the full license text for the image
"""

license_name = image_info["license"].upper()
license_version = image_info["license_version"].upper()
prefix = "" if license_name == "CC0" else "CC "

return f"{prefix}{license_name} {license_version}"


def _get_attribution_text(image_info):
"""
Generate the attribution text from the image info.
:param image_info: the info pertaining to the licensing of the image
:return: the attribution text
"""

title = image_info["title"]
creator = image_info["creator"]
full_license = _full_license(image_info)

return f'"{title}" by {creator} is licensed under {full_license}.'


def _get_attribution_height(text, font):
draw = ImageDraw.Draw(Image.new("RGB", (0, 0)))
_, _, _, height = draw.multiline_textbbox((0, 0), text, font)
Expand Down Expand Up @@ -219,7 +190,13 @@ def _print_attribution_on_image(img: Image.Image, image_info):

font = ImageFont.truetype(_get_font_path(), size=font_size)

text = _get_attribution_text(image_info)
text = get_attribution_text(
image_info["license"],
image_info["title"],
image_info["creator"],
image_info["license_version"],
license_url=False,
)
text = _fit_in_width(text, font, new_width)
attribution_height = _get_attribution_height(text, font)

Expand Down
44 changes: 0 additions & 44 deletions api/test/unit/utils/test_attribution.py

This file was deleted.

0 comments on commit 05d32e0

Please sign in to comment.