Daily Dev Notes 2024/05/25
I took the plunge today and finally ripped out djpress
into its own, stand-alone package. I mostly followed this guide from Django, which linked through to a tutorial on the Python Packaging site which explained how to build and upload it.
After following the steps, I managed to get the package deployed to PyPi, and created a new public repository to host the code!
Of course, as soon as I had uploaded it, I realised there was a fundamental error with the code, in that it relied on configuration settings that only existed in my stuartm.nz codebase. So I had to refactor that so that the default settings are in the package, and then I can over-ride them in my project settings file.
I got ChatGPT to help with this, and was pretty happy with the result. I created a new app_settings.py
file that holds all the configuration settings. And then created a conf.py
file that loads the default project settings or the app settings if they don't exist.
"""Configuration settings for DJ Press"""
from django.conf import settings as django_settings
from . import app_settings as default_settings
class Settings:
def __init__(self, default_settings_module, user_settings_module):
self._default_settings = default_settings_module
self._user_settings = user_settings_module
def __getattr__(self, name):
# If the setting is found in the user settings, return it
if hasattr(self._user_settings, name):
return getattr(self._user_settings, name)
# If the setting is found in the default settings, return it
if hasattr(self._default_settings, name):
return getattr(self._default_settings, name)
# If the setting is not found in either, raise an AttributeError
raise AttributeError(f"Setting '{name}' not found")
settings = Settings(default_settings, django_settings)
And then any time I want to access settings, instead of using: from django.conf import settings
, I just use: from djpress.conf import settings
. Nothing else in the code needs to change.