Skip to content

Commit

Permalink
Use pyupgrade to clean the codebase and upgrade the syntax for newer …
Browse files Browse the repository at this point in the history
…versions of Python.
  • Loading branch information
Richard Barran committed Aug 15, 2022
1 parent f15fe57 commit 168597f
Show file tree
Hide file tree
Showing 24 changed files with 35 additions and 68 deletions.
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@

# General information about the project.
project = 'django-photologue'
copyright = '{}, Justin Driscoll/Richard Barran'.format(datetime.datetime.now().year)
copyright = f'{datetime.datetime.now().year}, Justin Driscoll/Richard Barran'

# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
Expand Down
4 changes: 2 additions & 2 deletions photologue/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def add_photos_to_current_site(modeladmin, request, queryset):
'All photos in galleries %(galleries)s have been successfully added to %(site)s',
len(queryset)
) % {'site': current_site.name,
'galleries': ", ".join(["'{}'".format(gallery.title) for gallery in queryset])}
'galleries': ", ".join([f"'{gallery.title}'" for gallery in queryset])}
messages.success(request, msg)

add_photos_to_current_site.short_description = \
Expand All @@ -115,7 +115,7 @@ def remove_photos_from_current_site(modeladmin, request, queryset):
'All photos in galleries %(galleries)s have been successfully removed from %(site)s',
len(queryset)
) % {'site': current_site.name,
'galleries': ", ".join(["'{}'".format(gallery.title) for gallery in queryset])}
'galleries': ", ".join([f"'{gallery.title}'" for gallery in queryset])}
messages.success(request, msg)

remove_photos_from_current_site.short_description = \
Expand Down
6 changes: 3 additions & 3 deletions photologue/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,10 @@ def upload_photos_to_site(site: Site, zip: zipfile.ZipFile, gallery: Gallery, ph

for filename in sorted(zip.namelist()):

logger.debug('Reading file "{}".'.format(filename))
logger.debug(f'Reading file "{filename}".')

if filename.startswith('__') or filename.startswith('.'):
logger.debug('Ignoring file "{}".'.format(filename))
logger.debug(f'Ignoring file "{filename}".')
continue

if os.path.dirname(filename):
Expand All @@ -177,7 +177,7 @@ def upload_photos_to_site(site: Site, zip: zipfile.ZipFile, gallery: Gallery, ph
data = zip.read(filename)

if not len(data):
logger.debug('File "{}" is empty.'.format(filename))
logger.debug(f'File "{filename}" is empty.')
continue

photo_title_root = photo_defaults.title if photo_defaults.title else gallery.title
Expand Down
1 change: 0 additions & 1 deletion photologue/management/commands/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

from photologue.models import PhotoSize


Expand Down
3 changes: 0 additions & 3 deletions photologue/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
import photologue.models
import django.utils.timezone
Expand Down
3 changes: 0 additions & 3 deletions photologue/migrations/0002_photosize_data.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# encoding: utf8
from __future__ import unicode_literals

from django.db import models, migrations


Expand Down
3 changes: 0 additions & 3 deletions photologue/migrations/0003_auto_20140822_1716.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


Expand Down
3 changes: 0 additions & 3 deletions photologue/migrations/0004_auto_20140915_1259.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
import sortedm2m.fields

Expand Down
3 changes: 0 additions & 3 deletions photologue/migrations/0005_auto_20141027_1552.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


Expand Down
3 changes: 0 additions & 3 deletions photologue/migrations/0006_auto_20141028_2005.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


Expand Down
3 changes: 0 additions & 3 deletions photologue/migrations/0007_auto_20150404_1737.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
import sortedm2m.fields

Expand Down
3 changes: 0 additions & 3 deletions photologue/migrations/0008_auto_20150509_1557.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


Expand Down
2 changes: 0 additions & 2 deletions photologue/migrations/0009_auto_20160102_0904.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9 on 2016-01-02 09:04
from __future__ import unicode_literals

from django.db import migrations, models

Expand Down
2 changes: 0 additions & 2 deletions photologue/migrations/0010_auto_20160105_1307.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9 on 2016-01-05 13:07
from __future__ import unicode_literals

from django.db import migrations, models

Expand Down
4 changes: 2 additions & 2 deletions photologue/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,9 @@ def admin_thumbnail(self):
return _('An "admin_thumbnail" photo size has not been defined.')
else:
if hasattr(self, 'get_absolute_url'):
return mark_safe('<a href="{}"><img src="{}"></a>'.format(self.get_absolute_url(), func()))
return mark_safe(f'<a href="{self.get_absolute_url()}"><img src="{func()}"></a>')
else:
return mark_safe('<a href="{}"><img src="{}"></a>'.format(self.image.url, func()))
return mark_safe(f'<a href="{self.image.url}"><img src="{func()}"></a>')

admin_thumbnail.short_description = _('Thumbnail')
admin_thumbnail.allow_tags = True
Expand Down
6 changes: 3 additions & 3 deletions photologue/templatetags/photologue_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def cycle_lite_gallery(gallery_title, height, width):
html = ""
first = 'class="first"'
for p in Gallery.objects.get(title=gallery_title).public():
html += u'<img src="%s" alt="%s" height="%s" width="%s" %s />' % (
html += '<img src="{}" alt="{}" height="{}" width="{}" {} />'.format(
p.get_display_url(), p.title, height, width, first)
first = None
return html
Expand Down Expand Up @@ -75,7 +75,7 @@ def render(self, context):
if func is None:
return 'A "%s" photo size has not been defined.' % (self.photosize)
else:
return u'<img class="%s" src="%s" alt="%s" />' % (self.css_class, func(), p.title)
return f'<img class="{self.css_class}" src="{func()}" alt="{p.title}" />'


@register.tag
Expand Down Expand Up @@ -128,4 +128,4 @@ def render(self, context):
if func is None:
return 'A "%s" photo size has not been defined.' % (self.photosize)
else:
return u'<img class="%s" src="%s" alt="%s" />' % (self.css_class, func(), p.title)
return f'<img class="{self.css_class}" src="{func()}" alt="{p.title}" />'
10 changes: 4 additions & 6 deletions photologue/tests/factories.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# coding=utf-8

import datetime
import os

Expand Down Expand Up @@ -32,7 +30,7 @@ class GalleryFactory(factory.django.DjangoModelFactory):
class Meta:
model = Gallery

title = factory.Sequence(lambda n: 'gallery{0:0>3}'.format(n))
title = factory.Sequence(lambda n: f'gallery{n:0>3}')
slug = factory.LazyAttribute(lambda a: slugify(a.title))

@factory.sequence
Expand Down Expand Up @@ -77,7 +75,7 @@ class PhotoFactory(ImageModelFactory):
class Meta:
model = Photo

title = factory.Sequence(lambda n: 'photo{0:0>3}'.format(n))
title = factory.Sequence(lambda n: f'photo{n:0>3}')
slug = factory.LazyAttribute(lambda a: slugify(a.title))
image = factory.django.ImageField(from_path=LANDSCAPE_IMAGE_PATH)

Expand Down Expand Up @@ -112,12 +110,12 @@ class PhotoSizeFactory(factory.django.DjangoModelFactory):
class Meta:
model = PhotoSize

name = factory.Sequence(lambda n: 'name{0:0>3}'.format(n))
name = factory.Sequence(lambda n: f'name{n:0>3}')


class PhotoEffectFactory(factory.django.DjangoModelFactory):

class Meta:
model = PhotoEffect

name = factory.Sequence(lambda n: 'effect{0:0>3}'.format(n))
name = factory.Sequence(lambda n: f'effect{n:0>3}')
4 changes: 2 additions & 2 deletions photologue/tests/test_gallery.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ class GalleryTest(PhotologueBaseTest):

def setUp(self):
"""Create a test gallery with 2 photos."""
super(GalleryTest, self).setUp()
super().setUp()
self.test_gallery = GalleryFactory()
self.pl2 = PhotoFactory()
self.test_gallery.photos.add(self.pl)
self.test_gallery.photos.add(self.pl2)

def tearDown(self):
super(GalleryTest, self).tearDown()
super().tearDown()
self.pl2.delete()

def test_public(self):
Expand Down
20 changes: 9 additions & 11 deletions photologue/tests/test_photo.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-

import os
import unittest
from io import BytesIO
Expand All @@ -20,7 +18,7 @@ class PhotoTest(PhotologueBaseTest):

def tearDown(self):
"""Delete any extra test files (if created)."""
super(PhotoTest, self).tearDown()
super().tearDown()
try:
self.pl2.delete()
except:
Expand Down Expand Up @@ -132,11 +130,11 @@ class PhotoManagerTest(PhotologueBaseTest):

def setUp(self):
"""Create 2 photos."""
super(PhotoManagerTest, self).setUp()
super().setUp()
self.pl2 = PhotoFactory()

def tearDown(self):
super(PhotoManagerTest, self).tearDown()
super().tearDown()
self.pl2.delete()

def test_public(self):
Expand All @@ -152,7 +150,7 @@ class PreviousNextTest(PhotologueBaseTest):

def setUp(self):
"""Create a test gallery with 2 photos."""
super(PreviousNextTest, self).setUp()
super().setUp()
self.test_gallery = GalleryFactory()
self.pl1 = PhotoFactory()
self.pl2 = PhotoFactory()
Expand All @@ -162,7 +160,7 @@ def setUp(self):
self.test_gallery.photos.add(self.pl3)

def tearDown(self):
super(PreviousNextTest, self).tearDown()
super().tearDown()
self.pl1.delete()
self.pl2.delete()
self.pl3.delete()
Expand Down Expand Up @@ -239,7 +237,7 @@ def test_next_gallery_mismatch(self):
class ImageModelTest(PhotologueBaseTest):

def setUp(self):
super(ImageModelTest, self).setUp()
super().setUp()

# Unicode image has unicode in the path
# self.pu = TestPhoto(name='portrait')
Expand All @@ -254,7 +252,7 @@ def setUp(self):
ContentFile(open(NONSENSE_IMAGE_PATH, 'rb').read()))

def tearDown(self):
super(ImageModelTest, self).tearDown()
super().tearDown()
self.pu.delete()
self.pn.delete()

Expand All @@ -278,13 +276,13 @@ def raw_image(mode='RGB', fmt='JPEG'):
class ImageTransparencyTest(PhotologueBaseTest):

def setUp(self):
super(ImageTransparencyTest, self).setUp()
super().setUp()
self.png = PhotoFactory()
self.png.image.save(
'trans.png', ContentFile(raw_image('RGBA', 'PNG').read()))

def tearDown(self):
super(ImageTransparencyTest, self).tearDown()
super().tearDown()
self.png.clear_cache()
os.unlink(os.path.join(settings.MEDIA_ROOT, self.png.image.path))

Expand Down
4 changes: 2 additions & 2 deletions photologue/tests/test_resize.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ def test_clean_wont_allow_zero_dimension_and_crop(self):
class ImageResizeTest(PhotologueBaseTest):

def setUp(self):
super(ImageResizeTest, self).setUp()
super().setUp()
# Portrait.
self.pp = PhotoFactory(image__from_path=PORTRAIT_IMAGE_PATH)
# Square.
self.ps = PhotoFactory(image__from_path=SQUARE_IMAGE_PATH)

def tearDown(self):
super(ImageResizeTest, self).tearDown()
super().tearDown()
self.pp.delete()
self.ps.delete()

Expand Down
4 changes: 2 additions & 2 deletions photologue/tests/test_sites.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def setUp(self):
Create two example sites that we can use to test what gets displayed
where.
"""
super(SitesTest, self).setUp()
super().setUp()

self.site1, created1 = Site.objects.get_or_create(
domain="example.com", name="example.com")
Expand All @@ -36,7 +36,7 @@ def setUp(self):
self.photo2.sites.clear()

def tearDown(self):
super(SitesTest, self).tearDown()
super().tearDown()
self.gallery1.delete()
self.gallery2.delete()
self.photo1.delete()
Expand Down
2 changes: 1 addition & 1 deletion photologue/tests/test_views_gallery.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class RequestGalleryTest(TestCase):

def setUp(self):
super(RequestGalleryTest, self).setUp()
super().setUp()
self.gallery = GalleryFactory(slug='test-gallery')

def test_archive_gallery_url_works(self):
Expand Down
4 changes: 2 additions & 2 deletions photologue/tests/test_views_photo.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
class RequestPhotoTest(TestCase):

def setUp(self):
super(RequestPhotoTest, self).setUp()
super().setUp()
self.photo = PhotoFactory(slug='fake-photo')

def tearDown(self):
super(RequestPhotoTest, self).tearDown()
super().tearDown()
self.photo.delete()

def test_archive_photo_url_works(self):
Expand Down
4 changes: 2 additions & 2 deletions photologue/tests/test_zipupload.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class GalleryUploadTest(TestCase):
"""Testing the admin page that allows users to upload zips."""

def setUp(self):
super(GalleryUploadTest, self).setUp()
super().setUp()
user = User.objects.create_user('john.doe',
'[email protected]',
'secret')
Expand All @@ -28,7 +28,7 @@ def setUp(self):
'title': 'This is a test title'}

def tearDown(self):
super(GalleryUploadTest, self).tearDown()
super().tearDown()
self.zip_file.close()
for photo in Photo.objects.all():
photo.delete()
Expand Down

0 comments on commit 168597f

Please sign in to comment.