From f7455d161d25ac24bb6e8184a7d8894d48ba3965 Mon Sep 17 00:00:00 2001 From: Arjan Mossel Date: Thu, 30 Nov 2023 11:16:24 +0100 Subject: [PATCH 1/8] Add Django root-level wsgi.py for deployment module --- .gitignore | 1 + lidiabrowser/lidiabrowser/settings.py | 2 ++ lidiabrowser/wsgi.py | 20 ++++++++++++++++++++ 3 files changed, 23 insertions(+) create mode 100644 lidiabrowser/wsgi.py diff --git a/.gitignore b/.gitignore index 54caad0..d01d47c 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ *.sqlite3 __pycache__/ .python-version +htdocs/ diff --git a/lidiabrowser/lidiabrowser/settings.py b/lidiabrowser/lidiabrowser/settings.py index 398de33..5fb80fc 100644 --- a/lidiabrowser/lidiabrowser/settings.py +++ b/lidiabrowser/lidiabrowser/settings.py @@ -143,6 +143,8 @@ STATIC_URL = "static/" +STATIC_ROOT = "../htdocs" # Included for local testing + # Default primary key field type # https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field diff --git a/lidiabrowser/wsgi.py b/lidiabrowser/wsgi.py new file mode 100644 index 0000000..370f1b0 --- /dev/null +++ b/lidiabrowser/wsgi.py @@ -0,0 +1,20 @@ +""" +RSLab Django WSGI config for the lidiabrowser application. + +It exposes the WSGI callable as a module-level variable named ``application``. + +Since this Django-only setup does not use our default Cookiecutter workflow, +this root-level wsgi file is included to facilitate our deployment script. +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings") + +try: + application = get_wsgi_application() +except Exception as e: + # Ends up in Apache logs + print(e) From e2e50f3ed226902f80c5cb655603f32807395f2a Mon Sep 17 00:00:00 2001 From: Arjan Mossel Date: Thu, 30 Nov 2023 11:18:10 +0100 Subject: [PATCH 2/8] Add gunicorn to requirements --- requirements.in | 1 + requirements.txt | 10 ++++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/requirements.in b/requirements.in index 76ebc5b..fd9bc2e 100644 --- a/requirements.in +++ b/requirements.in @@ -4,3 +4,4 @@ pyzotero Django django-environ iso639-lang>=2.1.0 +gunicorn diff --git a/requirements.txt b/requirements.txt index 8487d83..4f5608c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,8 +1,8 @@ # -# This file is autogenerated by pip-compile with Python 3.11 +# This file is autogenerated by pip-compile with Python 3.10 # by the following command: # -# pip-compile requirements.in +# pip-compile # asgiref==3.7.2 # via django @@ -18,10 +18,14 @@ django-environ==0.11.2 # via -r requirements.in feedparser==6.0.10 # via pyzotero +gunicorn==21.2.0 + # via -r requirements.in idna==3.4 # via requests iso639-lang==2.2.1 # via -r requirements.in +packaging==23.2 + # via gunicorn pyparsing==3.1.1 # via bibtexparser python-dotenv==1.0.0 @@ -38,5 +42,7 @@ sgmllib3k==1.0.0 # via feedparser sqlparse==0.4.4 # via django +typing-extensions==4.8.0 + # via asgiref urllib3==2.1.0 # via requests From fc8fd2b9083a20cd60933650cda53ea40425678f Mon Sep 17 00:00:00 2001 From: Arjan Mossel Date: Wed, 15 Nov 2023 17:59:44 +0100 Subject: [PATCH 3/8] Move .env to repository root --- lidiabrowser/lidiabrowser/.env.dist => .env.dist | 0 README.md | 2 +- lidiabrowser/lidiabrowser/settings.py | 3 ++- 3 files changed, 3 insertions(+), 2 deletions(-) rename lidiabrowser/lidiabrowser/.env.dist => .env.dist (100%) diff --git a/lidiabrowser/lidiabrowser/.env.dist b/.env.dist similarity index 100% rename from lidiabrowser/lidiabrowser/.env.dist rename to .env.dist diff --git a/README.md b/README.md index 9180f75..6d8f833 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ Install the dependencies: pip install -r requirements.txt -Create a file `lidiabrowser/lidiabrowser/.env` with your Zotero library and authentication details: +Create a file `.env` in the repository root with your Zotero library and authentication details: ```sh ZOTERO_LIBRARY_ID=12345 diff --git a/lidiabrowser/lidiabrowser/settings.py b/lidiabrowser/lidiabrowser/settings.py index 5fb80fc..b59cbe4 100644 --- a/lidiabrowser/lidiabrowser/settings.py +++ b/lidiabrowser/lidiabrowser/settings.py @@ -10,18 +10,19 @@ https://docs.djangoproject.com/en/4.2/ref/settings/ """ +import os from pathlib import Path import environ root = environ.Path(__file__) env = environ.Env() -environ.Env.read_env() SITE_ROOT = root() # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent +environ.Env.read_env(os.path.join(BASE_DIR.parent, '.env')) # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/ From dd7a63cf9e0d5eda35d52358a192c2bfbd14de17 Mon Sep 17 00:00:00 2001 From: Arjan Mossel Date: Wed, 15 Nov 2023 18:05:21 +0100 Subject: [PATCH 4/8] Update workflow --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index be51999..cdfc4f1 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -37,5 +37,5 @@ jobs: ruff --output-format=github --target-version=py38 . - name: Test with pytest run: | - cp lidiabrowser/lidiabrowser/.env.dist lidiabrowser/lidiabrowser/.env + cp .env.dist .env pytest lidiabrowser From ee62f24b15ad0bd3a6048db9f83376493a67df50 Mon Sep 17 00:00:00 2001 From: Arjan Mossel Date: Thu, 16 Nov 2023 12:59:47 +0100 Subject: [PATCH 5/8] Use Django server settings from .env --- lidiabrowser/lidiabrowser/settings.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/lidiabrowser/lidiabrowser/settings.py b/lidiabrowser/lidiabrowser/settings.py index b59cbe4..7a9dd81 100644 --- a/lidiabrowser/lidiabrowser/settings.py +++ b/lidiabrowser/lidiabrowser/settings.py @@ -15,7 +15,14 @@ import environ root = environ.Path(__file__) -env = environ.Env() +env = environ.Env( + # Set casting, default value + # Defaults are for local development; set server config in .env + SECRET_KEY=(str, 'BmM7FkllTHs_5Q0nsdalUDUlV0Q-B5Pl5uVxwRQSr_E'), + DEBUG=(bool, True), + ALLOWED_HOSTS=(list, []), + STATIC_ROOT=(str, '../htdocs') +) SITE_ROOT = root() @@ -28,12 +35,12 @@ # See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = "django-insecure-m8_b*$)+y4!o3%-=$xf2kpiu6cqd16&0bekmw94^hfucpjzja&" +SECRET_KEY = env.str('SECRET_KEY') # SECURITY WARNING: don't run with debug turned on in production! -DEBUG = True +DEBUG = env.bool('DEBUG') -ALLOWED_HOSTS = [] +ALLOWED_HOSTS = env.list('ALLOWED_HOSTS') # Application definition @@ -144,7 +151,7 @@ STATIC_URL = "static/" -STATIC_ROOT = "../htdocs" # Included for local testing +STATIC_ROOT = env.str('STATIC_ROOT') # Default primary key field type # https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field From 85fcd678637a0e3eb8f1889f5737d33b548c60a2 Mon Sep 17 00:00:00 2001 From: Arjan Mossel Date: Thu, 16 Nov 2023 13:21:46 +0100 Subject: [PATCH 6/8] Use settings from the lidiabrowser project module --- lidiabrowser/wsgi.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lidiabrowser/wsgi.py b/lidiabrowser/wsgi.py index 370f1b0..0b67412 100644 --- a/lidiabrowser/wsgi.py +++ b/lidiabrowser/wsgi.py @@ -11,7 +11,7 @@ from django.core.wsgi import get_wsgi_application -os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings") +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "lidiabrowser.settings") try: application = get_wsgi_application() From a58966f9b167facba1555c3f861fc42fe4aa3f5a Mon Sep 17 00:00:00 2001 From: Arjan Mossel Date: Thu, 16 Nov 2023 14:10:49 +0100 Subject: [PATCH 7/8] Update README --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 6d8f833..d277ebd 100644 --- a/README.md +++ b/README.md @@ -29,5 +29,7 @@ To run on a local machine, use: cd lidiabrowser python manage.py migrate python manage.py runserver +python manage.py sync +python manage.py populate ``` From 3396c4ecef4befcb55991c55061aeeee26b85bf9 Mon Sep 17 00:00:00 2001 From: Arjan Mossel Date: Thu, 30 Nov 2023 11:19:45 +0100 Subject: [PATCH 8/8] Remove gunicorn; add in deployment module --- requirements.in | 1 - requirements.txt | 6 +----- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/requirements.in b/requirements.in index fd9bc2e..76ebc5b 100644 --- a/requirements.in +++ b/requirements.in @@ -4,4 +4,3 @@ pyzotero Django django-environ iso639-lang>=2.1.0 -gunicorn diff --git a/requirements.txt b/requirements.txt index 4f5608c..8bee7e3 100644 --- a/requirements.txt +++ b/requirements.txt @@ -18,14 +18,10 @@ django-environ==0.11.2 # via -r requirements.in feedparser==6.0.10 # via pyzotero -gunicorn==21.2.0 - # via -r requirements.in -idna==3.4 +idna==3.6 # via requests iso639-lang==2.2.1 # via -r requirements.in -packaging==23.2 - # via gunicorn pyparsing==3.1.1 # via bibtexparser python-dotenv==1.0.0