Skip to main content

Simple classes related to the django sites framework for clinicedc projects.

Project description

pypi travis codecov downloads

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?

  1. 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
...
...
  1. change wsgi.py

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "inte_edc.settings")

becomes this
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "inte_edc.settings.defaults")
  1. 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"),
    ),
}
  1. 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
  2. create dummy settings files for each site, for example;

# kojja.py
from ..defaults import *  # noqa
  1. 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.
  1. We use a .env file and set EDC_SITES_MODULE_NAME to inte_sites.sites.

  2. 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

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

edc_sites-0.1.19-py3-none-any.whl (25.5 kB view details)

Uploaded Python 3

File details

Details for the file edc_sites-0.1.19-py3-none-any.whl.

File metadata

  • Download URL: edc_sites-0.1.19-py3-none-any.whl
  • Upload date:
  • Size: 25.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.0.0.post20200309 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.1

File hashes

Hashes for edc_sites-0.1.19-py3-none-any.whl
Algorithm Hash digest
SHA256 5f4c24d4356b91f83c04dbfc41f31216e13aec0f265d4d5260223f13e29a7c6a
MD5 689a9a98db2f9110ba435500448a4853
BLAKE2b-256 2ceefd55b5b9eb14ba52d62ceac2e873a5f851d1d044c62d834b29904d729358

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page