Simple classes related to the django sites framework for clinicedc projects.
Project description
edc-sites
Site definitions to work with Django’s Sites Framework.
Define a sites.py. This is usually in a separate project module. For example, for project meta there is a module meta_sites that contains a sites.py.
# sites.py
fqdn = "meta.clinicedc.org"
meta_sites = (
(10, "hindu_mandal", "Hindu Mandal Hospital"),
(20, "amana", "Amana Hospital"),
)
Register a post_migrate signal in apps.py to update the django model Site and the EDC model SiteProfile on the next migration:
# apps.py
from .sites import meta_sites, fqdn
def post_migrate_update_sites(sender=None, **kwargs):
from edc_sites.add_or_update_django_sites import add_or_update_django_sites
sys.stdout.write(style.MIGRATE_HEADING("Updating sites:\n"))
add_or_update_django_sites(
apps=django_apps, sites=meta_sites, fqdn=fqdn, verbose=True
)
sys.stdout.write("Done.\n")
sys.stdout.flush()
For another deployment, we have alot of sites spread out over a few countries. In this case we pass a dictionary and separate the lists of sites by country.
For example:
fqdn = "inte.clinicedc.org"
all_sites = {
"tanzania":(
(10, "hindu_mandal", "Hindu Mandal Hospital"),
(20, "amana", "Amana Hospital"),
),
"uganda":(
(10, "kojja", "Kojja Clinic"),
(20, "mbarara", "Mbarara Clinic"),
(30, "kampala", "Kampala Clinic"),
),
}
In a mult-isite, multi-country deployment, managing the SITE_ID is complicated.
One approach is to manipulate the DJANGO_SETTINGS_MODULE.
The DJANGO_SETTINGS_MODULE is referred to by wsgi.py and can just as easily be referred to in settings itself. For example, instead of explicitly setting the SITE_ID you can use edc_sites utility get_site_from_environment.
What we would like to be able do is this:
# defaults as a test environment or whatever manage.py points to.
$ python manage.py runserver
>>> from django.conf import settings
>>> setting.SITE_ID
>>> 100
# which, with the new folder structure, is the same as this
$ python manage.py runserver --settings=inte_edc.settings.defaults
>>> from django.conf import settings
>>> setting.SITE_ID
>>> 100
# This loads for a specific site
$ python manage.py runserver --settings=inte_edc.settings.uganda.kojja
>>> from django.conf import settings
>>> setting.SITE_ID
>>> 170
Of course, having a custom settings file is easy, but managing these across multiple deployments can easily lead to code duplication. So let’s backup a bit. What do we need to setup?
change manage.py
...
...
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "inte_edc.settings.defaults")
...
...
or to be more helpful ...
def main():
default = "inte_edc.settings.defaults"
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "inte_edc.settings.defaults")
if os.environ.get("DJANGO_SETTINGS_MODULE") == default:
sys.stderr.write(
style.ERROR(
f"DJANGO_SETTINGS_MODULE not set. Using `{default}`. "
f"Assuming a test environment (manage.py).\n"
)
)
try:
from django.core.management import execute_from_command_line
...
...
change wsgi.py
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "inte_edc.settings")
becomes this
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "inte_edc.settings.defaults")
create a custom sites module that has a sites.py. In this case inte_sites.sites;
fqdn = "inte.clinicedc.org"
all_sites = {
"tanzania":(
(10, "hindu_mandal", "Hindu Mandal Hospital"),
(20, "amana", "Amana Hospital"),
),
"uganda":(
(10, "kojja", "Kojja Clinic"),
(20, "mbarara", "Mbarara Clinic"),
(30, "kampala", "Kampala Clinic"),
),
}
move settings.py to a folder and put all settings stuff in a defaults.py:
inte-edc/ inte_edc/ settings/ __init__.py defaults.py uganda/ __init__.py kampala.py kojja.py mbarara.py __init__.py wsgi.py inte_sites/ __init__.py apps.py sites.py manage.py
create dummy settings files for each site, for example;
# kojja.py
from ..defaults import * # noqa
update the settings defaults.py file. Replace SITE_ID=<site_id> …
# defaults.py
from edc_sites import get_site_from_environment
...
# extract country and sitename from DJANGO_SETTINGS_MODULE environment variable
EDC_SITES_MODULE_NAME = env.str("EDC_SITES_MODULE_NAME")
COUNTRY, SITE_ID = get_site_from_environment(
default_site_name="mbarara",
default_country="uganda",
app_name=APP_NAME,
sites_module_name=EDC_SITES_MODULE_NAME,
)
...
we choose "mbarara" and "uganda" for our default site_name and country.
We use a .env file and set EDC_SITES_MODULE_NAME to inte_sites.sites.
set the DJANGO_SETTINGS_MODULE for each environment the project is loaded in.
Unless we are testing, we’ll expect the DJANGO_SETTINGS_MODULE to be set. We’ll set it to a value that includes the county and site name. As you can see from the change to defaults.py above, we parse the value using get_site_from_environment.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Hashes for edc_sites-0.1.19-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5f4c24d4356b91f83c04dbfc41f31216e13aec0f265d4d5260223f13e29a7c6a |
|
MD5 | 689a9a98db2f9110ba435500448a4853 |
|
BLAKE2b-256 | 2ceefd55b5b9eb14ba52d62ceac2e873a5f851d1d044c62d834b29904d729358 |