From 765b543cd89400f0b2a1b02c4e58d2866791c742 Mon Sep 17 00:00:00 2001 From: Phil Gyford Date: Mon, 8 Apr 2024 14:38:49 +0100 Subject: [PATCH] Please ruff I'm convinced it keeps changing its mind about how to format strings. --- ditto/core/templatetags/ditto_core.py | 7 +++---- ditto/core/views.py | 5 +++-- ditto/flickr/admin.py | 14 ++++++------- ditto/flickr/fetch/fetchers.py | 5 +++-- ditto/flickr/models.py | 19 ++++++++--------- ditto/flickr/templatetags/ditto_flickr.py | 5 ++--- ditto/lastfm/models.py | 13 +++++------- ditto/twitter/admin.py | 25 +++++++++-------------- ditto/twitter/models.py | 6 +++--- tests/pinboard/test_fetch.py | 7 +++---- tests/twitter/test_fetch_fetchers.py | 14 ++++++------- 11 files changed, 53 insertions(+), 67 deletions(-) diff --git a/ditto/core/templatetags/ditto_core.py b/ditto/core/templatetags/ditto_core.py index 7e9b26c..cb18eae 100644 --- a/ditto/core/templatetags/ditto_core.py +++ b/ditto/core/templatetags/ditto_core.py @@ -160,8 +160,7 @@ def current_url_name(context): """ url_name = False if context.request.resolver_match: - url_name = "{}:{}".format( - context.request.resolver_match.namespace, - context.request.resolver_match.url_name, - ) + namespace = context.request.resolver_match.namespace + name = context.request.resolver_match.url_name + url_name = f"{namespace}:{name}" return url_name diff --git a/ditto/core/views.py b/ditto/core/views.py index 3b5de04..7dd6a03 100644 --- a/ditto/core/views.py +++ b/ditto/core/views.py @@ -240,8 +240,9 @@ def set_app_and_variety(self, **kwargs): app_slug, variety_slug ) elif variety_slug: - msg = "'{}' is not a valid variety slug for the '{}' app slug.".format( - variety_slug, app_slug + msg = ( + f"'{variety_slug}' is not a valid variety slug for " + f"the '{app_slug}' app slug." ) raise Http404(msg) elif app_slug: diff --git a/ditto/flickr/admin.py b/ditto/flickr/admin.py index d3d7152..12a3fc2 100644 --- a/ditto/flickr/admin.py +++ b/ditto/flickr/admin.py @@ -441,20 +441,18 @@ class PhotoAdmin(DittoItemModelAdmin): def show_thumb(self, instance): return mark_safe( - ''.format( - instance.thumbnail_url, - instance.thumbnail_width, - instance.thumbnail_height, - ) + f'' ) show_thumb.short_description = "Thumbnail" def show_image(self, instance): return mark_safe( - ''.format( - instance.small_url, instance.small_width, instance.small_height - ) + f'' ) show_image.short_description = "Small image" diff --git a/ditto/flickr/fetch/fetchers.py b/ditto/flickr/fetch/fetchers.py index 8078849..1c0e6e6 100644 --- a/ditto/flickr/fetch/fetchers.py +++ b/ditto/flickr/fetch/fetchers.py @@ -551,8 +551,9 @@ def _fetch_photos_in_photoset(self, photoset_id): page=page_number, ) except FlickrError as err: - msg = "Error when fetching photos in photoset {} (page {}): {}".format( - photoset_id, page_number, err + msg = ( + f"Error when fetching photos in photoset {photoset_id} " + f"(page {page_number}): {err}" ) raise FetchError(msg) from err diff --git a/ditto/flickr/models.py b/ditto/flickr/models.py index be95140..e403eb3 100644 --- a/ditto/flickr/models.py +++ b/ditto/flickr/models.py @@ -495,6 +495,9 @@ def upload_path(self, filename): class Meta: ordering = ("-post_time",) + def __str__(self): + return super().__str_(self) + def save(self, *args, **kwargs): if self.taken_time: self.taken_year = self.taken_time.year @@ -681,12 +684,9 @@ def _remote_image_url(self, size): # Medium size doesn't have a letter suffix. if self.PHOTO_SIZES[size]["suffix"]: size_ext = "_{}".format(self.PHOTO_SIZES[size]["suffix"]) - return "https://farm{}.static.flickr.com/{}/{}_{}{}.jpg".format( - self.farm, - self.server, - self.flickr_id, - self.secret, - size_ext, + return ( + f"https://farm{self.farm}.static.flickr.com/{self.server}/" + f"{self.flickr_id}_{self.secret}{size_ext}.jpg" ) def _video_url(self, size): @@ -912,10 +912,9 @@ def avatar_url(self): def original_icon_url(self): """URL of the avatar/profile pic at Flickr.""" if self.iconserver: - return "https://farm{}.staticflickr.com/{}/buddyicons/{}.jpg".format( - self.iconfarm, - self.iconserver, - self.nsid, + return ( + f"https://farm{self.iconfarm}.staticflickr.com/{self.iconserver}" + f"/buddyicons/{self.nsid}.jpg" ) else: return "https://www.flickr.com/images/buddyicon.gif" diff --git a/ditto/flickr/templatetags/ditto_flickr.py b/ditto/flickr/templatetags/ditto_flickr.py index 4fe8554..4007717 100644 --- a/ditto/flickr/templatetags/ditto_flickr.py +++ b/ditto/flickr/templatetags/ditto_flickr.py @@ -85,9 +85,8 @@ def photo_license(n): if n in licenses: if n in Photo.LICENSE_URLS and Photo.LICENSE_URLS[n] != "": return format_html( - '{}'.format( - Photo.LICENSE_URLS[n], licenses[n] - ) + f'' + f"{licenses[n]}" ) else: return licenses[n] diff --git a/ditto/lastfm/models.py b/ditto/lastfm/models.py index cb8144e..c47e4d3 100644 --- a/ditto/lastfm/models.py +++ b/ditto/lastfm/models.py @@ -111,10 +111,8 @@ def get_absolute_url(self): @property def permalink(self): "The Album's URL at Last.fm." - return "{}/music/{}/{}".format( - LASTFM_URL_ROOT, - self.artist.original_slug, - self.original_slug, + return ( + f"{LASTFM_URL_ROOT}/music/{self.artist.original_slug}/{self.original_slug}" ) @property @@ -339,10 +337,9 @@ def get_absolute_url(self): @property def permalink(self): "The Track's URL at Last.fm." - return "{}/music/{}/_/{}".format( - LASTFM_URL_ROOT, - self.artist.original_slug, - self.original_slug, + return ( + f"{LASTFM_URL_ROOT}/music/{self.artist.original_slug}/_/" + f"{self.original_slug}" ) @property diff --git a/ditto/twitter/admin.py b/ditto/twitter/admin.py index 089cb29..2ea2ef8 100644 --- a/ditto/twitter/admin.py +++ b/ditto/twitter/admin.py @@ -155,29 +155,24 @@ class MediaAdmin(admin.ModelAdmin): def show_thumb(self, instance): return mark_safe( - ''.format( - instance.thumbnail_url, - instance.thumbnail_w, - instance.thumbnail_h, - ) + f'' ) show_thumb.short_description = "" def show_image(self, instance): if instance.media_type == "photo": - html = ''.format( - instance.small_url, - instance.small_w, - instance.small_h, + html = ( + f'' ) else: - html = ''.format( # noqa: E501 - instance.small_w, - instance.small_h, - instance.small_url, - instance.video_url, - instance.video_mime_type, + html = ( + f'" ) return mark_safe(html) diff --git a/ditto/twitter/models.py b/ditto/twitter/models.py index 0153c9c..f173c71 100644 --- a/ditto/twitter/models.py +++ b/ditto/twitter/models.py @@ -607,9 +607,9 @@ def is_reply(self): def in_reply_to_url(self): "If it's a reply, the link to the tweet replied to." if self.is_reply: - return "https://twitter.com/{}/status/{}".format( - self.in_reply_to_screen_name, - self.in_reply_to_status_id, + return ( + f"https://twitter.com/{self.in_reply_to_screen_name}/" + f"status/{self.in_reply_to_status_id}" ) else: return "" diff --git a/tests/pinboard/test_fetch.py b/tests/pinboard/test_fetch.py index d8d4c55..8d2a491 100644 --- a/tests/pinboard/test_fetch.py +++ b/tests/pinboard/test_fetch.py @@ -74,10 +74,9 @@ def make_success_body( if method == "all": return posts_json else: - return '{{"date":"{}T09:48:31Z","user":"{}","posts":{}}}\t\n'.format( - post_date, - username, - posts_json, + return ( + f'{{"date":"{post_date}T09:48:31Z","user":"{username}",' + f'"posts":{posts_json}}}\t\n' ) # Check that all interface methods return expected results on success. diff --git a/tests/twitter/test_fetch_fetchers.py b/tests/twitter/test_fetch_fetchers.py index dd53f7f..dff5897 100644 --- a/tests/twitter/test_fetch_fetchers.py +++ b/tests/twitter/test_fetch_fetchers.py @@ -976,10 +976,9 @@ def test_saves_downloaded_image_file(self, download): FetchFiles()._fetch_and_save_file(self.image, "image") self.assertEqual( self.image.image_file.name, - "twitter/media/{}/{}/{}".format( - temp_filepath[-4:-2], - temp_filepath[-2:], - os.path.basename(temp_filepath), + ( + f"twitter/media/{temp_filepath[-4:-2]}/{temp_filepath[-2:]}/" + f"{os.path.basename(temp_filepath)}", ), ) @@ -994,9 +993,8 @@ def test_saves_downloaded_mp4_file(self, download): FetchFiles()._fetch_and_save_file(self.animated_gif, "mp4") self.assertEqual( self.animated_gif.mp4_file.name, - "twitter/media/{}/{}/{}".format( - temp_filepath[-4:-2], - temp_filepath[-2:], - os.path.basename(temp_filepath), + ( + f"twitter/media/{temp_filepath[-4:-2]}/{temp_filepath[-2:]}/" + f"{os.path.basename(temp_filepath)}", ), )