Skip to content

Commit

Permalink
Please ruff
Browse files Browse the repository at this point in the history
I'm convinced it keeps changing its mind about how to format strings.
  • Loading branch information
philgyford committed Apr 8, 2024
1 parent 94c2936 commit 765b543
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 67 deletions.
7 changes: 3 additions & 4 deletions ditto/core/templatetags/ditto_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 3 additions & 2 deletions ditto/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
14 changes: 6 additions & 8 deletions ditto/flickr/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,20 +441,18 @@ class PhotoAdmin(DittoItemModelAdmin):

def show_thumb(self, instance):
return mark_safe(
'<img src="{}" width="{}" height="{}" />'.format(
instance.thumbnail_url,
instance.thumbnail_width,
instance.thumbnail_height,
)
f'<img src="{instance.thumbnail_url}" '
f'width="{instance.thumbnail_width}" '
f'height="{instance.thumbnail_height}" />'
)

show_thumb.short_description = "Thumbnail"

def show_image(self, instance):
return mark_safe(
'<img src="{}" width="{}" height="{}" />'.format(
instance.small_url, instance.small_width, instance.small_height
)
f'<img src="{instance.small_url}" '
f'width="{instance.small_width}" '
f'height="{instance.small_height}" />'
)

show_image.short_description = "Small image"
Expand Down
5 changes: 3 additions & 2 deletions ditto/flickr/fetch/fetchers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
19 changes: 9 additions & 10 deletions ditto/flickr/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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"
5 changes: 2 additions & 3 deletions ditto/flickr/templatetags/ditto_flickr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
'<a href="{}" title="More about permissions">{}</a>'.format(
Photo.LICENSE_URLS[n], licenses[n]
)
f'<a href="{Photo.LICENSE_URLs[n]}" title="More about permissions">'
f"{licenses[n]}</a>"
)
else:
return licenses[n]
Expand Down
13 changes: 5 additions & 8 deletions ditto/lastfm/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
25 changes: 10 additions & 15 deletions ditto/twitter/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,29 +155,24 @@ class MediaAdmin(admin.ModelAdmin):

def show_thumb(self, instance):
return mark_safe(
'<img src="{}" width="{}" height="{}" />'.format(
instance.thumbnail_url,
instance.thumbnail_w,
instance.thumbnail_h,
)
f'<img src="{instance.thumbnail_url}" width="{instance.thumbnail_w}" '
f'height="{instance.thumbnail_h}" />'
)

show_thumb.short_description = ""

def show_image(self, instance):
if instance.media_type == "photo":
html = '<img src="{}" width="{}" height="{}" />'.format(
instance.small_url,
instance.small_w,
instance.small_h,
html = (
f'<img src="{instance.small_url}" width="{instance.small_w}" '
f'height="{instance.small_h}" />'
)
else:
html = '<video width="{}" height="{}" poster="{}" controls preload="metadata"><source src="{}" type="{}"></video>'.format( # noqa: E501
instance.small_w,
instance.small_h,
instance.small_url,
instance.video_url,
instance.video_mime_type,
html = (
f'<video width="{instance.small_w}" height="{instance.small_h}" '
f'poster="{instance.small_url}" controls preload="metadata">'
f'<source src="{instance.video_url}" type="{instance.video_mime_type}">'
"</video>"
)
return mark_safe(html)

Expand Down
6 changes: 3 additions & 3 deletions ditto/twitter/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
Expand Down
7 changes: 3 additions & 4 deletions tests/pinboard/test_fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
14 changes: 6 additions & 8 deletions tests/twitter/test_fetch_fetchers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)}",
),
)

Expand All @@ -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)}",
),
)

0 comments on commit 765b543

Please sign in to comment.