Skip to content

Commit

Permalink
Lint
Browse files Browse the repository at this point in the history
  • Loading branch information
zerolab committed Jan 21, 2024
1 parent 8ee0030 commit 62bb1d7
Show file tree
Hide file tree
Showing 20 changed files with 470 additions and 126 deletions.
1 change: 1 addition & 0 deletions src/wagtail_bynder/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured


def get_video_model_string():
"""
Get the dotted ``app.Model`` name for the video model as a string.
Expand Down
5 changes: 3 additions & 2 deletions src/wagtail_bynder/management/commands/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ def get_assets(self) -> Generator[dict[str, Any]]:
results = self.bynder_client.asset_bank_client.media_list(query)
if not results:
break
for asset in results:
yield asset

yield from results

page += 1

def get_outdated_objects(self, assets: dict[str, dict[str, Any]]) -> QuerySet:
Expand Down
13 changes: 7 additions & 6 deletions src/wagtail_bynder/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import math
import os

from datetime import datetime
from mimetypes import guess_type
from typing import Any
Expand All @@ -19,6 +20,7 @@

from wagtail_bynder import utils


logger = logging.getLogger("wagtail.images")


Expand All @@ -27,21 +29,21 @@ class BynderAssetMixin(models.Model):
bynder_id = models.CharField(
verbose_name=_("Bynder asset ID"),
max_length=36,
null=True,
blank=True,
unique=True,
editable=False,
db_index=True,
)
bynder_id_hash = models.CharField(max_length=100, null=True, editable=False)
bynder_id_hash = models.CharField(max_length=100, blank=True, editable=False)
bynder_original_filename = models.CharField(
max_length=255, null=True, editable=False
max_length=255, blank=True, editable=False
)
bynder_last_modified = models.DateTimeField(
null=True, editable=False, db_index=True
)

# Fields for more broader use
description = models.TextField(verbose_name=_("descripton"), blank=True)
# Fields for broader use
description = models.TextField(verbose_name=_("description"), blank=True)
copyright = models.TextField(verbose_name=_("copyright info"), blank=True)
is_archived = models.BooleanField(
verbose_name=_("asset is archived"), default=False
Expand Down Expand Up @@ -274,7 +276,6 @@ class BynderSyncedVideo(
)
fallback_source_url = models.URLField(
blank=True,
null=True,
verbose_name=_("fallback source URL"),
help_text=(
"A derivative using an MP4 container and the AVC (H.264) video codec, ideally with "
Expand Down
1 change: 1 addition & 0 deletions src/wagtail_bynder/templatetags/bynder_tags.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django import template
from django.conf import settings


register = template.Library()


Expand Down
5 changes: 4 additions & 1 deletion src/wagtail_bynder/utils.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import mimetypes
import os

from io import BytesIO

import requests

from asgiref.local import Local
from bynder_sdk import BynderClient
from django.conf import settings
from django.core.files.uploadedfile import InMemoryUploadedFile
from wagtail.models import Collection


_DEFAULT_COLLECTION = Local()


Expand All @@ -20,7 +23,7 @@ class DownloadedFile(BytesIO):


def download_file(url: str) -> DownloadedFile:
raw_bytes = requests.get(url).content
raw_bytes = requests.get(url, timeout=20).content
f = DownloadedFile(raw_bytes)
f.name = os.path.basename(url)
f.size = len(raw_bytes)
Expand Down
1 change: 1 addition & 0 deletions src/wagtail_bynder/views/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from .mixins import BynderAssetCopyMixin, RedirectToBynderMixin


if TYPE_CHECKING:
from django.http import HttpRequest, JsonResponse

Expand Down
1 change: 1 addition & 0 deletions src/wagtail_bynder/views/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from .mixins import BynderAssetCopyMixin, RedirectToBynderMixin


if TYPE_CHECKING:
from django.http import HttpRequest, JsonResponse

Expand Down
1 change: 1 addition & 0 deletions src/wagtail_bynder/views/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from wagtail_bynder.models import BynderAssetMixin
from wagtail_bynder.utils import get_bynder_client


if TYPE_CHECKING:
from django.http import HttpRequest, HttpResponse

Expand Down
1 change: 1 addition & 0 deletions src/wagtail_bynder/views/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from .mixins import BynderAssetCopyMixin, RedirectToBynderMixin


if TYPE_CHECKING:
from django.http import HttpRequest, JsonResponse

Expand Down
4 changes: 2 additions & 2 deletions src/wagtail_bynder/wagtail_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def editor_js():


if get_video_model():
try:
try: # noqa: SIM105
register_snippet(VideoViewSet)
except Exception:
except Exception: # noqa: S110
pass
52 changes: 30 additions & 22 deletions tests/test_bynderassetcopymixin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from unittest import mock

import responses

from django.test import TestCase
from wagtail.images import get_image_model
from wagtail_factories import ImageFactory
Expand All @@ -9,6 +10,7 @@

from .utils import TEST_ASSET_ID, get_test_asset_data


TEST_ASSET_DATA = get_test_asset_data(id=TEST_ASSET_ID)


Expand All @@ -30,12 +32,14 @@ def test_create_object(self):
# After fetching the data from bynder, the method should create a
# new object, call update_from_asset_data() to populate some
# fields values, then save the changes.
with mock.patch.object(
self.view.model, "update_from_asset_data"
) as update_from_asset_data_mock:
with mock.patch.object(self.view.model, "save") as save_mock:
# Run the code to be tested!
obj = self.view.create_object(TEST_ASSET_ID)
with (
mock.patch.object(
self.view.model, "update_from_asset_data"
) as update_from_asset_data_mock,
mock.patch.object(self.view.model, "save") as save_mock,
):
# Run the code to be tested!
obj = self.view.create_object(TEST_ASSET_ID)

# Assertions
update_from_asset_data_mock.assert_called_once_with(TEST_ASSET_DATA)
Expand All @@ -50,15 +54,17 @@ def test_update_object_when_object_is_up_to_date(self):
# After fetching the data from bynder, the method should call the object's
# 'is_up_to_date()' method, then will only update and save the object if
# `False` is returned
with mock.patch.object(
obj, "is_up_to_date", return_value=True
) as is_up_to_date_mock:
with mock.patch.object(
with (
mock.patch.object(
obj, "is_up_to_date", return_value=True
) as is_up_to_date_mock,
mock.patch.object(
obj, "update_from_asset_data"
) as update_from_asset_data_mock:
with mock.patch.object(obj, "save") as save_mock:
# Run the code to be tested!
self.view.update_object(TEST_ASSET_ID, obj)
) as update_from_asset_data_mock,
mock.patch.object(obj, "save") as save_mock,
):
# Run the code to be tested!
self.view.update_object(TEST_ASSET_ID, obj)

# Assertions
is_up_to_date_mock.assert_called_once_with(TEST_ASSET_DATA)
Expand All @@ -73,15 +79,17 @@ def test_update_object_when_object_is_outdated(self):
# After fetching the data from bynder, the method should call the object's
# 'is_up_to_date()' method, then will only update and save the object if
# `False` is returned
with mock.patch.object(
obj, "is_up_to_date", return_value=False
) as is_up_to_date_mock:
with mock.patch.object(
with (
mock.patch.object(
obj, "is_up_to_date", return_value=False
) as is_up_to_date_mock,
mock.patch.object(
obj, "update_from_asset_data"
) as update_from_asset_data_mock:
with mock.patch.object(obj, "save") as save_mock:
# Run the code to be tested!
self.view.update_object(TEST_ASSET_ID, obj)
) as update_from_asset_data_mock,
mock.patch.object(obj, "save") as save_mock,
):
# Run the code to be tested!
self.view.update_object(TEST_ASSET_ID, obj)

is_up_to_date_mock.assert_called_once_with(TEST_ASSET_DATA)
update_from_asset_data_mock.assert_called_once_with(TEST_ASSET_DATA)
Expand Down
3 changes: 1 addition & 2 deletions tests/test_document_chooser_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

from django.test import TestCase, override_settings
from django.urls import reverse, reverse_lazy
from wagtail.test.utils import WagtailTestUtils

from testapp.factories import CustomDocumentFactory
from wagtail.test.utils import WagtailTestUtils

from .utils import TEST_ASSET_ID

Expand Down
3 changes: 1 addition & 2 deletions tests/test_image_chooser_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

from django.test import TestCase, TransactionTestCase, override_settings
from django.urls import reverse, reverse_lazy
from wagtail.test.utils import WagtailTestUtils

from testapp.factories import CustomImageFactory
from wagtail.test.utils import WagtailTestUtils

from .utils import TEST_ASSET_ID

Expand Down
7 changes: 3 additions & 4 deletions tests/test_wagtail_overrides.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def assertNotRaises(self, exc_type):
try:
yield None
except exc_type as e:
raise self.failureException("{} raised".format(exc_type.__name__)) from e
raise self.failureException(f"{exc_type.__name__} raised") from e

def test_wagtail_views_names_have_not_changed(self):
"""
Expand All @@ -32,9 +32,8 @@ def test_wagtail_views_names_have_not_changed(self):
if they change, our custom views will not be loaded.
"""
for url_name, args in self.urls:
with self.subTest(url_name=url_name):
with self.assertNotRaises(NoReverseMatch):
reverse(url_name, args=args)
with self.subTest(url_name=url_name), self.assertNotRaises(NoReverseMatch):
reverse(url_name, args=args)

@override_settings(
BYNDER_DISABLE_WAGTAIL_EDITING_FOR_ASSETS=True,
Expand Down
6 changes: 5 additions & 1 deletion tests/testapp/factories.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
from wagtail_factories.factories import DocumentFactory, ImageFactory, CollectionMemberFactory
from wagtail_factories.factories import (
CollectionMemberFactory,
DocumentFactory,
ImageFactory,
)

from .models import CustomDocument, CustomImage, Video

Expand Down
Loading

0 comments on commit 62bb1d7

Please sign in to comment.