Skip to content

Commit

Permalink
🕸 VSCode Debugger (#85)
Browse files Browse the repository at this point in the history
* Adds config to run locally without docker in a VSCode context

Running the app locally without docker allows better debugging and avoiding docker-related issues.

Closes #84.

* Adds .env.example

* Adds gunicorn and python-dotenv dependencies

* Adds gunicorn as a dependency as well as a VSCode launch config for running it locally
  in cases where we want to run local tests with a more production-like performance load.
* Removes unused import.

* Removes failing unneeded spec for detecting PYTHON_VERSION in environment

* Adds pre-commit package to project to use ggshield

* Remove password from .env.example

It was just a dummy password, but trying to avoid triggering a GitGuardian
alert. Will install ggshield pre-commit hook in upcoming PR.

* Use 0.0.0.0 for localhost in launch config

* Standardizes dev to port 8000

* Migrates to pytest, add dotenv to settings

---------

Co-authored-by: Harpo Harbert <ryan_harbert@wgbh.org>
  • Loading branch information
afred and mrharpo authored Nov 3, 2023
1 parent 6985044 commit 05ddcb4
Show file tree
Hide file tree
Showing 15 changed files with 164 additions and 31 deletions.
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
OV_DB_ENGINE=django.db.backends.postgresql
OV_DB_HOST=0.0.0.0
OV_DB_PORT=5432
OV_DB_NAME=postgres
OV_DB_USER=postgres
OV_DB_PASSWORD=""
14 changes: 9 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@
__pycache__
*.pyc
.DS_Store
/venv/

# Database
db/
# Virtual environment
.venv

# Environment files
# Environment variable files
.env
.db
!.env.example

# VS Code
.vscode/
!.vscode/settings.json
!.vscode/launch.json
64 changes: 64 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"inputs": [
{
"id": "numWorkers",
"type": "promptString",
"default": "4",
"description": "Number of Gunicorn workers to run"
}
],
"configurations": [
{
"name": "OV Wagtail - Django dev server",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/manage.py",
"args": [
"runserver",
"0.0.0.0:8000"
],
"django": true,
"justMyCode": false
},
{
"name": "OV Wagtail - Gunicorn",
"type": "python",
"request": "launch",
"program": "gunicorn",
"args": [
"-w ${input:numWorkers}",
"-b 0.0.0.0:4000",
"ov-wag.wsgi:applications",
],
"django": true,
"justMyCode": false
},
{
"name": "Run Migrations",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/manage.py",
"args": [
"migrate",
"--noinput"
],
"django": true,
"justMyCode": false
},
{
"name": "Run Tests",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/manage.py",
"args": [
"test",
],
"django": true,
"justMyCode": false
},
]
}
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"python.testing.pytestArgs": [
"-v",
],
"python.testing.pytestEnabled": true,
"python.testing.unittestEnabled": false
}
2 changes: 1 addition & 1 deletion dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ services:
- .env
db:
env_file:
- .db
- .env
6 changes: 6 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ services:
db:
image: postgres:16-alpine
restart: always
ports:
- 5432:5432
environment:
- POSTGRES_DB=${OV_DB_NAME}
- POSTGRES_USER=${OV_DB_USER}
- POSTGRES_PASSWORD=${OV_DB_PASSWORD}
volumes:
- db:/var/lib/postgresql/data

Expand Down
5 changes: 5 additions & 0 deletions manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
import os
import sys

from dotenv import load_dotenv

# take environment variables from .env.
load_dotenv()

if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ov_wag.settings.dev")

Expand Down
5 changes: 5 additions & 0 deletions ov_wag/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os

from dotenv import load_dotenv

# take environment variables from .env.
load_dotenv()

PROJECT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
BASE_DIR = os.path.dirname(PROJECT_DIR)

Expand Down
4 changes: 1 addition & 3 deletions ov_wag/settings/dev.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .base import *
from ov_wag.settings.base import *

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
Expand All @@ -12,8 +12,6 @@
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'




try:
from .local import *
except ImportError:
Expand Down
3 changes: 1 addition & 2 deletions ov_wag/settings/production.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from .base import *
from os import environ
from ov_wag.settings.base import *

DEBUG = False

Expand Down
11 changes: 7 additions & 4 deletions ov_wag/tests/test_api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import wagtail_factories
from rest_framework import status
from rest_framework.test import APITestCase
from wagtail.core.models import Site
import wagtail_factories

from exhibits.models import ExhibitPageApiSchema
from exhibits.tests.factories import ExhibitPageFactory

Expand Down Expand Up @@ -41,7 +42,9 @@ def test_exhibit_api_schema_single(self):
Compare response against ExhibitSchema
"""
exhibit_page = ExhibitPageFactory.create(parent=self.__home_page())
response = self.client.get(f'/api/v2/exhibits/{exhibit_page.id}/', format='json')
response = self.client.get(
f'/api/v2/exhibits/{exhibit_page.id}/', format='json'
)
json = response.json()
self.assertValidSchema(json)

Expand All @@ -51,8 +54,8 @@ def test_exhibit_api_schema_multiple(self):
Compare response against ExhibitSchema
"""
exhibit_page = ExhibitPageFactory.create(parent=self.__home_page())
response = self.client.get(f'/api/v2/exhibits/', format='json')
ExhibitPageFactory.create(parent=self.__home_page())
response = self.client.get('/api/v2/exhibits/', format='json')
json = response.json()
for item in json['items']:
self.assertValidSchema(item)
Expand Down
16 changes: 2 additions & 14 deletions ov_wag/tests/test_env.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,15 @@
from os import path
from unittest import TestCase
from os import environ, system, path
from pprint import pp


class EnvTests(TestCase):
def test_env(self):
"""
Show the current environment
debug: print environ
"""
self.assertIn('PYTHON_VERSION', dict(environ))

# Uncomment to show full environ in logs
# pp(dict(environ))

def test_media_dir(self):
"""
Test if MEDIA_ROOT directory exists
debug: ls -la MEDIA_ROOT
"""
from ..settings.base import MEDIA_ROOT
from ov_wag.settings.base import MEDIA_ROOT

self.assertTrue(path.isdir(MEDIA_ROOT))

Expand Down
35 changes: 34 additions & 1 deletion pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,23 @@
[project]
# PEP 621 project metadata
# See https://www.python.org/dev/peps/pep-0621/
dependencies = ["Django<4.2,>=4.1.1", "wagtail<4.1,>=4.0.4", "wagtail-factories<3.2,>=3.1.0", "pydantic<2.0,>=1.10.2", "psycopg2<2.10,>=2.9.3"]
dependencies = [
"Django<4.2,>=4.1.1",
"wagtail<4.1,>=4.0.4",
"wagtail-factories<3.2,>=3.1.0",
"pydantic<2.0,>=1.10.2",
"psycopg2<2.10,>=2.9.3",
"python-dotenv>=1.0.0,< 2.0",
"gunicorn>=21.2.0",
]
requires-python = ">=3.11"

[build-system]
requires = ["pdm-backend"]
build-backend = "pdm.backend"


[tool.pdm.dev-dependencies]
dev = [
"pre-commit~=3.5",
]
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ wagtail >= 4.0.4, < 4.1
wagtail_factories >= 3.1.0, < 3.2
pydantic >= 1.10.2, < 2.0
psycopg2 >= 2.9.3, < 2.10
python-dotenv >= 1.0.0, < 2.0

0 comments on commit 05ddcb4

Please sign in to comment.