Skip to content

Commit

Permalink
Migrate to f-strings where convenient.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdlaird committed Feb 11, 2024
1 parent 03bcb51 commit 8fc8132
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 29 deletions.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,25 @@ nopyc:
clean: nopyc
rm -rf _build $(PLATFORM_VENV)

build: virtualenv
build: install
@( \
source $(MYPROJECT_VENV)/bin/activate; \
python manage.py collectstatic --noinput; \
)

build-migrations: env virtualenv install
build-migrations: install
@( \
source $(MYPROJECT_VENV)/bin/activate; \
python manage.py makemigrations; \
)

migrate: virtualenv
migrate: install
@( \
source $(MYPROJECT_VENV)/bin/activate; \
python manage.py migrate; \
)

test: virtualenv
test: install
@if [ ! -f ansible/stage.yml ]; then echo "ansible/stage.yml not found" & exit 1 ; fi
@if [ ! -f ansible/group_vars/web.yml ]; then echo "ansible/group_vars/web.yml not found" & exit 1 ; fi
@if [ ! -f ansible/hosts/stage ]; then echo "ansible/hosts/stage not found" & exit 1 ; fi
Expand Down
6 changes: 3 additions & 3 deletions conf/configs/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
# DateTime sanity

NORMALIZED_DATE_FORMAT = '%a, %b %d'
NORMALIZED_DATE_TIME_FORMAT = '{} at %I:%M %p'.format(NORMALIZED_DATE_FORMAT)
NORMALIZED_DATE_TIME_FORMAT = f'{NORMALIZED_DATE_FORMAT} at %I:%M %p'

# Email settings

Expand All @@ -113,7 +113,7 @@
EMAIL_USE_TLS = os.environ.get('MYPROJECT_EMAIL_USE_TLS', 'True') == 'True'
EMAIL_PORT = os.environ.get('MYPROJECT_EMAIL_PORT')
EMAIL_ADDRESS = os.environ.get('MYPROJECT_CONTACT_EMAIL')
DEFAULT_FROM_EMAIL = '{} <{}>'.format(PROJECT_NAME, EMAIL_ADDRESS)
DEFAULT_FROM_EMAIL = f'{PROJECT_NAME} <{EMAIL_ADDRESS}>'
EMAIL_HOST = os.environ.get('MYPROJECT_EMAIL_HOST')

EMAIL_HOST_USER = os.environ.get('MYPROJECT_EMAIL_HOST_USER')
Expand Down Expand Up @@ -177,7 +177,7 @@
'js/vendors/moment.js',
'js/vendors/moment-timezone.js',
),
'output_filename': 'js/{}_{}.min.js'.format(PROJECT_ID, PROJECT_VERSION)
'output_filename': f'js/{PROJECT_ID}_{PROJECT_VERSION}.min.js'
},
}
}
8 changes: 4 additions & 4 deletions conf/configs/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,31 +64,31 @@
'django': {
'level': 'ERROR',
'class': 'logging.handlers.RotatingFileHandler',
'filename': '/var/log/{}/django.log'.format(PROJECT_ID),
'filename': f'/var/log/{PROJECT_ID}/django.log',
'maxBytes': 50000000,
'backupCount': 3,
'formatter': 'standard',
},
'myproject_common': {
'level': 'INFO',
'class': 'logging.handlers.RotatingFileHandler',
'filename': '/var/log/{}/common.log'.format(PROJECT_ID),
'filename': f'/var/log/{PROJECT_ID}/common.log',
'maxBytes': 50000000,
'backupCount': 3,
'formatter': 'standard',
},
'myproject_auth': {
'level': 'INFO',
'class': 'logging.handlers.RotatingFileHandler',
'filename': '/var/log/{}/auth.log'.format(PROJECT_ID),
'filename': f'/var/log/{PROJECT_ID}/auth.log',
'maxBytes': 50000000,
'backupCount': 3,
'formatter': 'standard',
},
'myproject_myapp': {
'level': 'INFO',
'class': 'logging.handlers.RotatingFileHandler',
'filename': '/var/log/{}/myapp.log'.format(PROJECT_ID),
'filename': f'/var/log/{PROJECT_ID}/myapp.log',
'maxBytes': 50000000,
'backupCount': 3,
'formatter': 'standard',
Expand Down
6 changes: 3 additions & 3 deletions conf/monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
def _restart(path):
_queue.put(True)
prefix = 'monitor (pid=%d):' % os.getpid()
print('{} Change detected to "{}".'.format(prefix, path))
print('{} Triggering process restart.'.format(prefix))
print(f'{prefix} Change detected to "{path}".')
print(f'{prefix} Triggering process restart.')
os.kill(os.getpid(), signal.SIGINT)


Expand Down Expand Up @@ -121,7 +121,7 @@ def start(interval=0.5):
global _running
_lock.acquire()
if not _running:
print('Monitoring codebase for changes: (pid={})'.format(os.getpid()))
print(f'Monitoring codebase for changes: (pid={os.getpid()})')
_running = True
_thread.start()
_lock.release()
4 changes: 2 additions & 2 deletions conf/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@
locals()['PROJECT_ID'] = PROJECT_ID

# Load conf properties into the local scope
print('Using conf.configs.{}'.format(conf))
print(f'Using conf.configs.{conf}')
common_conf_module = __import__('conf.configs.common', globals(), locals(), [PROJECT_ID])
conf_module = __import__('conf.configs.{}'.format(conf), globals(), locals(), [PROJECT_ID])
conf_module = __import__(f'conf.configs.{conf}', globals(), locals(), [PROJECT_ID])

# Load common conf properties into the local scope
for setting in dir(common_conf_module):
Expand Down
14 changes: 7 additions & 7 deletions myproject/auth/services/authservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ def process_register(request, user):
:param user: the user that has been created
:return: a redirect to the next page in the registration flow
"""
logger.info('Registered new user with username: {}'.format(user.get_username()))
logger.info(f'Registered new user with username: {user.get_username()}')

emailutils.send_multipart_email('email/register',
{
'PROJECT_NAME': settings.PROJECT_NAME,
'site_url': settings.PROJECT_HOST,
'login_url': reverse('login'),
},
'Welcome to {}'.format(settings.PROJECT_NAME), [user.email],
f'Welcome to {settings.PROJECT_NAME}', [user.email],
[settings.DEFAULT_FROM_EMAIL])

set_request_status(request, 'info', 'You\'re good to go. Login to get started!')
Expand All @@ -57,7 +57,7 @@ def process_login(request, username, password):
if user is not None:
login(request, user)

logger.info('Logged in user {}'.format(username))
logger.info(f'Logged in user {username}')

if request.POST.get('remember-me', False):
request.session.set_expiry(1209600)
Expand All @@ -68,7 +68,7 @@ def process_login(request, username, password):
else:
redirect = reverse('portal')
else:
logger.info('Non-existent user {} attempted login'.format(username))
logger.info(f'Non-existent user {username} attempted login')

set_request_status(request, 'warning',
'Oops! We don\'t recognize that account. Check to make sure you entered your '
Expand All @@ -85,7 +85,7 @@ def process_logout(request):
"""
email = request.user.email
logout(request)
logger.info('Logged out user {}'.format(email))
logger.info(f'Logged out user {email}')


def process_forgot_password(request):
Expand All @@ -105,7 +105,7 @@ def process_forgot_password(request):
user.set_password(password)
user.save()

logger.info('Reset password for user with email {}'.format(email))
logger.info(f'Reset password for user with email {email}')

emailutils.send_multipart_email('email/forgot',
{
Expand All @@ -117,7 +117,7 @@ def process_forgot_password(request):

request.session.modified = True
except get_user_model().DoesNotExist:
logger.info('A visitor tried to reset the password for an unknown email address of {}'.format(email))
logger.info(f'A visitor tried to reset the password for an unknown email address of {email}')

set_request_status(request, 'info',
'You\'ve been emailed a temporary password. Login to your account immediately using the '
Expand Down
4 changes: 2 additions & 2 deletions myproject/auth/tests/testcaseauthviews.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def test_login_username_success(self):
userhelper.verify_user_not_logged_in(self)

# WHEN
response = self.client.post(reverse('login') + '?next={}'.format(reverse('settings')),
response = self.client.post(reverse('login') + f"?next={reverse('settings')}",
{'username': user.get_username(), 'password': 'test_pass_1!',
'remember-me': 'remember-me'})

Expand Down Expand Up @@ -182,6 +182,6 @@ def test_authenticated_view_success(self):
response2 = self.client.get(reverse('settings'))

# THEN
self.assertRedirects(response1, reverse('login') + '?next={}'.format(reverse('settings')),
self.assertRedirects(response1, reverse('login') + f"?next={reverse('settings')}",
fetch_redirect_response=False)
self.assertEqual(response2.status_code, 200)
4 changes: 2 additions & 2 deletions myproject/common/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ def ready(self):

# Get the dev server port (defaults to 8000 for Django, can be overridden with the
# last arg when calling `runserver`)
addrport = urlparse("http://{}".format(sys.argv[-1]))
addrport = urlparse(f"http://{sys.argv[-1]}")
port = addrport.port if addrport.netloc and addrport.port else 8000

# Open a ngrok tunnel to the dev server
public_url = ngrok.connect(port)
print("ngrok tunnel \"{}\" -> \"http://127.0.0.1:{}\"".format(public_url, port))
print(f"ngrok tunnel \"{public_url}\" -> \"http://127.0.0.1:{port}\"")

# Update any base URLs or webhooks to use the public ngrok URL
settings.PROJECT_HOST = public_url
Expand Down
4 changes: 2 additions & 2 deletions myproject/common/utils/emailutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ def send_multipart_email(template_name, context, subject, to, bcc=None):
:param bcc: A list of email addresses to which to BCC
:return:
"""
plaintext = get_template('{}.txt'.format(template_name))
html = get_template('{}.html'.format(template_name))
plaintext = get_template(f'{template_name}.txt')
html = get_template(f'{template_name}.html')
text_content = plaintext.render(context)
html_content = html.render(context)

Expand Down

0 comments on commit 8fc8132

Please sign in to comment.